Basic issue using Wire library with Arduino

Hello,

I’m new to the ATmega168, Arduino, and the Wire library. I’m trying to start as simply as I can, but I have to admit, I’m already having problems.

My environment is an Arduino NG rev. C (ATmega168) with the Arduino IDE v.0007 running on a Mac. (I’ve also run this on a PC.) I have not modified any Wire files provided in the v0007 IDE.

My goal is to use TWI/I2C to interface to a thermometer (DS1621).

I’m starting with a very basic sketch:

#include <Wire.h>

int ct = 0;

void setup() {
//Wire.begin(); // join i2c bus as Master
Serial.begin(9600);
Serial.println(“Init complete”);
}

void loop() {
Serial.print(ct);
Serial.println(" in loop()");
delay(1000);
ct++;
}

This sketch returns the following output:

Init complete
0 in loop()
1 in loop()
2 in loop()
3 in loop()
4 in loop()
5 in loop()

and so on.

If I uncomment the line that initializes Wire, I get no output. I never see “Init complete” nor any output from loop().

void setup() {
Wire.begin(); // join i2c bus as Master
Serial.begin(9600);
Serial.println(“Init complete”);
}

(No output from running this.)

Can anyone tell me what I am missing? I’ve run this with nothing attached to the Arduino board. I’ve run it with the entire DS1621 circuit attached to the board. It doesn’t change the behavior.

Given that www.arduino.cc links directly to the Wire library, I have a hard time believing that the library doesn’t work with a rev C board. But at this point, I can’t think of anything else to test.

Thanks for any suggestions any of you can give me!

– Dan

Just a follow-up....

I started altering Wire.cpp and twi.c to try to find where I was hanging in Wire.begin(). That forced several recompiles and when I backed out my changes, Wire.begin() no longer hung. I'm guessing the Wire.o and twi.o object files shipped in the 0007 distribution are not ok for an Arduino NG (ATmega168).

Unfortunately, my problems continue. I'm not reliably exiting Wire.endTransmission() because "twi_state" continues to equal "TWI_MTX", even many seconds after the start condition should have been sent.

More follow-up, in case it helps anyone. On a hunch, I added pull-up resistors to SDL and SDA pins (analog 4 & 5). Wire.endTransmission() stopped blocking immediately. I guess calling sbi(PORTD, 0) and sbi(PORTD, 1) in twi.c doesn't actually enable the internal pull-ups on an ATmega168. If I get the time, I'll see if I can find the docs on whatever internal pull-ups are available -- if any.

-- Dan

I found this link helpful: http://research.techkwondo.com/blog/julian/279 - you may need to make a change to the Wire library to get it to use the proper pins for the 168. I have gotten Wire to work fine with the DS1631, and am happy to give you that code - the major difference is the temperature resolution. I've also written a software I2C library that can use any pins you want - it's quite a bit smaller than Wire also.

D

Thanks for reminding me of the problem with pull-up - the code only worked with the ATmega8, not the ATmega168. It's been fixed for Arduino 0008.

Another thing you might want to watch out for is: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1175112203. I'll fix that for Arduino 0008 also.