In this illustration we will going to wire the Ultrasonic HC-SR04 with our Arduino board, to build a parking sensor which emits a beep as the sensor approaches or detect an obstacle. The closer the obstacle, the greater the frequency of the beep.
Required Components
1x Arduino UNO/MEGA/NANO/PRO
1x Ultrasonic Ranging Sensor Module HC-SR04
1x buzzer
1x Solder Less Bread Board
1x Jumper Wire
1x Resistor 330 Ohms
Wiring Diagram
This diagram composed of Arduino and HC-SR04 Ultrasonic Ranging Sensor and a Buzzer with resistance of 330 Ohms to limit the current.
The TONE, Normally used to produce sounds with Arduino, shows conflict with the library Ultrasonic also used in this project. The sound of the beep can be change as required by changing the value variable frequency and time. The pin Trigger sensor will connected to pin 13 of the Arduino and the pin Echo sensor will connected to Arduino pin 10. The sensor running at 5v. the buzzer goes on port 2 of the MCU Arduino. The variable cmMsec stores the value of the distance between the sensor and the obstacle. A series of command IF check the value cmMsec and update the variable delay which is used later in the program to determine the beeper drive frequency. The values read by the sensor can also be monitored by the serial.
Arduino Sketch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/* 14CORE Parking Sensor using HC-SR04 */ #include <Ultrasonic.h> // Include the HC-SR04 Code Library #include <NewTone.h> // Include the Buzzer Driven Code Library #define time 500 int frequency = 2000; int 14Buzz = 2; int delay = 1000; #define PINO_TRIGGER 13 // Set HC-SR04 Trigger pin to Arduino pin 13 #define PINO_ECHO 10 // Set HC-SR04 Echo pin to Arduino Pin 10 Ultrasonic ultrasonic (PINO_TRIGGER, PINO_ECHO); //Initialize the HC-SR04 Sensor void setup () { pinMode (14Buzz, OUTPUT); // Define Arduino as Buzzer Ouput Serial.begin (9600); // Initializing the serial at baud rate 9600 } void loop () { float cmMsec, inMsec; long microsec ultrasonic.timing = (); // Retun time of the signal from Echo cmMsec = ultrasonic.convert (microsec, Ultrasonic :: CM); //Calculate Mili Second distance in centimeter // Set delay according to the distance if (cmMsec> 80) { delay = 2000; } else if (cmMsec> 50 && cmMsec <80) { delay = 1500; } else if (cmMsec> 30 && cmMsec <50) { delay = 1200; } else if (cmMsec> 10 && cmMsec <30) { delay = 700; } else if (cmMsec <10) { delay = 300; } // Displays HC-SR04 data in centimeters in Serial Monitor Serial.print ("Centimeter"); Serial.print (cmMsec); Serial.print ("Delay"); Serial.println (delay); // Sends beep Newtone (Pinofalante, frequency, time); delay (delay); } |
Download Code library for Tone Generator | Zip
I just could not leave your website prior to suggesting that I actually
loved the standard info an individual provide
in your guests? Is gonna be back steadily to check up on new posts
Having read this I thought it was very informative.
I appreciate you spending some time and effort to put this content together.
I once again find myself personally spending a lot of time both reading and commenting.
But so what, it was still worth it!
I always spent my half an hour to read this webpage’s posts everyday along with a mug of coffee.
Good day! This is my first visit to your blog!
We are a team of volunteers and starting a new project in a community in the same niche.
Your blog provided us useful information to work on. You have
done a marvellous job!