Using a wii nunchuck This works and everything you need.

voodoojar

100 W
Joined
Jan 9, 2014
Messages
124
I've noticed this has been a hard nut to crack for a lot of people so here is everything you need and how to do it. I'm using an uno but as long as it's an Arduino with the Atmega328P chip you should be ok.

the Sketch -- http://voodoojar.com/wiiskate/nunchuck2.zip
the Libraries -- http://voodoojar.com/wiiskate/libraries.zip

How to do it
Uncompress nunchuck2.zip to desktop
Uncompress the libraries.zip to -- Documents -> Arduino (you are replacing the existing Libraries folder with this one.)
Open the nunchuck2.ino in Arduino IDE, verify sketch and upload to your board.

wiring --
arduino.jpg


Wii to Arduino
this is pretty standard but from wiimote using the sparkfun wiinunchuck adapter https://www.sparkfun.com/products/9281
from outside
gnd to gnd
+ to 5v on adruino
d to a4 on arduino
c to a5 on arduino

Arduino to ESC
ESC gnd to gnd on arduino
ESC to digital pin 9 on arduino
Leave the ESC + alone unless you want to power your arduino with it and you know it is putting out 5v. If you do want to power your arduino from the ESC -- ESC + to arduino Vin make sure no other power it going to your arduino or you will have a bad time

Open Serial monitor and test it out. (make sure you baud on Serial Monitor is set to 115200 or you wont see anything)
You will need to make adjustments based on the type of nunchuck and ESC but this will get you 99% there.



Problem I haven't sorted out -- when turning on power sometimes the nunchuck wont communicate with the Arduino. This could be the $8 nunchuck I'm using, the order I power up parts or who knows. I ordered a wired nunchuck for testing since that's the only way I can totally eliminate connection issues for testing.

Also, thanks to the people who wrote the libraries and Mantis who wrote the sketch.
 
benj said:
This is great, thanks for putting it up (and thanks Furp for his thread too).

But the golden question - how is it? Is the connection reliable when you're riding, and does the nunchuck have enough resolution to control power safely?

It seems like a reliable connection, my problem is getting the Y stick oriented so any up movement provides acceleration. The nunchuck is far from perfect but it is small and doesn't look like a gun.

I'm also making a custom control that I'm modelling right now that will just be a 1 channel esc controller.


daverobson08 said:
Thanks for taking the time to post this mate. Good resource :)

How does the code work? Is there any functionality to the C or Z buttons?

C = slow acceleration
Z= fast acceleration, plus hold Z to maintain speed.
 
It would be great to have it like it is for the metroboard (at least I think they have it that way):
Hold Z and stick up = accelerate (the further the faster), stick down = decelerate (further down = faster deceleration)
as longs as Z is pressed and the stick is in the center, it will stay at whatever throttle was last
release Z = no throttle
press c and stick up or down = brake, the further from center point, the stronger the brake

I thought there was a code for that way around somewhere, maybe it is the one from BLDC-ADDICT?
 
Does anyone have any idea on how to calibrate the wireless nunchuck with the esc? I have the hobbyking 150a. I know how to get the esc into calibrating mode i just need to know what buttons on the nunchuck to press and when. It calls for full throttle first then full brake then neutral. Ive tried what i thought was right but the motor only has 3 speeds and does not gradually speed up if i push the stick up, it only does fast and faster. Ive searched and i cant find anyone who has said how to calibrate this thing. Any help would be great.

Thank you
Tony
 
I haven't looked at the code, but note that most ESCs will interpret 2ms pulses as full throttle (1.5ms is half throttle, neutral on a car esc and 1ms is low throttle, reverse/brake for a car). You'll have to find out what button/button combination with your setup will give the esc the pulse width it is looking for.
 
I am going to preface this with the fact that I am a total noob at this programming business, and really have no idea what I am doing. I bought an arduino board awhile back, and finally got through the "blink" example on their website and was very proud of myself. So please take that into consideration.

Anyway, I've been meaning to try this for a long time, and I finally broke down and bought the Nyko Kama nunchuck for $6.75 shipped on Amazon. Of course, I hadn't actually figured out what I was going to do with it when it showed up, and I had no idea that the adapter you used even existed. So I broke the receiver open and found that it did not have the 0.1" pin spacing that the arduino headers do, so I couldn't just solder some header pins to the receiver. Does anyone know if I can get some device that will allow me to adapt 0.05" pin spacing to 0.1" pin spacing, so I can just solder pins to the receiver and stick it into my arduino? This was the best I could come up with for now:
2014_03_09_00_24_41.jpg

