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
49,817 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 April 2021

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 and ultrasonic sensor. We will be building an alarm circuit which incorporates a piso 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 to close, the buzzer will go off and the red light will be lit. Once no obstacle is detected, the green light tells us we are doing OK. 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 that we will be utilizing is a very common model, the CSIRO for which is very inexpensive. We will learn the penult of the CSIRO for 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 resistors and C. S are all for ultrasonic sensor, a red light and a green, a piso 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 fourpence. VXI is connected to five votes 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. VXI is connected to the five real on the breadboard, and we connect the fabled real to five wheels on the Arduino. Ground is connected to the ground rail on the breadboard, and we connect that ground real on the. The trigger pin is connected to PIN two on the Arduino. And the equipment is connected to three Dwinell. For our piso buzzer. In negative end of the partial buzzer is connected by a three thirty ohm resistor to ground and the positive pin of our buzzer is connected to pin level on the Arduino. We have two leads in our circuit for the reality, the cattle or the short spin is connected by a three to one resistor to ground. And the A. or the longer positive pin is collected to pin it on you, Dwinell, for our Greenly, the cathode is connected, fired up three thirty one rises to the ground and the positive, A. pin is connected to PIN nine on the Adreno. That's the wiring of our circuit. OK, let's go ahead and wearable circuit. If you look at our ultrasonic sensor, you can see that it has fourpence PVC, which will be connected to five on your adrenal ground, which would be connected to ground. The trigger pin is going to be connected to PIN to the original and the echo pin is going to be connected to three on your adrenal. So let's go ahead and why this happened in 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 real to the ground and the breadboard first. Right, swimming the ground connection and next connect, be five-fold real. So we go from fireboats. And they are doing no to the five old that's where up the ultrasonic sensor next. So first, big ground pin. We'll go to ground when the Arduino. Next, we can wire up the VXI. What the five votes, then we have our pin o trigger pin. That goes to Pintu near Dwina. Our goes to pen three. Then we can wipe out Piso buzzer. The positive and the piso buzzer goes to 11 under Adreno. We have a three day day on resistor from ground or from the negative penalty, parts of us are going to grow. So just make sure. I'm just going to run that resistor straight to the ground here. Now, Connecter leads. Is is regulating. And greenlighting. In negative cathodes of the LEDs are connected to ground via 331 resistors, so connect those now. That's the first one and this is the second one. And finally, we need to connect the positive leads, so for the red, the A. Ghost, an eight on Dwina. And finally, for the green, the positive goes to my Adreno. OK, that completes the wiring for a circuit, OK? It's going to be cool. 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 and the alarm would go off. So right now, the green Olivia's lit. That means our circuit is in a ready or key 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 about five inches coming in. Four inches. And finally. It's good, no. All right, you see the. Alarm goes off, so the red light 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. Go ahead and upload that. And now you'll see when they start moving my hand toward the center again. And within six inches and coming closer, five inches. More than 40 inches, so as I start moving closer, the center should go off. We go so once as less than three inches, it goes off. One final tweak and 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. Kloser. Seven, Eric. All right, so you'll see us 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 two a.m. and the equipment has been three sensors connected up in three. And yet we know next comes our only deepens the Green-Eyed. The A. is connected depending on what we know, and the anode of the red light is connected to pin it on the adrenal finally for our buzzer. The positive kind of our buzzer is connected to an 11 under Adreno. The next global variable with the clear is the reach. This is the maximum allowable range before our alarm is triggered. The initial setting is two 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 light 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 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 pens are defined as outputs, and initially we would want to send a high signal to the green red pen to turn it on and turn off our red led. Now we are ready to go. Let's examine the look 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 and we have a short delay. Then we write high to the trigger and have a delay of five microseconds and then we write low again to that trigger. 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 reason for that to occur. To accomplish this, we use the pulse and 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 and function shortly. Next, we want to cochinita 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 direction that was taken from a Palsson function or that was retuned from our polzin function. And we can also determine the distance and centimeters by calling the microseconds two centimeters function passing in the direction. Next, we simply print those values out so we can see the distances, and that was what was displayed in us here on monitor, and here is where we have the actual logic to determine if our alarmism, if the inches that was returned is less than the range we know we should send out a lot. So that's where we would turn the Green Valley off to an underrated city and give that son the buzzer by using our tone function if we were OK and the inches that was written was greater than the range, we're good to go. So the degree would be set high, the red LCD 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 the 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's 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 is the link, so essentially the pulse in function reads a pulse either high or low on a pin. So if you specify that the value is high, Paulson would wait for the pentacle high, started time and then wait for it to go look. Hence its 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 arguments that you can specify for the pulse and function in first one, just which is the first one is the one that we used. It simply takes in the pin value and the value you want to track, whether it's high or low. You can also call the pulse and function using a time out additional parameter and basically what that says is it's in a number of microseconds to wait for the pulse to be completed. And if that time limit is reached, then the pulse and 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 CSIRO for model and the pinots were explained. So now you will be familiar with how to connect it in a circuit. You were 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 you are doing. Let's move on to our next project.