Introduction: Sonar
In order to do this project you will need:
-Four Wires
-Arduino Uno
-Ultra Sonic Module
-This sensor measures the distance between it and another object.
-The setup requires to have a wire connected to each of the pins of the sonar sensor.
Steps:
-Connect the VCC connector using a wire to the 5V located on the Arduino UNO board
-TRIG to the desired pin on the Arduino board(for example pin 9)
-ECHO to the desired pin on the Arduino board(for example pin 10)
-GND to GND on the Arduino UNO board
Coding:
-before setup() you must initialize the pins along with the sensor_value and float duration(you'll need it later)
int TRIGpin=9;
int ECHOpin=10;
float duration=0;
int Sensor_Value=0;
void setup()
-you must set up the pins by doing pinMode(10,OUTPUT) and pinMode(9,INPUT) with Serial.begin(9600)
void loop()
-Inside the loop, you must send a LOW and HIGH signal, to the output pin, using digital write command and in between, there must be a delay of 2 seconds and a delay of 10 seconds after. You can you the delay() command or the delay microseconds command
- now you must have a duration command that will allow values measured by the sensor to be "spit out" at equal intervals. The setup will look like this:
pulseIn(echo,HIGH)
now you need to serial print the value and now you're done with the setup.
Calibration
- In order to calibrate the sensor, take measurements at a certain distance, for example, every 2 inches with multiple tests for each inch.
- take the average and find the equation of it using y= ax+b.
- now the only new code you'll need is to indicate that duration is the x and the distance is y along with initializing distance in the setup
New Codes
distance=(a*(duration)+b)
int distance
}
Data Table
Trial 1 Trial 2 Trial 3 Average
4inch: 587 593 641 590
6inch: 860 911 911 894
8inch: 1281 1239 1228 1247
10inch:1513 1489 1507 1503
12inch:1791 1788 1794 1791
The line of best fit: y=0.007x+0.016
distance=0.007(duration)+0.016
4) To indicate if the door is open, put the sonar right up against the door while it is closed. If the sonar reads 0 inches then it is closed. Anything higher means it is opened.
Final Code
int TRIGpin=9;
int ECHOpin=10;
float duration=0;
int distance;
void setup() {
pinMode(9,OUTPUT);
pinMode(10,INPUT);
Serial.begin(9600);
Serial.println("1");
}
void loop()
{
digitalWrite(9,LOW);
delayMicroseconds(2);
digitalWrite(9,HIGH);
delayMicroseconds(10);
//Serial.println(Sensor_value);
duration= pulseIn(ECHOpin,HIGH);
//Serial.println(duration);
distance=(0.007*duration)+0.016;
Serial.println(distance);
delay(200);
}
Comments