I built a robot for a competition and had the bright idea of using the tilt-sensing abilities of the Nunchuk to control a forklift in the front of the bot, after reading about grabbing data from it on the todbot blog
. It worked pretty well, and I thought I had a really novel idea. That is, until I saw frank26080115's Nunchuk-controlled robot
. It was eerily similar to my bot, down to the radio links and even the project enclosures we used for the transmitter box (A PacTec rubber-sided handheld box, I believe). He sure did beat me to posting it.
The big difference we had, though, were the microcontrollers. I'm a big fan of PICs, not because of features or speed or anything, but because you can get free samples of anything you want from Microchip and the enormous userbase and amount existing code written for every language and compiler. So ported over the code written for the Arduino
to the 18F2550. I chose this one because it's one of the fastest PIC 18Fs (it's spec'd for 48MHz but I've found they'll run up to 160MHz
), and it has a hardware I2C master (easier to use and faster than software I2C). The result was a much faster and more optimized Nunchuk-reading controller than would be possible with the Arduino.
Anyways, now that the competition is over, I cleaned up the code a bit and reworked it into a standalone "Nunchuk Reader chip
." Basically, all you have to do now is connect the Nunchuk to the chip, power it up, and get serial data out of it. Interfacing this to the $50 bot should be a snap.
1) Connect the Nunchuk to the 18F2550
You could cut the wires like I did (I was in a rush. I cried tears when I cut that beautiful perfect cable. I'm sorry, Nunchuk.) and connect like so:
18F2550 - Nunchuk
Pin 22 | Yellow
Pin 21 | Green
Pin 20 | Red
Pin 19 | White
Or you could get these adapters
and connect them with "c" to pin 22 and "-" to pin 19. Yep, that's right, a connector made for the Arduino works fine on the PIC.
Please don't cut the connector, for the sake of your perfect Nunchuk.
2) Flash the program to the 18F2550.Shazam!
Oh wait, that was camera flash. Well, you should follow the instructions of your PIC programmer to do this step.
The source code is attached (uses the Microchip C18 compiler). I also included a compiled .hex for running it from a 20MHz crystal.
I personally recommend the PICKit 2, because it's cheap ($35 isn't that much considering you can use this for years), supports a huge (and growing, with firmware updates) list of PICs, and constantly updated and expanded by Microchip as they add new products to their line. It's USB, so you don't have to worry about whether your next computer will have a serial port or if the serial port can provide the right voltage. Support is so-so, but definitely better than what you'd get if you bought a cheap serial JDM programmer or something.
3) Power it up!
The Nunchuk is designed to handle 3.3V power, so that's what I use. Through testing, I found that the highest I2C clock you can reliably use is 300kHz. It's probably spec'd for only 100kHz though. At 5V, you can get 400kHz and above... at the cost of premature Nunchuk death (maybe) and higher current draw (definitely).
4) Connect to serial port, or robot, or whatever!
The data comes out on pin 17 (the yellow wire not attached to anything in the pics) at 9600-8-N-1, the most commonly used, but I usually test with my computer at 115200 baud. At this rate, the Nunchuk can't keep with the the PIC's request for data, so you have to insert a delay into the main loop.
The preamble/header bytes are 0xFF, 0x00, followed by the 6 bytes that come out of the Nunchuk. The meaning of the 6 bytes are explained here
Now you have chip that reads data from the Nunchuk and outputs it in a very microcontroller-friendly format. It's dirt cheap -- if you have a programmer, all you need is a crystal ($1.50 each from Electronic Goldmine). Have fun with it. If you do something awesome with it, tell me.