Thursday, August 20, 2026

How I used Arduino and a DF Player Mini as a sound card

Introduction

Originally, the sounds in my Ford(ish) railbus were provided by a cheap MP3 player with each track being triggered by flicking a switch on the transmitter to move forwards and back through the various sound effects (See How I used an MP3 Player as a soundcard). This was a bit cumbersome and there was a one second gap as each new track was played.

More recently, I replaced with antiquated MP3 player with a DF Player Mini module (See How I used a DF Mini Player as a sound card). This has many similarities to the old MP3 Player in that it includes an amplifier and also plays tracks from a Micro SD Card. Its advantage is that it plays .WAV tracks with no noticeable gap when it loops a track or switches to another. Its disadvantage was that the tracks still needed to be triggered by flicking a switch on the transmitter and, as with the MP3 Player, the horn could bot be sounded separately.

So, I speculated about using an Arduino microprocessor to automate some of the switching of tracks and also providing the much-needed horn sounds. I had previously used Arduino to provide radio control and sound effects for a diesel loco (see Arduino-based radio control). However, the radio control seemed a bit twitchy compared with my Deltang/Micron based system and was not as precise. However, I gained some useful experience in using an Arduino to switch MP3 tracks on a DF Player Mini.

Armed with this knowledge (my blog is useful in reminding me how I did something previously), I explored writing some similar code which would respond to the input from the ESC on the Deltang Rx61b receiver/controller used in the railbus.

 

Voltage Divider 

The first step was to transform the motor output from the ESC on the Deltang receiver into something suitable for an Arduino analogue input. Whereas, the ESC provides 0 - 12v PWM output, the Arduino can only measure 0 - 5v input. Furthermore, the motor supply from the ESC is reversible and so this needed to be rectified for the Arduino analogue input.

Fortunately, my mate in Australia is a dab hand with electronics and so guided me through the voltage divider circuitry needed to convert the 0 - 12v reversible PWM input into a more acceptable 0 - 5v output.

Note: R2 = 80/(Vb-8) kΩ.   IF Vb is less than 8V you don't need R2. 

So, with my supply voltage being around 12v, R2 = 80/(12-8)k = 20kΩ

 Alternatively, you could purchase a ready made DFR0051 analogue voltage divider module from Farnell at around £3.50 GBP (at current prices).


 

Connecting up the Arduino


I used an Arduino Nano as I had a couple in stock and also there is plenty of space beneath the roof of the railbus to accommodate it.

  • VIN - connects to the 5v output from a voltage regulator
  • GND - connects to the negative (0v) output from the battery or voltage regulator
  • A0 - connects to the output from the voltage divider above
  • D8 - connects to Pad 5 on the Deltang Rx61b (via a 1k resistor)
  • D10 - connects to Tx on the DF Player Mini
  • D11 - connects to Rx on the DF Player Mini (via a 1k resistor)

 NOTE: If you decide to use two sets of batteries - one for the motor and one for the electronics, make sure the negative leads for both batteries are connected.

 

Connecting up the DF Player Mini

  •  VCC - connects to the 5v positive output from either the voltage regulator or the Arduino Nano
  • GND - connects to the negative (0v) output from the battery or voltage regulator
  • Tx - connects to D10 on the Arduino Nano
  • Rx - connects to D11 on the Arduino Nano (via a 1k resistor)
  • SPK_1 - connects to an 8Ω speaker
  • SPK_2 - connects to an 8Ω speaker

 I connected a Li-ion 3S battery pack, a Micron MR603, a motor, the voltage regulator, the voltage divider circuitry, the Arduino Nano, the DF Player Mini and a speaker together on a breadboard so that I could use it to test my Arduino code.

 
 

NOTE:  The Rx61b is a very early incarnation of Deltang's RC equipment for trains and did not have the facility to re-program 0v outputs on its pads. Consequently, the output from Pad 5 needed to be passed through a transistor inverter circuit to switch the 3.3v output to a 0v output to trigger the Arduino. I maybe could have adjusted the code to sense this input, but the inverter circuitry already existed in the original version of the sound system.

