Provide your layout with sound … at the wave of a hand!
Provide your layout with sound .. at the wave of a hand!
Our hobby extends its fingers into many pies; carpentry, chemistry and electronics being just a few. However engines can run upon surfaces other than a baseboard. Equally the ability to mix resins with catalysts may only be required if you are interested in creating a stretch of water or a lake. Furthermore, it’s perfectly possible for trains to happily run around in circles without a plug in sight. In the past they achieved this by being clockwork, today they might very well be running on batteries. Taken together, this means that, at a very basic level, it’s possible to run a model train on just a table top leaving your imagination to fill in all the gaps. But where’s the fun in that?
Open up any railway modelling magazine, or pay a visit to YouTube and fantastic layouts are there to greet you. Trains run through wonderfully detailed landscapes, expertly built buildings compete for your attention with little cameos fight each other to amuse or intrigue you. Almost everywhere you look there is something that can either delight or inspire.
Giants amongst us laid down the footprint for such experiences. Pushing the barriers and prepared to wonder “What if”, they created a world of modelling possibilities that we can today totally immerse ourselves in. Inventions like static grass, point motors and glues that can almost do anything but dance and sing, being so common a place that we rarely pause to consider how things must have been before they entered the scene.
The journey is not at an end though. Many companies, small and large, are still taking up the challenge that was laid down by those pioneering individuals. Succeeding to the point, that fully working point rodding barely raises an eyebrow. These and many other similar products grace our layouts, either pleasing the eye or performing some function that once upon a time was considered to be nigh on impossible.
Electronic gadgetry, as in the past, is continuing to open up whole new world of possibilities for us and amongst their number are things called microcontrollers. Small, programable, chips that are the length of your finger and smaller; each able to perform functions that once upon a time required machines the size of whole buildings. The BBC Micro, Raspberry Pi and the Arduino are all examples of this relatively new technology and it is the capabilities of the latter that we are now going to explore. To be more precise, we are going to discuss how to combine the talents of an Arduino with some other electrical wizardry to add sounds to our layout. All at the wave of a hand !
The aim is to add some electronic systems to a layout such that, as someone waves a hand or arm over a specific point, sounds such as train announcements or rushing water get played or stopped. Yes, of course, very similar effects can be achieved by employing just a switch. But where is the magic or mystery in that for small eyes? By making our layouts more interactive, especially from the point of view of exhibiting them, they surely become more interesting and appealing?
At first sight, the wiring required for this project appears slightly overwhelming. However, break the loom down into its constituent parts, namely the wires required for the speakers, MP3 player and Arduino in turn, and things start looking far more tidier and straightforward.
As stated in the sidebar, if a breadboard and some jumper wires are employed, then the only soldering that is required within this whole project is that needed to connect the wires to the speaker itself. The jumper wires being simply slid over the requisite pins.
A Ground wire then needs to be connected to Pin 2 of the MP3 and a 5v supply provided to Pin 8. Pins 7 and 6 communicate with the Arduino board which controls when, and when not, sounds are to be played by the MP3 player. Again, if a breadboard and jumper wires are used, this becomes pretty much a plug and play operation.
Now, at this point we could just go ahead and write some code and the MP3 player would respond by playing whatever sounds have been recorded upon the mini SD card. Providing of course some power has been provide to the Arduino board first!
Arduino boards can be powered up in a number of different ways. The ubiquitous USB cable, not only providing a means of loading the Arduino with programming instructions, but also power. Equally a 9v power supply from a plug would work well. as so too would a 9v battery.
As the aim on this occasion is to provide a portable diorama, a 9v battery is the power source of choice. It could simply be connected to the Arduino as and when, but, more control can be had if a On/Off switch forms part of the connection. That would though involve a spot more soldering!
Ok, so now our Arduino board has power, no code instructions as yet, but we will be coming to that in a while, and it is connected to a mini MP3 player which in turn is wired up to a 3W speaker. So, how does the motion detector get to join in the game?
The role of the motion detector, as its name suggests, is to detect any sort of motion and report it to the Arduino board which will then decide whether or not to instruct the MP3 player to start playing or, indeed, to stop doing so.
Three wires are required to implement this role.
Almost there! The last bit of wiring that needs to be done is to link the LED to the Arduino. This LED will light up whenever the motion sensor is in sensing mode rather than reinitialising itself.
And that’s it! All the wiring is now finished and done with.
You might though be viewing the results of your efforts and thinking that it looks a fine birds nest of wires going here there and everywhere. Unfortunately the MP3 player does require a number of connections and they can make the resulting loom appear rather untidy. But, scruffy as it may indeed be, it will all work. Well, once the Arduino has been provided with some coding instructions that is!
Now this is an assumption, definitely a generalisation, that the age profile of the group most likely to be reading this article probably does not include the most computer literate people in the world. Accordingly the thought of actually writing a computer program (Arduino folk call them sketches!) will be somewhat off-putting to some.
But wasn’t building your first baseboard? Or how about the first time that you turned your hand to some scratch building or creating some scenery? It’s one of life’s more interesting rules that new things do arrive with baggage and that only experience, and indeed practice, will lighten the load. That said, a big gulp usually helps the first step to be taken. So take a deep breath because we are about to dive into the code!
Arduinos are mini computers and as such they have rather tiny minds. So small in fact that they can only ever run one program at a time. Indeed, they come ready loaded with a simple one that, if you were to power up your Arduino right, would start running. In fact that is how you start any program that is loaded onto your Arduino, you just provide it with 9v’s of power and it will automatically run whatever program is has onboard.
However, the one that is provided straight out of the box isn’t really al that exciting. See that blinking light on the Arduino? Well that’s it! Yep, that is all the program, called ‘Blink’, does. It makes a light on the Arduino blink on and off. We though need a program that does slightly more than that. So lets set about replacing ‘Blink’ right now!
We make a start by making a list of just what it is that we want our ‘sketch’ to do, and in what order.
1. Initialise the Motion Sensor
2. Initialise the MP3 player
3. Wait until a motion is detected
4. Then, either play a sound or stop a sound being played
5. Go back to waiting for a motion to be detected
Five individual steps then.
But, before we can start writing any code to implement them we first need a coding environment within which to construct our program. Hopefully its safe for me to assume that, as you are reading this via the Internet, you have access to it. Alas though that might not be via a P.C or Tablet. Without one of those you cannot download the software that you need to program an Arduino I’m afraid. Sorry!
If though you do have a P.C or Tablet then visit this site https://www.arduino.cc/en/Main/Software and download the Arduino IDE. Don’t worry. It’s free!
Now, I’m very aware that this is a railway modelling site, not a hackers forum. Accordingly I am not going to go into chapter and verse as to how this code works. Of course some of the basics will be covered but beyond that, not much!
Hopefully though you will be encouraged to know more about Arduino’s and their possibilities. So a visit to https://dronebotworkshop.com/ is much to be recommended. The guy speaks plain Canadian , not techie, and he explains how to program and wire up an Arduino in a very straightforward and clear manner. Furthermore, his site contains many examples of how using an Arduino will be of interest to a railway modeller.
Ok, get the coding environment up and running on your P.C and then we can get programming!
Select the File menu option and then click New
Arduino programs must always contain these two code routines in order to work. The first called ‘setup’ is where you initialise variables and set up the devices etc that you want the Arduino to communicate with. In our case we will be instructing it how to communicate with the MP3 player, Motion Sensor and LED.
The second routine called ‘loop’ also does exactly what its name suggests. Any and all of the code contained within this routine being run again and again, infinitum. The first loop commencing just as soon as all the code in ‘setup’ has been completed, the last whenever you turn the Arduino off. Accordingly it is within this routine that we will place all the code that decides whether or not the MP3 file should be played in response to any motion being detected by the sensor.
To save your time and energy you can cut and paste this code right into your program.
// Link to Libraries
// Declare Objects
SoftwareSerial objSoftwareSerial(10, 11); // Initialise the SoftwareSerial object and link it to digital pins 10 and 11
// Declare Global Variables
# define ACTIVATED LOW
// Input from Motion Sensor
int Motion_Detection_Pin = 7;
int readyLED = 12;
// Status Variables
boolean First_Sound = true;
boolean IsPlaying = false;
boolean IsActive = false;
// Variable to store value from PIR
if (IsActive == true)
IsActive = true;
// Setup LED as Outputs
// Setup Motion Detection Pin as Input
// Initial Delay to stabilize sensor
// Get value from motion sensor
Motion_Detection_Pin_Reading = digitalRead(Motion_Detection_Pin);
if (Motion_Detection_Pin_Reading == 1)
IsActive = false;
digitalWrite(readyLED, LOW); // Turn LED off
digitalWrite(readyLED, HIGH); // Turn LED on
IsActive = true;
// Stop Sound
IsPlaying = false;
First_Sound = false;
// Starting Sound
IsPlaying = true;
Hopefully the comment lines (Those beginning with a double slash //) provide some clues as to what is happening. But basically we start off by telling the Arduino which of its pins talk to which devices. Then, inside the ‘setup’ routine, we initialise the motion sensor and MP3 player. The motion sensor requires some time to get used to the ambient light (Around 30 seconds) so there will be what appears to be an endless wait until its ready to commence operations. Hence the reason for the LED which will come on whenever the motion sensor is operational.
Incidentally each time the motion sensor detects movement it sends a value of 1 to the Arduino and then resets itself (A period of around 9 seconds). Accordingly the LED will be off for the duration of this procedure.
A waterfall diorama, set up in the case within one of ScaleModelScenery’s baseboards, lends itself to sound effects.
Whenever a motion is detected, using boolean variables (Variables that can only ever be set to True or False) the program then determines whether or not to start or stop the sound being played by the MP3 player.
You might have noticed an area labelled ’Link to libraries’. Libraries are bits of code that have already been written and so can be included inside your programs to perform particular tasks. The one named ‘SoftwareSerial’ already forms part of the programming environment but ‘DFRobotDFPlayerMini’ does not. Accordingly you will need to download it from this address https://www.arduinolibraries.info/libraries/df-robot-df-player-mini
The program next needs to be compiled and sent over to the Arduino. Making sure that the Arduino is connected to the computer via its USB cable, select this symbol from the toolbar. Your code will then be compiled and then sent over to the Arduino where it will immediately start running.
That’s if everything goes well. You may instead get some error messages from the compiler complaining that your code isn’t correct. It will also indicate just where in the code it feels things aren’t quite right. So correct any such problems and then click the icon again. Hopefully this time everything will be fine!
Now, at the wave of a hand over the motion sensor and assuming that the LED is lit, you should hear your sound file being played. Then, once the LED light has relit, wave your hand again and the sound should stop.