2014_03_09_00_25_07.jpg



So, after getting it all wired up I downloaded your code and uploaded it to the board. First I got a bunch of gibberish, and then noticed your instruction to change the baud rate to 115200. With that done, I finally got some readable output. But, regardless of stick position/button state, the serial monitor spat out the same thing: "Joy = -132 Speed = 95". The receiver light would blink in response to button presses but no change would be evident in the output window, and the joystick seemed to do nothing at all. After several hours of reading on the arduino forums (some threads dating back to 2008, lol) it turns out that the Nyko uses a different initialization routine than other wireless nunchucks.

About halfway down the page here, it turns out that the Nyko needs some delay in the initialization/handshake in order to work properly. I believe that lines 80-100 of your WiiChuck.h library are responsible for this process, and I tried to edit them accordingly. Changing line 94 from a 1ms delay to a 30ms delay, and adding a 30ms delay immediately after line 99 seem to have partially corrected the problem.

It takes up to 5 resets to get a solid connection, but once it is going my serial monitor outputs values that change with respect to stick position, and the button inputs work as well. At with the nunchuck controls at neutral, I get "Joy = -2 Speed = 95" and the joystick output varies from -131 to 123 depending on position. "Speed" however does not change.


Does anyone know why my sync up is so flaky? Also, I assume that "Speed" should change based on "Joy". What am I missing here? And, again, does anyone have a more elegant way to do the wiring now that I have chopped the connector off my receiver?
 
kkEdlund said:
torqueboards said:
Kkedlund - I might be able to get one working tomorrow. Will try and post what I find.

That would be awesome. I'd like to know how it works for you.

EDIT: I have the throttle map working how I want it to, and I now have use of my brakes. The throttle hold feature is still not working right, but that shouldn't be too bad. The main problem I'm having is the nunchuck will only sync when I upload the sketch. If I power up the Arduino normally (from USB or my ESC BEC) it returns illegal values (Joy = -130. Minimum is -129) and keeps looping through Setup(). I guess I didn't fix the WiiChuck library as well as I thought.

Will post if I have any progress. If any programming gurus want to look over the code I'm dealing with, let me know and I'll post it
 
benj said:
Hi there. I've done a fair bit of Arduino coding before and happy to take a look. I don't have the wii hardware, but I might be able to figure it out anyway. PM me or upload it somewhere if you want :)

The libraries Voodoo was using were way too confusing for me to follow, and I couldn't get the controller to sync up with any degree of reliability. I am decent at coding in MatLab, and apparently the syntax is quite similar to C. I worked through several examples on the Arduino page in the last few days, got a different and better organized library, and wrote my own sketch to control the ESC using Arduino's built-in servo library. If you want to look over the code and give me some feedback, I'd be very appreciative. It works in theory (the serial.print values are what I expect them to be) and the board powers up and runs fine on my bench, but I don't know how it will handle in a real application. Its snowing here, so I can't test it. :roll:
 
kkEdlund- I hooked my receiver up and everything. The serial returned Joy=-132 and Speed=95 when the nunchuck is off but as soon as I turn the nunchuck on, the values switch to Joy=123 and Speed=95. The buttons are unresponsive- have you bumped into this problem with your Nyko?
 
I have been trying this with the Logic3 wireless nunchuck and I have had no end of problems with the signal dropping out, erroneous values, etc. I also had a lot of trouble with syncing to the ESC.

I have ordered the Quanum 2.4GHz transmitter and will probably be using that until the wii nunchuck is bulletproof.

