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

Triggering a Buzzer with Sensor Input

One of the more common activities when building a sensor-based control system is to trigger an indicator when the sensor reading exceeds a threshold value. The sample sketch, VernierLibTutorialDCUThreshold, will turn on a buzzer connected to DCU line D1 when the applied force on a Vernier Dual-Range Force Sensor exceeds 5 newtons. This sketch assumes the buzzer is connected to lines D1 and GND on the DCU. Plug the sensor and DCU into the Analog 1 and Digital 2 ports, respectively, on the Vernier Arduino Interface Shield or into Analog Protoboard Adapter and Digital Protoboard Adapter wired as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section.

Note you can substitute other Vernier Analog (BTA) sensors. Simply modify the value of the threshold variable in the sketch. You can also combine up to three electrical devices in your project (we recommend using only the first three digital lines for this type of application).

/* VernierLibTutorialDCUThreshold (v2017)
 * This sketch turns on a DC electrical device connected to DCU
 * line D1 when a Vernier Analog (BTA) sensor exceeds a
 * threshold value.
 * Plug the DCU into the Digital 2 port on the Vernier Arduino 
 * Interface Shield or into a Digital Protoboard Adapter wired 
 * to Arduino pins 6, 7, 8, and 9. Plug the sensor into the 
 * Analog 1 port on the Vernier Arduino Interface Shield or 
 * into an Analog Protoboard Adapter wired to Arduino pin A0.

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

float sensorReading; //create global variable to store sensor reading
float threshold=5.0; //set threshold value for a Dual-Range Force Sensor
void setup() {
  Vernier.autoID(); //identify the sensor

void loop() {
  sensorReading = Vernier.readSensor(); //get one data point
  if(sensorReading > threshold) //check if threshold exceeded
    Vernier.DCU(1); //turn on DCU line D1
    Vernier.DCU(0); //turn off all DCU lines
  delay(500); //wait half second



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