Introduction: Wii Nunchuk Controlled Model Railroad
A while back, I came across this Instructable which inspired me to create my own version of that project with some of my own additions. A Wii Nunchuk communicates via the I2C bus and there is an Arduino library to easily integrate the controller in our projects.
In this project, let's control our model railroad layout with a Wii Nunchuk. Let's get started!
Supplies
For this project, you will need:
- An Adafruit Motor Shield V2
- *An Arduino microcontroller board compatible with the Adafruit Motor Shield V2(UNO, Leonardo, etc)
- A Wii Nunchuk
- A common-cathode, 7-segment LED display.
- 4 male to male jumper wires(For connecting the Wii Nunchuck to the Arduino board)
- 2 male to male jumper wires(For connecting the track power to the motor shield)
- ^2 male to male jumper wires(For connecting each turnout to the motor shield)
- °8 wires to connect the LED display to the Arduino board.
- A 330 Ohm Resistor(For connecting the common cathode of the LED display)
- A 12-volt DC power source with a current capacity of at least 1A(1000mA).
* Other non-compatible Arduino microcontrollers can also be used with the motor shield as long as they have I2C.
^ The code in the project can control only 3 turnouts, for controlling more turnouts, more motor shields can be stacked on top of the earlier one and the code can be modified to accommodate them.
° You can either cut an end of each jumper wires and solder them to the display's pins, you can also make a breakout board by soldering the display on a dot-matrix PCB and connect the pins to a 1x8 female header.
Step 1: Program the Microcontroller
I would recommend you going through the code before uploading to understand how it works. It will help you modify it in the future.
Step 2: Prepare the 7 Segment Display
You can either make wiring connections on the breadboard or you can solder the display module on a PCB and add some male or female header pins to connect them to the microcontroller using jumper wires. If you are soldering the display on a PCB, connect the 330Ohm resistor to the common pin of the display.
Step 3: Set Up the Layout
You can set up a basic layout with a turnout or two to test all the controls.
Step 4: Install the Motor Shield on the Microcontroller Board
Install the motor shield by carefully aligning the pins of the shield with the headers of the microcontroller board. The pins of stackable shields can be bent and require care.
Step 5: Connect the Feeder Track to the Motor Shield
Connect the track power feeder wires to the output terminal marked M4 of the motor shield.
Step 6: Connect the Turnouts to the Motor Shield
The terminals marked M1, M2, and M3 correspond to turnouts 1, 2, and 3. This information is useful while determining which number to assign to each turnout.
Step 7: Connect the Nunchuk to the Microcontroller
Connect the +ve supply(VCC) wire of the Nunchuk to the +3.3-volt DC source(Most Arduino boards have a 3.3-volt power pin), -ve supply(GND) wire of the Nunchuk to the GND of the microcontroller, SCL wire of the Nunchuk to the SCL pin of the microcontroller and the SDA wire of the Nunchuk to the SDA pin of the microcontroller.
In most Arduino boards, pin A4 is SDA, and pin A5 is SCL.
Step 8: Connect the LED Display to the Microcontroller
Connect the common pin of the display to GND through a 330Ohm resistor if it has a common cathode, else connect the common pin to +5-volts if it has a common anode. Proceed with making the following connections:
- Segment A to D4
- Segment B to D5
- Segment C to D6
- Segment D to D7
- Segment E to D8
- Segment F to D9
- Segment G to D10
Double-check all the wiring connections and make sure the segments are connected to the right digital outputs of the microcontroller.
Step 9: Connect the Setup to Power
You can connect the 12-volt DC power source to the microcontroller board's DC jack or to the motor shield's power input terminal block.
Step 10: Place the Locomotive on the Tracks
Place a locomotive, preferably in a siding, in the layout. Use of a rerailing tool is recommended, especially for steam locomotives to ensure the locomotive is set on the track properly.
Step 11: Power Up the Setup
Turn of the power, you should be able to see the number '1' is displayed on the LED display. If the display does not light up, check if the common pin is connected and check the wiring connections again.
Step 12: Test the Controls
You can watch the above video to understand the controls and check if the controls of your setup work as shown above.
If moving the joystick or tilting the Nunchuk to either side does not give any response, as shown above, check the wiring connections of the Nunchuk, the track power, the turnouts, and make sure both the Nunchuk and the motor shield are not faulty.
Step 13: Expand the Setup
Since the motor shield used in the project communicates via I2C with the microcontroller, you can add multiple shields with distinct addresses by stacking them(You can learn this here). By modifying the code appropriately, you can control dozens of turnouts, and perhaps, some other things on your layout. There is a lot to do.
If possible, do share your creation. All the best!