Edit Article

It is very easy to use an ultrasonic sensor with Arduino, these can be used to make a robot, musical instruments, etc.. Here is a very simple minimal tutorial for using an ultrasonic sensor with Arduino.

EditSteps

  1. Both_ultrasonic_sensors.jpg
    1
    Connect the sensor to the Arduino, and write a simple program that displays the distance measured to the Serial Monitor
    • There are two commonly available ultrasonic sensors, the HC-SR04 from Sain-Smart, and the Parallax PING. They work in a very similar manner. The HC-SR04 has four wires, and the Parallax has 3 wires. This tutorial covers using the HC-SR04.
    • There are four pins on the ultrasonic sensor. Here is how we want to connect them.
      Ultrasonic_tester.jpg
      • VCC - connect this to one of the 5V power connections on the Arduino UNO.
      • GND - connect this to one of the ground connections on the Arduino UNO.
      • Trig - connect to port 12.
      • Echo - connect to port 13.
  2. 2
    Enter in the code. Here is the minimal code you need to get the sensor working. We'll just report the distance measured to the Serial Monitor.

We could really use your help!

Can you tell us about
room organization?
Yes
No
room organization
how to rearrange your room
Can you tell us about
hairstyling?
Yes
No
hairstyling
how to make your hair look shorter than it is
Can you tell us about
estate planning?
Yes
No
estate planning
how to set up an estate
Can you tell us about
singing?
Yes
No
singing
how to sight sing
Thanks for helping! Please tell us everything you know about
...
Tell us everything you know here. Remember, more detail is better.
Tips
Provide Details.
Please be as detailed as possible in your explanation. Don't worry about formatting! We'll take care of it. For example:
Don't say: Eat more fats.
Do say: Add fats with some nutritional value to the foods you already eat. Try olive oil, butter, avocado, and mayonnaise.

EditSample Code

#define trigPin 12
#define echoPin 13
void setup(){
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(){
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else{
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}


These sensors work by emitting a sound pulse, and measuring the time it takes for the pulse to reflect back (we cannot hear the sound, i.e. ultrasonic). Velocity = distance / time, and distance = velocity * time, the sensor will report the time it takes for the pulse to reflect, but we actually want half that amount, because going there and back is actually twice the distance to the object. Then we just need the velocity, which for us is the speed of sound in air.


Upload this code to your Arduino. If you place an object in front of the sensor within its operating range, you should see the distance measured using the Serial Monitor. I was able to get it to work using my hand. Good Luck !

Serial_16cm.png
Ultrasonic_sensor_long_test.JPG

Article Info

Categories: Format | Music

Thanks to all authors for creating a page that has been read 643 times.

Did this article help you?
Yes No