The sound effects

 The next job was to edit the sound files needed. Fortunately, I had already purchased a series of suitable sound files of a Ford Model A car from the StockMusic.com website and so was able to incorporate these with other sounds captured from YouTube with Audacity - a free open source audio editing package. See - How I used Audacity to create sounds for my railbus - for more information.

 I ended up with ten sound files:

  • Track 001 = Engine start (6 sec)
  • Track 002 = Idle (24 sec)
  • Track 003 = Idle plus horn (1 sec)
  • Track 004 = Accelerate to cruise (31 sec)
  • Track 005 = Cruise (1 min 19 sec)
  • Track 006 = Cruise plus horn (2 sec)
  • Track 007 = Decelerate to idle (12 sec)
  • Track 008 = Engine off (3 sec)
  • Track 009 = Horn (1 sec)
  • Track 010 = Silence (15 sec)

 These were copied on to a 512Mb Micro SD Card. I have found that, not only do the files have to be numbered correctly - ie 001, 002, 003, 004 etc - they have to be copied to the card in the correct order, otherwise the DF Player gets confused. Also, it is advisable to reformat the card before saving the files to it.

I'm sorry, but I can't share my sound files as this would infringe the copyright agreement for the files I downloaded from StockMusic.com

 

The code

 I am no coding expert. I make copious use of the online reference facility on the Arduino website and search online for pieces of code to do specific jobs (eg to differentiate between a short press and long press of a digital (push button) input). The beauty of coding is that you can keep trying things out and, if it doesn't work as you expect, you can edit it and try again - you get instant feedback.

OK, it is a bit more complicated than that as sometimes, you have no idea why what you've written doesn't work and so you have to research the internet for a better way of doing it, but the more you try, the better at it you become.

Here's the entire code for the sound  

/*  
Arduino connections
A0 - Motor input from voltage divider circuit DO NOT connect directly to motor leads!!
D8 - 0v trigger for horn (short press) and engine start/stop (long press)
D10 - Tx on DF Player Mini
D11 - via 1k resistor to Rx on DF Player Mini
VIN - 5v +ve
GND - Ground (same as for Arduino)

SD Card tracks
Track 001 = Engine start (6 sec)
Track 002 = Idle (24 sec)
Track 003 = Idle plus horn (1 sec)
Track 004 = Accelerate to cruise (31 sec)
Track 005 = Cruise (1 min 19 sec)
Track 006 = Cruise plus horn (2 sec)
Track 007 = Decelerate to idle (12 sec)
Track 008 = Engine off (3 sec)
Track 009 = Horn (1 sec)
Track 010 = Silence (15 sec)
*/

#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>

SoftwareSerial mySerial(10, 11); // D10 RX (thro 1k resistor), D11 TX
DFPlayerMini_Fast myMP3;

int analogPin = A0; // ESC through voltage divider
int Ch5Pin = 8; // 0v trigger for Horn (short press) and engine on/off (long press ) - Ch5 on Deltang or Micron Rx and Tx
int Lights = 1; // Flag - 0 = lights off - 1 = lights on
int Horn = 1;
int Engine = 1;
const int SHORT_PRESS_TIME = 1000; // Duration for long press on horn / engine trigger - 1000 milliseconds = 1 second

bool ch5Triggered = false; // the previous state from the input pin to test for short or long press
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0; // When the horn/engine trigger was pushed
unsigned long releasedTime = 0; // When it was released

int val = 0; //Current value of ESC output (subtracted from 1023 as Micron / Deltang ESC starts high and goes lower as speed increases)
int maxval = 0; //Max value of output from ESC (as this will vary dependent on charge in battery pack)
int minval = 1023; //Min value of output from ESC (How high the throttle reaches)
int currentTrack = 10; //Track currently playing (starts off with silence)


void setup() {
Serial.begin(9600);
mySerial.begin(9600);
myMP3.begin(mySerial);
pinMode (Ch5Pin, INPUT_PULLUP); // Sets the horn / engine trigger to high initially
delay(1000); // Helps prevent any falso triggers
myMP3.loop(10); // Loop play silence
}

