Tag Archives: piezo speaker

Sound and pitch change with Arduino

Warning: this is a really really annoying little project. If you want to irritate everyone around you, then this project is for you.

My initial idea was to connect more than one pressure sensor to it and make it like a miniature piano. But I could only find one pressure sensor so I made do with it. 

Diagram:

Image

 

The code I used for this is really simple. I used the tone() function to code for what would normally be 5 lines of regular code.

int speakerPin = 12;
int pressurePin = 0;
 void setup()
{
}
 void loop()
{
  int reading = analogRead(pressurePin);
  int pitch = 200 + reading / 4;
  tone(speakerPin, pitch);
}
 
 
When you apply pressure to the sensor, the pitch of the note will change. It’s really annoying though so use with caution.