Bluetooth and Arduino

Rhinochugger

Rhinochugger

Retired Oik
27 Oct 2009
24,708
389
North West Norfolk
Arduino - can someone advise me on a decent vintage before I get to the wine warehouse tomorrow morning? :nerd::nerd:
 
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
Perhaps sometime soon someone will be offering Plug and Play Kits that just need a little bit of Soldering to the Motor and perhaps Battery Wires.
How's this for Plug'N'Play..........bit of rush this evening, do a piece about it later, modified the existing code to run, uses the same APP as per Rik's demo...

127376_deb556fed646418d43e5153bdaa66efb.png
 
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
Plug N Play..........



For type select, UNO R3
For select, UNO R3 CH340G+cable



Option HC-06 4Pin Board




127396_9ed192652efb5e0bf753e33771ad9d84.png


Use the same Bluetooth Module and Android APP as per Rik's article, requires some minor modifications to the original program code.

I'll put together a "how to" plus some alternatives for the "H Bridge Driver" used for motor control.

Whist searching, found some more useful items, that would make Plug "N" Play more accessible for those who would like to have a dabble, will put together some notes.

Refer to this post 2773, https://www.gscalecentral.net/threads/what-have-you-been-doing-today-as-part-of-your-g-scale-interest.310793/page-139 for the LC DCC M4, minor modifications, and would do the same job, also could be used as a full blown Analogue Controller.
 
Last edited:
M

Michael

Registered
26 Jan 2010
78
15
A very nice solution for indoor model railways that can also be used outdoors. Also, a great way to introduce people to Arduino programming and could easily be used with the ESP8266 and all other similar Arduino IDE compatible processors.

As for the LocoFi wi-fi solution, the phone app is the weakest link (personal experience, a phone to control an outdoor train is useless), but it does make it easy to get started as you only have to play with the receiver.

However, as the protocol is known (from the Arduino sketch) it would be relatively easy to make a transmitter using an Arduino Nano, bluetooth module (HC-05 as master), a few buttons and potentiometer(s) or an ESP32 that has integrated bluetooth as well as wi-fi.

Please note there is a potential problem with the circuit diagram on the ArduinoRailwayControl website. Arduinos have 5 volt outputs and the HC-06 is a 3v3 device. The 3v3 output from the HC-05 will drive the Arduino but the 5 volt output from the Arduino to the HC-06 should be reduced to 3v3 by the use of two resistors (1K and 2k are suggested if you do an internet search). Whilst it obviously works it is bad engineering practice and it is possible you will blow the HC-06.


Michael
 
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
Reference Michael's note above, resistor circuit for the RX signal connection modification for the circuit diagram on the ArduinoRailwayControl website, refer to post 27.
 
Last edited:
musket the dog

musket the dog

Model railways, 00, 009 and G. Kayaking, remote co
31 Oct 2009
508
0
Leicester
nlrr.webs.com
A very interesting piece of kit, I had been pondering over Raspberry Pis and wondering if there was any railway control potential (mainly for points and signalling) but maybe a step back to the Arduino is actually more suitable for a greater number of applications.

I am currently about to invest in acquiring the gear to convert my stock to radio control and had my heart set on the Deltang system. This throws an interesting spanner in the works.

Obviously the initial outlay is less, soldering and basic programming aren't beyond my range of skills. From Rik's experiments range and use outdoors don't seem to be as big an issue as they might have once been with Bluetooth devices (I can happily wonder all over my garden while my phone syncs with my cheapo bluetooth speaker).

I suppose the downsides would be the larger packaging space needed? I imagine finding space for 3 boards and a voltage regulator might be difficult in smaller locos or ones less boxy by design.

Additionally what happens if you introduce two systems? Can the app switch between the two individually, or would it be a case of resyncing and using a single loco at a time?
 
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
Corrected diagram,....Bluetooth Power moved from D3....to 5V pin...., resistor voltage divider for correct 3.3V logic level.

127524_fa9f632ccf42abe1ec7688eccd4d8c53.jpg
 
Last edited:
  • Like
Reactions: ge_rik
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
I suppose the downsides would be the larger packaging space needed? I imagine finding space for 3 boards and a voltage regulator might be difficult in smaller locos or ones less boxy by design.
Additionally what happens if you introduce two systems? Can the app switch between the two individually, or would it be a case of resyncing and using a single loco at a time?
Each Bluetooth Module can be assigned a Name, other than the meaningless jumble of numbers that are the default, just a case of connect and disconnect to the Bluetooth Module required, all can/are stored on the phone, just a matter of selecting which one is needed.


Selection of H Bridges and Arduino's
127472_ef7614f9efe54ed123a6b3d8e1a6a629.png



