Monday, July 01, 2019

Controlling a loco with a Bluetooth phone app and Arduino - Getting started

Contents


Introduction

Having progressed from analogue track power (see How I did the electrics) to DCC (see Digital developments) and then on to battery power and radio control using Deltang components (see Getting started with Deltang), I felt I had reached my ideal control system. However, technology doesn't stand still and so I became interested in the potential of Bluetooth based control. It seemed to offer a lot of potential and seemed reasonably cost effective. I toyed with the idea of investing in a BlueRail receiver but at nearly £80 per loco, it seemed quite a lot to invest for evaluation purposes. I have dabbled with using Picaxe programmable microprocessors for a couple of projects (see How I control my signals and How I control my points remotely), so when I came across some online articles describing the Bluetooth control of model trains using Arduino components I became interested.

What is Bluetooth?

Bluetooth is a form of low power radio communication which is built in to most modern forms of portable technology devices such as mobile phones, tablet computers and car radios. It enables two way communications between devices so, for example, music files stored on a mobile phone can be played remotely on a car radio and the radio can be used to control the phone.


The two devices need to be 'paired' before communication can take place and the distances over which the communication can take place is limited to around 10 metres though distances of 30 metres have been reported. The system is quite robust and reliable and as its use is widespread, there is a wealth of reasonably priced equipment available and plenty of tried and tested examples of applications of the technology which are freely available online.

What is Arduino?

Arduino is a low cost electronics system which is programmable using open source software. Open source means that it is free to use and can be adapted and extended by anyone with the knowledge and skills to do so.
The core system can be easily expanded through the connection of modules and components which can sense environmental conditions and control the actions of devices such as motors, servos and electrical and electronic gadgets. As Arduino has become very popular among hobbyists, educationalists and developers, so the range of compatible hardware has increased and the prices have fallen.

So what does this mean for model train control?

It means that someone with sufficient know-how can create a control system for model trains to suit their own needs. There is a range of Arduino units available which form the heart or, more  accurately, the brains of the system.  Electronic modules are readily available to connect with the Arduino board to add functionality - for example, motor controllers, radio control transmitters and receivers, sensor units, audio players, amplifiers, servo controllers, etc., etc., etc..

Arduino Nano board
The Arduino unit is connected to a computer for programming and then, once the set of instructions has been downloaded to the Arduino, it is able to carry out a series of functions independently. In terms of Bluetooth, a receiver and a motor controller are connected to an Arduino board, the board is then programmed to enable it to be controlled from a smart phone or tablet computer. Furthermore, in addition to instructions being sent from the phone or tablet to control the speed and direction of the motor, other things can be controlled such as sound cards, lighting and remote couplers. It is even possible to transmit sounds stored on the smart device to the loco. In addition, the loco can communicate information back to the smart device, such as its speed or, in the case of a live steam loco, the boiler pressure and gas setting, provided appropriate sensors have been fitted. Lonesome features such as pointwork, uncouplers, signals, lighting and station announcements could also be put under the control of a Bluetooth equipped smart device. It should even be possible to put an entire layout under computer control. Theoretically, any number of locos or accessories could be controlled from one smart device, though there is a limitation on how many could receive sounds from it.

As will be seen, it is possible to set up a simple Bluetooth system to remotely control one or more locos for under £20 per loco and, if you shop around, for considerably less.


A simple basic system

Fortunately, there are plenty of enthusiasts out there who have recognised the advantages of controlling trains with Bluetooth and have provided information and resources for free or for a modest sum.

Steve Massikker of ArduinoRailwayControl.com has provided simple way for anyone to get started. In addition to providing clear instructions as to the equipment needed, how to wire it up and the code which needs to be downloaded to the Arduino Nano or Uno board, he has also made a simple app available for Android and iPhone devices which can be installed free of charge to enable us to evaluate its potential. In addition he has more sophisticated apps for reasonable prices.

His video clearly and concisely explains the process:

Rather than installing the system to control my loco through the track, I wanted to control a battery powered loco running on my 45mm gauge, 16mm scale garden railway.

How I went about it.

1. I downloaded and installed the Arduino IDE programming software on my laptop computer from https://www.arduino.cc/en/Main/Software

2. I purchased the three bits of equipment needed from eBay, namely:

  • 1 x Arduino Nano board (£3.55)
  • 1 x  HC-06  Bluetooth receiver (£4.90)
  • 1 x L298N motor controller (£5.95)

Total cost £14.40. I could have bought the items more cheaply had I been prepared to wait for them to be delivered from China.


