View Single Post
  #2   Spotlight this post!  
Unread 24-01-2015, 14:41
dave1027 dave1027 is offline
Registered User
FRC #1027
 
Join Date: Feb 2007
Location: West Springfield, MA
Posts: 14
dave1027 will become famous soon enough
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);
}