Udemy

Ultrasonic Sensor Alarm in Arduino

A free video tutorial from Lee Assam
Electrical and Software Engineer, University Instructor
Rating: 4.4 out of 5Instructor rating
8 courses
55,896 students
Ultrasonic Sensor Alarm

Lecture description

This project introduces us to the HC-SR04 Ultrasonic Sensor. We will be creating an alarm circuit with a buzzer and warning LEDs. You will understand how an ultrasonic sensor works and how to interact with it in code. This is a very handy addition to your electronics projects for obstacle avoidance.

Learning Objectives:

  • How an ultrasonic sensor (HC-SR04) works
  • The pinouts and how to connect an ultrasonic sensor in a circuit
  • How the pulseIn() function works

Learn more from the full course

Arduino Bootcamp : Learning Through Projects

Build 15+ complete Arduino projects from scratch, a car controlled using an app, cell phone, games, LEDs, sensors, sound

09:27:06 of on-demand video • Updated November 2022

Build a remote-controlled car you can drive with a smart phone app
Build your own cell phone that you can make/receive calls and send/receive text messages with
Build games using the Arduino incorporating light, sound and joystick controllers
Understand components like ultrasonic sensors, motor drivers, servos, transistors, bluetooth/wifi/gsm modules
Understand many common electronic components and how they work
Have the confidence to build complex electronics projects
Learn how to prototype electronics projects
Be a confident maker and prototyper
English [Auto]
This project, we will be looking at a very useful component for your electronics projects an ultrasonic sensor. We will be building an alarm circuit which incorporates a piezo buzzer and red and green LEDs to warn when an object has been detected or comes within a specified range of our sensor. When the object is too close, the buzzer will go off and the red Led will be lit once no obstacle is detected. The green Led tells us we are doing okay. Let's take a look at the objectives by successfully completing this project. We will have mastered the following items. We will learn what an ultrasonic sensor is and how it works. This specific ultrasonic sensor we will be utilizing is a very common model, the SRO four, which is very inexpensive. We will learn the pinout of the C four and how to connect it in a circuit. We will also be introduced to the pulse in function and how it is used in conjunction with the ultrasonic sensor to perform measurements critical to the operation of our circuit. If you are ready to begin, let's get this project started. Here are the parts we will need an Arduino Uno, a USB cable, a breadboard three three 30 Ohm resistors and CSR four ultrasonic sensor, a red led and a green led. A piezo buzzer and connecting wires. Let's first address how ultrasonic sensors operate, similar to the mechanism by which bats use to navigate, which is sonar. The sensor transmits a wave signal, which is referred to as a ping and waits for an echo of that signal to be received. If the wave comes into contact with an obstacle, it is reflected back. And once the reflection is detected by the sensor, a calculation can be performed using the time it took for the wave to return. This is how the distance between the sensor and the object can be derived. The pulse in function is used to measure the time between when a waveform is sent until the time it is received. It actually accomplishes this by looking at the time a signal takes to transition from low to high or high to low. We will talk more about this function when we dive into the code. If you take a look at the layout of the ultrasonic sensor, you will notice it has four pins. VCC is connected to five volts on the Arduino and ground is connected to ground. The trigger and echo pins are connected to other digital pins on the Arduino, and the readings from these are used as part of the measurement process. Let's take a look at the circuit diagram for our project. So you'll notice here is the ultrasonic sensor and there are four pins. VCC is connected to the five volt rail on the breadboard and we connect the five volt rail to five volts on the Arduino. Ground is connected to the ground rail on the breadboard, and we connect that ground rail on the Arduino. The trigger pin is connected to pin two on the Arduino. And the echo pin is connected to pin three on the Arduino. For our piezo buzzer. The negative end of the buzzer is connected via a three 30 ohm resistor to ground. And the positive pin of our buzzer is connected to pin 11 on the Arduino. We have two LEDs in our circuit for the red led. The cathode or the shorter pin is connected via a 330 ohm resistor to ground. And the anode or the longer positive pin is connected to pin eight on the Arduino. For our green led, the cathode is connected via that 330 ohm resistor to ground and the positive anode pin is connected to pin nine on the Arduino. That's the wiring of our circuit. Okay, let's go ahead and wire up our circuit. If you look at our ultrasonic sensor, you can see that it has four pins VCC, which will be connected to five volts on the Arduino ground, which will be connected to ground. The trigger pin is going to be connected to PIN two on the Arduino, and the echo pin is going to be connected to pin three on the Arduino. So let's go ahead and wire this up in the circuit. When you connect your ultrasonic sensor, you'll want to connect the sensor and the receiver away on the facing outside of the breadboard. That way we can test the actual obstruction. So go ahead and connect that. Let's connect the ground rail to the ground on the breadboard. First. All right. So make that ground connection. And next, I'll connect the five volt rail. So we go from five volts. And the Arduino to the five volt rail. That's where the ultrasonic sensor next. So first the ground pin. We'll go to ground on the Arduino. Next we can wire up the VCC. We go to five volts, then we have our pin or trigger pin. That goes to pin two the Arduino. Our echo pin goes to pin three. Then we can wire up our piezo buzzer. The positive and the piezo buzzer goes to pin 11 on the Arduino. We have a three 30 ohm resistor from ground or from the negative pin of the buzzer going to ground. So I'll just make sure. I'm just going to run that resistor straight to ground here. Now we'll connect our LEDs. Is readily. And the green led. The negative cathodes of the LEDs are connected to ground via three 30 ohm resistor. So I'll connect those now. That's the first one. And this is the second one. And finally, we need to connect the positive of our LEDs. So for the red, the anode goes to pin eight on the Arduino. And finally, for the green, the positive goes to pin nine on the Arduino. Okay. That completes the wiring for our circuit. Okay, let's go ahead and upload the code. While that's uploading, I will open up the serial monitor. All right. So you'll notice initially that I'm displaying the distance that an obstacle is closest to the sensor. And initially we have set the range to be two inches. So if an object comes within less than two inches of the sensor, that's when the buzzer or the alarm would go off. So right now, the green is lit. That means our circuit is in a ready or okay status. And now I'm going to start moving my hand towards the sensor. So here we go. If you take a look at the measurements, you will see that the distance is decreasing. So six inches now, my hand is at five inches coming in. Four inches. And finally. It's still good. And now. All right. You see the. Alarm goes off. So the red Led will be lit and the buzzer will sound as soon as my hand is with less than two inches from the sensor. All right. Now I'm going to tweak the code and actually increase this to three inches. I'll go ahead and upload that. And now you'll see when I start moving my hand towards the sensor again. And within six inches, I'm coming closer. Five inches. I'm within four inches. So as I start moving it closer, the sensor should go off. There we go. So once it's less than three inches, it goes off. One final tweak. I'll change the range to five inches. So what that means is, is when my hand comes within five inches of the sensor, it should go off. So let's start. Bringing it closer. I want seven. Here I come. All right. So you'll see at about five inches, less than five inches, that's when the buzzer will go off. All right. So as you can see, it's fairly accurate for longer distances. And you can play with that range setting to tweak the distance, the allowable distance of an obstacle from the ultrasonic sensor. Let's take a look at the code for our ultrasonic sensor circuit. Initially, at the beginning, we define our trigger pin as two, since it's connected to pin two on the Arduino and the echo pin as pin three since it's connected to pin three on the Arduino. Next comes our Led pins, the green Led. The anode is connected to pin nine on the Arduino and the anode of the red Led is connected to pin eight on the Arduino. Finally for our buzzer. The positive pin of our buzzer is connected to pin 11 on the Arduino. The next global variable we declare is the range. This is the maximum allowable range before our alarm is triggered. The initial setting is two and inches. So what this means is if your obstacle comes within just less of two inches from the ultrasonic sensor, then the buzzer would go off and the red Led will be lit. You can tweak and change this variable accordingly to experiment with different ranges for your project. Next in our setup function, we have to do a few things. We first set up our serial communication speed. Then we initialize the sensor pins. The trigger pin is an output. Since this is the pin that generates the output pulse and the echo pin is an input since it reads the pulse that is reflected back. Of course, our green and red led pins are defined as outputs and initially we would want to send a high signal to the green led pin to turn it on and turn off our red led. Now we are ready to go. Let's examine the loop function. Initially to take a reading, we have to generate a clean, high pulse. So to do this, we first generate a low signal. We write low to the trigger pin. We have a short delay. Then we write high to the trigger pin, have a delay of five microseconds, and then we write low again to that trigger pin. So that would generate a clean, nice pulse which is sent out for us. When that pulse comes into contact with an obstacle and is reflected, it comes back. And what we need to do is measure the duration for that to occur. To accomplish this, we use the pulse function passing in our echo pin in which we are taking the reading and we want to specify that we're looking for that high to low transition. So we pass in high as the argument here. We will take a little more in-depth look at the documentation for the pulse in function shortly. Next we want to calculate the inches and centimeters to our obstacle. So there are a couple of convenience functions we can use microseconds to inches. We can call that function passing in the duration that was taken from our pulse in function or that was returned from our pulse function. And we can also determine the distance in centimeters by calling the microseconds to centimeters function passing in the duration. Next, we simply print those values out so we can see the distances. And that was what was displayed in our serial monitor. And here is where we have the actual logic to determine if our alarm is sound. If the inches that was returned is less than the range we know we should sound the alarm. So that's where we would turn the green off to an on the red and give that sound the buzzer by using our tone function. If we were okay and the inches that was returned was greater than range, we're good to go. So the led the green Led would be set high, the red Led will be turned off and there would be no alarm sounding from the buzzer. Lastly, in our program, these are the two convenience functions that do the calculations for us in microseconds to inches and microseconds to centimeters. So the calculations for these, if you require further documentation, there is a link here which explains what the calculations are and there are some documentation from the functions here. Let's take a look at the pulse in function. So I'm going to Google that. And here's the link. So essentially, the pulse function reads a pulse either high or low on a pin. So if you specify that the value is high, pulse in would wait for the pin to go high. At time and then wait for it to go low. Hence it's measuring the time that it takes for the reflection to come back from our obstacle. As you can see, there are a couple of different variations of of arguments that you can specify for the pulse function. The first one, just which is the first one, is the one that we used. It simply takes in a pin value and the value you want to track, whether it's high or low. You can also call the pulsing function using a timeout additional parameter. And basically what that says is it's the number of microseconds to wait for the pulse to be completed. And if that timeout is reached, then the pulsing function would just return zero. To summarize in this project, you learned what an ultrasonic sensor is and how it works. You were specifically introduced to the HC4 model and the Pinouts were explained. So now you will be familiar with how to connect it in a circuit. You will also introduced to the pulse in function and the important role it plays in taking measurements for the ultrasonic sensor. This ultrasonic sensor is a very useful component and can be incorporated to beef up your robot and car projects for obstacle avoidance. You now know how to use it and interact with it using your Arduino. Let's move on to our next project.