void loop() {

currentState = digitalRead(Ch5Pin);// read the state of the horn / engine trigger input

if(currentState == LOW && !ch5Triggered) { // trigger button has been pressed
ch5Triggered = true;
pressedTime = millis(); // When the button was pressed
}
if(currentState == HIGH && ch5Triggered) { // button is released
ch5Triggered = false;
releasedTime = millis(); // When the button was released

long pressDuration = releasedTime - pressedTime;

if(pressDuration < SHORT_PRESS_TIME )
Horn = 0; // Sets the horn flag to 0
else
Engine = 0; // Sets the engine flag to 0
}

val = analogRead(analogPin); // Read the input level from the ESC voltage divider circuit (NB 0 - 1023)
if(val>maxval) maxval=val; // Records the max input level
if(val<minval) minval=val; // Records min input

if (Engine == 0 && currentTrack == 10) { // If long press and current track is silence
myMP3.play(1); // then play engine start
delay(6000); // length of the engine start track (ie six seconds)
myMP3.loop(2); //Now loop the idle sound track
currentTrack = 2;
Engine = 1; // Resets the long press trigger flag
}

if (Engine == 0 && currentTrack == 2){ // If long press and current track is idle sound ....
myMP3.play(8); // .... then play the engine cut off track
currentTrack = 10;
delay(3000); // Length of the engine cut off track (ie 3 seconds)
Engine = 1; // Reset the long press trigger flag
}

if (Horn == 0 && currentTrack == 2){ //If short press and current track is idle sound .....
myMP3.play(3); //.... then play the idle + horn track
delay(1800); // Length of the horn + idle track (ie 1.8 seconds)
myMP3.loop(2); // Carry on looping the idle sound track
Horn = 1; // Reset the short press trigger flag
}

if (Horn == 0 && currentTrack == 5){ // If short press and current track is cruise sound ....
myMP3.play(6); // .... then play the cruise plus horn sound ....
delay(2000); // ... for two seconds ....
myMP3.loop(5); // .... and then carry on looping the cruise sound track
Horn = 1; // Reset the short press trigger flag
}

if (Horn == 0 && currentTrack == 10){ // If short press and current track is silence .....
myMP3.play(9); // .... then play the horn sound
delay(1500); // ... for 1.5 seconds ....
myMP3.loop(10); // ... then carry on looping silence
Horn = 1; //Reset the short press triggee flag
}

if(val < maxval - 10 && currentTrack == 2) { //If the input from the ESC voltage divider drops 10 units below the max value while idling ....
myMP3.play(4); // ... then play the acceleration sound track ....
delay(29000); // ... for 29 seconds ....
myMP3.loop(5); // ... and then play the cruise sound track
currentTrack = 5;
val = 0; //Reset the input value to 0 to prevent triggering the next if loop
}

if (val > minval+ 120 && currentTrack == 5){ //If crusing and the input value from the ESC rises 120 units above the minimum value ...
myMP3.play(7); // ... then play the deceleration track ....
delay(12000); // ... for 12 seconds ....
myMP3.loop(2);// ....and then play the engine idle sound...
currentTrack = 2;
minval = 1023; // Reset the minimum input value ....
maxval = 0; // ... and the maximim input value (just in case!)
}
Engine = 1; //Reset long press flag if it is pressed and triggers nothing
Horn = 1; // Ditto for short press flag
delay(100); //To help reduce false readings

}




Hopefully, the comments alongside each of the statements help explain how the code works.

If you decide to follow in my footsteps, you will have to tweak the timings and the variables to suit your sound files and the ESC output values to match the characteristics of your motor and model.

Once the railbus has been turned on, a long press on the bind button (Channel 5 input) will trigger the engine start sound and then loop the engine idle sound. At any point, a short press on the bind button will sound the relevant horn sound file. A small turn of the throttle knob on the transmitter will make the gear grinding and acceleration sound file start. once this has finished the cruising sound file starts. Reducing the speed of the railbus to around half will trigger the deceleration sound file which then loops the idle sound. A long press of the bind button while the idle sound in playing will trigger the engine cut-off file to start which then leads to the silent sound file looping.

It actually took me nearly two weeks to perfect the code. Even so, it's not perfect and I might experiment with a revised version of it sometime in the future, but for now it seems to work. 

 

 Costs

  •  Arduino Nano = c£4.50 GBP (eBay) or c£1.99GBP (AliExpress)
  • DF Player Mini = c£4.00 GBP (eBay) or c£1.00GBP (AliExpress)
  • Micro SD Card = c£4.00 GBP (eBay) or £0.99 GBP (AliExpress) 
  • 12v - 5v Voltage regulator = c£2.75GBP (eBay) or c£1.50 (AliExpress)
  • Voltage divider parts = (c£1.50GBP (eBay) or c£0.50GBP (AliExpress)
  • OR DF Robot Analogue voltage divider = £3.50GBP (Farnell)
  • Miniature speaker = c£5.00 GBP (eBay) or c£2.50 (AliExpress)

It's assumed you will have an existing battery powered locomotive with radio control into which the sound system will be fitted. 

No comments: