Build it step by step
0 of 7 doneTest one sensor
Print its state on the serial monitor.
Prepare the SD card
Copy eight note files named 0001 to 0008.
Test the player
Play file 1 from a short sketch.
Add the amplifier
Connect DAC_R and DAC_L to the PAM8403.
Wire eight sensors
Use the ribbon cable and label each end.
Mount on the stairs
Aim each beam across its step.
Tune the sensors
Adjust each module's screw for the room's light.
How do you build a musical stairs?
Each step of a staircase has a beam sensor, and stepping on it plays a piano note through a speaker. You need Arduino Mega 2560 R3, IR obstacle avoidance sensor module × 8, DFPlayer Mini MP3 module, Speaker 4 Ω 3 W, PAM8403 stereo amplifier module, Rainbow ribbon cable (10 core), 5 V 2 A USB adapter, SanDisk microSD card 32 GB (A1, Class 10), Resistor 1 kΩ and Tactile push button 6 mm. It takes 7 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 10
Wiring
| From | To | Why |
|---|---|---|
| IR module 1 to 8 OUT | Mega pins 22 to 29 | One input per step |
| IR modules VCC and GND | Mega 5V and GND through the ribbon cable | Sensor power |
| DFPlayer VCC and GND | 5 V adapter and GND | Player power (share ground with the Mega) |
| Mega TX1 (pin 18) | 1 kΩ resistor to DFPlayer RX | Serial commands |
| Mega RX1 (pin 19) | DFPlayer TX | Replies |
| DFPlayer DAC_R | PAM8403 R input | Audio |
| DFPlayer DAC_L | PAM8403 L input | Audio |
| PAM8403 5V and GND | 5 V adapter and GND | Amplifier power |
| PAM8403 R output + and − | Speaker | Sound |
| Button | Mega pin 30 and GND | Sound on and off |
Code
Mount a sensor at the edge of each step pointing across the step so a foot blocks its beam; adjust each module's screw until the LED goes off in the open and on when a hand is in front. The cheap modules can be fooled by strong sunlight, so this suits an indoor stair. The DFPlayer plays one file at a time, so a new note replaces the old one rather than overlapping; for real chords you would use a sampler chip or a small computer. The 1 kΩ resistor on the DFPlayer's RX pin prevents noise on the serial line. Keep wires off the walking surface, secure the ribbon cable so nobody trips, and use the button to silence it at night. Use only a low-voltage 5 V supply from a proper adapter, and keep the amplifier volume modest. Arduino IDE, board "Arduino Mega or Mega 2560".
Runs on a virtual Arduino Mega 2560 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Musical stairs: eight steps, eight notes. Library: DFRobotDFPlayerMini. Board: Arduino Mega 2560.
// On the SD card put eight MP3 notes named 0001.mp3 (low C) to 0008.mp3 (high C) in the root folder.
#include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini player;
const byte STEP_PIN[8] = {22, 23, 24, 25, 26, 27, 28, 29};
const byte BTN = 30;
bool wasBlocked[8] = {0};
bool soundOn = true;
unsigned long lastBtn = 0;
void setup() {
for (byte i = 0; i < 8; i++) pinMode(STEP_PIN[i], INPUT); // module output is LOW when something blocks the beam
pinMode(BTN, INPUT_PULLUP);
Serial1.begin(9600);
Serial.begin(115200);
if (!player.begin(Serial1)) { Serial.println("DFPlayer not found: check the SD card and wiring"); while (1); }
player.volume(18); // 0 to 30
}
void loop() {
if (!digitalRead(BTN) && millis() - lastBtn > 400) { soundOn = !soundOn; lastBtn = millis(); }
for (byte i = 0; i < 8; i++) {
bool blocked = (digitalRead(STEP_PIN[i]) == LOW);
if (blocked && !wasBlocked[i] && soundOn) { // play once when a foot arrives
player.play(i + 1); // the newest note replaces the one playing
Serial.print("Step "); Serial.println(i + 1);
}
wasBlocked[i] = blocked;
}
delay(5);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Mega 2560 with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
DFPlayer not found: check the SD card and wiring
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Wave a hand at step 1 | Low note plays once |
| Step on each in turn | A rising scale |
| Hold a foot on a step | Note plays once, not repeatedly |
| Press the button | Sound stops and starts |
If it does not work
Sensor always blocked
Turn its screw to lower the sensitivity.
Note repeats
Check the edge detection and that the beam is steady.
No sound
Check the SD card format (FAT32) and the file names.
Crackling
Use a separate good 5 V supply for the amplifier and player.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the musical stairs?
Arduino Mega 2560 R3, IR obstacle avoidance sensor module × 8, DFPlayer Mini MP3 module, Speaker 4 Ω 3 W, PAM8403 stereo amplifier module, Rainbow ribbon cable (10 core), 5 V 2 A USB adapter, SanDisk microSD card 32 GB (A1, Class 10), Resistor 1 kΩ and Tactile push button 6 mm.
How long does the musical stairs take to build?
About 240 minutes for a intermediate maker, in 7 steps.
How do I know the musical stairs works?
Wave a hand at step 1: Low note plays once. Step on each in turn: A rising scale. Hold a foot on a step: Note plays once, not repeatedly.
Sensor always blocked: what should I check?
Turn its screw to lower the sensitivity.
Note repeats: what should I check?
Check the edge detection and that the beam is steady.
No sound: what should I check?
Check the SD card format (FAT32) and the file names.
Crackling: what should I check?
Use a separate good 5 V supply for the amplifier and player.
