Showing posts with label electronics. Show all posts
Showing posts with label electronics. Show all posts

Connecting a Wii nunchuck to Arduino

May 6, 2013

After getting started with the Arduino Uno, in order to get 3-D accelerometer data from the Wii nunchuck and onto the Arduino, you will need the following:
  • a Wii nunchuck: there are some generic non-Nintendo brand nunchucks available for sale for around $6-7 (like this one) but the reviews on those seemed a bit mixed on their longevity. The official one by Nintendo goes for about $18 on Amazon currently, so I ended up buying a used Nintendo brand nunchuck off eBay to try to get the benefits of both.
  • Nunchucky breakout adapter ($3, adafruit) though there are other kinds of nunchuck adapters as well. This is so that you don't have to cut up into the wires of the nunchuck and can just have something to plug it into instead.
The Solarbotics Nunchucky breakout board adapter comes from adafruit in two pieces, like so:



That piece on the right, mine actually came with 6 pins instead of just 4, but you can just snap off 2 of them. GND = ground and 3.3V = power source, while Data = the data coming in off the nunchuck and CLK = a clock for the serial connection. The nunchuck has standard I2C interface, which allows you to read input from the accelerometer inside (x/y/z force), the two buttons, and the joystick (2 axis). Basically even though you hold it in your hand as one device, the nunchuck is actually all these different devices bundled together into one, and the serial connection allows you to read and distinguish the input from different parts of the nunchuck.

Anyway, the pins need to be soldered to the board so that it ends up looking like this:



My Hackbright instructor Christian brought his soldering iron to do this with some lead-free solder that I just bought for $5 at Radioshack. The soldered part lets you plug the 4 pins into the Arduino board with some stability and conductivity for the data to pass through, from A2 to A5 of the Analog pins:



The Wii nunchuck plugs in to the adapter at the bit with the gold bars. The nunchuck's plug comes with  bits on the sides that you can squeeze to release the locking mechanism for plugging/unplugging it:



VERY IMPORTANT NOTE: plug in the nunchuck with the Nintendo logo facing up! There's a plastic bit on the bottom. The adapter will fit in the nunchuck just fine upside down, but this won't match up with the Arduino code because the pins would be all backwards then. I lost a whole afternoon to this, don't repeat my mistake!

From here, you can get the nunchuck library written by Tod E. Kurt. Of the files in the Github repository, you really only need the ones in the WiichuckDemo folder. WiichuckDemo.ino is the file with the example code that you'd want to focus on, while nunchuck_funcs.h is the library of functions you can call from your .ino file to get data off the nunchuck, like whether a particular button has been pressed down.

When you run these files from the Arduino IDE with your nunchuck all plugged in to the Arduino and the Arduino plugged into your computer, this is what you should end up seeing:



Watching that data come in as you wave the nunchuck around is seriously magical! Tod E. Kurt has some extra info in these slides (PDF file) he used for a class (the relevant slides start about halfway through) but what's in this post should be enough to get you set up. The x, y, and z acceleration data should report values from 1-255 with these directions:



Even when you just leave the nunchuck sitting on the table, it will show some values because it's reporting acceleration, not absolute position in space, so even while in a still position, there will be the downward force of gravity acting on it.

