Last Updated:05/10/2021
How To 2 - Six Ultrasonic Sensors with an UNO
Lessons >> How To >> How To 2 - Six Ultrasonic Sensors with an UNO
Lesson Contents
In example 3 of the tutorial on using the HC-SR04 Ultrasonic Sensor
I created a function that would allow multiple HC-SR04 Ultrasonic sensors to used in a simple way.I was very happy with the function and had just started to do some trials no my car when I realised I had an issue. I wanted to use PWM control on the speed controller and have multiple HC-SR04's and just didn't have enough pins on an UNO as each HC-SR04 requires 2 pins and PWM control of the L298N motor controller board would require six pins.
I could upgrade to a Mega 2560 but decided to take a different route and did some tests with a 74HC595 8 bit shift register as this would give me access to multiple extra pins as these chips can be daisy chained. .These chips are very cheap (�2.50 for 5 on ebay) and come in a lot of the Arduino starter kits. If you are buying online make sure you buy the DIP version as they are breadboard friendly and easy to solder.
I'm not going to do a full tutorial on the 74HC595 as there is an excellent one at https://www.arduino.cc/en/Tutorial/Foundations/ShiftOut. So let's just look at the basics.
Below is the basic circuit, the chip requires 3 pins from the Arduino but gives us 8 outputs as seen in the diagram below.
The output pins are numbered as in the diagram below with pin Zero (0) coming from the top side of the chip with the seven other pins along the bottom.
Now we come to the results of sending a value.
If we send a value of 142 (binary 10001110) we see that the pins on the 74HC595 are set to the same pattern but reversed as pin 0 is the lowest bit.
Once we inderstand the principle it now allows us to turn the pins on and off just as we would with a normal digitalWrite() to an Arduino pin except all the pins receive a value at the same time.
When it comes to using these pins with the HC-SR04 Ultrasonic sensor there are some limitations as this chip only bit shifts outwards, it is the equivelent of having the pinMode set to OUTPUT.
Although there is a matching chip for bit shifting values into the Arduino, the issue is that we need to use the pulseIn() function so the echo pins need to be linked to a real Arduino pin. However as the trigger just sends a pulse it can use a pin from the 74HC595.
Although this example is dealing with the Ultrasonic sensor, on my Arduino car I will also use some pins from the 74HC595. copnnected to the L298N motor driver board as this will save another 4 pins and will use the same logic as this example.
The wiring diagram is shown below for 3 Sensors.
Example 1: HCSR04BitShiftV1.ino
I have altered the code from example 3 on the HC-SR04 tutorial and just commented out the old function lines to make it easier to explain how the function has been changed. The function still takes a value for the triggerPin and echoPin except this time the trigger pin is not an Arduino pin and instead is the binary value of the pin we want to go HIGH on the 74HC595.
The example could be exapnded to use all the free pins allowing 9 Ultrasonic sensors to be controlled from a single UNO, however taking into account the pins I will need for the L298N motor driver my realistic limit is 7 and for that I will need to daisy chain a second 74HC595 for the motor driver board outputs.
Click to Download code:HCSR04BitShiftV1.ino
/* 29/09/2021
*
* HCSR04BitShiftV1
*
* Trigger HC-SR04 units with a 74HC595
*
* Works :-)
*
* pins used
*
* pin 2 echo 1 HCSR04
* pin 3 echo 2 HCSR04
* pin 4 echo 3 HCSR04
*
* pin 8 74HC595 ST_CP pin 12
* pin 11 74HC595 DS pin 14
* pin 12 74HC595 SH_CP pin 11
*/
//HCSR04 echo pins
const int echoPin1 = 2;
const int echoPin2 = 3;
const int echoPin3 = 4;
//binary pin values
const int triggerPin1 = 1; //00000001
const int triggerPin2 = 2; //00000010
const int triggerPin3 = 4;//00000100
//Pin connected to ST_CP of 74HC595
const int latchPin = 8;
//Pin connected to DS of 74HC595
const int dataPin = 11;
//Pin connected to SH_CP of 74HC595
const int clockPin = 12;
void setup() {
Serial.begin(9600);
Serial.println("HCSR04BitShiftV1...");
//HCSR04 echo pins
pinMode(echoPin1,INPUT);
pinMode(echoPin2,INPUT);
pinMode(echoPin3,INPUT);
//74HC595
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
Serial.print(1);
Serial.print(" - ");
Serial.println(get74HC595DistanceHCSSR04(triggerPin1,echoPin1 ));
delay(500);
Serial.print(2);
Serial.print(" - ");
Serial.println(get74HC595DistanceHCSSR04(triggerPin2,echoPin2));
delay(500);
Serial.print(3);
Serial.print(" - ");
Serial.println(get74HC595DistanceHCSSR04(triggerPin3,echoPin3));
delay(500);
}
int get74HC595DistanceHCSSR04(int funcTriggerPin, int funcEchoPin){
/*
Serial.print(funcTriggerPin,BIN);
Serial.print(" - ");
Serial.println(funcEchoPin);
*/
//An unsigned long is needed because the numbers produced can be much larger
//than an int can hold (max value 32,767)
unsigned long duration; //time taken for echo
int myDistance;
//digitalWrite(funcTriggerPin, LOW);
digitalWrite(latchPin, LOW);//stop anything changine while writing
shiftOut(dataPin, clockPin, MSBFIRST, 0);//send all zeros
digitalWrite(latchPin, HIGH);//allow pins to change
//A tiny delay 2 millionths of a second
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
//same as turing the LED on in the blink sketch
//digitalWrite(funcTriggerPin, HIGH);
digitalWrite(latchPin, LOW);//stop anything changine while writing
shiftOut(dataPin, clockPin, MSBFIRST, funcTriggerPin);//pin to go HIGH
digitalWrite(latchPin, HIGH);//allow pins to change
//pin stays HIGH (5v) for 10 microseconds to trigger a pulse
delayMicroseconds(10);
//Pin taken LOW (0v) after pulse triggered ready for next pulse
//digitalWrite(funcTriggerPin, LOW);
digitalWrite(latchPin, LOW);//stop anything changine while writing
shiftOut(dataPin, clockPin, MSBFIRST, 0);//send all zeros
digitalWrite(latchPin, HIGH);//allow pins to change
duration = pulseIn(funcEchoPin, HIGH);
myDistance = duration * 0.34 / 2;
return myDistance;
}
Additional Resource Links
For more information on using the 74HC595 see the tutorial at https://www.arduino.cc/en/Tutorial/Foundations/ShiftOut.Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use How To 2 - Six Ultrasonic Sensors with an UNO as a reference.