Servo Motor Duplo Train Track Switcher 6379 (B4R,Node-RED)

Experiments & prototyping with the Arduino.
Site Admin
Posts: 211
Joined: 07 Oct 2017, 12:16

Servo Motor Duplo Train Track Switcher 6379 (B4R,Node-RED)

Postby rwblinn » 23 Oct 2017, 11:36

Remote control a Duplo Train Track Switcher.

Build in a servo motor in the Duplo Train Track Switcher 6379. The servo motor is controlled by an Arduino UNO connected to a
  • PC via Arduino IDE Serial Monitor and
  • Raspberry Pi via Node-RED with a Dashboard UI.
The switcher indicates the direction via LED. A maximum of 10 Train Track Switches can be controlled.
The solution will be part of a planned Make Project Duplo Train Control.

Parts Needed
  • 1x Arduino UNO
  • 1x Mini-Servo Motor Tower Pro SG90
  • 1x Duplo Train Track Switcher 6379
  • 1x LED RED + 330 Ohm Resistor
  • 1x LED GREEN + 330 Ohm Resistor


Code: Select all

Servo Motor = Arduino
VCC = 5v
Signal = PWM 8
LED RED = Arduino
- = GND
+ = Digital PWM 9
LED GREEN - Arduino
- = GND
+ = Digital PWM 10


  • Servo moves between 0 (turn right) and 140 (turn left) degrees. Note: if the servo starts to make noise at 140 try another value for max range, i.e. 130 or 150.
  • Servo steering is connected to the switcher via 2x2 Brick. This brick has a 2mm hole in the middle for the servo.
  • Servo is connected to a 2x2 brick, sitting on top of a 2x2x2 brick on the left side of the switch. The top 2x2 brick has a square hole (5 x 10 mm) for the servo.
  • Top 2x2 brick has a red and green led on the left stubs to indicate the switch direction.

B4R Source Code

Solution PC
The Arduino connects to a COM port. For the initial test, connected to a PC Port COM5.
An asynchronous stream is used to read and handle incoming data via the serial line.
The servonr and the angle via are send via the serial line - byte 0 = servonr, bytes 1,2,3 = servoangle:
Example switching servo 0 to angle 140 by sending 0140. To set the angle to 0, send 00

Note: When testing using the Arduino IDE Serial Monitor, ensure the B4R IDE is disconnected after uploading the program to the Arduino.

Solution Node-RED
Tested the solution as described, also with Node-RED running on a Raspberry Pi.
The Arduino connects to the Raspberry Pi USB port /dev/ttyUSB0. Hint: check the ports with command ls /dev/tty*.
A simple Node-RED Dashboard (addon) flow is defined using a serial out node, a switch, functions to convert the switch angle, text node and gauge.
The serial out node has a properties /dev/ttyUSB0:115200-8N1. For other properties, kept the default.

Software Versions
Raspberry Pi Raspian v9.1, Node-RED v.0.17.5, nodejs v6.11.4, Node-RED Dashboard v2.5.1

Node-RED Flow

Code: Select all

{"id":"fb9b868c.a83b68","type":"serial out","z":"6eb3e321.ba79dc","name":"","serial":"b3caf8ef.980558","x":490,"y":120,"wires":[]},
{"id":"c0917bf4.4754d8","type":"ui_switch","z":"6eb3e321.ba79dc","name":"Switch","label":"Switch Direction","group":"a8c6acf1.e8654","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"00","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0140","offvalueType":"str","officon":"","offcolor":"","x":130,"y":120,"wires":[["fb9b868c.a83b68","7806d873.bdc168","58a02a6b.f9b704"]]},
{"id":"9c09336e.9f9d","type":"comment","z":"6eb3e321.ba79dc","name":"Dashboard Train Track Switcher 0","info":"","x":180,"y":60,"wires":[]},
{"id":"65b5584b.cf0ef8","type":"ui_gauge","z":"6eb3e321.ba79dc","name":"Switch Indicator","group":"a8c6acf1.e8654","order":0,"width":0,"height":0,"gtype":"gage","title":"","label":"","format":"{{value}}","min":0,"max":"140","colors":["#00b500","#e6e600","#ca3838"],"seg1":"70","seg2":"71","x":720,"y":260,"wires":[]},
{"id":"7806d873.bdc168","type":"function","z":"6eb3e321.ba79dc","name":"Get Switch Angle","func":"// Get the switch angle from the Track Control string\n// Track Control string:\n// ServoNr 0 - 9\n// ServoAngle 0 - 150\n// Example servo 0 setting angle 150: 0150\nvar angle = msg.payload;\nangle = angle.substring(1, angle.length);\nmsg.payload = angle;\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":260,"wires":[["65b5584b.cf0ef8"]]},
{"id":"a20b1268.9d2308","type":"inject","z":"6eb3e321.ba79dc","name":"Reset Track","topic":"","payload":"00","payloadType":"str","repeat":"","crontab":"","once":true,"x":150,"y":260,"wires":[["c0917bf4.4754d8"]]},
{"id":"58a02a6b.f9b704","type":"function","z":"6eb3e321.ba79dc","name":"Get Switch Position","func":"// Get the switch position from the Track Control string\n// Track Control string:\n// ServoNr 0 - 9\n// ServoAngle 0 - 150\n// Example servo 0 setting angle 150: 0150\nvar angle = msg.payload;\nangle = angle.substring(1, angle.length);\n\nvar direction = \"Unknown\";\n\nswitch (angle) {\n    case \"0\":\n        direction = \"Left\";\n        break;\n    case \"140\":\n        direction = \"Right\";\n        break;\n}\n\nmsg.payload = direction;\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":200,"wires":[["fe560bcd.ab3d68"]]},
{"id":"a8c6acf1.e8654","type":"ui_group","z":"","name":"Track Switcher 0","tab":"b8cec0d3.34b7d8","disp":true,"width":"6"},
{"id":"b8cec0d3.34b7d8","type":"ui_tab","z":"","name":"Duplo RailRoad","icon":"dashboard"}

  • Experiment is completed.

Python Hint
To test the serial communication on the Raspberry Pi, Python is also an option. The PySerial library is required:

Code: Select all

sudo apt-get install python-serial

Options to enable the Serial Interface:
Option 1

Code: Select all

Raspberry Pi Configuration > Interfaces > Serial > Enabled

This will add the line enable_uart = 1 to /boot/config.txt
Option 2

Code: Select all

cd /boot
sudo nano config.txt
Add the line:
enable_uart = 1

Example Python 2.7 Code

Code: Select all

import time
import serial

print "Starting program"
ser = serial.Serial('/dev/ttyUSB0', baudrate=115200,
#Important to provide short break to get the line settings
        # ser.write("0150")

        # To read any response from the serial line
        # while True:
        #        response = ser.readline()
        #        print("python printed:", response)
        print("Error Occurs, Exiting Program")

Return to “Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest