Vernier Software and Technology
Vernier Software & Technology
Arduino and Vernier Sensors

Motion Detector

The Motion Detector is probably the most popular of all the Vernier digital sensors. It is used to measure the position of moving objects by sending ultrasound from the gold foil diaphragm and then “listening” for an echo.

The Arduino measures the time from the ultrasound generation to the echo return. The library function, Vernier.readMotionDetector(), uses the speed of sound in room-temperature air and the time for an echo return to calculate the distance to the object in centimeters. Place this function in the loop() portion of your sketch.

distance = Vernier.readMotionDetector();

The sample sketch, VernierLibTutorialMotionDetector, reads the distance to an object and displays the results to the Serial Monitor or Serial Plotter. It assumes you have plugged a Motion Detector into the Digital 1 port on the Vernier Arduino Interface Shield or a Digital Protoboard Adapter wired to Arduino pins 2, 3, 4, and 5 as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section.

/* VernierLibTutorialMotionDetector (v2017)
 * This sketch reads the distance to an object and displays the
 * results to the Serial Monitor or Serial Plotter.
 * 
 * Plug the Motion Detector into the Digital 1 port on the 
 * Vernier Arduino Interface Shield or into a Digital Protoboard 
 * Adapter wired to Arduino pins 2, 3, 4, and 5.
 */

#include "VernierLib.h" //include Vernier functions in this sketch
VernierLib Vernier; //create an instance of the VernierLib library

float distance; //create global variable to store sensor reading
 
void setup() {
  Serial.begin(9600); //setup communication to display
}

void loop() {
  distance = Vernier.readMotionDetector(); //read one data value
  Serial.print(distance); //display data value 
  Serial.println(" cm"); //print units
  delay(100); //wait a tenth of a second
}


Contents

Introduction

Vernier Engineering Contest

Vernier recognizes instructors for creative uses of Vernier sensors to introduce engineering concepts and/or practices. Vernier Engineering Award »

Go to top