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

Reading Analog Sensor Data Using Auto-ID

One of the best features about Vernier sensors is that they have a built-in autoID capability. You can automatically detect which sensor you are using and read a data point with just two simple commands.

Vernier.autoID(); Vernier.readSensor();

The Vernier.autoID() function is placed in the setup() portion of your sketch. This function checks for a standard analog (BTA) sensor connected to the Analog 1 port on the Vernier Arduino Interface Shield or an Analog Protoboard Adapter wired to Arduino pin A0 as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section. (Note the VernierLib library does not support sensors plugged into the Analog 2 port on the Vernier Arduino Interface Shield or a second Analog Protoboard Adapter. To collect data from a second sensor you will need to manually calibrate raw voltage readings as described in Reading Raw Voltage.)

If a sensor is found in the Analog 1 port, it will read information about it, including:

  • sensorName – the full name of the sensor found in the catalog
  • shortName – an abbreviated name often used for labeling columns in data tables
  • sensorUnits – a standard metric measurement
  • sensorNumber – the product identifier
  • calEquationType – the type of equation used for calibration. For most sensors, calEquationType = 1, indicating a linear calibration. A few sensors, such as temperature, require a more complex calibration equation.
  • slope – a linear calibration variable
  • intercept – a linear calibration variable
  • cFactor – a variable used in nonlinear calibrations
  • voltageID – the voltage read from sensors with built-in resistors
  • page – the calibration information stored in sensors with memory chips

To access information about the sensor, you would use a statement like:

Serial.print (Vernier.sensorName());

This statement will print the full name of the sensor on the Serial Monitor. The Serial Monitor is a popup window that allows you to see data sent to and from your Arduino. You can access the Serial Monitor from the Tools menu.

The Vernier.readSensor() function will read one data point from the sensor connected to the Analog 1 port. To continuously read data from the sensor, you should put the function in the loop() portion of your sketch. The sample sketch, VernierLibTutorialAnalogRead, will take sensor readings every half second and print the data with units to the Serial Monitor. You can try out different sensors simply by plugging in a new sensor and pressing the Reset button.

/* VernierLibTutorialAnalogRead (v2017)
 * This sketch reads a data point from a Vernier Analog (BTA) 
 * sensor once every half second and prints the sensor reading 
 * with units to the Serial Monitor.
 * 
 * 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
 
void setup() {
  Serial.begin(9600); //setup communication to display
  Vernier.autoID(); //identify the sensor being used
}

void loop() {
  sensorReading = Vernier.readSensor(); //read one data value
  Serial.print(sensorReading); //print data value 
  Serial.print(" "); //print a space
  Serial.println(Vernier.sensorUnits()); //print units and skip to next line
  delay(500); //wait half 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