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 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 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. You can refer to the connection diagram here for completing the connections:
Next, you have to provide the inputs to the board. Four output GPIO pins from the raspberry pi control the direction of rotation of the two motors. The two terminals of the motors are then connected to the 4 output terminals of the board. The motors then, based on the command from the raspberry pi are powered by the 9V battery. The logic for controlling the motors from the raspberry pi is as given below:
Here, HIGH means 5V signal or digital 1 and LOW is 0V signal or digital 0. Eg: GPIO.output(5,1), this command sends a HIGH signal (digital 1) to pin no 5 on the raspberry pi. Thus each motor’s direction can be controlled by writing HIGH/LOW signals through two GPIO pins from the raspberry pi.
Next, you need to wire a push button to the raspberry pi as shown in the above connection diagram. This button is used to activate and de activate the robot. After wiring the robot, you need to attach the wheels. Use double side tape to fix the parts together on the robot chassis. After which, you will have a setup that almost looks like this:
After connecting the motors you need to check them. Use the code below to check the motors and the L293D. Make sure you have powered the driver board (L293D) and given the connections as per the above diagram. After that, copy the code below and save it as a python file: motor.py on your raspberry pi. Then run it using the command: sudo python motor.py. You will notice that both the motors rotate in one direction first and then after a second rotates in the opposite direction. This process repeats until you interrupt it.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(5,GPIO.OUT) #Left motor input A GPIO.setup(7,GPIO.OUT) #Left motor input B GPIO.setup(11,GPIO.OUT) #Right motor input A GPIO.setup(13,GPIO.OUT) #Right motor input B GPIO.setwarnings(False) while True: print "Rotating both motors in clockwise direction" GPIO.output(5,1) GPIO.output(7,0) GPIO.output(11,1) GPIO.output(13,0) time.sleep(1) #One second delay print "Rotating both motors in anticlockwise direction" GPIO.output(5,0) GPIO.output(7,1) GPIO.output(11,0) GPIO.output(13,1) time.sleep(1)#One second delay
Step 3: Uploading the code for the raspberry pi robot
After completing all the hardware setup, you need to download and copy this python program from here to your raspberry pi. This program called: robot.py, when executed using this command: sudo python robot.py, will bring life to your robot and it will begin moving when you press the push button. And you will notice how it avoids objects in front of its sensors and navigates freely.
This program is really simple. The robot is activated when the user presses the push button, after that the robot moves forward and checks whether any obstacles show up in front of it. Whenever your IR modules detects an object within 5cm in front of it, it tells the raspberry pi that an object is near it (sending digital LOW signals). Then the pi sends commands to the motor, making it move in the reverse direction and then turning right/left and again the robot moves forward by dodging the object. The robot gets de activated when we press the push button again.
After copying the code to your raspberry pi, you can make it truly wireless by using a smartphone battery bank to power it. And a USB wifi dongle to communicate with it. You can either extend the display of your laptop via VNC server and a LAN cable by using this tutorial. Or use SSH to connect remotely to your pi from the terminal wirelessly using this tutorial. And finally, check out the video showing the raspberry pi robot in action: