Electronic Gates for Wooden Trains
This morning I built the controller and software for the electronic gate system I’ve been working on for my sons’ wooden trains. This brought interactive control to raising and lowering the gates through a web app. The result: it was a hit with my kids and they enjoyed it the rest of the day!
The gates themselves are 3D printed gate housings with an SG90 9g RC servo that supports and opens the gate. I designed these in OpenSCAD and printed them out on a Da Vinci 1.0 3D printer. After printing the only assembly required was two screws to hold in the servo and the rest pushed together. For the gate it is beneficial to leave it as a push-fit so that under abuse from the trains it will simply fall off rather than cause some component to break.
The parts for the controller are:
- Raspberry Pi Model B – This is an older model and I’d recommend either the Model B+ or the Raspberry Pi 2 though the instructions will vary a bit from this page.
- Adafruit Servo Hat – This plugs into the Raspberry Pi and allows for control of up to 16 RC servos.
- Edimax EW-7811UN USB WiFi adapter – This is a common USB WiFi adapter for the Raspberry Pi.
- 3 ft. Servo Extension Cables – The servo cables are quite short and these let them reach across the table.
- 8 GB SD Card – An SD Card a few GB in size. For a few dollars more you can get one double the size.
- 2 5V power supplies around 1A. I used an old phone charger with a Micro-USB connector for the Raspberry Pi and a separate power supply to connect to the Servo Hat.
The website for the Servo Hat includes a note: “can be used with the Model A or B if you use a tall 2×13 header instead of the included 2×20″. Another approach to getting this to fit on the Model B is to remove the component video output connector from the Raspberry Pi. I clipped it off and connector (the yellow one) and the Servo Hat fit perfectly onto the board. I also placed some electrical tape over the solder joints on the bottom side of the Servo Hat to prevent it from shorting out on the HDMI connector and capacitor on the Raspberry Pi board.
When fully assembled it is about the size of a deck of playing cards:
The high level process for installing and prepping the base system is shown below which assumes some general familiarity with these systems and steps.
- Install Arch Linux.
- Connect the Raspberry Pi to a wired Ethernet connection.
- Run dhcpcd to acquire an IP address.
- Install packages for WiFi setup, I2C which is used to interface with the Servo Hat, and Python.
pacman -Sy dialog wpa_supplicant i2c-tools python2-pip base-devel python2-flask
- Run wifi-menu to configure the WiFi.
- Edit /etc/wpa_supplicant/wpa_supplicant.conf to configure persistent WiFi security settings.
- Set the WiFi to be enabled at bootup. Use the name provided when running wifi-menu, e.g.
netctl enable wlan0-mynet
- Install the Python I2C libraries:
pip2 install smbus-cffi
- Edit /etc/modules-load.d/raspberrypi.conf and add the two lines to enable I2C support:
- Edit /boot/config.txt and uncomment device_tree_param=i2c_arm=on
At this point SSH should be working over WiFi into the Raspberry Pi.
The control software provides the web front end and also the server-side code to control the servos.
The code is up on GitHub at:
Once checked out and copied over to the Raspberry Pi it can be run with the following command which will start the server and listen on port 5000:
The intent is that this is utilized through a tablet form factor but it works well also on other formats as well.
I had also considered the ESP8266 WiFi module for this project in combination with the more generic Adafruit Servo Driver. I opted for the Raspberry Pi due to the self-contained nature as the ESP8266 approach would have needed a separate system to run the web application.
Given that the Servo Hat can control 16 servos I am curious what other types of servo-controlled features would work well in this format.