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.



13 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