The inherent problem with presenting this below as a solution, will have the masses running for the hills!, always going to be compromise, between keeping it simple and trying to reduce the footprint... and ensuring a moderate level of success for anyone who would like to explore the ideas further, conscious that techno babble has to be low key!


OK, so how small can you go... everything previously discussed can be condensed, ATTINY85, L293D H Bridge, Bluetooth, add a couple of resistors, voltage regulator circuit or board, and away you go.................
127474_ccbed4325c96d4e586b26736206c3917.png
 
Last edited:
M

Michael

Registered
26 Jan 2010
78
15
I am currently about to invest in acquiring the gear to convert my stock to radio control and had my heart set on the Deltang system. This throws an interesting spanner in the works.
If you need to make your decision now, I would say go with Deltang until this bluetooth system is proven. I've been using Arduinos in locos outdoors since 2013 so I have a lot of experience. Picture from 2013 of Accucraft Lyn controlled by an Arduino Nano and nrf24l01 wifi board with an Arduino Uno in the 'smart' brick, as I called it :)
127478_fc339421331ab61d1b0936b2fbfb097a.jpg

Now whilst I have no experience of bluetooth, I know that connectivity is the main issue in the garden and that whilst it might work so many metres on a bench, when you stick it in a loco, especially a metal one, it's a different matter.

The Arduino/nrf24l01 combination worked but the range was bad, 5 - 10 meters at best.

In 2018 I switched to the ESP8266 which has an integral wi-fi chip and a Tenda router and it is now working happily in my 7 locos and 2 wagons, around a track that is 70 foot long.

I would therefore say that you need to prove that this bluetooth solution works reliably in a metal loco around a large garden before investing too much time or money in it.

I have an HC-06 and many Arduino Nanos so would be more than happy to help test it if you wanted. Unfortunately, my phone is a Samsung S4 mini which is Android 4 and the app requires Android 5 and above. My wife won't be too happy if I keep using her newer phone in the garden!

Michael
 
ge_rik

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
9,596
132
Cheshire
www.riksrailway.blogspot.com
Corrected diagram,....Bluetooth Power moved from D3....to 5V pin...., resistor voltage divider for correct 3.3V logic level.
Nano will accept up to 12V on it's VIN pin....voltage regulator surplus to requirements.



View attachment 254153

Or, as the Input Voltage is less than 12V, can move jumper on L298 for 5V to supply power to the Nano on the 5V pin.


View attachment 254154
Thanks for your work on this. I must admit to being a belt and braces sort of guy and so, although the li ion pack is nominally rated at 11.1v, when it is fully charged it can exceed 12v, hence the voltage regulator. I wondered if I could use the 5v terminal on the L298 but being new to all this, I wasn't sure.

Rik
 
ge_rik

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
9,596
132
Cheshire
www.riksrailway.blogspot.com
If you need to make your decision now, I would say go with Deltang until this bluetooth system is proven. I've been using Arduinos in locos outdoors since 2013 so I have a lot of experience. Picture from 2013 of Accucraft Lyn controlled by an Arduino Nano and nrf24l01 wifi board with an Arduino Uno in the 'smart' brick, as I called it :)
View attachment 254158
Now whilst I have no experience of bluetooth, I know that connectivity is the main issue in the garden and that whilst it might work so many metres on a bench, when you stick it in a loco, especially a metal one, it's a different matter.

The Arduino/nrf24l01 combination worked but the range was bad, 5 - 10 meters at best.

In 2018 I switched to the ESP8266 which has an integral wi-fi chip and a Tenda router and it is now working happily in my 7 locos and 2 wagons, around a track that is 70 foot long.

I would therefore say that you need to prove that this bluetooth solution works reliably in a metal loco around a large garden before investing too much time or money in it.

I have an HC-06 and many Arduino Nanos so would be more than happy to help test it if you wanted. Unfortunately, my phone is a Samsung S4 mini which is Android 4 and the app requires Android 5 and above. My wife won't be too happy if I keep using her newer phone in the garden!

Michael
Interesting stuff, Michael. I've been tinkering with the PWM frequencies on the Arduino to try and cut down motor buzz. What setting do you use?

Rik
 
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
Re-edited post #27, based on the info in post #30.

A tad concerned that the li ion battery packs could either be X volts, or Y volts at the time of charging, re worked the circuit with the voltage regulator to keep on the safe side of voltage levels to power the Nano.

If, an accurate voltage level after charging a li ion battery pack is established, then as an alternative, power could diverted from the 5V output on the L298N to power the Nano.
 
Last edited:
John S

John S

