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?
1.Raspberry Pi B/B+ or 2 and basic peripherals: SD card, keyboard, mouse, etc.
2.IR sensor modules like this.
7.Breadboard and double side tape.
8.Male to male/Female to male jumpers.
9.9V Battery and connectors.
10.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 pithen using a python script checks whether the GPIO pins connected to theIR sensor modules goes low. If it does go low, then it commands the DCmotors 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. Todo 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 asper 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 apython file- irtest.py:import RPi.GPIO as GPIOimport timeGPIO.setwarningsFalseGPIO.setmodeGPIO.BOARDGPIO.setup3, GPIO.IN #Right sensor connectionGPIO.setup1, GPIO.IN, pull_up_down=GPIO.PUD_UP #Left sensor connectionwhile True:i=GPIO.input3 #Reading output of right IRsensorj=GPIO.input1 #Reading output of left IRsensor
if i==0: #Right IR sensor detects anobjectprint “Obstacle detected on Left”,itime.sleep0.1elif j==0: #Left IR sensor detects anobjectprint “Obstacle detected on Right”,jtime.sleep0.1After saving this file and running it: “sudo python irtest.py”. You will noticethat when you block the sensor with your hand, following output getsprinted on the screen:
Step 2: Connecting the motors with L293D
After testing the IR sensor modules, next you need to connect and test the theL293D 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:
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 deactivate 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 GPIOimport timeGPIO.setmodeGPIO.BOARDGPIO.setup,GPIO.OUT #Left motor input AGPIO.setup,GPIO.OUT #Left motor input BGPIO.setup11,GPIO.OUT #Right motor input AGPIO.setup13,GPIO.OUT #Right motor input BGPIO.setwarningsFalsewhile True:print “Rotating both motors in clockwise direction”
GPIO.output,1GPIO.output,0GPIO.output11,1GPIO.output13,0time.sleep1 #One second delayprint “Rotating both motors in anticlockwise direction”GPIO.output,0GPIO.output,1GPIO.output11,0GPIO.output13,1time.sleep1
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 pushbutton. And you will notice how it avoids objects in front of its sensors and navigates freely.
Raspberry Pi robot DIY HackingThis 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 (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 deactivated 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 viaVNC 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:
Low cost PCB at PCBWay - only $5 for 10 PCBs and FREE first order for new members
PCB Assembly service starts from $30 with Free shipping all around world + Free stencil
Extra 15% off for flex and rigid-flex PCB