Introduction: Ultrasonic Sensor Testing With Arduino.
IN A SIMPLE WAY!
Step 1: The Intro...
Hi! This is my first Instructable. Here, I am going to show you how to test your Ultrasonic Sensor in a very simple way.
So, how does an Ultrasonic Sensor works?
It is simple, as in the picture, the Ultrasonic Sensor has two " Eyes". One eye sends an Ultrasonic wave, which is more than the maximum limit that humans can hear. When the wave hits an obstacle, it rebounds. Then the other eye receives the rebounded wave, which calculates the distance between the obstacle based on the time taken for the wave to rebound.
Note: I am only 13, so if you found any grammar mistakes, please ignore it! And the code is written completely by me! And no one helped me in the electronics or this instructable. I made it completely myself.
Step 2: Parts Required..
Hardware:
1 x Arduino (I used UNO R3 but any model works)
1 x Ultrasonic sensor.
A few female jumper cables.
A few male jumper cables.
1 x A/B type USB cable (Usually included with Arduino)
Software;
Arduino IDE 1.6.6.
THATS IT!!
Step 3: Wiring... (Very Simple!)
Connect the VCC pin of the Sensor to the 5V pin of your Arduino.
Connect the GND pin of the Sensor to any of the GND pins on the Arduino.
Connect the ECHO pin of the Sensor to the pin 9 on the Arduino.
.Connect the TRIG pin of the Sensor to the pin 10 on the Arduino.
THATS ALL!!
Step 4: The Code..
Copy this code and paste it in your Arduino IDE and press UPLOAD button.
// Ultrasonic Sensor testing code. Written by a 13 year old.
#include // Imports the NewPing Library.
int ledPin =(13); // Add the onboard LED on pin 13.
int trigPin = (10); // Add the Trig pin on pin 10.
int echoPin = (9); // Add the ECHO pin on pin 9.
int duration, distance; // Add types 'duration' and 'distance'.
void setup()
{
pinMode (ledPin, OUTPUT); // The LED must be controlled by Arduino, it means it is an output type.
pinMode (trigPin, OUTPUT);// Same as above, the TRIG pin will send the ultrasonic wave.
pinMode (echoPin, INPUT); // The ECHO pin will recieve the rebounded wave, so it must be an input type.
}
void loop()
{
digitalWrite (ledPin, LOW); // Here, LOW means off and HIGH means on.
digitalWrite (trigPin, HIGH);
delay(50);
digitalWrite (trigPin, LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
if(distance <=30) // If the sensor detects an obstacle less than 30 cm in distance, the LED will start to blink.
digitalWrite (ledPin, HIGH);
delay(50);
if(distance >=30)// If no obstacle is there within 30 cm, the Led should turn off.
digitalWrite (ledPin, LOW);
delay(50);
Serial.print("cm");
Serial.println(distance);
}
IF EVERYTHING WENT WELL, IF YOU TAKE YOUR HAND INFRONT OF THE SENSOR, THE LED WILL BLINK!
8 Comments
10 months ago on Step 4
// Ultrasonic Sensor testing code. Written by a 13 year old.
// Imports the NewPing Library.
int ledPin = (13); // Add the LED on pin 13 and GND.
int trigPin = (10); // Add the Trig pin on pin 10.
int echoPin = (9); // Add the ECHO pin on pin 9.
int duration, distance; // Add types 'duration' and 'distance'.
void setup() {
pinMode(ledPin, OUTPUT); // The LED must be controlled by Arduino, it means it is an output type.
pinMode(trigPin, OUTPUT); // Same as above, the TRIG pin will send the ultrasonic wave.
pinMode(echoPin, INPUT); // The ECHO pin will recieve the rebounded wave, so it must be an input type.
Serial.begin(9600); //in-order to begin serial monitor
}
void loop() {
digitalWrite(ledPin, LOW); // Here, LOW means off and HIGH means on.
digitalWrite(trigPin, HIGH);
delay(50);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance <= 60) // If the sensor detects an obstacle less than 30 cm in distance, the LED will start to blink.
digitalWrite(ledPin, HIGH);
delay(50);
if (distance >= 60) // If no obstacle is there within 30 cm, the Led should turn off.
digitalWrite(ledPin, LOW);
delay(50);
Serial.print("cm");
Serial.println(distance);
}
just two mistakes. but well-done!!
2 years ago
Hello
Great code.
By the way where do you find the "pulseIn" function?
Thanks
3 years ago
when i am testing the distance is showing as 0 CMS
please help me how to verify this problem
Reply 2 years ago
Same problem but no answer the reason wire damage or sensor damage?
Tried every code no , checked wire connections , I guess sensor is fine though
:(
Question 3 years ago on Step 3
How can I make this go more Distance
Thanks
Question 4 years ago
This coding are which language
7 years ago
Great instructable! I'm looking forward to seeing more!
Reply 7 years ago
Thank you! I will surely make more soon!