Arduino Distance Sensor Alarm

by Vigas B in Circuits > Arduino

1247 Views, 1 Favorites, 0 Comments

Arduino Distance Sensor Alarm

Screen Shot 2020-06-16 at 4.16.18 PM.png

My name is Vigas Balachandran and today I will be providing you with my computer engineering final project. For this project, I have chosen to do an Arduino Distance Sensor alarm using a distance sensor and a piezo. This circuit works because using the distance sensor, you are able to send a value to the piezo allowing it to play a sound. In this case, when the distance sensor reads a certain distance, the piezo plays a sound with a frequency that corresponds to it. Now let's go onto the materials!

Materials Needed

Screen Shot 2020-06-16 at 4.17.59 PM.png

For this project, you will need 5 main components. These components are...

1. Piezo (to make the sound)

https://www.amazon.ca/BQLZR-Black-Electromagnetic-...

2. Ultrasonic Distance Sensor (to read the distance)

https://www.amazon.ca/Sainsmart-HC-SR04-Ranging-De...

3. 220 Ohms Resistor (to reduce the voltage for the RGB Led) (3 resistors needed)

https://www.amazon.com/EDGELEC-Resistor-Tolerance-...

4. RGB Led (to display the light corresponding to the closeness of the object)

https://www.amazon.com/EDGELEC-Tri-Color-Multicolo...

5. Arduino Uno MicroController (to control all of the components)

https://www.amazon.com/Arduino-A000066-ARDUINO-UNO...

The last component is wiring, which you will need to connect all of your components together. One optional material is a breadboard, which can help make you circuit much easier to organize.

Now, let's go onto the wiring!

Wiring

Screen Shot 2020-06-18 at 11.57.22 AM.png

For the wiring, the picture above can help you with connecting all of the components together.

1. Connecting the Power Source
Since we are using components that each require a power source, the smart move is to connect the power from the Arduino straight into the breadboard so that multiple components can use it.

2. Connecting the Ultrasonic Distance Sensor
Now that you have the power connected, the next thing that you need to do is connect the Ultrasonic Distance Sensor to the Arduino. To do so, connect the both the trig and echo pin to any one of the digital pins (the spot does not matter, but you need to remember the placement so you can implement it into the coding). After connecting both of those, connect the GND pin into ground, and then the Vcc pin into power.

In this case, the echo pin will be the input and the trigger pin will be the output. The echo pin will get the actual readings from the sensor, which will then be converted into actual distance using the Arduino, and then outputted to the piezo through the trigger pin.

3. Connecting the Piezo
After the distance sensor is connected, you know need to connect the Piezo to the Arduino. Connect the positive end of the Piezo to one of the analog pins of the Arduino, and then connect the negative end of the Piezo to ground.

4. Connecting the RGB Led
Finally, the last thing to do for the wiring is to connect the RGB Led to the Arduino. If you notice on the led, there are four pins, three of which have a colour specified and the remaining being the cathode. Connect each of the colour pins to a 220 Ohms resistor, and then the resistors to a digital pin each. The resistor will control the voltage going into each of the colours, therefore reducing the brightness of the Led. With more resistance, the less bright it will get, hence why the resistance is not too high. The last thing to do is connect the cathode to ground.


By completing these steps, you will have finished the wiring for this project. Now the last thing to do is to add in the coding for the Arduino.

Coding 1 - Variables

Screen Shot 2020-06-18 at 1.16.08 PM.png

The first thing that you need to do is declare/initialize the variables. In this case, it will be all of the components that are in a digital pin or an analog pin. To do so, you have to type the following...

const int (variable name) = (variable value);
int (variable name) = (variable value);

The variable name is anything that you want to call the variable while the variable value is the digital/analog pin that you set it to. For the trig, echo and piezo pin, you want to keep the variable as const, because you do not want to change the value of the variable. Whereas on the other hand for the RGB Led, you want to keep them as a normal int because their values may change in the middle of the circuit according to the color that you choose.

The picture above shows how my variables are set.

Coding 2 - Setup Method

Screen Shot 2020-06-18 at 1.43.20 PM.png

Now that you have declared your variables, you now need to incorporate them into your actual circuit by using the setup method. In the setup method is where you establish which pin is output or input. You can do so, by typing in the following...

pinMode((variable name),(INPUT/OUTPUT));

An example of this that I previously mentioned was the trigger and echo pin. I previously stated that the echo pin was the input and the trigger pin was the output. Here is how I showed it.

pinMode(echoPin, OUTPUT);
pinMode(trigPin, INPUT);

By incorporating all of the variables into the setup method, you are essentially adding them into the circuit and then can be later controlled with the loop method.

Coding 3 - Loop Method

Screen Shot 2020-06-18 at 2.16.10 PM.png

The last part of coding is the loop method. This part is however, the largest part of the coding section, since this method is where all the controlling happens. The first thing that you want to do is declare variables for reading the distance of the sensor. You can do so by typing this...

long duration,inches;
int timer;

By adding a comma, you can declare multiple variables of the same type at once. Now that these variables are declared, you can add the code for the ultrasonic sensor. This code is specifically for the sensor, allowing it to read the pulse, and then convert that into a distance. This is shown below.

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
inches = duration * 0.0133 /2;/

By using this code, you can find the duration (in time) of the signal from the sensor, which is then calculated to find the distance in inches.

The next part is actually setting the piezo to turn on and off when it reads a certain distance. You can do so by using an if statement.

ex.)

else if ((inches>25)&&(inches<=50)) {
digitalWrite(piezoPin, HIGH);
delay(50);
digitalWrite(piezoPin, LOW);
analogWrite(green, 69); (The colour values chosen here are specific to my circuit)
analogWrite(red, 255);
analogWrite(blue, 0);

}

By using an if statement, you can set specific parameter for the distance, and then set the piezo to high then low to create the sound. You can also see that the colour pins are set to analogWrite. The reason why they are analogWrite is because now we are using specific values instead of just high and low. By using these values, you can get the specific colours you want for the RGB Led. One important thing to remember is the time variable that you created at the beginning of this method. The time variable controls the amount of delay that the piezo has, as the object comes closer to the sensor. The closer the object gets, the less delay there is. You can show that in the code by putting the following at the end of the method.

time = inches*10;
delay(time);

In the end, your final loop code should have the initialization of the distance and time variables, the ultrasonic sensor code, the if statement blocks (each block represents how many stops your sensor will read), and finally the line for calculating the time delay.

Video of Circuit

If you have made it this far into the instructable, I will now congratulate you on finishing the Arduino Distance Sensor Alarm. If you want to see how exactly the final circuit works, click the video below. Thank you for completing this project!

Ps. There is no sound in the video because it is just a screen recording. You can see that the piezo is working though because of the vibrations.