Wii Nunchuk Controlled Model Train

 by MakersBox
Featured
Using an Arduino microcontroller, an Adafruit motor shield, and a Wii Nunchuk, you can create a intuitive, programmable, model train controller to run your layout.  Amaze your friends.  Entertain your kids.  Not quite DC, not quite DCC.  You're headed for the hacker lands of PWM (pulse-width modulation).

Why do this?
 - Pulse-width modulation allows smooth operation at slow speeds.
 - Set realistic rates of acceleration and braking.
 - Set maximum speed to avoid derailments.
 - Program operational responses to meet your operating style.
 - Control train based on sensors, auto reversing, whatever.
 - Because you can, and it is cool.


 
Remove these adsRemove these ads by Signing Up

Step 1: Beg, Borrow, or Build

ePIM7589.JPG
You will need to beg, borrow, or build a couple of things, all well documented.  Don't be afraid.

1.  Wii Nunchuk.  - The only part of this that isn't "open source" or completely hackable.  It is, luckly, relatively inexpensive, easy to obtain (you may already have one on hand), and incredibly versatile.  For $20 you are getting:
          2-axis joystick
          3-axis accelerometer
          2 pushbuttons
          Countless hours of Nintendo product engineering (priceless)

2.  WiiChuck Adapter.  - While you can cut your cable to access the wires, your kids will hate you when they go to play Lego StarWars next time.  A couple of bucks will get you an adapter and guilt-free access to a software library written by the guy that came up with it (www.sparkfun.com/commerce/product_info.php), or your could build your own using this Instructable.

3. Arduino Microcontroller - You can buy or build one of these.  They are easy and fun to use, and with the shields, can be put to a number of uses, repeatedly.  They come in a number of variants.  I like the newer Duemilanove since it has USB built in.  Here is one source: www.adafruit.com/index.php

4. Motor Controller - As versatile as the Arduino is, it can't drive a DC motor on it's own.  You can breadboard something together using a H-Bridge chip like the SN754410, as shown here, or buy a shield kit that fits right on the Arduino and comes with software libraries and examples.  I'm using one I built from Adafruit shown here.

5. Model Train - (Sold separately). Or any small DC motor you might want to drive.  I'm actually doing this for a small "switching puzzle" layout I've got built.  It is only 1' x 4', and I want it to be easily (and safely) operated by children.  It involves a lot of slow-speed back-and-forth, unlike the typical full-throttle round-and-round stuff.  You can learn more about the layout on the last page.
C.Mahoney says: Apr 20, 2012. 4:41 PM
very nice project!!
I need some help with the program.
these are the errors that i get:


nunchuck.cpp:324:28: error: nunchuck_funcs.h: No such file or directory
nunchuck.cpp:325:21: error: AFMotor.h: No such file or directory
nunchuck.cpp: In function 'void nunchuck_init()':
wii_train_controller:152: error: 'Wire' was not declared in this scope
nunchuck.cpp: In function 'void nunchuck_send_request()':
wii_train_controller:163: error: 'Wire' was not declared in this scope
nunchuck.cpp: In function 'int nunchuck_get_data()':
wii_train_controller:181: error: 'Wire' was not declared in this scope

