Making Music


This page is not very well named. The idea is to make a basic musical instrument. It won't be that musical though - honestly, it's more of a noise machine. You press a button to play a tone and use a trimmer potentiometer to control the pitch of the tone.

You Will Need

  • 1 x Piezo Buzzer
  • 1 x 10K Trimmer Potentiometer
  • 1 x Pushbutton
  • 1 x 10 KOhm Resistor
  • Jumper Wires

Making The Circuit

Arduino Circuit Diagram

Programming The Arduino

const int trimPin = 0;
const int buzzerPin = 9;
const int buttonPin = 7;
void setup()
  pinMode(buttonPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

void loop()
  int buttonState = digitalRead(buttonPin);
  if (buttonState==LOW)
    int trimpos = analogRead(trimPin);
    int f = map(trimpos, 0, 1023, 300, 1000);
    tone(buzzerPin, f);

The only interesting statement in the code is the map() function. It allows us to map the range of values we receive from the trimpot into a range of frequencies we want to use. If you look again at the frequencies for the notes on the Playing A Tune page, you can swap the last 2 parameters of the function for the range of frequencies you want to use.