0% found this document useful (0 votes)
5 views3 pages

Arduino 21 Key Piano

The document outlines the setup for a 21-key piano using three 74HC4051 multiplexers connected to an Arduino. It includes pin connections, a frequency table for the notes, and Arduino code to play the notes when buttons are pressed. The code defines the necessary pins, initializes them, and contains a loop to detect button presses and play corresponding notes through a speaker.

Uploaded by

mounikatheeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Arduino 21 Key Piano

The document outlines the setup for a 21-key piano using three 74HC4051 multiplexers connected to an Arduino. It includes pin connections, a frequency table for the notes, and Arduino code to play the notes when buttons are pressed. The code defines the necessary pins, initializes them, and contains a loop to detect button presses and play corresponding notes through a speaker.

Uploaded by

mounikatheeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Arduino 21-Key Piano Using 74HC4051

Pin Connections

Three 74HC4051s to Arduino (for 21 keys):


MUX1:
S0 -> D2, S1 -> D3, S2 -> D4, INH -> GND, Z -> A0
Inputs Y0-Y7 -> Buttons 1 to 8

MUX2:
S0 -> D2, S1 -> D3, S2 -> D4, INH -> D6 (LOW to enable), Z -> A1
Inputs Y0-Y7 -> Buttons 9 to 16

MUX3:
S0 -> D2, S1 -> D3, S2 -> D4, INH -> D7 (LOW to enable), Z -> A2
Inputs Y0-Y4 -> Buttons 17 to 21

Speaker:
Speaker + -> D9 through 100 Ohm resistor
Speaker - -> GND

Power:
Arduino connected via USB (laptop) for power and programming
Frequency Table (21 Notes)

1. C4 - 261.63 Hz
2. C#4 - 277.18 Hz
3. D4 - 293.66 Hz
4. D#4 - 311.13 Hz
5. E4 - 329.63 Hz
6. F4 - 349.23 Hz
7. F#4 - 369.99 Hz
8. G4 - 392.00 Hz
9. G#4 - 415.30 Hz
10. A4 - 440.00 Hz
11. A#4 - 466.16 Hz
12. B4 - 493.88 Hz
13. C5 - 523.25 Hz
14. C#5 - 554.37 Hz
15. D5 - 587.33 Hz
16. D#5 - 622.25 Hz
17. E5 - 659.25 Hz
18. F5 - 698.46 Hz
19. F#5 - 739.99 Hz
20. G5 - 783.99 Hz
21. G#5 - 830.61 Hz
Arduino Code for 21-Key Piano
#define S0 2
#define S1 3
#define S2 4
#define INH2 6
#define INH3 7

#define Z1 A0
#define Z2 A1
#define Z3 A2
#define SPEAKER_PIN 9

float freqs[21] = {
261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99,
392.00, 415.30, 440.00, 466.16, 493.88, 523.25, 554.37,
587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61
};

void setup() {
pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT);
pinMode(INH2, OUTPUT); pinMode(INH3, OUTPUT);
digitalWrite(INH2, HIGH); digitalWrite(INH3, HIGH);
pinMode(Z1, INPUT_PULLUP); pinMode(Z2, INPUT_PULLUP); pinMode(Z3, INPUT_PULLUP);
}

void loop() {
for (int ch = 0; ch < 8; ch++) {
selectChannel(ch);
if (digitalRead(Z1) == LOW) playNote(ch);
digitalWrite(INH2, LOW);
if (digitalRead(Z2) == LOW) playNote(ch + 8);
digitalWrite(INH2, HIGH);
digitalWrite(INH3, LOW);
if (ch < 5 && digitalRead(Z3) == LOW) playNote(ch + 16);
digitalWrite(INH3, HIGH);
}
}

void selectChannel(int ch) {


digitalWrite(S0, ch & 1);
digitalWrite(S1, (ch >> 1) & 1);
digitalWrite(S2, (ch >> 2) & 1);
}

void playNote(int index) {


tone(SPEAKER_PIN, freqs[index]);
delay(300);
noTone(SPEAKER_PIN);
}

You might also like