Arduino Basics Lesson 3-1: Ultrasonic DME

For the “Arduino For Kooks” course, I recommend you get the Arduino Starter Kit available here.

Now that we’ve learned the basics of digital and analog signalling on an Arduino, let’s start exploring with different kinds of sensors. The ultrasonic sensor module is a rudimentary SONAR device that emits and detects a particular high-frequency sound pulse. We can use this pulse to determine the location and relative position of objects.

The Circuit:

Using jumpers, connect +5V on the Arduino to VCC on the sensor and GND to GND. Connect the sensor’s Trigger and Echo pins to the Arduino at D2 and D4 respectively.

The Sketch:

const int trigPin = 2;
const int echoPin = 4;
void setup() {
void loop()
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print("in, ");
long microsecondsToInches(long microseconds)
{return microseconds / 74 / 2;
long microsecondsToCentimeters(long microseconds)
{return microseconds / 29 / 2;}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.