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

Produce a Tone Out that Depends on Sensor Reading

A simple, but useful, sketch with analog sensors is to use the raw voltage from any analog sensor to control the frequency of the sound produced. This sketch is based on the Tone2 Tutorial.

Read the voltage from the analog sensor in the normal way. Then use the Map and Tone commands to produce the tone. There are several ways that we have found this kind of system useful. First, for blind or low-vision students, it is great to have a tone produced that indicates the sensor reading. Also, sometimes when you are looking for a change in a sensor reading, it is easier to listen for a change than it is to stare at a meter or a graph.

Note that you do not need to deal with calibration in this sketch, since it is the raw voltage from the sensor that controls the frequency.

You can modify this sketch in many ways:

  • If you want to change the range of frequencies produced, change the last two numbers in the Map command, which are the lowest and highest frequencies to be produced.
  • If you have a sensor that puts out a voltage in a limited range, change the first two numbers in the Map command to the range of input voltages to be expected from the sensor.
  • As written, the program sends the analog voltage measure to the Serial Monitor. Because this is unnecessary; remove the Serial.println statement, if you like.
/*
VernierFrequency (v 2017.07)

This sketch produces a tone on a speaker connected to pin D9 of
the Arduino. It is based on the Tone 2 tutorial.
(see http://arduino.cc/en/Tutorial/Tone2).

Note that you do not deal with calibration here. Any Vernier
Analog sensor which uses the Sig1 (0 to 5 volt) line will work
if it is plugged into the BTA 1 connector.

If you want to change the range of frequencies, change the last two
numbers in the line:  
int thisPitch = map(sensorReading, 400, 1000, 120, 1500)
They are the minimum and maximum frequencies produced.
If you want to change the sensitiviy of the system, change the first
two numbers of the same line. They are the range of expected readings 
from the sensor.

See www.vernier.com/arduino for details.

*/
int outputPin=9;

void setup() 
{
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below
  // depending on the range your sensor's giving:
  int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

  // play the pitch:
  tone(outputPin, thisPitch, 10);
  delay(1);        // delay in between reads for stability
}


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