For my project, I basically modified the example Wiichuck code so that it would collect data while you held down the big z-button and stop once you let it go. Pressing down the z-button prints out "start", then while you keep that button pressed down, it collects the xyz acceleration vectors, until you let it go, when it prints out "stop". I also added in a timer library to more reliably initiate collecting data every millisecond (Tod Kurt uses a counter variable in the Arduino's loop), though in practice this actually gives readings about every 100ms due to the nunchuck having a sampling rate of about 100Hz.

Getting started with Arduino

April 16, 2013

I got started on the hardware portion of my Hackbright final project (3-D gesture authentication using a Wii nunchuck and an Arduino board, aka motion-based passwords) today and want to log the tutorials that I found particularly helpful as a total electronics newbie.

One of the reasons I never really considered a career in software engineering was that I thought to be good at it, you had to be one of those people who had been building computers from spare parts in the garage since age 10. With the rise of web apps, this doesn't seem to be the case anymore, but I figured that while I was at Hackbright and had access to the excellent instructors here, I should push myself out of my comfort zone and learn something totally new. So, I come to this with some vague memories of circuits and electricity from physics classes in high school and college (I never took AP Physics and then I was a Biology major, so it was just the calculus-based Intro to Physic co-requisite course).

For the Arduino part, I decided on the $49.50 Budget Pack for Arduino (Arduino Uno R3) - Uno w/328 sold by Adafruit Industries so that I'd have a decent variety of extra parts (wires/LEDs etc.) to play around with a bit, particularly if I ended up wanting to try other projects down the line.


I have a Mac laptop that also has a VirtualBox to run Ubuntu (a particular "flavor" of the Linux operating system) using the Unity user interface, so I set both of them up to be able work on the Arduino following the steps in Lesson 0 on Learn Arduino.

The VirtualBox was a bit finicky about recognizing the plugged Arduino from the USB port, so I followed these steps to get that working (with a quick detour to this post in order to get the user group permissions bit working). And even then, I would sometimes get this "programmer is not responding" error. Following these recommendations to hit the reset button would sometimes work, but not always, so I mostly gave up on working on the Arduino from within the VirtualBox.

I then followed it through to Lesson 2 and got the LED working (if you want to modify the code in the blink tutorial, the reference for syntax you should use to program the Arduino sketches is here), but realized that I didn't actually understand how the breadboard worked.


For that, I found Adafruit's founder's site of Arduino tutorials and started over from there. The software setup was pretty much the same, but I did get an explanation for what the four black bumpy things that came in the same box as the board were: rubber bumper feet.

From this:

To this (back view):

Anyway, there was then a good explanation of the breadboard in Lesson 3 but I wanted to add a bit more explanation.


Basically, half-size breadboard that you get with the budget kit is composed of two sets of two different pieces: a "power rail" (the skinnier ones on the left and right sides that have the + and - signs and red/blue lines running vertically down) and a block of 30 sets of 5 row holes (not sure what the technical term for that is). The way these work is that the power rail, once connected, will provide power up and down all the rows, while the rows only have connectivity within their own numerical row. In the above photo, I outlined each different section in a bright blue box.

By convention, you connect the power (3.3V or 5V) in from the Arduino to the positive (red, +) side and then back out from a negative (blue, -) to the ground (GND) on the Arduino board. So, you could bring power in at the top left and out from the top right, or in from the bottom left and out from the top right, or from only in the middle, and you would still have access to that power all the way up and down the power rails.

The rows, on the other hand, are only connected to each horizontally. So 1A-1E are all connected, and if you connected 1A to power, something connected to 1C would be able to access that power as well. 2A-2E would be totally independent, as would all the rest of the rows, unless you deliberately connected something in row 1 to another row. These rows also don't go across the dip in the middle, so 1A-1E are separate from 1F-1J unless you connect them somewhere along the way.

Another thing to note while you're working your way through the LED tutorials is that when you have a directional element like the LED that has a positive end and a negative end (unlike resistors which don't have a particular direction), you connect it on the board positive-positive and negative-negative. So one configuration could be:
  1. A wire from the 3.3V on the Arduino to a hole in the + column.
  2. The positive end of the LED in another hole in that same + column.
  3. The negative end of the LED in a hole in a - negative column (doesn't really matter which, but the LED probably can't stretch across the whole width of this particular board...if you had additional wires or a resistor in here though, there's nothing stopping you from using the whole width if you wanted to).
  4. A wire from another hole in the - column to a GND on the Arduino.
One cool thing to do is supply the power pin 13 to hook it up to an LED on the breadboard while running the blink "sketch" (Arduino programs are called sketches, I guess), because then you'll get the LED on the breadboard to blink on and off as well and not just the tiny one on the Arduino board itself.

Ta da! I'll write a post as well about the exact steps to hook up the Arduino up to a Wii nunchuck because that caused quite a bit of brow furrowing for me today as well. It turned out to be not that complicated but I didn't find any super detailed descriptions or photos of what to do exactly, so I will put that up.

[edited 5/5/13] This post from the Make magazine blog is a new overview of the differences between the different microprocessors you can use for hardware projects: Arduino vs. Raspberry Pi vs. BeagleBone.