i2c, wire.h and digital pin problems

All,

I've got the Arduino working over i2c to the wii nunchuck and blinkm using wire.h as described by todbot.
http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
Analog pins 2,3,4,5

Separately, I have a 74HC595 working using the shiftout instructions.

5v, gnd, digital pins 8, 11, 12

I'd like to be able to send signals to the 595 based on values from the nunchuck. Problem is, when I plug them both in together and run a program that includes the wire.h library, I get garbage out the pins for the 595, and I don't get readings from the nunchuck. For this example, I'm not even connecting to the 595 pins from my program.

Any ideas why the wire.h or i2c code would be interfering with the analog pins?

The hardware I2C on the ATMEGA168 uses the same pins as Analog 4 & 5. So when I2C mode is enabled, these pins get converted to digital signals for the I2C connection and you can no longer use them for analog. Since this is how the ATMEGA168 is built, you can't use other pins for hardware I2C.

There were some people working on a software I2C that could use different pins. Search the forums for this.

The hardware I2C on the ATMEGA168 uses the same pins as Analog 4 & 5. So when I2C mode is enabled, these pins get converted to digital signals for the I2C connection and you can no longer use them for analog. Since this is how the ATMEGA168 is built, you can't use other pins for hardware I2C.

There were some people working on a software I2C that could use different pins. Search the forums for this.

Understood, but I don't think this is my problem; I'm using the standard i2c pins, analog 4 and 5; and somehow that's screwing up digital pins 8, 11, 12. Does "i2c mode" disable/change other pins?

Does "i2c mode" disable/change other pins?

No.

And I think the use of A2 & A3 to supply GND & +5v is a waste of two analog pins.

Did you read the comments section.

I was having trouble with todbot's example not properly doing pins 2&3 doing power and ground, but I found the missing parts in Laen's Freqin Wii, thanks for that.

// This sets up analog pins 2 and 3 to provide power and ground.
byte pwrpin = PC3;
byte gndpin = PC2;
DDRC |= _BV(pwrpin) | _BV(gndpin);
PORTC &=~ _BV(gndpin);
PORTC |= _BV(pwrpin);
delay(100); // wait for things to stabilize

I think I might end up getting the wiimote for interfacing with puredata or any OSC or midi app. But the wiichuck is great for standalone projects.

Left by C60 on March 5th, 2008

You might want to contact the author direct and explain the weirdness. The "hack" seems to be on a continuous journey of corrections so perhaps what your experiencing is something else he needs to be aware of.

There's no mention of pull-up resistors being used on A4 & A5 either, unless the Wii already has those, it's something else worth checking.

Posted this question on the todbot page.

I'm suspecting power/ground/pullup issues on the i2c (using analog 2+3 for ground does seem odd/lazy, you're right), so that's the route I'll check next. Actually I have a standard 2d accellerometer I'll probably use instead of the nunchuck for the project I'm working on a the moment, but I'd like to know if i2c and digital pins can play well with each other, for future projects.

Posted this question on the todbot page.

I'm suspecting power/ground/pullup issues on the i2c (using analog 2+3 for ground does seem odd/lazy, you're right), so that's the route I'll check next. Actually I have a standard 2d accellerometer I'll probably use instead of the nunchuck for the project I'm working on a the moment, but I'd like to know if i2c and digital pins can play well with each other, for future projects.

Yeah, i2c and digital pins play fine together "under normal circumstance". I've got 2 Lilypads connected over i2c, plus 2 serial devices, and a 595 controlling a bunch of LEDs as a visual feedback mechanism - and it works flawlessly.

Make sure you post a conclusion to assist others before they go pulling their Nunchucks apart.

Hi, I'm the creator of the "wiichuck adapter" and the "nunchuck_funcs.h" library referenced by the OP. Everything said above is correct as far as I know. I've used a '595 with a Wii Nuchuck with no problems. The "Wire" library and the underlying I2C stuff doesn't interfere with any digital or analog pins beyond analog pins 4 & 5.

justfred, the fact that the your '595 is "going crazy" makes me wonder if you've got some PWM going on. Are you doing 'analogWrite() anywhere? Alternatively, it could be that the pins going to the '595 aren't being configured as outputs. Also what do you mean by "For this example, I'm not even connecting to the 595 pins from my program." Do you mean you can't get data from the Nunchuck when it's the only thing connected to the Arduino? Have you tried this? Also, have you gotten the '595 to work with it being the only thing connected to the Arduino?

John_Ryan, I'm unclear what you mean by "The 'hack' seems to be on a continuous journey of corrections". The only change made to the wiichuck adapter hardware & software in the two months since my blog post went online was the addition of the function "nuchuck_init_with_power()" to enable analog pins 2 & 3 to be used as power & ground so one doesn't need a breadboard and wiring to play with a Nunchuck with an Arduino. This has been really popular with new Arduino users.

