Ultrasonic Sensor HC-SR04 alarm system
utopian-io·@pakganern·
0.000 HBDUltrasonic Sensor HC-SR04 alarm system
#### What Will I Learn?
In this intructable you will learn how to make an alarm system using the Ultrasonic Sensor HC-SR04, buzzer and 7 LEDs with shift register. and does the shift register works, so lets start this.

#### REQUIREMENTS
We need this couple of components in this tutorial.

- Ultrasonic Sensor HC-SR04
- Buzzer
- LEDs
- 74HC595N Shift register
- Arduino board
- Breadboard
- Jumper wires
- 220 ohms resistor
#### Difficulty
- Intermediate
#### Tutorial Contents
Lets start this tutorial by following the circuit diagram that i made on fritzing

The HC-SR04 ultrasonic ranging sensor, can provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.

We were going to use Shift register to lessen the amount of wires that will connected to the arduino board, beacause we use 6-7 LEDs here, a shift register used for data storage or for the movement of data and are therefore commonly used inside calculators or computers to store data such as two binary numbers before they are added together, or to convert the data from either a serial to parallel or parallel to serial format.

We will be using the 220 ohms resistor to control the ammount of current that flows on the led, just to extend their lifespan and not to damage them beacasue of too much current their recieved. we will connect the the resistor to the short leg of the led or the common cathode GND part of the led, the long LEG of the led should be the anode or the VCC part. a piezo speaker or buzzer will be connected to the pin 8 on the arduino as indicated on the sketch , the short leg of the buzzer should be connected to the GND rail on the breadboard.

**SOFTWARE**
We are done building the circuit so lets start to set up and make a code for this. we are going to use the arduino ide, to set the sketch for this, if you dont have make sure to download the Arduino IDE for your specific operating system. I’ll leave a link to where you can download this software: https://www.arduino.cc/en/Main/Software

