Arduino Train Junior Lite

Arduino Train for Kids 2

Own railway models collection is certainly good. But if you have children, their enthusiasm for your "toy" will cost you dearly!
So I came up with an Application that unites you.
Description see on HACKSTER.IO

It is enough to take any children's railway, the main thing is that the locomotive had a motor and a place for batteries. You can even buy a few simple sets with plastic rails, and manage several trains at the same times. Lego Trains, Big Thomas Coal Steam and so on is good example.

Despite the fact that the application interface is made for children, the application itself supports the version of Protocol 2 of the URB project. And this means that the same application can be controlled also the railway layout, though not all, but only trains under control driver A. Also the application supports the installation of Arduino sensors, which makes the train behave a bit like a robot.

Before running my application, pair your device and the Bluetooth module HC-06 or yours Bluetooth shield way through the Android settings.

 


Garden Train

 

Garden Train

Bluetooth phone app train control for Garden Railways

  • Any number of controlled locomotives
  • 20-x speed thrust control
  • Extended lights and signals control
  • Unique function – a wireless decoupling system on loco
  • No restrictions! You can modify this system for any construction of locomotives or use it in your control system
  • Universal button for servicing loco devices (for example, cabin lighting, start / stop of the snow removal rotor or steam generator and other things)
  • Simple protocol (API) with extensibility
Garden Train

Simple and easy start

The simplest system consists of any Arduino board, a Bluetooth module and any Motor-driver with a voltage corresponding to the battery and the current consumed by the DC motor. As an example, I will use the standard parts of the project's URB: Arduino NANO, bluetooth module HC-05 and L298N. All these details together cost less than 10 US dollars.

Your locomotive battery should have a voltage of between 9 and 12 volts. If you take voltage from the rails, then you need to add a step-down DC-DC converter to the Arduino's power supply circuit and a diode with a capacitor of significant capacitance (about 1000 μF).

Train on battery

Small diesel garden railway loco
Simple circuit

The elementary circuit for using Arduino Garden Train App


Sketch for Arduino NANO

The sketch for Arduino is based on the example of Arduino IDE named SerialEvent. A four-character command parsing from the application is a block of code – if (stringComplete) { ... }. After which you can program any reaction of the microcontroller Atmega to these commands. Simple command protocol can be extended according to your requirements. Currently, the application has implemented 10 control commands and 20 commands for setting the motor rotation speed. Upload sketch to Arduino NANO.

After connecting via the Bluetooth, you can view these commands in serial terminal on the Arduino IDE of your computer with the USB cable connected.

Simple sketch for Garden Train
API

Connect App to different locomotives

Push Bluetooth Icon

Choose the loco by MAC address

Connect established

Push icon again for disconnecting, and reconnect to another loco

You can install this solution on multiple locomotives. The bluetooth connections in this project work as point-to-point without routing. Since each bluetooth module included in this scheme has its own unique MAC address, by switching these addresses in the application you can manage them all. Also, you and your friends can manage simultaneously multiple trains by installing the application on multiple phones.


More features. Start and disconnect commands.

My application, when successfully connected to the locomotive, sends the command «999z» to Arduino, and when it is disconnected, the command «888z» is also sent. Let's add a code to the sketch that will flash the floodlights of locomotive when it is successfully connected and turn off all the lights when it is turned off. Accordingly, you need to add LEDs to the circuit and add the code for manage control them from the application.

Since the main idea of my project is that you can completely rewrite the sketch code and come up with and implement your functions and ideas, these commands can also be used to turn on the engine sound of a locomotive or steam generator at connecting. And switching off them when disconnecting bluetooth from loco. You can also override the purpose of all other commands.

Buttons and features App Garden Train

More loco power

The maximum current from one pin Arduino is less than 40mA. In order for spotlights on a large-sized locomotive to be realistic, powerful LEDs with a current of more than 200 mA are needed, and large currents are also needed to control the servo drive. Therefore, it is logical to add a high current booster to the circuit. It can be done on transistors, but I prefer to use chips like Darlington arrays, etc.

If you are going to use a load with 5 volt voltage (for example, servos), then you also need to add a stepdown DC-DC converter to the circuit. And accordingly, slightly change the power scheme for the Arduino board. It is also very useful if you are going to add sound and many other accessories for Arduino (sensors, actuators, etc.)

In this sketch, added function via pressing a «decoupling» button for the processl of the train uncoupling mechanism. The servo lowers the trailer for 2 seconds and then returns the hitch hook to its original position. Like all the other examples, you can change all the parameters of the mechanism and the code of the sketch for your coupling mechanism. You can also add any blinking or fading effects to the code of the spotlights (see the URB project).

More Power sketch

Customization

The Arduino Garden Train app is a wireless remote control. You can use this application with any type of equipment placed on your locomotive. This can be not only a system based on classic Arduino boards. You only need to create a Bluetooth channel between the locomotive and the phone – simple four-character commands sent by the application can be processed by any microcontroller. For example, you can use ESP32, with integrated Bluetooth module and a large amount of memory. Or STM microcontrollers popular series of AVR.

Project MCUs

In my opinion, I provided all the necessary commands, but if you need more, send me a message. The application also implemented feedback. As an example of this type of functionality, you can see the online status of the locomotive accumulator when it is BT connected. Examples of this and other functions are given in this chapter with the code for Arduino as a starting point.

State of loco battery

State of battery

After the connection is established, it is enough to send the battery status from Arduino using a simple code:

byte data;
...
Bluetooth.print(data);
Bluetooth.print("z");

Where data is a variable with a value from 0 to 100, which can be produced by reading and mapping from any analog input of the microcontroller voltage values when using a bridge resistive circuit.

Voltage measuring

Without this data, the icon remains gray. If the data value is less than 40, then the icon turns yellow, if less than 20, it turns pink.

Setting PWM frequency on 120Hz

By default, the PWM sampling rate for Arduino is about 500 Hz. If you lower it to 120 Hz, the power of the electric motor at low revs increases, the unpleasant high-frequency squeak of the motor armature also decreases.

In all circuits presented above, the engine is controlled from pin 9 of Arduino (Timer 1). To decrease PWM to 120Hz needs add to the void setup () code block a line:

TCCR1B = TCCR1B & B11111000 | B00000100;

If the circuit includes a servo, then you need to replace the standard Servo.h library with an alternative – for example ServoTimer2.h

Sound

The values of all speeds are defined in the sketch, so you can use a popular solution based on DFPlayer Mini MP3 Player Module MP3 For Arduino. Or use ESP32 chip capabilities by adding an amplifier.

See also popular blog: Peckforton Light Railway.

Voltage measuring

Sound

how use sound