pi-tutorials
pi-tutorials
Keith's Pi Tutorials
105 posts
Content posted here will be of interest to followers or users of the Raspberry Pi, including the occasional video Tutorial on completing specific tasks on the Pi.
Don't wanna be here? Send us removal request.
pi-tutorials · 7 years ago
Video
tumblr
Lets get some code on this thing
So now we have the electronics sorted, lets have a look at the code. I’m borrowing heavily from this resource. I chose to use this code since the project was very well documented with some nice videos. The code also uses the same gyroscope as me. One downside is that this code is designed for stepper motors and I’m using cheap DC motors.
Before I jumped into the main code, I created a quick script just to check the motors were working. I created a few functions and ran them to ensure the motors moved correctly. If you look at this code you can also see how I am using the input pins of the motor driver to set direction and the enable pin with PWM to set the speed.
/* * Drive Motors with an Arduino Mini Pro using an L239D 'H' bridge * Keith Ellis * July 2018 * */ #define motor_left_A 2 // Motor Left Pin A - High = forward #define motor_left_B 4 // Motor Left Pin B - High = reverse #define motor_left_En 3 // Motor Left Enable Pin, PWM on this pin will control speed #define motor_right_A 5 // Motor Right Pin A - High = forward #define motor_right_B 7 // Motor Right Pin B - High = reverse #define motor_right_En 6 // Motor Right Enable Pin, PWM on this pin will control speed void setup() { pinMode(motor_left_A, OUTPUT); pinMode(motor_left_B, OUTPUT); pinMode(motor_left_En, OUTPUT); pinMode(motor_right_A, OUTPUT); pinMode(motor_right_B, OUTPUT); pinMode(motor_right_En, OUTPUT); } void loop() { AllMotorsFwd(127); // Speed from 0 - 255 delay(100); AllMotorsRev(255); delay(75); AllMotorsFwd(127); delay(100); AllMotorsRev(255); delay(120); AllMotorsFwd(127); delay(50); AllMotorsRev(255); delay(75); AllMotorsFwd(127); delay(100); AllMotorsRev(127); delay(125); } void LeftMotorFwd(int speed){ //Speed is int from 0 - 255 digitalWrite(motor_left_A, HIGH); digitalWrite(motor_left_B, LOW); analogWrite(motor_left_En, speed); } void LeftMotorRev(int speed){ //Speed is int from 0 - 255 digitalWrite(motor_left_A, LOW); digitalWrite(motor_left_B, HIGH); analogWrite(motor_left_En, speed); } void RightMotorFwd(int speed){ //Speed is int from 0 - 255 digitalWrite(motor_right_A, HIGH); digitalWrite(motor_right_B, LOW); analogWrite(motor_right_En, speed); } void RightMotorRev(int speed){ //Speed is int from 0 - 255 digitalWrite(motor_right_A, LOW); digitalWrite(motor_right_B, HIGH); analogWrite(motor_right_En, speed); } void AllMotorsFwd(int speed){ //Speed is int from 0 - 255 LeftMotorFwd(speed); RightMotorFwd(speed); } void AllMotorsRev(int speed){ //Speed is int from 0 - 255 LeftMotorRev(speed); RightMotorRev(speed); } void LeftMotorStop(void){ digitalWrite(motor_left_A, LOW); digitalWrite(motor_left_B, LOW); digitalWrite(motor_left_En, LOW); } void RightMotorStop(void){ digitalWrite(motor_right_A, LOW); digitalWrite(motor_right_B, LOW); digitalWrite(motor_right_En, LOW); } void AllMotorsStop(void){ LeftMotorStop(); RightMotorStop(); }
This worked as planned, you can download this code here
After editing the Balance Bot code to take out references to the stepped motors and copying in my motor functions I ended up with the code here.
Before running this code, the balance point of the robot needs to be determined. This can be done by running the calibration code here Balnce the robot upright and open the serial console. Make a note of the Balance value and paste it into the variable acc_calibration_value on line 17 of the main Balance_robot.ino code.
After messing about with the PID variables I managed to get the robot balancing of sorts as seen above. To tune the PID values I used this video here. I was well chuffed, but it is far from perfect. I’ll leave this post here for now, my next post will run through PID tuning and weight placement.
5 notes · View notes
pi-tutorials · 7 years ago
Text
Motor driver decisions
There are so many types of motor driver to choose from. The easiest decision was whether it was a DC motor driver or a stepper motor driver. I am using the cheap Chinese yellow DC motors, so DC motor driver it is. In my part box I had several motor drivers at my disposal.
L293D
L298N
Cytron 10A 5-30V Dual Channel DC Motor Driver
Dagu Arduino Mini Driver Board
I ruled out the Cytron controller, this is large and way over spec’ed for the motors I’m using. It is a very nice driver though. We have used it in the Ipswich Makerspace PiWars entry for the past three years. I ruled out the L298N driver, these are nice and little, easy to use, but the pin out is hard to use with strip board. The Dagu controller was interesting, this is what came with the robot chassis I’ve stripped down. It is an Arduino compatible board complete with built in motor driver. I was very tempted to use this, but documentation is hard to come by. I eventually decided not to use this. I did not want to be messing about trying to understand how this worked, I’d rather deal with stuff I’ve used before and understood. I’m sure I’m gong to get stuck many times on this journey, but I did not want to be wasting time just trying to get this board working.
So I chose to use the humble L293D. I’ve used this before and understand it well, it was also ideally suited to the motors I’m using and it’s nice and small.
How to drive the L293D?
The L293D is a dual H bridge driver. It has the following connections
Power for logic - 5v
Power for motors, I’m using 7.2v - 9v
Ground
2 x H bridge, both of which have the following connections
Enable
Input A
Input B
Output A
Output B
The motor is connected to the outputs and the inputs are driven by a micro controller. To drive a motor, one input must be high and the other must be low. Also the enable pin must be high. To reverse the motor just switch the inputs, so low becomes high and high becomes low.
To control the speed of the motor, the high input pin can be a PWM signal, again if reversing the motor the other input pin needs a PWM signal. So to drive two motors both at variable speeds with the enable pins permanently wired to 5v, only four PWM pins are needed.
The Arduino Pro mini does not have many PWM pins, so I’m going to use it slightly differently. It will take six pins but will only need two PWM pins. To do this I will drive the enable pins with PWM signal to control the speed of the motors. The direction will be controlled with the two input pins, by setting them either high or low.
Here is my setup of the L293D motor driver and Arduino.
Tumblr media
4 notes · View notes
pi-tutorials · 7 years ago
Note
Hi Keith, test completed :). I used a big part of your tutorial to make my bird cam to steam video to the internet. Thanks for that! You mentioned that you maybe will make some additional futures for the bird box like sending tweets when movement has been detected. Is this still in the pipeline?
Hi, sorry only just seen this message, suspect it is very old. No sorry, the bird box project did not get any further. Too busy with other things. Glad it was useful.
0 notes
pi-tutorials · 7 years ago
Note
Hey keith ! My names Jason I'm currently trying to get your tutorial working I'm having trouble with an error at setting up the left and right axis its saying attribute error when I run the robot module I was just wondering if you could help me I've added your python3 library to my directory called robotRock and downloaded toms input library but still nothing with my rock candy controller accept this attributes error for your (ly, ry) section please help! Should I just back a new SD ?
Sorry, suspect this message is very old, but I’ve only just seen it. Did you get this working in the end?
0 notes
pi-tutorials · 7 years ago
Note
Hi. REally like the bird box project. Did you manage to add a motion sensor to it at all?
Sorry, only just seen this message, suspect it is very old. I did get a test movement detection script working, but never implemented it. Sorry.
0 notes
pi-tutorials · 7 years ago
Note
Hi, I am trying to set up a Pi to monitor temperature based on your Bird Box. I am fairly new to the Pi. All goes well until I get to the command "sudo pip-3.4 install thingspeak", which returns "sudo: pip3.4: command not found" I tried deleting and re-installing Python, and "Python3" confirms I have version 3.4.2. Any Suggestions
Sorry, only just seen this message, suspect it is very old. Out could try sudo pip3 instead.
0 notes
pi-tutorials · 7 years ago
Note
Hi Keith, is it possible to update annotate_text with readings from sensors while recording (not streaming) to a file?
Sorry, not sure when you posted this question. Probably quite a while ago. But I’ve only just seen it. I’m sure t is possible to record sensor data to a file. I’ve not done it. Would be very similar to how I did it, but instead of recording to a stream you would have to specify a file name.
0 notes
pi-tutorials · 7 years ago
Photo
Tumblr media Tumblr media Tumblr media
Electronics
So now I have a Chassis for my Balance Bot, it consist of couple of acrylic sheets with a couple of cheap DC motors. The things to think about for controlling this bot are
Processor or brains
Gyroscope / accelerometer
Motor controller
Power system
Let’s start off with the processor. I normally use Raspberry Pi’s for this sort of thing, but for the actual balancing system I think a dedicated micro controller would be better. This will not get interrupted dealing with WiFi, reading controllers and any other high level stuff. I had a few 3.3v Arduino Pro Mini clones, so this is what I will use. This does not have a USB socket so to programme I needed to get a USB to serial converter, I used this one from Pimoroni
As mentioned in my previous posts, I have a MPU-6050 6DOF gyroscope/accelerometer and I’ve also seen a few projects on the internet use this device so this is exactly what I will be using. It connects to the Arduino over the I2C bus.
The are many options for motor drivers, they can be purchased in many shapes and sizes. I wanted to keep the device quite small and I had some L293D motor driver chips in my storage box, so this is what I’ll be using for this project. The motors I’m using are not high power so the little 16 pin DIP chip will be perfect.
Power systems can get complicated very quickly. The Arduino needs 3v3, the MPU 6050 needs 5v and the motors need at least 5v with the motor driver needing 5v to power the internals of the chip. I wanted to keep the bot light since the motors are not very powerful. In line with my current philosophy, I wasn’t going to start over thinking this again, I plumped for a 2000mAh 3.7v Lipo battery with an Adafruit 1000mA boost/charge board. This gave me a small battery with 1000mA current at 5v to power the other components. The Arduino I have has a built in voltage regulator so I could power from 5v and it would drop it down to the required 3v3. All the other components I could power straight from the 5v from the boost/charge board.
To give me flexibility, I cut a bit of strip board down to size, added some fixing holes and soldered all the components on and wired together. I put a quick sketch together to test the motors, powered it up and nothing. I spent some time checking my wiring with data sheets for the Arduino and motor controller and found a problem. It turned out I had wired the motor controller incorrectly. So I quickly corrected this and this time the motors fired up, both forward and reverse.
I mounted the battery and strip board circuit onto the chassis. I now had a platform that worked, all that was left to do was programme the Arduino to tell it how to balance. Easy right! Think I’ll leave his part to another day.
10 notes · View notes
pi-tutorials · 7 years ago
Photo
Tumblr media Tumblr media Tumblr media Tumblr media
PiWars in the balance
So this year I’d like to build a robot, but I want to do something different. And maybe not even enter PiWars.
I’ve wanted to build an inverted pendulum balance bot for a few years now, maybe now is the time to do it. I’m not a maths or programming expert, so I will have to lean heavily on the internet if I am to get this done. So off to my favourite search engine and begin the search.
A few posts I found looked interesting:
This post on Instructables looks interesting, it uses a similar chassis to the one I won in the first PiWars, an MPU-6050 gyroscope, which I also had laying around, an Arduino and an H-bridge motor controller.
This post which describes a number of robots nicely.
This post is very well described and comes with a set of nicely made videos. Again the MPU-6050 is used but the bot is driven by stepper motors.
Sometimes (actually quite often) I find it is possible if over think things and never actually get round to building anything. So still not having a clear plan I stripped down the chassis of the little bot I won at PiWars and reassembled to resemble the main components of a self balancing robot.
It was starting to look like something that could eventually balance itself on its two wheels, but would it?
Next up, I needed to think about the electronics. But for now I’ll leave for another post.
2 notes · View notes
pi-tutorials · 7 years ago
Photo
Tumblr media
To PiWars or not to PiWars
It was back in 2014 that a small group of us from Ipswich Makerspace came together and entered the first PiWars competition held in Cambridge. It turned out quite well and we won our category, under £75. The robot shown was one of our prizes, now enhanced with several years of dust.
Fast forward a few years and we have now competed in all four of the PiWars competitions, coming runner up in two and winning two. The last win, by just one point separating us from second place contestant [Brian Corteil](https://coretecrobotics.co.uk/).
It is great fun competing, and I always learn lots whilst preparing the robot, however it is hard work and takes all my spare time for about half a year in the lead up to the competition. So I have been thinking hard about whether to compete again in 2019. The competition is always heavily over subscribed, so I thought by not entering would give someone else an opportunity to compete.
However, the community surrounding the Raspberry Pi and PiWars is exceptional, very friendly and always willing to help and share experiences. I thought it would be great to give something back, but how? Maybe build a demonstration robot, mentoring, helping teams solve problems. Who knows, I’ve not made up my mind yet, and entries to PiWars don’t close until September, so I could enter again, but at this point that option looks quite slim.
8 notes · View notes
pi-tutorials · 8 years ago
Video
youtube
I've been looking for a nice cheap bench power supply for a while. I started off with an ATX power supply break out from Dangerous Prototypes. This has done me for a while but is very limited with just 12v, 5v and 3.3v available. So when I saw a video from Dave Jones of the EEVBlog showing a cheap unit from RD Tech I ordered one.
It is easy to use, it has four connectors, two for the input voltage and two for the output. It acts like a buck convertor, so cannot supply voltages any higher than the input voltage. There are several versions available, the unit I got was the 30V 5A model, this accepts input voltages up to 40V and outputs 30v at 5amps.
It it a self contained unit but does not have a case, so I designed one in Fusion360, the design is available on Thingiverse
This video shows how the power supply works and as an added bonus describes how it may be used to determine a suitable motor driver for a connected motor.
4 notes · View notes
pi-tutorials · 8 years ago
Video
youtube
Tiny4WD Robot - Part Two
Introduction
Firstly, you may have noticed this is part two, if you have not seen part one, please check it out here This post will take you through the code I am using on my Tiny4WD robot, and show you how to control a robot with a joypad in just eight lines of code, yes, eight lines. But before I do I must credit some people who have done most of the hard work.
Brian Corteil, who designed the Tiny4WD robot and so kindly provided the files to enable me to get it laser cut. If you don't have a laser cutter, you can buy the kit from Pimoroni
Tom Oinn, who wrote the great library to allow joypad controllers such as the PS3, PS4 or Rock Candy controllers to be interfaced with both python2 and python3. The library is available on GitHub and can easily be installed via Pip or Pip3 pip install approxeng.input
4Tronix, who make the excellent Picon Zero motor controller board and provided the sample code from which I made my library.
Eight lines of code
So let's get right to it, it is possible to control a robot using a joypad in just eight lines of code. To do this I use Tom Oinn's approxeng.input library and a library I wrote for the 4Tronix Picon Zero motor driver board. I'll get to both of these libraries later, but let's look at those eight lines of code.
import piconzero from approxeng.input.selectbinder import ControllerResource robot = piconzero.Robot() with ControllerResource() as joystick: while True: ly = joystick.axes.get_value('ly') ry = joystick.axes.get_value('ry') robot.set_motors(ly, ry)
Let's take this one line at a time:
Import my 4Tronix library
Import Tom Oinn's imput library
Create a robot object to control the 4Tronix motor controller
Create a joystick object to read inputs from our joypad, if only one compatible controller is attached, this is all that is needed
Let's keep looping
Save the position of the left analogue stick to variable ly
Save the position of the right analogue stick to variable ry
Push the values of the left and right stick into the robot object and set the wheels in motion
I think I could have actually done this in seven lines by combining lines 6 & 7 into one line ly, ry = joystick.axes.get_value('ly', 'ry') but I'll keep it as it is for now. I think this is quite cool, being able to control a robot in tank mode (left stick controlling left wheels and right stick controlling right wheels) in just eight lines of code and if you use a Rock Candy controller you do not even have to worry about Bluetooth pairing.
Getting the code
So to get this working you need both my library for the 4Tronix Picon Zero board and Tom's input library. Grab my library from GitHub or clone into your current directory with.
$ git clone [email protected]:keithellis74/Picon-Zero.git
You will need to copy the file piconzero.py into the same directory as your robot code, so that it can be found when it is imported.
Alternatively you could clone my Tiny4WD-robot repository which has my sample code and the piconzero.py library already placed in the correct place. You can view the repo here or clone it with:
$ git clone [email protected]:keithellis74/Tiny4WD-robot.git
To get Tom's input library I recommend you read the excellent documentation, but it is simple to install. I'm using python3 so I installed with:
$ sudo apt-get install python3-dev python3-pip gcc $ pip3 install approxeng.input
If you are using python2, just remove the 3's from both lines above. Now we have the libraries installed, the 8 lines of code should work.
But I don't have a 4Tronix motor controller
That's not a problem, a lot of motor controllers can be driven using gpioZero. If your motor driver is controlled directly by the gpio pins rather than over I2C you can definitely use gpioZero, if you have a recent version of Raspbian then gpioZero will already be installed, if not just sudo apt-get install python3-gpiozero
You then need the gpio BCM pin numbers that your motors are using, then in my eight lines of code replace line 1 with
from gpiozero import Robot
Replace line 4 with
robot = Robot(left=(4, 14), right=(17, 18))
Where the numbers for left are the two gpio pin numbers for the left motor and ditto for the right motor.
Then change the 8th line to
robot.value = (ly, ry)
The final code would look like:
from gpiozero import Robot from approxeng.input.selectbinder import ControllerResource robot = Robot(left=(4, 14), right=(17, 18)) with ControllerResource() as joystick: while True ly = joystick.axes.get_value('ly') ry = joystick.axes.get_value('ry') robot.value = (ly, ry)
Just remember to change the gpio pin numbers in line 4 to the pins your motors are controlled by.
Note, I have been unable to test the gpioZero part above, but pretty sure it should work. Please let me know if it does not.
I don't like tank controls
This is not a problem either, but will likely take more than eight lines of code, but not many more. Tom has an example of using a single analogue stick to control left, right, forward and backwards. Check it out here, the function to take a look at is mixer().
11 notes · View notes
pi-tutorials · 8 years ago
Video
youtube
Tiny4WD Robot - Part 1
At the recent PiWars competition, I saw some great wheels on the Pimoroni stall, they were small, orange and looked very grippy. I could not resist so I purchased them, there and then. Now having the wheels felt good, but what good are wheels without a robot.
I remembered that Brian Corteil had designed a Tiny4WD robot and published build instructions in The MagPi magazine. It was a simple robot, but has lots of potential, so in the next couple of weeks I started buying the remaining parts I needed:
Micro metal gear motors
Motor mounts
Motor connection shims
Motor controller board - 4Tronix Picon Zero
Rock Candy joy pad
I already had a Raspberry Pi Zero and lipo battery and boost converter.
I also needed a chassis, Fortunately Brian had put his files on GitHub and Ipswich Makerspace had recently got a new laser cutter, so this seemed like a perfect opportunity to try out the cutter. I got myself some acrylic and at the next Makerspace meeting I downloaded the files and sent them to the cutter, 10 minutes later I had a robot chassis. Perfect. Now my excuse for building this robot was to let me 6 year old son build it, so one weekend he did, including the soldering of the motor shims to the motors, I was very proud.
If you want to do this the easy way, just by Brian's Tiny4WD robot kit from Pimoroni
Take a look at my video for a rundown of the robot, a future post will cover a code walk through.
7 notes · View notes
pi-tutorials · 8 years ago
Photo
Tumblr media
This weekend I was privaledged to take part in the third PiWars competition held in Cambridge at the Cambridge Computer Laboratory. This is a challenge based robotics competition. All robots have to have a Raspberry Pi at their heart. We entered as part of the Ipswich Makerspace team. the team consisted of Phil, Jon and myself. Phil and Jon wrote the code and built the robot, I designed the robot in Fusion360.
It turned out we didn't do a bad job, our results were as follows:
Line Follower challenge - 1st
Deranged Gold - 1st
Straight Line Speed Teat - 2nd
Overall in our class (Professional or expert hobbyists) we came - 2nd
With our trophey being presented to us by the one and only Dr Lucy Rogers
What a fantastic weekend.
1 note · View note
pi-tutorials · 9 years ago
Link
I've been working on this weather station for some time, today I managed to get the parts mounted on my shed, so I thought it was about time to publish the files to allow others to print if they so wish. Download here
Tumblr media
The sensors I got were from Maplin in the UK. I used the following sensors: Anemometer
Wind vane
Rain gauge
Vertical pole
16mm dia Aluminium tube from B&Q
I used the 16mm Aluminium tube for the horizontal tube and the Mapline 20mm dia tube for the vertical post. I designed and 3D printed the following brackets.
Mounting bracket the the Anemometer
Mounting Bracket for the Wind Vane
Mounting bracket for the rain sensor
Base bracket to support the vertical post
Bracket to connect the horizontal and vertical post
Now this is mounted, I just need to get the code up and running. I have already got the code for the Wind Vane, I edited the Raspberry Pi Foundations code for their weather station and adjusted it so it would work with the Rasp.io analog zero. the code is here
So I'm now going to get the Raspberry Pi installed in the shed and complete the rest of the code. I'll post again when i have more news.
9 notes · View notes
pi-tutorials · 9 years ago
Text
Raspberry Pi work station
Introduction
I've been using Raspberry Pi's for a while now, and 99% of the time I use them headless, whilst this is fine when I am at home with a know network setup, sometimes I wish I had a easy way to use a display.
I have an HDMI Pi screen but it is quite small a 9" and is a bit messey when using it out and about with the two power supplies, HDMI cable, finding somewhere to prop the display and having to carry a mouse and keyboard around.
What I really want is a self contained box with a screen, keyboard, mouse and a single power supply to power it all.
Pi in a box
I've seen a few people do something similar, most notable are Ben heck with his Lunch Box Dev Kit and there a many similar things on Instructables. Alex Eames of RasPi.TV has also converted an old laptop screen for use as a monitor here
So I planned on getting an old tool box from somewhere, buy a dead laptop from ebay (with a working screen), take the LCD screen out, but a controller board and power supply and mount it in the tool box. I'm hoping to have a self contained box with single power supply to power the screen and Pi, space to store a keyboard and mouse and maybe a USB hub too.
Lets get started
So to kick this off I ordered a laptop off ebay, it was £5, but unfortunately delivery was £12 so total cost was £17. It was a Fujitu Simmens Lifebook S7020 The screen shots showed the screen working so it looked like a good candidate.
Here's what it looked like
Tumblr media
It was delivered about 5 days later and at the last Ipswich Makerspace meeting I started taking it apart. It took a supprisingly long time to take it apart and some of the screws were particually hard to find. As a result by the time I had finished with it, it was not in such good condition.
Tumblr media
But I got the screen out undamaged
Tumblr media
The back looked like this with the model number LTD141ECEF
Tumblr media
Next steps
So now I need to track down a driver board, I'm having dificulty trying to find a compatable board at the moment, but I am not giving up, so once I make further progress I shall post back here.
12 notes · View notes
pi-tutorials · 9 years ago
Link
Last Wednesday I was humbled to be invited to a reception with the Duke of York by the Raspberry Pi Foundation. It was a great evening and met lots of fantastic Pi Community people.
The Raspberry Pi Foundation have blogged about it here.
1 note · View note