Arduino Train for Kids


Introduction: Arduino Train for Kids

About: Hello. Main name is Steve. I'm designer and former engineer from Moscow.

This simple project will introduce your children to the fascinating world of Arduino. You only need ANY Toy Loco with a DC motor, four details from Arduino and a little imagination. The application can be installed on several Android modules and your childs will played multiple trains simultaneously. And if you add sensors and LEDs, then you will get a robot train. Instructions and the application you will can adapt even to other electrical toys.

Step 1: Details and Construction

For this model, I took the simplest set and I had to place the batteries in the car. But you can take a bigger toy, or even a LEGO train. You just need to figure out how to place wires and parts inside the locomotive (otherwise the children will disassemble your project faster than you can show them a new toy).

So you need: Arduino Nano, Bluetooth module HC-06, motor-driver L9110 and, optionally IR sensor and several LEDs. If your locomotive does not have room for four AAA batteries, then you will need two battery boxes for it. The choice of these particular parts is not mandatory, you can use ready-made Arduino Shields, but it's probably very large for a toy train.

Step 2: Electrical Connections

Please note, when connecting LEDs as a GND contact, I used the Arduino's D8 and D12 outputs, and then set a LOW level for them in the sketch. So the design turns out a little more compact. Resistance for LEDs is about 150 Ohm. If you use an infrared sensor, then set the desired response threshold uses variable resistance.

Step 3: Sketch

#define SENSOR_IR 2
#define L9110S_AIA 3 #define L9110S_AIB 5 #define GND1 8 #define LED1 9 #define LED2 10 #define FLOODLIGHT 11 #define GND2 12

static unsigned long preMillis; String inputString = ""; boolean stringComplete = false; int locoSpeed, counter = 0; bool tick, directionForward, directionBackward; bool sensorActive = false, alarm_allRed = false;

void setup() { // Initialize serial Serial.begin(9600); inputString.reserve(4);

// Pins pinMode(GND1, OUTPUT); pinMode(GND2, OUTPUT); pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(FLOODLIGHT, OUTPUT); pinMode(L9110S_AIA, OUTPUT); pinMode(L9110S_AIB, OUTPUT); pinMode(SENSOR_IR, INPUT);

digitalWrite(GND1, LOW); digitalWrite(GND2, LOW); digitalWrite(FLOODLIGHT, HIGH); delay(300); digitalWrite(FLOODLIGHT, LOW); }

void loop() {

if (stringComplete) {

// TRAIN CONTROL if (inputString.charAt(0) =='a') {

// TROTTLE if (inputString.charAt(1) =='0') { if (inputString.charAt(2) =='0') locoSpeed = 0; // Stop if (inputString.charAt(2) =='2') locoSpeed = 80; if (inputString.charAt(2) =='4') locoSpeed = 110; if (inputString.charAt(2) =='6') locoSpeed = 140; if (inputString.charAt(2) =='8') locoSpeed = 170; } if (inputString.charAt(1) =='1') locoSpeed = 200;

// MOVE DIRECTION if (inputString.charAt(1) =='d') { if (inputString.charAt(2) =='f') { directionForward = true; directionBackward = false; } if (inputString.charAt(2) =='b') { directionForward = false; directionBackward = true; } if (inputString.charAt(2) =='s') { locoSpeed = 0; directionForward = false; directionBackward = false; // DC motor conflict prevention digitalWrite(L9110S_AIA, LOW); digitalWrite(L9110S_AIB, LOW); delay(400); } } }

// LIGHT if (inputString.charAt(0) =='l') { if (inputString.charAt(2) =='0') digitalWrite(FLOODLIGHT, LOW); if (inputString.charAt(2) =='1') digitalWrite(FLOODLIGHT, HIGH); }

// SENSOR ON/OFF if (inputString.charAt(0) =='s') { if (inputString.charAt(2) =='0') sensorActive = false; if (inputString.charAt(2) =='1') sensorActive = true; }

inputString = ""; stringComplete = false; }

if (digitalRead(SENSOR_IR) == HIGH || !sensorActive) {

if (directionBackward) { digitalWrite(L9110S_AIB, LOW); analogWrite(L9110S_AIA, locoSpeed); } if (directionForward) { digitalWrite(L9110S_AIA, LOW); analogWrite(L9110S_AIB, locoSpeed); } alarm_allRed = false;

} else { // STOP at IR sensor

digitalWrite(L9110S_AIA, LOW); digitalWrite(L9110S_AIB, LOW); Serial.println("alarmz"); alarm_allRed = true; }

if (locoSpeed > 0 && !alarm_allRed) { static unsigned long blinkMillis; if (millis() > (blinkMillis + 300)) { tick = !tick; blinkMillis = millis(); } digitalWrite(LED1, tick); digitalWrite(LED2, !tick); } else if (alarm_allRed) { digitalWrite(LED1, HIGH); digitalWrite(LED2, HIGH); } else { digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); } }

// FUNCTIONS void serialEvent() { while (Serial.available()) { char inChar = (char); inputString += inChar; if (inChar == 'z') { stringComplete = true; } } }

Step 4: Android App

The application allows you to turn on the searchlight, control the speed and direction of the train and makes funny sounds. The principle of data transmission on bluetooth and a detailed description of the principles of management you can find on my site.

You can install the application of Arduino Train for Kids 2 from Google Play.



    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest
    • Water Contest

      Water Contest