Introduction: Ultrasonic Sensor Interface With Arduino

Products used in this instructable are purchased from www.robotscraft.com

1) Arduino Uno R3

http://robotscraft.com/arduino-boards/6-arduino-un...

2) Ultrasonic sensor

http://robotscraft.com/distance-sensors/41-ultraso...

The Ultrasonic range finder is an ultrasound sensor from Parallax able of detecting objects up to a 3 mts distance. The sensor counts with 3 pins, two are dedicated to power and ground, while the third one is used both as input and output. The pin dedicated to make the readings has to be shifting configuration from input to output according to the PING specification sheet.

Step 1: Interfacing With Arduino

First we have to send a pulse that will make the sensor send an ultrasound tone and wait for an echo. Once the tone is received back, the sensor will send a pulse over the same pin as earlier. The width of that pulse will determine the distance to the object.

Step 2: Code

For code click link below

Code

int ultrasoundValue = 0;
int timecount = 0; // Echo counter int ledPin = 13; // LED connected to digital pin 13 22 void setup() { beginSerial(9600); // Sets the baud rate to 9600 pinMode(ledPin, OUTPUT); // Sets the digital pin as output } void loop() { timecount = 0; val = 0; pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output /* Send low-high-low pulse to activate the trigger pulse of the sensor*/ digitalWrite(ultraSoundSignal, LOW); // Send low pulse delayMicroseconds(2); // Wait for 2 microseconds digitalWrite(ultraSoundSignal, HIGH); // Send high pulse delayMicroseconds(5); // Wait for 5 microseconds digitalWrite(ultraSoundSignal, LOW); // Holdo¹ /* Listening for echo pulse*/ pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input val = digitalRead(ultraSoundSignal); // Append signal value to val while(val == LOW) { // Loop until pin reads a high value val = digitalRead(ultraSoundSignal); } while(val == HIGH) { // Loop until pin reads a high value val = digitalRead(ultraSoundSignal); timecount = timecount +1; // Count echo pulse time } /* Writing out values to the serial port*/ ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue serialWrite(‘A’); // Example identifier for the sensor printInteger(ultrasoundValue); serialWrite(10); serialWrite(13); /* Lite up LED if any value is passed by the echo pulse*/ if(timecount > 0) { digitalWrite(ledPin, HIGH); } /* Delay of program*/ delay(100); }

Comments

author
seamster (author)2016-07-11

Nice, thanks for sharing :)