3. In addition, I bought an adjustable step down voltage regulator from eBay to provide the Nano with 5 volts as the loco I will be controlling has a 3S 11.1v li-ion power pack (£2.18). I could have made my own voltage regulator circuit but wanted to keep things simple.
The specification for the Nano indicates that it can handle inputs of up to 12 volts, but sometimes the voltage of the pack exceeds this immediately after recharging and so I prefer to play safe.

3. I downloaded the control code needed from the ArduinoRailwayControl.com website and pasted it into the Arduino IDE program on my laptop.


4. I connected the Arduino Nano to my laptop and uploaded the code to it from Arduino IDE. Note: Because the Nano I bought from eBay was a cheaper clone of the original, I had to download and install a driver from  http://www.wch.cn/downloads/file/5.html before  the IDE program on my laptop would recognise the Nano was connected. It was only a 5 minute job (Use Chrome to access the site to translate it into English).

5. Once the Nano had been programmed and disconnected from the laptop, I connected the  components together, following the guidance on the ArduinoRailwayControl.com website.
Source:arduinorailwaycontrol.com

6. In addition, I wired up the voltage regulator to supply power to the Nano, and wired up a charge socket and on off switch for the loco.

7. I then downloaded and installed the free app on to my tablet computer from the link on the ArduinoRailwayControl website.

Source: arduinorailwaycontrol.com

8. I turned on the loco, including its new control system and then opened up the app on my tablet


9. I paired the loco with the app.

10. I could then control my loco using the app on my tablet.



Conclusions

I found the process to be fairly straightforward, but then I am fairly confident with technology and quite happy to tinker around with electronics and a soldering iron, though it is possible to connect everything together with Dupont female to female and male to female cables.

The only complication was getting the laptop to recognise the Arduino Nano and having to install a driver, but this wouldn't have been necessary if a genuine Arduino Nano had been bought from a reputable supplier.

In terms of performance, a drawback for me in using a phone or a tablet to control my locos is seeing the screen in the sunshine when outside in the garden. When using my Deltang based radio transmitters, I seldom look at them when running my trains. I can feel the speed control knob and the switches and buttons without having to look at them. This means I can focus on watching the loco, particularly when shunting.

Another small drawback for me was the size of the speed increments in the default coding. The speed control has nine 'notches' from zero to full speed. I found the loco increased and decreased speed in discernible steps as the throttle was opened or closed. This was particularly noticeable when the loco was starting off and moving slowly - quite important when shunting. However, by changing the values in the array at the beginning of the Arduino code, it was possible to fine tune the way the loco responds at slower speeds (see Part 2 - Tweaking the speed steps).

Overall, Bluetooth enabled technology does seem to offer a flexible and relatively cheap was of controlling model trains. It certainly seems to be future-proofed as it is likely that apps will be further developed. No doubt, at some point in the not to distant future, Bluetooth will be superseded by something more sophisticated but in the meantime it does appear to offer an interesting and readily extendable method of controlling trains.

For more information see

26 comments:

Fairywood Light Railway said...

Thanks again Rik for another brilliant blog. The information is very precise and informative. It does look to be the same type of technology I used when I was building N Gauge layouts. That was a system called Rocco Z21, it used pretty much the same kind of Bluetooth tech, but far more advanced, but it would because it was mainly for indoor layouts.

Again thanks Rik, this is one a may explore. Can the Arduino be used on a MacBook Pro......

GE Rik said...

Hi Rod
Thanks for your feedback. Yes, there's a download for Mac on the Arduino website plus loads of tutorials and resources - https://www.arduino.cc/en/Main/Software

Rik

PatrickJT said...

Rik have enjoyed reading elements of your blog. Came across this item on the "Garden Railways" FB page. Have only recently converted a Bachmann Tram and a Bachmann Big Hauler to run on a couple of 18650 cells with a PWM thing to control speed. was looking for a remote option and think this may well fit the bill. Ha thats a pun if ever. Just ordered a selection of bits to enable me to build a couple regretably the HC06 came out at €10 so if it works I'll get some more from UK.

GE Rik said...

Glad it was useful, Patrick. I'm still experimenting so, as they say, 'Watch this Space'. Presently working on providing sound using a Mini SD Card player. Will post where I'm up to when I get everything working reliably.

Rik

Ukjim said...

Your many skills astound me Rik. Yet another very informative read. Thanks for everything you bring to further the enjoyment of the railway modelling community. This would make an excellent article for the Garden Rail mag. I enjoyed your Snailbeach wagon slot in the July edition. All the best, Jim

PatrickJT said...

Have got all the bits now, finding this Arduino thing very interesting. Have you checked the ARC site in the last few days ? he is working on a Garden Rail project which looks well worth keeping an eye on. Includes controlling multiple locos from one control via bluetooth.

