|
Re: Help with Ultrasonic Sensor on Arduino Uno
i have done this a couple times, those sensor are great.
First ensure it is wired correctly:
when looking down at the sensor, with the pins at the bottom, supply ground on the right most pin and +5v to the next pin to the left.
connect the 3rd pin from the left to analog input 0.
Below is a basic version of a sketch that will work with this sensor.
long inches = 0;
long raw=0;
long rawavgsum=0;
long rawavg=0;
const int avgcounter = 90;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
}
void loop(){
for (int i = 0; i<avgcounter;i++)
{
raw = analogRead(0);
rawavgsum=raw+rawavgsum;
delay(10);
}
rawavg = rawavgsum/avgcounter;
inches = rawavg/2; //At 5 V on Vcc, the sensor is rated 9.8mV/ inch. pin range isis 0-1024 counts per 0-5V, 1 count = 4.8mV, 2 counts = 1 inch
Serial.print("avg Inches: ");
Serial.print(inches);
Serial.print("\n");
rawavgsum=0;
delay(250);
}
|