Author Topic: Wii Nunchuk Data Reader  (Read 4775 times)

0 Members and 1 Guest are viewing this topic.

Offline GHFTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Wii Nunchuk Data Reader
« on: March 22, 2008, 01:44:52 PM »
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.  ;D

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 :o), 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:
Code: [Select]
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. ;D

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. ;D

-Xo W.
« Last Edit: July 04, 2008, 11:03:35 PM by GHF »

Offline GHFTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: Wii Nunchuk Data Reader
« Reply #1 on: March 22, 2008, 01:48:44 PM »
Eh, just wondering, should I have posted this as a tutorial or something in the Member Tutorials?

(BTW, sorry to everyone for not finishing that helicopter tutorial. I think I need another summer to work on it some more before posting a tutorial.)

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: Wii Nunchuk Data Reader
« Reply #2 on: March 22, 2008, 03:00:24 PM »
Nice dude

Yea man it's a Pactec box, free stuff is awesome

Where are your pull up resistors? I've had the Nunchuck function using a bus under 300khz but not 400khz because I didn't use a pull up resistor to 3.3V. Do not use the resistors built into your PIC, the Nunchuck malfunctions if the bus voltage is over it's supply voltage, it will freeze until a power-on reset.
I even used clamping diodes on my I2C bus because AVR pins are high for a brief moment during reset. If you have both the resistor and the diodes with 3.3V, then you can push the Nunchuck to 400khz without any issues.

Is that a Dimension Engineering motor driver? Can't wait to see the robot.

I have a PICkit2 too, creepy... but mine is a clone

Offline GHFTopic starter

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 0
Re: Wii Nunchuk Data Reader
« Reply #3 on: March 22, 2008, 04:17:25 PM »
I'm running the PIC at 3.3V just so I won't have to worry about trying to interface between two things running at different voltages (you can see the PICKit software set at 3.3V), but I guess it might work better with the pullups.

I didn't really mind that it wasn't higher than 300kHz because most of the time was spent transmitting data. Sending 8 bytes at 2400 baud takes a pretty long time in comparison.

That is a Sabertooth 2x10A. Dimension Engineering controllers are really nice compared to the Robot Power Scorpions I switched from.

I cannibalized my robot and a sumo bot (that explains the big 550 gearhead motors) to build something for the Trinity Fire Fighting contest though, and I have no video of it, so I guess it'll never be seen in all its glory. Now that I think of it, there's no reason I can't make the Trinity bot Nunchuk controlled for fun.

-Xo W.

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: Wii Nunchuk Data Reader
« Reply #4 on: March 23, 2008, 12:20:06 AM »
if you kept the connector, make a I2C slave device so you can have your own controller, good for cheating in guitar hero and DDR

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
Re: Wii Nunchuk Data Reader
« Reply #5 on: March 27, 2008, 05:02:47 AM »
Quote
Eh, just wondering, should I have posted this as a tutorial or something in the Member Tutorials?
Well, the forum is just for summarizing your bot, as you did. The member tutorials would require more details, like schematics and step-by-step stuff, etc. You are more than welcome to write it up, there is definitely demand for wii hacks.

 


Get Your Ad Here