One thought I had was to put a Wixel wireless module (http://www.pololu.com/product/1336) into the nunchuck casing and use a sprung trigger connected to a potentiometer a bit like this:

aAMCGWI.png


This would probably turn into a project in itself so I think I will just get the mechanics of my board working first!
 
I just managed to interface my Nyko Kama with the arduino but have the same restart problem as kkEdlund now. The library voodoo provided did not work for me so after much searching I found another one that's pretty concisely written. I learned a lot about I2C from reading online and tried many methods of troubleshooting including pull-up resistors and such but the I believe I had the same issue as kkEdlund. I played around with adding a delay after the handshake or initialization code and everything has been working great (analog, x, z, accel all returning accurate values). It's about 4 AM here but after I get some sleep I will write a program to take the nunchuck inputs and output that to the ESC and investigate why the serial works so flawlessly during a fresh install of the program but not after it has been restarted.

Regards.
 
So after some more experimentation, I fixed the problem with restarting. The code now consistently works after a fresh restart (Re-plug in USB). My theory is that when the nunchuck disconnects, the receiver loses its initialized state and outputs bullshit to serial. Luckily, the BS sensor values are quite over 255 (max nunchuck analog stick value) or below 0 (for me, accelerometers are like in the -2000's). I wrote a short conditional that takes into account of this and reinitializes the nunchuck and now everything is fine.

A new problem: In the case of a lost connection, the receiver continues to send the last known working values for about 8 seconds before it starts sending values that it usually does in the disconnected state. This makes it hard to program a failsafe... What do you guys think? What kind of failsafe are you guys using in the case of a dead battery or lost signal?
 
lpbug said:
So after some more experimentation, I fixed the problem with restarting. The code now consistently works after a fresh restart (Re-plug in USB). My theory is that when the nunchuck disconnects, the receiver loses its initialized state and outputs bullshit to serial. Luckily, the BS sensor values are quite over 255 (max nunchuck analog stick value) or below 0 (for me, accelerometers are like in the -2000's). I wrote a short conditional that takes into account of this and reinitializes the nunchuck and now everything is fine.

A new problem: In the case of a lost connection, the receiver continues to send the last known working values for about 8 seconds before it starts sending values that it usually does in the disconnected state. This makes it hard to program a failsafe... What do you guys think? What kind of failsafe are you guys using in the case of a dead battery or lost signal?

Once I got a real library to works with, my initialization problems went away.

I got this working late Tuesday, but I still have the same problem with implementing a failsafe. The bullshit values it spits out after dropping the connection are within the usable range, so I have no idea how to have a check for this situation. However, I've never had a lost connection since I switched to the new library. I need to walk >75 feet away from the receiver to break the connection, other than that it has been rock solid.

LPBUG, when the connection drops are your bs values the same, or do they change? Mine are always constant once connection is lost (by removing the transmitter battery, lol), and are NEVER the same as it the neutral throttle position. I think I will try implementing a counter that increments if ALL 7 values (c, z, x accel, y accel, z accel, x stick, y stick) are the same as in the previous iteration of the main loop, and a conditional that calls void setup() if the counter value exceeds 50. My sketch updates throttle values 10-100 times per second depending on stick position (quick implementation of a throttle curve, lol), so that would be at most 5 seconds until a restart. It seems unlikely that all the accel and stick values would be identical for any period of time during normal operation, but if you set the control down it would reset like mad. I haven't looked at the accelerometer outputs, but I think that is the key to determining if a connection is established.

Also, I have <2" of wire between the receiver and the arduino, and it seems that pull-up resistors are unnecessary in my situation. Sorry I didn't see your posts earlier; was there anything else you are struggling with other than the failsafe? I have the initialization pretty much figured out, and put a 4 mile test ride on my board today with no problems. So the system seems to be pretty solid overall, just needs some refinements for ease of use. Once I have everything down to a plug-and-play setup, I will start a new thread with download links if anyone is interested.
 
For the record, hooking up one of these receivers with reverse polarity is an exceedingly bad idea. They do not seem to have any protection for that eventuality, and I think mine is dead.

I suppose I will be walking to class tomorrow...
 
Aw that's a bummer. When the connection drops (I also tested this by pulling out battery), the values stay constant at whichever value it was at before the battery was pulled. I suppose you can implement a 5 second check on whether accelerometer values are changing but that's still slow in my opinion. Right now with the conventional RC car controllers we're using, power off to the Tx automatically means that throttle becomes neutral which is as safe as it gets. Simple, but usually simple is reliable. And as of now, I don't see a reliable solution for implementing a failsafe on the board.

Another point though- even though the serial takes around 8 seconds to stop sending constant values and send BS values that are out of the normal parameter, the receiver starts blinking as soon as signal is lost. I wonder if there is a way to detect when signal is lost more quickly.

If this turns into a bigger project than it currently is, then I would probably end up making my own controller. Transceiver chipsets aren't all that expensive and I can recycle the trigger potentiometer in the GT2B and 3D print the casing.
 
I'm going to try working on the wii nunchuk as well shortly. I just ordered some stuff for testing and arduino nano to see if I can get it working w/ that as well as the wii adapters.

I was thinking of just going custom as well. I know the Xbee modules we might be able to use. Once we get a working prototype that's 3d printed. I can make a mold and we can have it injection molded. I'm going to work on making an injection molded wheel adapter soon as well.
 
Back
Top