Build Raspberry Pi robots: Best Tutorial for beginners

 
Raspberry Pi robot DIY HackingIf 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.

Raspberry Pi robot DIY Hacking

Raspberry Pi robot components

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.
  3. Geared DC motors.
  4. L293D driver board.
  5. Robot chassis and wheels.
  6. Caster wheel.
  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.

Raspberry Pi robot DIY Hacking

IR sensor on the raspberry pi robot

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.

Raspberry Pi robot DIY Hacking

Raspberry Pi robot

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

Raspberry Pi robot DIY Hacking

Raspberry Pi GPIO pin out

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.

Raspberry Pi robot DIY Hacking

Connection diagram- Raspberry Pi IR sensor

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:

Raspberry Pi robot DIY Hacking

IR sensor output from python

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:

Raspberry Pi robot DIY Hacking

Raspberry Pi robot connection diagram

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:

Raspberry Pi robot DIY Hacking

L293D raspberry pi control logic

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:

Raspberry Pi robot DIY Hacking

Raspberry Pi robot assembly

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.

Raspberry Pi robot DIY Hacking

Raspberry Pi robot DIY Hacking

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:

Recommended Posts
Showing 59 comments
  • Jeremy
    Reply

    Hi, I need help finding a push button and 220 resistor, help ASAP would be great. Using this for schools Beta Club. Thanks!

  • James Kidd
    Reply

    Hello there,

    I was wondering whether the following item would work as a driver board and if it does how many I would have to purchase to control 4 motors

    https://www.ebay.co.uk/itm/161783712236

    Thanks

  • T.V.2211
    Reply

    Hi, does someone have sites for me where I can find the right stuff for builiding a robot on the raspberry pi 3? The links here doesn’t work at all. And besides I am completely new with the Raspberry pi so I have nooo idea what kind of stuff I need and which in specifically.

  • Namrah
    Reply

    Can I get full steps to make this? I’m new to this.

  • saksham malhotra
    Reply

    Could you please explain how you configured the pi to connect wirelessly using wifi? I currently use ethernet cable and putty(ssh) to connect to pi using my laptop. I have a raspberry pi 3 model B so, which has in built wifi so can you explain how should I connect to it wirelessly? Thanks in advance

  • Mudit
    Reply

    This is great. I was able to get this up in no time. Now i am working on controlling the bot using keyboard. One point though its a challenge to get robot looking good with all the wires out. I also want to get to the point where this you just strap on the pi and make it work. Wish me luck :)….Will post once i get this out.

  • Ashish
    Reply

    Which pins on L293D motor driver shield connect to Raspberry GPIO ?
    Pin numbers on board are 0-13 Aref and GND , I am not sure of which signify L293 D inputs and outputs

  • Khoi Sousa
    Reply

    I will answer to all questions:
    superruzzo: Yes, you will need 2 l293d boards
    m : Anykind of 9v battery should work!
    Dude: Google “pullup resistor circuit”
    Ranson Dot: Try reinstalling raspbian, try sudo apt-get update than try running the python program again
    kd : DOING THAT WILL FRY YOUR PI!!!
    Kaiv : Try going to Ebay
    kostas : You will need 2 driver boards. Try googling “l293d raspberrypi 4 motors” and to speed up the robot, try feeding the motor driver with 12v 1amp
    John Doe: Try using another tutorial
    husinul : Alok IS WRONG! YOU CAN USE SSH! (I tried with a turtlebot2 running ROS and even the arrow keys work!)
    Vega : It’s a bug in the code! Try using another code
    CM : Yeah it’ll work. Any L293D motor shield will work
    Dhiraj Gupta : About any RPM will work
    Saruj Sarup : It’s a bug in the code!
    Gokul : I’m afraid you can’t.
    partha sarathi banerjee : Open CV stands for Open Computer Vision which uses an webcam to process images (try googling for more info)

    Any other questions, email me at khoi.sousa@gmail.com

    Cheers!

    • Lia E.
      Reply

      Does it have to be geared

  • superuzzo
    Reply

    Hi!
    Link to DC Motors used for the project is broken.
    What DC Motors should I use (which specs)? Can you provide an example, please?

    Is this L293D driver board ok?
    http://www.ebay.com/itm/Push-Pull-Four-Channel-Motor-Driver-Module-Board-L293D-/131533986300?hash=item1ea00959fc:g:b~oAAOSwstxVeYG8

    What if I would use 4 wheels with 4 motors….
    Do I need to add one more L293D driver board?

    Thank you.

    S

  • m
    Reply

    Hello, do you have any recomendation for the number of mAh for the 9V battery? should a 260mAh enough and what will be the difference let says with a 600mAh? Thanks!

  • Dude
    Reply

    Where placed resistor on connection diagram?

  • S.yunus parvez
    Reply

    I can Connect Infra Red Sensor In the place of IR sensor Modules

    • Ranson Dot
      Reply

      It keeps saying “invalid syntax” I paid money for this I need help!!!

  • kd
    Reply

    can i connect a 12v battery to l293d and rpi ?

  • Kaiv
    Reply

    From where can i take the same l293d motor board like you

  • utkarsh
    Reply

    plz help..i copied the same code but when i want to switch off the robot and i press the button it gets deactivated for 1second and starts automatically..where i am making error..plz help as soon as possible..

    • jayakrishnan
      Reply

      import RPi.GPIO as GPIO #this should work i havent tried it yet
      import time
      GPIO.setmode(GPIO.BOARD)
      GPIO.setup(3,GPIO.IN)
      GPIO.setup(12,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
      GPIO.setup(16.GPIO.IN, pull_up_down=GPIO.PUD_UP)
      GPIO.setup(5,GPIO.OUT)
      GPIO.setup(7,GPIO.OUT)
      GPIO.setup(11,GPIO.OUT)
      GPIO.setup(13,GPIO.OUT)

      GPIO.output(5,0)
      GPIO.output(7,0)
      GPIO.output(11,0)
      GPIO.output(13,0)

      flag=0
      while True:
      j=GPIO.input(12)
      if j==1:
      flag=1
      print”ROBOT ACTIVATED”,j

      while flag==1:
      i=GPIO.input(3)
      k=GPIO.input(16)
      if i==0:
      print”obstacle dected on right”,i
      GPIO.output(5,1)
      GPIO.output(7,0)
      GPIO.output(11,1)
      GPIO.output(13,0)
      time.sleep(1)

      GPIO.output(5,0)
      GPIO.output(7,1)
      GPIO.output(11,1)
      GPIO.output(13,0)
      time.sleep(2)
      if k==0:
      print”obstacle dected on left”,k
      GPIO.output(5,1)
      GPIO.output(7,0)
      GPIO.output(11,1)
      GPIO.output(13,0)
      time.sleep(1)

      GPIO.output(5,1)
      GPIO.output(7,0)
      GPIO.output(11,0)
      GPIO.output(13,1)
      time.sleep(2)

      elif i==0 and k==0:
      print”obstacle on both sides”
      GPIO.output(5,1)
      GPIO.output(7,0)
      GPIO.output(11,1)
      GPIO.output(13,0)
      time.sleep(2)

      GPIO.output(5,1)
      GPIO.output(7,0)
      GPIO.output(11,0)
      GPIO.output(13,1)
      time.sleep(4)

      elif i==1 and k==1:
      print”no obstacles”,i
      GPIO.output(5,0)
      GPIO.output(7,1)
      GPIO.output(11,1)
      GPIO.output(13,0)
      time.sleep(0.5)
      j=GPIO.input(12)
      if j==1:
      flag=0
      print”deactivated”
      GPIO.output(5,0)
      GPIO.output(7,0)
      GPIO.output(11,0)
      GPIO.output(13,0)
      time.sleep(1)

      • jayakrishnan
        Reply

        Make sure to indent properly

  • daanish peerkhan
    Reply

    sir I did not understood from where did raspberry pi got power?

    • Alok
      Reply

      The power is from a power bank. The Raspberry Pi just needs 5v 1A Current. We can get it from a suitable power bank

  • kostas
    Reply

    Hello guys..how to speed up robot?thank you

  • kostas
    Reply

    hello..i have robot 4 wheel..i need two L293D driver board?how to connect them to raspberry??thank you

  • John Doe
    Reply

    i just realised an error in the inputs table. Why is it that when both inputs are the same (HIGH,HIGH or LOW LOW) braking occurs? Think thats a typo]

    Please rectify

  • husinul
    Reply

    do we can both use ssh raspi2 with my laptop through wifi and share the internet connected to the raspi by my laptop screen

    • Alok
      Reply

      No. I don’t think we can. You can just control the Pi. Pi should have another connection

  • Vega
    Reply

    I’d like to ask this again as I didn’t see a response:

    Why is only one sensor specified as pull up?
    GPIO.setup(3, GPIO.IN) #Right sensor connection
    GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Left sensor connection

    Great tutorial by the way :o)

  • ujwal vinay
    Reply

    this is a simple project for children.i like it very much

  • CM
    Reply

    Hi! Do you think this motor drive shield will work for this project? Thanks!

    http://www.dx.com/p/waveshare-l293d-motor-drive-control-shield-board-blue-367738#.VmHK37iDFBc

  • Dhiraj Gupta
    Reply

    what rpm dc geared motor to be used ?

  • Reply

    Hi there, You have done a fantastic job. I’ll definitely digg it and personally suggest to my friends.
    I’m confident they will be benefited from this site.

  • Nidhiya
    Reply

    Good stuff indeed (y)

  • Berly
    Reply

    Nice blog thanks for sharing useful information. For more robotics tutorials and learning visit Robotics tutorials

  • A
    Reply

    how do I increase robot’s speed? by increasing GPIO.output(13,0) 13 to some higher value?

  • Sahaj Sarup
    Reply

    Why is only one sensor specified as pull up?
    GPIO.setup(3, GPIO.IN) #Right sensor connection
    GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Left sensor connection

  • amal
    Reply

    how many tyre in this car

    • Midhun
      Reply

      2 Robot wheel and 1 Castor wheel.

  • Gokul
    Reply

    Amazing tutorial..
    How do i use whatsapp for commanding the robot?

  • partha sarathi banerjee
    Reply

    what is computer vision (Open CV) ?

    • Arvind Sanjeev
      Reply

      Did you try Googling ?

  • Midhun
    Reply

    I would to like make obstacle avoiding robot.please help me arvind
    would i need to buy wi-fi dongle for it.

  • Arun Yadav
    Reply

    Thanks for the post, it inspired to make my own, I made it using Arduino.

  • Anuvind Asok
    Reply

    Made it , it really worked :D , Thnks arvind

  • keerthana p
    Reply

    can i connect a laptop instead of a television in order to run a raspberry pi computer?

pingbacks / trackbacks

Leave a Comment

34 + = 36

Start typing and press Enter to search