We are going to add ZIP libraries for the sketch like what i did on my previous tuts, thanks to https://github.com/dusnoki/ultrasonic-led-buzzer-no-delay for this code,
**buzzer pinout**
int tonePin = 8; // buzzer pin
**ultrasonic pinout**
int trigPin = 9; // sensor trigger pin
int echoPin = 10; // sensor echo pin
**shift register pinout for the leds**
int clockPin = 11; // shift register clock pin
int latchPin = 12; // shift register latch pin
int dataPin = 13; // shift register data pin
**you can copy the the code below;**
<p>
<hr/>
<pre><code>int tonePin = 8; // buzzer pin
int trigPin = 9; // sensor trigger pin
int echoPin = 10; // sensor echo pin
int clockPin = 11; // shift register clock pin
int latchPin = 12; // shift register latch pin
int dataPin = 13; // shift register data pin
unsigned long previousMillisLEDS = 0; // initialization of the previosMillis for LEDS
unsigned long previousMillisLOW = 0; // initialization of the previosMillis for LOW distance buzzer
unsigned long previousMillisMID = 0; // initialization of the previosMillis for MED distance buzzer
unsigned long previousMillisHIGH = 0; // initialization of the previosMillis for HIGH distance buzer
const int intervalLEDS = 100; // interval of refreshing the LEDS state
const int intervalLOW = 800; // interval of the LOW distance buzzer
const int intervalMID = 400; // interval of the MED distance buzzer
const int intervalHIGH = 100; // interval of the HIGH distance buzzer
const int freqLOW = 1500; // frequency of the LOW distabce buzzer
const int freqMID = 1800; // frequency of the MED distabce buzzer
const int freqHIGH = 2000; // frequency of the HIGH distabce buzzer
const int durLOW = 100; // on time of the LOW distabce buzzer
const int durMED = 100; // on time of the MED distabce buzzer
const int durHIGH = 80; // on time of the HIGH distabce buzzer
const byte patterns[9] = { // initialization of the patterns the LEDS are going to display
B00000000, // all LEDS OFF
B00000001, // 1 LED ON
B00000011, // 2 LEDS ON
B00000111, // 3 LEDS ON
B00001111, // 4 LEDS ON
B00011111, // 5 LEDS ON
B00111111, // 6 LEDS ON
B01111111, // 7 LEDS ON
B11111111, // 8 LEDS ON
};
int prox = 0; // initialization of the proximity value (0-8)
int dur; // initialization of the duration between the Trigger and Echo signal of the sensor
int dist; // initialization of the distance between the sensor and the object in front of it (in centimeters)
void setup() {
pinMode(tonePin, OUTPUT); // set tone pin to OUTPUT
pinMode(trigPin, OUTPUT); // set trigger pin to OUTPUT
pinMode(echoPin, INPUT); // set echo pin to INPUT
pinMode(clockPin, OUTPUT); // set clock pin to OUTPUT
pinMode(latchPin, OUTPUT); // set latch pin to OUTPUT
pinMode(dataPin, OUTPUT); // set data pin to OUTPUT
}
void loop() {
unsigned long currentMillis = millis(); // set the currentMillis variable to the current number of milliseconds from the start of the loop
if ((unsigned long)(currentMillis - previousMillisLEDS) >= intervalLEDS) { // check if the time between the current time and previous time for LEDS is larger or equal to the interval the LEDS should stay on
digitalWrite(latchPin, LOW); // set the latch pin to LOW
digitalWrite(trigPin, LOW); // set the trigger pin to LOW
delayMicroseconds(2); // delay 2 microseconds
digitalWrite(trigPin, HIGH); // set the trigger pin to HIGH and send out a sound signal
delayMicroseconds(100); // delay 100 microseconds
digitalWrite(trigPin, LOW); // set the trigger pin to LOW
dur = pulseIn(echoPin, HIGH); // caluclate the duration between the trigger and echo
dist = dur / 2 / 29; // calculate distance in centemeters based on the speed of sound
prox = map(dist, 0, 48, 8, 0); // map the distannce between 0 and 48 cm to a value between 0 and 8
shiftOut(dataPin, clockPin, MSBFIRST, patterns[prox]); // send the pattern to the shift register based on the prox value (0-8)
digitalWrite(latchPin, HIGH); // latch the shift register
previousMillisLEDS = currentMillis; // set the previousMillis for LEDS to the current time in milliseconds from the start of the loop
}
if (prox < 0) { // if we get a proximity value less than 0 set it to 0
prox = 0;
}
else if (prox == 6) { // if we get a proximity value of 6
if ((unsigned long)(currentMillis - previousMillisLOW) >= intervalLOW) { // check if the time between the current time and previous time for LOW buzzer interal is larger or equal to the interval the buzzer should stay on
tone(tonePin, freqLOW, 100); // set the tone pin to the LOW frequency and an on time of 100 milliseconds
previousMillisLOW = currentMillis; // set the previousMillis for LOW buzzer interal to the current time in milliseconds from the start of the loop
}
}
else if (prox == 7) { // if we get a proximity value of 7
if ((unsigned long)(currentMillis - previousMillisMID) >= intervalMID) { // check if the time between the current time and previous time for MID buzzer interal is larger or equal to the interval the buzzer should stay on
tone(tonePin, freqMID, 100); // set the tone pin to the MID frequency and an on time of 100 milliseconds
previousMillisMID = currentMillis; // set the previousMillis for MID buzzer interal to the current time in milliseconds from the start of the loop
}
}
else if (prox == 8) { // if we get a proximity value of 8
if ((unsigned long)(currentMillis - previousMillisHIGH) >= intervalHIGH) { // check if the time between the current time and previous time for HIGH buzzer interal is larger or equal to the interval the buzzer should stay on
tone(tonePin, freqHIGH, 80); // set the tone pin to the HIGH frequency and an on time of 80 milliseconds
previousMillisHIGH = currentMillis; // set the previousMillis for HIGH buzzer interal to the current time in milliseconds from the start of the loop
}
}
}</code></pre>
<p>
if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. in this gif file i remove the resistor form the led beacause ive test to put it on the external battery adn the light so dim so i pull out the shift registers on the leds, when the ultrasonic detects the moving object near the sensor it should blinks the leds in sequence.

I hope you enjoy this actitvity if want to learn how arduino works, and how to make a sketch, then maybe this site http://educ8s.tv/ might help you, i was inspired by http://sudokin.com/ for this wonderful stuff. thank you
**You can also check my previous posts:**
<a href="https://utopian.io/utopian-io/@pakganern/tm1637-4-digit-7-segment-display-with-arduino">TM1637 4 digit 7-segment display with Arduino</a>
<a href="https://utopian.io/utopian-io/@pakganern/how-to-adjust-led-brightness-using-potentiometer-visual-programming-xod">How to adjust LED brightness using potentiometer/ visual programming XOD</a>
<a href="https://utopian.io/utopian-io/@pakganern/how-to-make-12-led-chaser-without-shift-register">How to make 12 LED chaser without shift</a>
<a href="https://utopian.io/utopian-io/@pakganern/control-led-using-push-buttons-visual-programming-using-xod">Control LED using Push Buttons/ Visual programming using XOD</a>
<a href="https://utopian.io/utopian-io/@pakganern/control-servo-motor-using-joystick">Control Servo motor using Joystick</a>
<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@pakganern/ultrasonic-sensor-hc-sr04-alarm-system">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>