9.12 Example: SonarRangerDisplay

/* sonarRangerDisplay
This script uses the Velleman VMA 306 SONAR to determine 
the distance to an objectThe round-trip distance is displayed 
on the serial plotter. Be sure to turn on the serial monitor 
from the Arduino IDE. */

#define VCC 2
#define TRIG 3
#define ECHO 4
#define GND 5
long distance, duration;

void setup(){
   Serial.begin(9600);
   pinMode(VCC, OUTPUT);
   pinMode(TRIG, OUTPUT);
   pinMode(ECHO, INPUT);
   pinMode(GND, OUTPUT);
   digitalWrite(VCC, HIGH);
   digitalWrite(GND, LOW);
}

void loop(){
   // send 10 uSec pulse to TRIG
   digitalWrite(TRIG, LOW);
   digitalWrite(TRIG, HIGH);
   delayMicroseconds(10);
   digitalWrite(TRIG, LOW);
   // read the echo delay
   duration=pulseIn(ECHO, HIGH);
   distance=duration * 0.017;
   Serial.print(25); // These 4 lines will keep the
   Serial.print(" "); // serial plotter scale between
   Serial.print(0);// 0 and 25 provided that the
   Serial.print(" ");// object distance/2.54 is less than 25
   Serial.println(distance/2.54);
}

 

License

Icon for the Creative Commons Attribution 4.0 International License

Applied Electrical Engineering Fundamentals by David J. McLaughlin is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book