Page 1 of 1

Remote controlled battery powered motorized locomotive running on Duplo train track (B4R, Node-RED, B4J, ABMaterial)

Posted: 09 Nov 2017, 10:07
by rwblinn
Create a remote controlled functional battery powered motorized locomotive running on LEGO® Duplo train track.

  • Locomotive move forwards, reverse and break, set speed, obstacle detection, set obstacle detection distance, turn headlight on / off with dimming option, buzzer.
  • Free modelled using a mix of LEGO Duplo train & other parts.
  • Communication between the devices via MQTT.
  • Remote Control via web browser.
  • Remote Control applications based on
  • Locomotive controller program written in B4R by Anywhere Software.
The solution will be part of a planned Make Project Duplo Train Control.
Warning: The solution is not recommended for children 5 years and younger.

Parts Needed
  • 1x Raspberry Pi
  • 1x ESP8266 NodeMCU v1.0
  • 1x ESP12E Motor Shield
  • 1x DC Motor
  • 1x HC-SR04 Distance Sensor
  • 1x LED WHITE + 220 Ohm Resistor
  • 1x Piezo Buzzer
  • 1x Battery 9v
  • 1x Power Switch


Code: Select all

Locomitive Wiring
DC Motor = Motor Shield
DC = A+
GND = A-

9V Battery Pack = Motor Shield
+ = VIN via Switch

HC-SR04 = Motor Shield
VCC = 3.3v
Trig = D7 (GPIO13)
Echo = D8 (GPIO15)
Note: for the HC-SR04, 5v is more suitable but because VIN is powered with 9v, 3.3v is used instead.
The sensitivity is low but gives a nice side effect of slowly stopping the loco if an obstacle is detected.
For a next version of the project a voltage step-down device is planned.

LED Headlight = Motor Shield
+ = D6 (GPIO12) via 220Ohm resistor

Buzzer = Motor Shield
+ = D5 (GPIO14)


Notes: This is the locomotive prototype acting as a proof of concept.
Locomotive Chassis:
  • Top left: head part of the train 10810 with 5mm white LED and buzzer inside the chimney
  • Top middle: cabin 3x2 holding the motor shield + NodeMCU
  • Top right: cabin 3x2 holding the 9v battery with power switch
  • Bottom left: distance sensor
  • Bottom middle: brick 2x4x2 with the DC motor embedded. The wheels are from Lego technic
  • Bottom right: carriage 2x3 (by cutting carriage 2x6 into half)

Download B4R source code, Node-RED flow, B4J ABMaterial source code

Software Versions
Raspberry Pi Raspian v9.1/Linux 4.9.59-v7+, Node-RED v.0.17.5, nodejs v6.11.4, Node-RED Dashboard v2.5.1, B4R v2.20, B4J 5.81, ABMaterial 4.00

  • Experiment is completed.
  • Planned for next version: Voltage step-down 9v to 5v (9v to VM, VM to Voltage step-down 5v to VIN, HC-SR04 to VIN instead 3.3v)

Additional Information

The MQTT protocol is used to communicate between the locomotive and a client.
The B4R program running on the locomotive controller mainly subscribes to topics with payload to control the locomotive.
Only the program version is published.
Any MQTT client can be used to control the locomotive, but for this solution Node-RED is selected.

MQTT Topics subscribed by the B4R program running on the locomotive controller
train/brk = stop the locomotive
train/fwd = move the locomotive forwards with speed as payload
train/rev = move the locomotive reverse with speed as payload
train/distancethreshold = set the threshold for the obstacle detection
train/headlight = turn the locomotive headlight on / off
train/buzzer = turn the locomotive buzzer on / off
train/version = get the B4R program version

MQTT Topics published by the B4R program running on the locomotive
train/version = B4R program version

Node-RED is running on a Raspberry Pi. Defining & controlling the locomotive is done via web browser.
Recommend to read

Node-RED Locomotive Control Dashboard
The Node-RED-Dashboard is used for the UI to control the locomotive.
Quick install via:

Code: Select all

cd ~/.node-red, then run
npm i node-red-dashboard

  • Access the dashboard via web browser with url http://raspberry pi ip-address:1880/ui
  • Node-RED has a responsive layout handling various screen sizes (PC, Tablet, Smartphone)

Node-RED Locomotive Control Flow
  • Access the flow via web browser with url http://raspberry pi ip-address:1880
  • The flow communicates via MQTT topics with payload, i.e. to move the locomotive forwards publish topic train/fwd with payload 70. 70 is the speed (possible range 0-100)

Screen shot of the ABMaterial LocoControl application running on an Android Smartphone in Browser.

{ Motivated by making solutions, having fun and share }