How to create a DIY proximity sensor with an Arduino Uno board and an ultrasonic sensor
Hello,
Thank you for being of reader. This tutorial is made for you if you are a kid or adult who want to learn more about STEM and how to make a homemade proximity sensor device with a Arduino uno board and HC-SR04.
It will be easy and fun to do at home knowing that you just need a computer and a couple of electronic components that you can buy online with the link that I have put down there for you for( less than $30)
Let's get started. ;-)
Ultrasonic sensor
An ultrasonic sensor is quite used around the world and even in objects that you used everyday. It starts with your smartphone to detect if you have your head by the phone (which turns on or off the speaker. Yeah!!! I have found it to be really helpful, lol). You can also find it on your car with the alarm when backing up. It allows you to protect your bumper and not crash your car.
The ultrasonic sensor we used is called "HC-SR04" and is really inexpensive (~$3-4 a piece and sometimes less). It has 4 pins.
1 - HC-SR04 details:
4 pins : VCC, GND, Trigger, Echo
Resolution: 0.3 cm (1/100 of inch)
Range of measurement: 2cm to 450 cm
Voltage ( 5 volts)
2 - How the ultrasonic sensor works
The HC-SR04 will emit a sound by applying a 10 microsecond signal on the "Trig " pin and will wait for it to come back and receive it on the "Echo" pin if it finds a object on it's path.
Since the speed of the sound is 340m/s, we need to measure the time the "echo" pin will receive the signal. We will divide that time by two since it went through the path twice ( it went and came back).
3 - Formula for the sound speed
Here is the formula of the speed of the sound:
Speed = (Distance/Time)
We know the speed of the sound (340 m/s). If we know the the time, we can figure out the distance.
Distance = Speed (meter) x Time (in seconds)
With the formula above, the speed is in meter but will be translate into inches as well (It is just used for the calculus).
4 - Example of calculus
Let's say we see a lightning form a distance and that we hear the thunder 6 seconds later. Knowing the speed's sound is 340 m/s, we can deduct that the lightning was at 2000 meters (2 km, ~1.2 miles) with the formula above.
What brings us to following calculation:
Distance (meter) = Speed (meter) x Time (in seconds) = 340 x 6 = 2040 meters (~1.2 miles)
We are now done for the calculus explanation.
Buzzer
The buzzer is a small electronic component that transform a electronic signal into a sound. It's like a speaker but in less sophisticated. It makes sounds like the 80's video games under different frequencies. The one we use for for the schematic works 5 volts and had 2 pins ( 2 wires: 5V and GND).
Materiel to build the ultrasonic proximity sensor system with an Arduino board
Your first need to gather all the following materiel (Click on them to buy them online on Amazon):
An Arduino Uno board or Arduino Mega (depending of what you have home, but there is no difference)
Jumper wires
All the links can be found if you want to buy them and make your own ultrasonic proximity system with the Arduino board.
Schematic of the watering system
Right bellow is the complete electronic schematic of the whole project.
Explanation of the wiring of the proximity sensor to the arduino
A 9 volts battery is connected to the pin of the Arduino Uno to furnish power. The (+) pin is connected on the "Vin" on the Arduino. And the (-) pin of the battery is connected on the "GND" on the Arduino side.
The buzzer need is connected on pin 5 for the positive side of the buzzer. And the negative pin of the buzzer is connected to "GND" on the Arduino.
The Ultrasonic sensor got 4 pins which are : GND, VCC, Echo and Trigger. "GND" is connected to GND on the Arduino board, "VCC" is connected to "+5V" on the board. "Echo" is connected to pin 8 of the Arduino and "Trig" goes to pin 9.
Code of the proximity sensor and the Arduino Uno board
#define Echo 8 // Declaration Echo pin on pin 8
#define Trig 9 //Declaration Trig pin on pin 9
// defines variables
long Time; // variable for the Time of sound wave travel
int distance; // variable for the distance measurement in centimeters
int distance_inches; // variable for the distance measurement in inches
void setup() {
pinMode(Trig, OUTPUT); // Sets the Trig pin
pinMode(Echo, INPUT); // Set the Echo pin
pinMode(6,OUTPUT); // Pin for buzzer
Serial.begin(9600);
}
void loop()
{
// Prepare the Trig pin
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10); // Activate the Trig pin by sensing a 10 microsecnds: Send the wave out
digitalWrite(Trig, LOW);
Time = pulseIn(Echo, HIGH); // Time of reception of wave back
// Calculating the distance
distance = Time * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
distance_inches = distance * 2.54;
//Condition: if distance inferior than 15cm, turn on the buzzer
if(distance <15)
{
digitalWrite(6,HIGH); // Turn the buzzer on
delay(1); // Wait for 1 seconds
digitalWrite(6,LOW); // Turn the buzzer off
delay(1);
}
//Distance in centimeters
Serial.print("Distance in centimeters: ");
Serial.print(distance);
Serial.println(" cm");
//Distance in inches
Serial.print("Distance in inches: ");
Serial.print(distance_inches);
Serial.println(" in");
}
How to test it
Connect all the electronic components (Proximity sensor, battery, buzzer and others) to the Arduino board like showed in the electronic schematic of the Arduino project above.
To test the the overall schematic, we need to upload the above code to the Arduino Uno board with the Arduino IDE.
Keep the Arduino board connected to the computer with a USB cable to see the threshold is reached
In the code, we put the threshold for the ultrasonic sensor at 15 cm (~half a foot). So to start with the testing, put your hand or a object beyond that threshold. When above that threshold, the ultrasound sensor connected to the Arduino board should not detect anything and the buzzer will be silent
Now, move your hand or the object below the threshold of 15cm (~half a foot), the the buzzer connected to the Arduino board should make a sound.
When doing all that manipulation, you can also take a look on the Arduino Terminal when the threshold is reached.
Possible improvements of the project
We can add little of chili to the project, by that I mean we can add more electronic modules to it.
It could be to adding a LCD screen. So we can read the values directly to the screen. Or add different levels of proximity alarm when the approaching the sensor (eg: when obstacle at: 100cm (~1 yard), then 50 cm (one and a half feet), then 20cm (~1 foot) and so on).
Kids are really inventive when working on improvements. They got ideas that we don't see. ;-)
Do not hesitate to share your comments and ideas in the comment section at the end of the article.
Video of the proximity sensor in action
Here is a quick video of the ultrasonic proximity sensor in action.
Follow on the Facebook and the newsletter for weekly activities
Don't forget to subscribe to the newsletter (at the bottom of the page) and the Facebook group if you want more weekly STEM related activities for education. Such as: electronics, 3D printing, chemistry, coding, robotics and more. If you want to learn how to make a robot, learn how to code, Learn how to make a video game and how to use electronics, please join the newsletter and the Facebook group (Create and build and learn STEM with fun projects).
Stay connected for another STEM related articles for kids and adults. ;-)
Comments