Projects / Musical stairs

Musical stairs

Intermediate4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. MEGA

    Test one sensor

    Print its state on the serial monitor.

  2. MEGASANDIS

    Prepare the SD card

    Copy eight note files named 0001 to 0008.

  3. MEGA

    Test the player

    Play file 1 from a short sketch.

  4. MEGA

    Add the amplifier

    Connect DAC_R and DAC_L to the PAM8403.

  5. MEGARAINBO

    Wire eight sensors

    Use the ribbon cable and label each end.

  6. MEGA

    Mount on the stairs

    Aim each beam across its step.

  7. MEGA

    Tune the sensors

    Adjust each module's screw for the room's light.

In short

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

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
IR module 1 to 8 OUTMega pins 22 to 29One input per step
IR modules VCC and GNDMega 5V and GND through the ribbon cableSensor power
DFPlayer VCC and GND5 V adapter and GNDPlayer power (share ground with the Mega)
Mega TX1 (pin 18)1 kΩ resistor to DFPlayer RXSerial commands
Mega RX1 (pin 19)DFPlayer TXReplies
DFPlayer DAC_RPAM8403 R inputAudio
DFPlayer DAC_LPAM8403 L inputAudio
PAM8403 5V and GND5 V adapter and GNDAmplifier power
PAM8403 R output + and −SpeakerSound
ButtonMega pin 30 and GNDSound 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.

CheckExpected
Wave a hand at step 1Low note plays once
Step on each in turnA rising scale
Hold a foot on a stepNote plays once, not repeatedly
Press the buttonSound 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 →

Ask Lark AI

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.