Oh and the practice of using I/O pins as a power source isn't as odd as one might think. In battery powered or other power-critical applications, you'll often seen sensors powered by I/O pins (particular in AVR-based designs, since AVRs can source 40mA per pin), so they can be switched on and off as the application deems it necessary.

Of course, in the wiichuck adapter case, it was done purely for convenience of the user. If it bugs you (i.e. you think it "lazy") you can simply plug the wiichuck adapter into a breadboard, wire it up, and power the Nunchuck off the normal +5V supply provided by Arduino.

John_Ryan, I'm unclear what you mean by "The 'hack' seems to be on a continuous journey of corrections". The only change made to the wiichuck adapter hardware & software in the two months since my blog post went online was the addition of the function "nuchuck_init_with_power()" to enable analog pins 2 & 3 to be used as power & ground so one doesn't need a breadboard and wiring to play with a Nunchuck with an Arduino. This has been really popular with new Arduino users.

Anything used outside or beyond it's intended or original application, is commonly referred to as a "hack". I skimmed through the comments section and stumbled across reference to an issue concerning A2&A3, that you addressed with the above modification.

Don't get me wrong, I think it's very clever - "continuous improvement" would probably have been a more appropriate phrase to use :wink:

Hi, I'm the creator of the "wiichuck adapter" and the "nunchuck_funcs.h" library referenced by the OP. Everything said above is correct as far as I know. I've used a '595 with a Wii Nuchuck with no problems. The "Wire" library and the underlying I2C stuff doesn't interfere with any digital or analog pins beyond analog pins 4 & 5.

justfred, the fact that the your '595 is "going crazy" makes me wonder if you've got some PWM going on. Are you doing 'analogWrite() anywhere? Alternatively, it could be that the pins going to the '595 aren't being configured as outputs. Also what do you mean by "For this example, I'm not even connecting to the 595 pins from my program." Do you mean you can't get data from the Nunchuck when it's the only thing connected to the Arduino? Have you tried this? Also, have you gotten the '595 to work with it being the only thing connected to the Arduino?

John_Ryan, I'm unclear what you mean by "The 'hack' seems to be on a continuous journey of corrections". The only change made to the wiichuck adapter hardware & software in the two months since my blog post went online was the addition of the function "nuchuck_init_with_power()" to enable analog pins 2 & 3 to be used as power & ground so one doesn't need a breadboard and wiring to play with a Nunchuck with an Arduino. This has been really popular with new Arduino users.

Thanks for getting back to me, todbot! Didn't mean to imply that your stuff was a hack (except in the complimentary sense)! Thought I'd read somewhere (Blinkm docs?) that using 2+3 was sub-optimal but easier.

I'm going to try to figure it out, but...
-Not using analogWrite anywhere.
-595 works great when I'm not loading nunchuck code.
-Only latchPin is configured as output to begin with; the others are set to output when data is sent out. I'm using code almost directly from the ShiftOut page.
-Now I'm trying backtracking and adding the 595 code to the Blinkmchuck code.-Nunchuck works great when using only your code. Except...interesting. When I comment out the Blinkm code, the nunchuck code doesn't work! This could very likely be the problem!

...
void setup()
{
...
    nunchuck_init(); // send the initilization handshake
    Serial.print("nunchuck ready\n");
...
}

Hmph. This is with the Blinkm unplugged, and no 595 code or plugs. I'll see what I can run down.

I've got the latest version of yr nunchuck_funcs code.

Well, heck, there it is.
nunchuck_init_with_power();
fixed it. I had the new lib but the old code.

Hehe, yeah most things I do with Arduino is pretty hacky. :slight_smile:

justfred replied to me separately that he figured out the problem by using "nunchuck_init_with_power()" instead of "nunchuck_init()". The former is used when you have have the nunchuck plugged directly into the Arduino (pins 2&3 supply power) while the latter is used when you have the nunchuck plugged into breadboard and powered from a proper supply. I really should've documented that better as it's confused a few people.

Hehe, yeah most things I do with Arduino is pretty hacky. :slight_smile:

Lol, nice one, glad it worked out.

Hehe, yeah most things I do with Arduino is pretty hacky. :slight_smile:

justfred replied to me separately that he figured out the problem by using "nunchuck_init_with_power()" instead of "nunchuck_init()". The former is used when you have have the nunchuck plugged directly into the Arduino (pins 2&3 supply power) while the latter is used when you have the nunchuck plugged into breadboard and powered from a proper supply. I really should've documented that better as it's confused a few people.

Thing is, with the blinkm in, it worked fine with just nunchuck_init() becase the blinkm was set to get power from those pins; but still caused some problem on the other side of the board.

Tonight I will have a nunchuck-driven piano!