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..
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)
Arduino IDE 1.6.6.
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.
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'.
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.
digitalWrite (ledPin, LOW); // Here, LOW means off and HIGH means on.
digitalWrite (trigPin, HIGH);
digitalWrite (trigPin, LOW);
if(distance <=30) // If the sensor detects an obstacle less than 30 cm in distance, the LED will start to blink.
digitalWrite (ledPin, HIGH);
if(distance >=30)// If no obstacle is there within 30 cm, the Led should turn off.
digitalWrite (ledPin, LOW);
IF EVERYTHING WENT WELL, IF YOU TAKE YOUR HAND INFRONT OF THE SENSOR, THE LED WILL BLINK!