Meanwhile will be putting my first one together today.

PatrickJT said...

Hello again Rik, just had a go at building the circuit. I was trying to use a UNO as it was easier to use jumper cables. Anyway it just would not work, so I swapped for a Nano did it all again checked, and rechecked still no go. Of course I did not use your wiring colours which added to my confusion.

Then I realised what had happened , I had followed your diagram with the voltage regulator. When I looked at steves image I realised the Nanos where facing opposite directions. BUT the wiring that you had drawn in was wrong way. 12 and 13 from hc 06 need swapping over, vcc from bt goes to 5v not d4. After juggling the wires it works.
I did not use the voltage regulator as was only using 4.8 volts, but will increase battery voltage. However I read up on the l298n and saw that the 3rd screw terminal is supposed to be a regulated 5volt feed so could be used to power the Nano. I did try but only got 3.3v between that and GND, that could be down to my aaa cells getting low. Possibly worth checking out as its one less component.
Despite the probs I think I learned from it all so quite happy really.

Hope you dont mind but thought I ought say something. If I am wrong then give me a good slapping.

GE Rik said...

Hi Patrick
Thanks for pointing out the error. I'll correct the diagram. Even though I double checked, it's inevitable from time to time a mistake is made. Glad you figured it out in the end.

Rik

GE Rik said...

Thanks Jim
Glad you enjoyed the GR article. I've got an idea for another one which I might pass in front of the editor sometime soon.

Rik

JimmyB said...

Rim, really interesting, and worth a “play”, however I notice that the app is only on Android, and not apple, or have I missed something.

Jim B

GE Rik said...

Hi Jim
Yes - I only have Android devices and so no experience of IOS apps. You could try LOFI which has the same features plus a few more - and is intended for use in schools so should be relatively easy to configure. It's also designed to work with Arduino.

Rik

tom sullivan said...

Hi Rik, Thanks for publishing the info on the Arduino system.
I'm just getting back into Garden RRng and this system looks like it will give me what I need at very reasonable pricing.

I'm looking to battery power my converted "Big Hauler" which is now my Irish engine that I had been posting on the Garden forum.
I'm not looking at anything more technical than:
Battery power
Android control using a tablet or cell phone. (Bluetooth would be ok too)
No sound
I'm satisfied with the 9 speed steps at this point. (just want to get things running)

I have been doing DCC installs for several years in On30 and HO, I know how to solder electronics and can read basic wiring schematics, but I'm no "software developer"
Is what I am asking for doable? What Arvinto parts do I need for 1 loco set.
Thanks in advance.....tom

GE Rik said...

Hi Tom
I'd advise you to look at the latest offering from Steve Massikker at Arduinorailwaycontrol.com. He's developed a new system specifically for battery powered garden railway locos. Plenty of clear guidance for the novice.

Rik

PatrickJT said...

Tom bit late but I converted my Big Hauler a while back . Works quite well especially with the paid for app but the free one is fine too. My only problem has been that whereas I can run just plain old vanilla battery power using 2 18650 coupled to a PWM thing. The Arduino version needs at least 3 but they dont last as long. Probabaly rubbish batteries. It was a fun project but in the end I prefer to stick in batteries set the speed and just let it run around the loop of track.

The paid for version needs a bit more know how,so I'd stick with the free version. BTW Steve is helpful if you get stuck.

PatrickJT said...

I just wrote a long reply has it disappeared ?

tom sullivan said...

Rik and Patrick, thanks for your replies. Both very helpful. I'm just getting back into the garden rwy. with a retirement move to Charleston SC.
Home is being built (ready by the end of the year, fingers crossed!). I'm debating two ways to go: First, get a large loop up and run trains (battery) at a set speed while sitting in a comfortable chair holding a glass of cabernet..........
or second, .........building something along the line of Rik's beautiful (and operations oriented) Peckforton Lt. Rwy. A lot depends on finding a couple of like minded guys who would enjoy operating a garden railway. (I'm sure there around, just have to find them)
Rik's work with Arduino have been fascinating given the relative cost savings over previous RC options. I just don't have the computer background (yet) to work with the components on my own.
The idea of using bluetooth on my phone/tablet to control seems more than reasonable but I'd prefer to have my own "handheld" throttle if that can be accomplished........
I'm all ears to your thoughts and comments.......tom

GE Rik said...

Hi Tom
I'm experimenting with using 2.4gHz RC modules to make a 'proper' handheld transmitter and receiver setup. Some success but finding it a lot more fiddly than the Bluetooth setup, so certainly not for the faint hearted. Bluetooth is the easier way to go, particularly as you only need to focus on the receiver in the loco. I am thinking of exploring using a Bluetooth module in a handheld transmitter case as Bluetooth does seem to be more reliable than the 2.4gHz RC modules I'm presently fiddling with.

