Introduction: Raspberry Pi Based Wall Avoiding Robot - FabLab NerveCentre
Please note: This instructable is not complete - but may be helpful
Published to allow the excellent people at Derry's NerveCentre to take the code out to schools
I hope to get back to this but my current job is keeping me from it for now.
This instructable is an alternative to my other wall avoiding robot found here:
The intention is to offer an alternative platform and coding in order to achieve the same result.
For this project we use the Raspberry Pi and the familiar L293D dual h-bridge chip. The ultrasonic sensor is the widely used HC-SR04.
The coding is done in Python, which I find to be considerably easier to interpret for those who haven't done a lot of coding.
Enjoy the project and please give any feedback you can so that any necessary improvements can be made.
Step 1: What You Need
A Raspberry Pi - I've used a B+ but any RPi will work with some edits to pin numbers used to drive each motor.
A motor driver - I've used the L293D PCB kit from here: http://www.rkeducation.co.uk/L293D-Project-PCB.php... - Due to a job change, I am unlikely to be able to answer questions on alternatives so I hope the information later on will be enough.
2 X right angle geared DC motors & wheels - you can use any DC motors for this.
A HC-SR04 ultrasonic sensor - very widely used and available for RPi & Arduino projects.
A 9V battery and battery snap - this is to run the motors.
A 5V battery - I've used an Anker battery to run the RPi.
Wiring - a selection of female-female and female-male wires.
Resistors - just 2 are needed to create a voltage divider. I have used a 100Ohm and a 220 Ohm pairing. See details below about creating a voltage divider - ultimately though one resistor needs to be as close to twice the resistance of the other.
A small breadboard - you could do without this if you use insulating tape or something instead.
Step 2: Let's Look at Using the Pi to Get a Reading From the Ultrasonic Sensor
I learned how to do this through the tutorial on ModMyPi.com which is available here:
Attached is a schematic which shows how to apply the voltage divider.
Briefly, this is used to drop the voltage on the signal back from the ECHO pin on the sensor. 5V leaves the pin and goes through a resistor which creates a voltage drop. At this point, there is a split - one branch goes to GND through a resistor which takes away twice as much voltage - the other branch has only gone through one of these resistors and so only reduces the voltage to 3.3V.
The Pi needs this signal to be reduced to 3.3V otherwise the US sensor will work but will return spurious readings. Thus the need for the voltage divider.
You can refer to the attached link if you need to breadboard this. I have attached (later) some manufacturing files for producing a small voltage divider if you have the capabilities for it.
To test the ultrasonic sensor you can use the attached code (if not working from my image available on Github). To do this, simply download the file and in LXTerminal on the RPi navigate to the folder where you have downloaded the file. Then type "sudo python FILENAME.py".
Step 3: Now Let's Look at Getting Some Motors to Drive
Now we can hook up some motors.
Everything can be connected as per the image. I find it very helpful to try to avoid repeating any colours so that you can easily see where each connects.
In the code attached (which demonstrates motor movement) you will notice that I have used a naming standard for the 3 pins which link the RPi to the motor driver. For example:
Motor1A = 36 - this indicates a pin to connect IP1 or IP2 to.
Motor1B = 38 - this indicates a pin to connect IP1 or IP2 to.
Motor1E = 40 - this indicates a pin to connect EN1 to.
The same is true for the "Motor2*" series:
Motor1A = 33 - this indicates a pin to connect IP3 or IP4 to.
Motor1B = 35 - this indicates a pin to connect IP3 or IP4 to.
Motor1E = 37 - this indicates a pin to connect EN2 to.
Once these are connected, we need to connect each motor terminal up also. Connect one motor to MA and the other to MB - which wire goes into each side for the motors is quite arbitrary as no damage will be done.
Again, you can download the file attached and run the python code as per previous instructions.
Step 4: Getting It All Running Together
At this point, you probably have a mess of wiring something like the picture attached!
Other than building a chassis, you're all set to get the whole thing working together.
Attached is a python script (which is again run as per previous instructions) that will make a distance measurement using the ultrasonic sensor every 0.5 of a second. If this measurement falls below 10cm one of the wheels reverses. When mounted all together on a chassis, this will mean the robot will drive around and turn if it senses anything close by.
Step 5: Building a Chassis Etc.
Here are some files you can use to build yourself a chassis and some other parts to mount motors etc.
Laser Cut chassis: pi_bot_us.svg
3D printed motor mounts of various types are available at: http://www.thingiverse.com/tcanning/designs
A great 3D printed mount (designed by Shaun Saggers) for the HC-SR04 is available at: http://www.thingiverse.com/thing:189585
Finally, If you want to create your own small voltage divider you can use the attached PNG to mill/etch one.
Of course, for the above, access to a laser cutter etc is necessary. Alternatively I recommend thick card and a lot of tape!