Can some one help Please
MakersBox (author) in reply to C.MahoneyApr 20, 2012. 6:58 PM
It sounds like the sketch can't find the extra libraries. You need to download and install both the motor library from Adafruit and the Nunchuck library. Check the Arduino forum if you need help with that.
C.Mahoney in reply to MakersBoxApr 21, 2012. 5:20 AM
thanks!
when i go to get hub which of the files should i get(motor-shield)? it wont let me download all of them at once and how to i save them properly?
Henry_Best says: Nov 3, 2011. 10:28 PM
Have you thought about using magnetic reed switches to detect when a train is approaching the end of tracks? All you need is a couple of small bar magnets glued to the underside of the loco and trailing vehicle (for reverse operation). The reed switches, which are very cheap, can be easily disguised as 'between track' fittings and can be painted without affecting their operation. If you're using a common ground, just connect one side of the (normally open) reed switch to that and take the other side back to one of the Arduino pins, held at 5v by a 1k resistor. Poll that pin to detect when it's grounded and apply the brakes! The magnets should have the same orientation as the reed switches (don't have them at right angles to each other) otherwise you may get a double switching as the N and then the S poles move over the switch.
You can also use reed switches for train differentiation. Imagine two locos, one with two magnets spaced 1" apart and the other with the magnets 2" apart. With 3 reed switches spaced 1" and 1" you can detect if switches 1 and 2 are closed at the same time or if switches 1 and 3 are closed at the same time. Simply ground one end of switch 1 and connect the other end to both switches 2 and 3. The outputs of switches 2 and 3 will tell you which train is present.
My idea is for an automatic 'hump' shunting yard. The wagons would have a bar code stuck to the underside and a bar code reader in the hump would read the barcode and switch the points to the appropriate track. I don't yet know how to do this, but I'm sure that it could be done with an Arduino!


BuckarooBanzai says: Sep 10, 2011. 1:13 AM
Great Instructable Ken, and Dawson Station is an exceptional piece of craftsmanship.
mewithaflower says: Feb 8, 2011. 3:08 PM
Hello, this is a great project but I cant download the code! could you put another link up so I could get the code please? this link gives me a tmp file!? Regards Richard
MakersBox (author) in reply to mewithaflowerFeb 8, 2011. 6:17 PM
I uploaded a zip file. Give it a try.
mewithaflower in reply to MakersBoxFeb 9, 2011. 1:43 AM
Works great, i've learnt loads already. cheers dude
randomhand says: Jan 21, 2010. 1:19 PM
Saw this layout on www.carendt.com. Realy nice job
aseaheru in reply to randomhandOct 5, 2010. 9:23 AM
dito
Doc Holliday says: Mar 14, 2010. 10:41 AM
Has anybody used this with DCC?  I wouln't want to burn out a lot of locos, turnouts,  and other controlled stuff.

On my DC garden layout, this might be perfect.
aseaheru in reply to Doc HollidayOct 5, 2010. 9:23 AM
you probaly will need the entire wii to get the parts cus you will need antennas...... and receavers
Honus says: Jan 19, 2010. 12:13 PM
Neato -I like it!

It would definitely be cool for slot cars. My little boys have a hard time controlling the car speed and keeping them on the track. One thing you could also do is have lane changing on a routed wood track (or track changing for trains) by using either a solenoid or servo motor to move the track by pressing one of the nunchuck buttons. I wrote some code a couple of years ago that allows access to all the nunchuck functions so I might have to try that out.
MakersBox (author) in reply to HonusJan 19, 2010. 2:16 PM
You are one step ahead of me.  Operating turnouts is the next step.  I was going to use servos, but then I found this neat little circuit which will use the Atlas remote switches which I already have:

http://www3.sympatico.ca/kstapleton3/751D.HTM

I haven't put the accelerometer readings to use yet.  Maybe I could use a throwing action to make the turnouts switch?
Honus in reply to MakersBoxJan 20, 2010. 3:49 PM
I've set up a nunchuck so the accelerometer and joystick functions move servos and the buttons trigger transistors to turn on sounds/lights. I figured for slot cars I'd use the joystick to control acceleration/braking and the buttons could control two lane changers. Don't know what I'd use the accelerometer for....
aseaheru in reply to HonusOct 4, 2010. 9:30 AM
top speed? normaly shunting is a max speed of 15 mi.....
inventordude14 says: Jan 24, 2010. 7:40 AM
  nice layout! heres a picture of mine (still in progress)
J&J tech 014.jpg
MakersBox (author) in reply to inventordude14Jan 24, 2010. 8:26 AM
Has there ever been a layout that wasn't "still in progress"?

Looks good.  Nice low-angle action shot.

Ken
aseaheru in reply to MakersBoxOct 4, 2010. 9:29 AM
never. aways changing. random question, have you heard of Gn15?
Doc Holliday in reply to MakersBoxMar 14, 2010. 10:42 AM
Yup,

The last one.
aseaheru in reply to Doc HollidayOct 4, 2010. 9:29 AM
nothing is ever finished.
aseaheru says: Oct 4, 2010. 9:28 AM
did not know you wii-d dawson station. cool.
stephenniall says: Jan 17, 2010. 12:47 PM
This is handy i hate derailing skelectric cars ! .

will have to look into buying an arduino
Doctor What says: Jan 16, 2010. 9:08 PM
 What a magnificent application of the Wiimote!

I love how hackable the wiimote and nunchuck attachment are.  They have tons of uses yet to be unlocked!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!