All the best with your new venture. I have the best of both worlds in that I run prototypically from time to time or at other times just put a train on and run it while sitting in a chair with a glass of Prosecco.

Rik

Anonymous said...

Hi Rik
I have an embryonic layout using Massoth DCC. Having spent a season cleaning track etc I have decided, following your own example, to convert my loco fleet to battery RC - just about to complete my first conversion. Am I right in thinking that one limitation of Bluetooth is the range - my garden is about 0.5 acre and the distance from one end to another of the layout is already nearly 100ft and I plan on extending further as my confidence grows.

Thanks so much, like many others, for this blog - it has been invaluable in helping me to develop the layout and I'm really looking forward to tinkering with the battery RC - I used to dabble in electronics as a lad and it's great to rekindle old interests.

David

GE Rik said...

Hi David
Thanks for the feedback.

According to those who know, the range of Bluetooth is supposed to be 100m, so theoretically you should be ok. However, it depends on whether you have a clear line of sight between tx and Rx. Foliage, rocks and people can disrupt the signal and also if the RX is mounted inside a plastic or metal bodied loco. The max range I can test in my garden is 25m and it was fine with that, even when sheds were in the way. I can only suggest you give it a try with one loco.

Rik

PatrickJT said...

I reckon I have about 20-25 metres range easy but by then I am in the trees, one thing I noted is that if your loco loses the signal it just carries on regardless. Thats ok unless you have a crash situation hmmmmph !. I have an idea that you then have to remake the connection if signal gets lost. Correct me if wrong, what I will say is that battery power makes it more convenient for a casual user, no more cleaning or checking connectivity, just bung a train down and play.

GE Rik said...

It should regain the signal itself as soon as it gets back in range or re-emerges from behind the obstacle. Yes, no more track cleaning! This makes it a lot easier to run trains on the hoof.

Rik

Douglas said...

Hi, I really enjoyed this thread, which I found from being introduced to your Youtube channel by a friend.

I've just started down the garden railway road, having bult a railway during lock down, and am now building up a roster of motive power to run trains. My objective is similar yours,to run a railway, but sometimes just sitting in the garden to watch the trains run by a few times. Ive gone down the 2,4 Ghz route, which so far Im very pleased with.

The connection with this thread is that my first few locos used arduino based ESCs, which were cheap to put together driven by standard model aircraft receivers. They worked reasonably well, enough for me not to have replaced them. I used a command which divided the range of power settings between maximum and minimum power into 256 steps. I also discovered that the PWM frequency could be changed from the just under 500hz default to 3.9khz, which got rid of the sound of the motor vibrating at PWM tone when starting and running slowly. I feel that the loco also runs more smoothly using 3.9khz.

The only problem I've had was with the 5v regulated supply on one loco to the arduino, receiver and L298N controller. Like you I used an adjustable DC converter to regulate this supply down from the 20v battery voltage to drive the motor. I set it to just under 5 v when I set it up, but in operation over a few weeks it rose to 8v. I found out when the arduino board failed..... Luckily the motor controller and the receiver survived, and arduino Nanos arent expensive.

Ive adjusted the power back to 5v, but I dont trust it completely now, so I may replace it with a traditional voltage regulator, but they do waste significant amount of battery power.
Another approach may be to thread lock the adjustable resistor and monitor the receiver voltage over the radio control system. One of my contollers allows me to display 3 parameters on the it's screen and sound alarms if the rxer voltage strays out of spec.

My thoughts are now turning to signalling and point control. Ive seen your signalling video Rik, and I'll soon be studying the blog!

GE Rik said...

Sounds like you've made an excellent start, Douglas. I'll be interested to hear how you get on with an Arduino points controller. I used Pickaxe with LGB point motors because they were already installed when I used track power and then DCC. I would probably have used servos if I was starting from scratch.

Rik

meyrambyek said...

Hello, it is very good project. I'am working railway sector in Mongolia. I need to make "Peckforton Light Railway" arduino project. But i have't arduino nano control code for what you used own project. Plesase send to my email meyrambyek1010@gmail.com for control code.

GE Rik said...

Hi
I downloaded the code from the Arduino Railway Control website - https://arduinorailwaycontrol.com/sketches/V2_JL_first.ino

Rik

Technical said...

Bluetooth transmits data between devices over short distances using radio waves. When two Bluetooth-enabled devices are paired, they create a secure connection and can communicate wirelessly. A Bluetooth-enabled device broadcasts a signal to find other devices.