Lab: Tone Output with Arduino

This time we learned how to generate simple tones on an Arduino.

First, we used two photoresistors connected to analog pin 0 in a voltage divider circuit, to change the tone of a speaker connected to pin 8. Using the tone() command we can change the frequency of the sound through the values given by the sensor.



Arduino code:

void setup() { Serial.begin(9600); } void loop() { int analogValue = analogRead(A0); Serial.println(analogValue); int sensorReading = analogRead(A0); // map the results float frequency = map(sensorReading, 0, 1023, 5023, 0); // change the pitch, play for 10 ms: tone(8, frequency, 10); }

Later, as a personal project, I decided to use three sensors in order to give more versatility to the resulting sound.
I used a potentiometer combined with a photoresistor to change the tone through two different inputs, by multiplying their frequency values. Then I used another potentiometer to change the rythm of the tone through delay().


Here you can see a video of my results:

Arduino code:

void setup() { Serial.begin(9600); } void loop() { // get a sensor reading: int sensorA = analogRead(A0); int sensorB = analogRead(A1); int sensorC = analogRead(A2); Serial.println(sensorC); float frequencyA = map(sensorA, 0, 50, 50, 200); float frequencyC = frequencyA * map(sensorC, 0, 1023, 5000, 0); tone(8, frequencyC, 10); delay(sensorB/10); }

Leave a Reply