Introduction: Sonar Range Finder Arduino
Welcome to the Sonar Range Finder!
These instructions are to set up and test a simple Arduino Sonar sensor, and each step will have photos for reference.
Step 1: How to Connect the Sonar
- Plug the sonar sensor into the breadboard
- Connect the "VCC" wire into the "5V" port in the Arduino, the "TRIG" wire into pin 9 (not limited to pin 9), the "ECHO" wire into pin 10 (not limited to pin 10), and the "GND" wire into the GND port.
Step 2: Writing the Code
- Set your variables "TRIG" and "ECHO" to their respective pin numbers. Also create a variable named "distance"
- In the void setup( ) { , initialize the 'TRIG" and "ECHO" pins with PINMODE( ), setting the TRIG variable as the OUTPUT and ECHO as INPUT. Inculde "Serial.begin(9600) under void set up as well. End with " } "
- In the void loop ( ) {
- digitalWrite (TRIG, LOW);
- delayMicroseconds (2);
- digitalWrite (TRIG, HIGH);
- delayMicroseconds (10);
- This sends out the sound wave, so in order for the echo pin to receive it we need to create...
- Set "int duration = pulseIn (ECHO, HIGH);
- Serial.printIn (duration);
- delay (2000); }
Step 3: Test It Out!
To calibrate and test the values, we will set up a table:
DISTANCE(inches)DURATION
2
4
6
8
With this table, we will use a ruler and a sturdy flat object (like a notebook) to record the sensor value readings when the object is at a specified distance from the sensor. You can turn the breadboard on its side to make the measurements easier to record.
Once the duration values have been recorded, create a graph of your values, with DISTANCE on the X axis and DURATION on the Y axis. Then create an equation for that line.
Now to test the distance values, replace the "Serial.printIn (duration);" line under "int duration" with your equation. Be mindful that you have to replace X and Y with distance and duration respectively. Then under that write "Serial.printIn (distance);" .
By doing this we should see if the Serial Monitor will read out the approximate distance or your flat object!