Sense Sound with Arduino – Components, Programming and Device.
Making Music with Arduino – How to Sense Sound with Arduino
Arduino is a popular open-source microcontroller platform that can be used to create interactive projects. With the help of an Arduino, you can make music and sense sound using sensors, transducers and other components. In this tutorial, we’ll look at how to use Arduino to sense sound and turn it into music. We’ll also discuss the components and programs that you’ll need for this project.
Components Needed for Sensing Sound Using Arduino
- Arduino Uno board
- Speaker
- LEDs
- Jumper cables
- Piezo sensor/transducer
- Resistors
Setting Up the Circuit for Sensing Sound with Arduino
To set up the circuit for sensing sound with Arduino, first connect the 5V output pin on the Arduino board to the 5V pin on the piezo sensor. Then connect the GND pin on the Arduino board to the GND pin on the piezo sensor. The signal pin on the piezo sensor should be connected to digital pin 3 on the Arduino board. Finally, connect the speaker and LEDs to their respective pins on the Arduino board. Refer to the diagram below for more details.
Programming for Sensing Sound and Playing Music
Once the circuit is all set up, you’ll need to write the code to get it to work. We’ll be using an Arduino sketch for this. The code will accept input from the piezo sensor, detect the frequency of the sound, and then play a corresponding note from the speaker. You can also add LED lights to light up in time with the music.
The code below is the full Arduino sketch for sensing sound with Arduino and playing music. Make sure to include the resistor values for your setup.
int piezoPin = 3; // Piezo pin
int ledPin = 7; // LED pin
int speakerPin = 8; // Speaker pin
int sensorValue = 0; // Sensor value
int frequency = 0; // Frequency of sound
int note = 0; // Note of sound
void setup() {
Serial.begin(9600);
pinMode(piezoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(piezoPin); // Read sensor value
frequency = map(sensorValue, 0, 1023, 20, 2000); // Map sensor value from 0-1023 to 20-2000
note = frequency / 100; // Calculate note from frequency
Serial.println("Frequency: " + String(frequency) + " | Note: " + String(note));
tone(speakerPin, frequency); // Play note
digitalWrite(ledPin, HIGH); // Turn on LED
delay(100); // Short delay
noTone(speakerPin); // Stop sound
digitalWrite(ledPin, LOW); // Turn off LED
delay(50); // Short delay
}
That’s it! Now you can upload the code to your Arduino board and start making music. You can experiment with different sounds to see what kind of music you can create.