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

Picture of 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.

LEVEL: ADVANCED

Prerequisite:

  • 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

Picture of Get These Thing Setup

Hardware Requirements:

Software Setup:

  • sudo apt-get install pigpio

Step 3: Conneting the Stuff

Picture of 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
H2 ECHO Pin On USS2
L3 GPIO Pin of your choice for trigger2
H3 Trig Pin On USS2
L4 GPIO Pin of your choice for ECHO2
H4 ECHO Pin On USS2
GND GND Rail

Example connection on the USS

VCC	+5v
GND GND Rail
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

Picture of 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 @ http://Mikronauts.com # # 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(['127.0.0.1:11211'], 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()

Comments

SiDawg (author)2016-05-08

Don't forget to peel the clear plastic film off the blue sensor holders </pet peeve>

Meena_Bassem (author)2016-05-03

http://gpiozero.readthedocs.io/en/v1.2.0/api_input...

dude, use gpiozero library, but make sure first it's version 1.2.0

enjoy :D

ric96 (author)Meena_Bassem2016-05-03

nice, this makes stuff a lot simpler... I rote the code before gpiozero existed. But i will be looking forward to modifying it.

thnx

Meena_Bassem (author)ric962016-05-03

i did the same once too till i discovered it. there's even a library for "Motor" and "Robot".
things can't be any easier
ps : the documentation is for V1.2.0. it didn't work for me at first because, later, i realized i was using an older version of the library :D