Registered
5 Nov 2017
634
110
Exeter, Devon, UK
tinkering with the PWM frequencies on the Arduino to try and cut down motor buzz.

Set PWM frequency for D9 & D10
Timer 1 divisor to 256 for PWM frequency of 122.55 Hz
TCCR1B = TCCR1B & B11111000 | B00000100;



Rik, try changing the line above in the program to one of the values below.....

TCCR1B = TCCR1B & B11111000 | B00000001; // 31KHz
TCCR1B = TCCR1B & B11111000 | B00000010; // 3.9KHz
TCCR1B = TCCR1B & B11111000 | B00000011; // 490Hz (default)
TCCR1B = TCCR1B & B11111000 | B00000101; // 30.6Hz
 
Last edited:
  • Like
Reactions: ge_rik
PhilP

PhilP

G Scale, 7/8th's, Electronics
5 Jun 2013
22,947
334
Tamworth, Staffs.
My wife won't be too happy if I keep using her newer phone in the garden!
"But Darling, it IS a mobile phone.."

<Edit - Friday>
Strange.. I am having a situation where I 'post', but then the article is not there, and text is still in the new post box..
I posted this yesterday, but see it is not in the thread, and the above text still in the new post edit box.

An odd missed post, down to me, but this is now one of a handful of times it has happened over the last couple of weeks?
 
M

Michael

Registered
26 Jan 2010
78
15
Interesting stuff, Michael. I've been tinkering with the PWM frequencies on the Arduino to try and cut down motor buzz. What setting do you use?
It depends if you want a hum or a whistle.:) I adjust the value for each loco as it depends on the motor. Normally I use around 5000 to 8000 Hz but interesting to see that the bluetooth example used 122.5Hz. Does anyone know what frequency Deltang uses?

As per John's post, the options are rather restrictive on an Arduino. It's a bit easier with an ESP8266: analogWriteFreq(8000);
 
PhilP

PhilP

G Scale, 7/8th's, Electronics
5 Jun 2013
22,947
334
Tamworth, Staffs.
Does anyone know what frequency Deltang uses?
Default is 'auto' = fastest, which is 12KHz or 16KHz, dependent on model..

It can be altered in programming.. 15Hz makes a motor in a boat sound 'pop-pop-pop'.
 
ge_rik

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
9,596
132
Cheshire
www.riksrailway.blogspot.com
Set PWM frequency for D9 & D10
Timer 1 divisor to 256 for PWM frequency of 122.55 Hz
TCCR1B = TCCR1B & B11111000 | B00000100;



Rik, try changing the line above in the program to one of the values below.....

TCCR1B = TCCR1B & B11111000 | B00000001; // 31KHz
TCCR1B = TCCR1B & B11111000 | B00000010; // 3.9KHz
TCCR1B = TCCR1B & B11111000 | B00000011; // 490Hz (default)
TCCR1B = TCCR1B & B11111000 | B00000101; // 30.6Hz
Trouble with that is it only gives a limited range of values it shoots up from 490Hz to 30kHz with nothing in between. I want to play around with 12kHz or 16kHz.

Looks like there's a library which can be included which gives much finer control over the PWM frequencies.


I'm presently giving that a go.

Rik
 
ge_rik

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
9,596
132
Cheshire
www.riksrailway.blogspot.com
It depends if you want a hum or a whistle.:) I adjust the value for each loco as it depends on the motor. Normally I use around 5000 to 8000 Hz but interesting to see that the bluetooth example used 122.5Hz. Does anyone know what frequency Deltang uses?

As per John's post, the options are rather restrictive on an Arduino. It's a bit easier with an ESP8266: analogWriteFreq(8000);
I wish they'd include something as straightforward as that in the Arduino coding by default. Even with the PWM library it's a bit convoluted.

Rik
 
Greg Elmassian

Greg Elmassian

Registered
8 Mar 2014
2,727
136
San Diego
www.elmassian.com
Most people quickly went over 12 KHz because of the audible buzzing, thus the introduction of "ultrasonic" PWM.... some motors do run better at a much lower PWM, but it is usually older current hungry ones.

Greg
 
ge_rik

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
9,596
132
Cheshire
www.riksrailway.blogspot.com
Latest developments. I've now incorporated sound from a small SD Player module. The tracks are played either in response to key presses on the app (ie the horn and the engine start / stop) and the speed setting determines which of three tracks play (idle, slow speed, fast speed). Not yet perfect. The sound files need a bit of tinkering, but the principle seems to work OK.

Had to revert to the default PWM setting of 490Hz as the PWM library in the Arduino compiler developed a fault.

Getting there ... albeit slowly!


Rik