I hooked up two photocell sensors and a piezo speaker to my breadboard and arduino to test analog input and tone output.

First, I needed to find the maximum and minimum sensor values in order to map the range to the speaker’s pitch range. To do this, I asked the microcontroller to read the values from pin 0. When I tried to check the input sensor range the first time, I used two photocells that are slightly different sizes, and I wasn’t able to get min and max values for my analog input. When I changed the larger photocell for a smaller one, I was able to get a min value of 188 and a max value of 941. That is my sensor range.

Two photocell sensors and piezo speaker circuit

Then, in my code, I mapped this range using the map( ) function.

 void setup() {

 void loop() {
   int sensorReading = analogRead(0);
   // map the results from the sensor reading's range
   // to the desired pitch range:
   int pitch = map(sensorReading, 188, 941, 100, 1000);
   // change the pitch, play for 10 ms:
   tone(8, pitch, 10);

Tone Output Using Arduino from Suzanne Kirkpatrick on Vimeo.

Next, I tried a slightly more complex circuit that plays a simple melody from a set of music notes stored in a library called “pitches.h”. This worked, and so I tried exchanging the two photocell sensors for three force-sensitive resistor sensors.

Playing Tones Using Force Sensors from Suzanne Kirkpatrick on Vimeo.

No Responses to “Lab – Tone Output Using Arduino”  

  1. No Comments

Leave a Reply