Tuesday, June 3, 2008

Hardware

The circuit is composed of several parts. It is not very complex, as you can see from the following block diagram:

The nunchuck communicates by I2C with the microcontroller, which in turn processes the data and sends 'mouse data' to the computer by USB. There is one detail, though. The nunchuck works at 3.3V, while USB gives 5V. Some have reported that the nunchuck works correctly when wired with 5V [1], and I've tried and it works, but it will probably reduce the nunchuck's lifespan. So I've inserted a voltage converter between the nunchuck and the microcontroller. The more elegant solution would be to use the microcontroller at 3.3V, but when I built the circuit I did not know if one could do the USB communication at that voltage. The PIC18F2550 datasheet discusses such possibilities around page 165.

The whole circuit is represented in the following schematic:

I will briefly describe each 'subsystem'.

  • Oscillator
    A 20MHz crystal is used as the oscillator, just as in the PICDEM USB demo board. Since we will be using code written for that circuit, we should try to be as compatible as possible. Still, to use other crystal you just need to set the appropriate configuration bits.

  • ICSPIn-Circuit Serial Programming is a useful method to program the microcontroller. You just plug in the programmer in the ICSP header and you can reprogram the chip as needed. In my case I used a PicKit2, but any other programmer that supports ICSP should work. The 1KOhm resistor is used to provide insulation from the rest of the circuit, as recommended in the PicKit2 user guide. If you plan on programming your microcontroller by USB bootloading, the inclusion of a permanent ICSP header is unnecessary. You just have to program the chip using ICSP once (in another circuit, for example) and use the USB bootloader to reprogram it when needed.
  • USBThe USB connection is quite simple. The +5V power is drawn from the USB port. I happened to switch the USB + & - so it wasn't working, but if the computer doesn't communicate (start enumeration) with the microcontroller, they are probably switched or the microcontroller is not working well (bad programming?). If the computer tells you that the device failed enumeration, they are well connected but you probably have electromagnetic interference between the wires. When I first built the circuit on a breadboard that was fairly common, in a stripboard it still happened some times, mostly because I was using a small board inserted in a USB extension cord connected to my circuit with long wires. Ugh. It works properly with a savaged USB cord from a el-cheapo USB hub. I could also have cut the extension cord in half, but I'll delay that until the next project. The 220 nF capacitor is needed for the internal 3.3V regulator in the microcontroller, used to drive the USB transciever. The datasheet indicates that the regulator's power is not enough to drive anything other that the transciever and pullups, so we can't use it in the nunchuck. The datasheet advises 220 nF, but I think I am using a 100 nF capacitor and it works.
  • Bootloading Using the appropriate firmware found in the 'MCHPFSUSB v1.3 USB Framework' we can do USB bootloading. USB bootloading consists in programming the microcontroller directly by the USB connection. You still need to program the chip once with ICSP, to load the 'Host' program that is capable of rewriting the code of the 'main' program in the microcontroller. To use the utility provided in the USB Framework by microchip, you need two pushbuttons: on in the reset pin, and the other in the RB4 pin. They work as pullups that short to ground when the button is down. If you reset the microcontroller normally, the bootloaded jumps to the main program and it all works normally. If you reset the microcontroller while holding down the S2 pushbutton, the bootloader will not jump to the main program but will instead start the microcontroller in 'programming' mode, and you will be able to program it from the computer.
  • Voltage Regulator As said, the nunchuck's circuits are designed for 3.3V, but, for example, the LIS3L accelerometer datasheet specifies the absolute maximum voltage as 6V and it works with 5V power, but it shouldn't be very good for the nunchuck's health. So I used a 3.3V regulator between the nunchuck and the microcontroller. I wanted to use a 3.3V Regulator, but my local store did not have those, so I used and adjustable voltage regulator LM117 with the appropriate resistors.
  • Potentiometer We want to change the mouse speed as needed. The easiest method to do it (that I know of) would be to read an analog voltage between 5V-0V in the AN8 port, controlled by a potentiometer. That's it.
  • Connecting to the Nunchuck - Using the resulting 4 wires – 3.3V, Clock, Data, GND - we only need to connect them to the nunchuck. In most of the sites I've read in the internet people have cut the cord, except for this one that created some nice connectors. I started by connecting wires directly to the nunchuck port and it worked well. In the final circuit, due to space constraints, I decided to cut the cord, but any of the two methods work. For the connector 'pinout', check this site and if you cut the cord check this one.

Cost

Parts

Cost

PIC18F2550

8 €

USB Cable

~ 1 € (salvaged)

Voltage Regulator

1 €

2x Button

1 €

Resistances, Capacitors, Wires, Stripboard, Headers

~ 2 €

20MHz Cristal

1 €

Plastic Box

1.5 €

Nunchuck

20€

Potentiometer

1.5€



Total

37€


And that's it. Here is how my circuit looks like assembled in a stripboard:

  1. The microcontroller, with the ICSP header and buttons removed:

  2. The Voltage Regulator with connection to the nunchuck:


4 comments:

Fernando said...

Dear FSilva,

Would you be willing to setup a couple of theses Nunchunk mouses for us?

Thanks!

Fernndo

nico said...

Dear FSilva,

Great work, it seems to be achievable...

Can you give me the PIC program you use ? Or post it with tour article ?

Thanks in advance

Nicolas

核战幸存者 said...

All the pictures crashed? Please fix that

zedon said...

With 4 hid lights , your crops will never suffer from dehydration. This will keep your water spending down as well.