Introduction: Add 6 Ultrasonic Distance Sensors to Existing Raspberry Pi Robot

This is NOT a tutorial to make a Raspberry Pi robot with 6 Ultrasonic Sensors.

This is a tutorial shows the best method to add 6 Ultrasonic Sensors directly to a raspberry pi without the use of any micro controller like Arduino.



  • I would be referring to Ultrasonic Sensors as USS in short.
  • All +5v, +3v and GND pins need to be connected to a +5v, +3v or GND rail respectively.

Step 1: Watch This Video to Understand What We Will Be Making

The Raspberry Pi's GPIO work on 3.3v and USS works on 5v, although we can always use resistor to limit current, Bi-Directions Level converters are a much more elegant solution.

Step 2: Get These Thing Setup

Hardware Requirements:

Software Setup:

  • sudo apt-get install pigpio

Step 3: Conneting the Stuff

Example connection on the Bi-Directional Level Shifter

LV	+3.3V
HV +5V
L1 GPIO Pin of your choice for trigger1 H1 Trig Pin On USS1 L2 GPIO Pin of your choice for ECHO1
L3 GPIO Pin of your choice for trigger2
H3 Trig Pin On USS2
L4 GPIO Pin of your choice for ECHO2

Example connection on the USS

VCC	+5v
Trigg H1 Level Shifter
Echo H2 Level Shifter

As you can see we have two USS using one same Level shifter so for 6 USS we have 3 Level Shifters.

You can refer to this document for connecting the USS through Level Shifter to the Raspberry Pi (Click Here)

Step 4: Finally Running All 6 Sensors

This is the Python code i use to run all the 6 USS

As you can see Memcached can also be use to extract values from this script.

This scrip works on the pins as mentioned in the example document provided in the previous step, pin numbers need to be edited specifically.

#!/usr/bin/env python
# # HC-SR04 interface code for the Raspberry Pi # # William Henning @ # # uses joan's excellent pigpio library # # Does not quite work in one pin mode, will be updated in the future #

import time import pigpio #import memcache #mc = memcache.Client([''], debug=0) def _echo1(gpio, level, tick): global _high _high = tick

def _echo0(gpio, level, tick): global _done, _high, _time _time = tick - _high _done = True

def readDistance2(_trig, _echo): global pi, _done, _time _done = False pi.set_mode(_trig, pigpio.OUTPUT) pi.gpio_trigger(_trig,50,1) pi.set_mode(_echo, pigpio.INPUT) time.sleep(0.0001) tim = 0 while not _done: time.sleep(0.001) tim = tim+1 if tim > 50: return 2900 return _time

pi = pigpio.pi('localhost',1234)

if __name__ == "__main__": my_echo1 = pi.callback(10, pigpio.RISING_EDGE, _echo1) my_echo0 = pi.callback(10, pigpio.FALLING_EDGE, _echo0) my_echo1 = pi.callback(25, pigpio.RISING_EDGE, _echo1) my_echo0 = pi.callback(25, pigpio.FALLING_EDGE, _echo0) my_echo1 = pi.callback(8, pigpio.RISING_EDGE, _echo1) my_echo0 = pi.callback(8, pigpio.FALLING_EDGE, _echo0) my_echo1 = pi.callback(5, pigpio.RISING_EDGE, _echo1) my_echo0 = pi.callback(5, pigpio.FALLING_EDGE, _echo0) my_echo1 = pi.callback(12, pigpio.RISING_EDGE, _echo1) my_echo0 = pi.callback(12, pigpio.FALLING_EDGE, _echo0) my_echo1 = pi.callback(16, pigpio.RISING_EDGE, _echo1) my_echo0 = pi.callback(16, pigpio.FALLING_EDGE, _echo0)

while 1:

print "DISTANCE 1: ",(readDistance2(24,10)/58) print "DISTANCE 2: ",(readDistance2(9,25)/58) print "DISTANCE 3: ",(readDistance2(11,8)/58) print "DISTANCE 4: ",(readDistance2(7,5)/58) print "DISTANCE 5: ",(readDistance2(6,12)/58) print "DISTANCE 6: ",(readDistance2(19,16)/58) #mc.set("d1",(readDistance2(24,10)/58)) #mc.set("d2",(readDistance2(9,25)/58)) #mc.set("d3",(readDistance2(11,8)/58)) #mc.set("d4",(readDistance2(7,5)/58)) #mc.set("d5",(readDistance2(6,12)/58)) #mc.set("d6",(readDistance2(19,16)/58)) time.sleep(0.05)

#   my_echo1.cancel()
#   my_echo0.cancel()
Robotics Contest 2016

Participated in the
Robotics Contest 2016

Make it Move Contest 2016

Participated in the
Make it Move Contest 2016

Sensors Contest 2016

Participated in the
Sensors Contest 2016