Luis Estades
Published

Project #7 Musical Instrument

Using a Piezo and some pushbuttons, we made a small musical instrument on the breadboard

BeginnerShowcase (no instructions)185
Project #7 Musical Instrument

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Speaker, Piezo
Speaker, Piezo
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Resistor 220 ohm
Resistor 220 ohm
×1
Resistor 1M ohm
Resistor 1M ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

screenshot_2023-02-28_144657_ZDiJN0c4z6.png

Code

Untitled file

Arduino
int notes[] = { 262, 294, 330, 349 };

void setup() {
  //start serial communication
  Serial.begin(9600);
}

void loop() {
  // create a local variable to hold the input on pin A0
  int keyVal = analogRead(A0);
  // send the value from A0 to the Serial Monitor
  Serial.println(keyVal);

  // play the note corresponding to each value on A0
  if (keyVal == 1023) {
    // play the first frequency in the array on pin 8
    tone(8, notes[0]);
  } else if (keyVal >= 990 && keyVal <= 1010) {
    // play the second frequency in the array on pin 8
    tone(8, notes[1]);
  } else if (keyVal >= 505 && keyVal <= 515) {
    // play the third frequency in the array on pin 8
    tone(8, notes[2]);
  } else if (keyVal >= 5 && keyVal <= 10) {
    // play the fourth frequency in the array on pin 8
    tone(8, notes[3]);
  } else {
    // if the value is out of range, play no tone
    noTone(8);
  }
}

Credits

Luis Estades
13 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.