Build Raspberry Pi robots: Best Tutorial for beginners
If you are a beginner to Raspberry Pi and were looking for a simple hardware project, then look no further. This tutorial will show you to develop a python based robot which avoids obstacles and navigates freely.
Obstacle avoiding robots are fairly common and easy to make. Here, you can use this project to add object avoidance functionality to your robot. Or simply use it to start messing around with python and the hardware peripherals on raspberry pi. This system uses IR modules to detect objects, but we will get to the technical side later. So, if you have a raspberry pi and want to build something based on hardware using it, just scroll down and have fun :). Check out the video at the bottom to see how the raspberry pi obstacle avoiding robot works. And if you are a true beginner, you can always use our free eBook on Raspberry Pi and Arduino to get started from step 0.
What are the stuff required to do this?
- Raspberry Pi B/B+ or 2 and basic peripherals: SD card, keyboard, mouse, etc.
- IR sensor modules like this.
- Geared DC motors.
- L293D driver board.
- Robot chassis and wheels.
- Caster wheel.
- Breadboard and double side tape.
- Male to male/Female to male jumpers.
- 9V Battery and connectors.
- Push button and 220R resistor.
How does it work?
The entire working of this robot is really simple, nothing to sweat about The whole system avoids colliding into obstacles thanks to its onboard sensors. Here, this robot uses two IR sensor modules which can detect objects within a range of 5-6cm. This sensor outputs a digital LOW (0V) signal when there is an object within its range. And outputs a digital HIGH (5V) signal otherwise.
So how does these IR sensors work? IR stands for Infra Red, which is a wavelength of light not visible to the human eye (but can be seen through our smartphone cameras!). These modules consist of a pair of receiver and transmitter IR leds. When an object gets in front of the IR sensor, the surface of the object reflects a part of the IR light back to the receiver. Thus, the receiver then outputs a LOW signal notifying that an object is in front of the sensor.
These sensors are wired to the GPIO input pins of the raspberry pi. The pi then using a python script checks whether the GPIO pins connected to the IR sensor modules goes low. If it does go low, then it commands the DC motors to move in the reverse direction first and then turn. Moreover, this robot is initially activated when we push the button on the breadboard, after which the raspberry pi commands the DC motors to move forward via the L293D driver board. You can check out the demo video at the bottom of this page to see how this robot works.
Step 1: Connecting the IR Sensors for the raspberry pi obstacle avoiding robot
First, you need to turn ON your raspberry pi after connecting it to the monitor, keyboard, etc. Then we need to check the IR sensor modules. To do this, connect the IR modules to your raspberry pi as shown in the following diagram. Power the sensor by providing 5V (+ pin), GND (- pin) from the raspberry pi. And connect the B pin on the sensors to raspberry pi’s GPIO pins 3 and 16. You can check out the raspberry pi GPIO pin out as per the pin diagram here. We are using the GPIO.BOARD configuration, which means the pins are numbered based on their normal order on the board (1,2,3,..). Read the pin configuration on the sensor module and connect correspondingly.
Next, you need to copy and paste the following code and save it as a python file- irtest.py:
import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(3, GPIO.IN) #Right sensor connection GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Left sensor connection while True: i=GPIO.input(3) #Reading output of right IR sensor j=GPIO.input(16) #Reading output of left IR sensor if i==0: #Right IR sensor detects an object print "Obstacle detected on Left",i time.sleep(0.1) elif j==0: #Left IR sensor detects an object print "Obstacle detected on Right",j time.sleep(0.1)
After saving this file and running it: “sudo python irtest.py”. You will notice that when you block the sensor with your hand, following output gets printed on the screen:
Step 2: Connecting the motors with L293D
After testing the IR sensor modules, next you need to connect and test the L293D module and the motors. Power the L293D module by connecting the + and – pins of the board to the 9V battery. Also, connect the “-” of the board to the GND of raspberry pi.
For more detail: Build Raspberry Pi robots Best Tutorial for beginners