Wire.h and pin 13

I have an LED hooked up to pin 13 using the following code:

void setup() {
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
}

As soon as I include the Wire header file in this code, instead of getting a steady output from my LED I get an intermittent blinking.

I'm not able to use Wire and i2c with a HMC6352 compass (which I've successfully interfaced with a PIC previously) so I'm trying to troubleshoot... and I get all the way back to the beginning with this problem with the status LED.

Has anybody successfully used Wire yet? Did you have the same problem with pin 13?

I'm using a USB Arduino with version 6 of the software. Thanks for any help! - Charlie

A little more info...

If I add Serial.begin(9600) to my setup and then try to send Serial messages in my loop this doesn't work either with Wire.h included.

It basically seems like including Wire.h hangs everything

I'm running the Arduino software on OSX 10.8

this is a bump.

Is anybody able to even include Wire.h without it screwing up your sketch? If I don't hear otherwise, I guess I'll go ahead and post in the Bugs forum...

On windows, I can include Wire.h in my sketch without problems.

Platform: Board - Arduino NG. IDE - Arduino 0006 on Windows XP.
Circuit: Just the board, connected to PC via serial-over-USB. I'm looking at the built-in LED on pin 13. Ok, on re-reading your post, I've repeated all tests with an external LED and resistor on pin 13.

Steps to test:

Step 1. I start with your basic "switch LED on" sketch, but with the following line added.
#include <Wire.h>
It runs with a solid LED, no probs.

Step 2. I extend to also do some serial out and to blink the LED a bit. Again no problems.
Complete sketch:
#include <Wire.h>

void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
Serial.println("so far, so good...");
delay(2000);
}

More?
Sorry, I haven't used the wire library, nor do I have the means to test it more fully right now.

Someone else?

Thanks,

I'll try it under Windows shortly, see if I can get results there, and then post back to the board.

Thanks again. - Charlie

I just also tried to use the wire library in my sketch (running osX 10.4.8). I have the same problems. When I want to make an output via "Serial.println" nothing is printed into the serial monitor. It seems that Wire.h hangs everything.

here is my sketch:

#include <Wire.h>

void setup()
{
Serial.begin(9600); // start serial for output
}

void loop()
{
delay(100);
Serial.println("hi");
}

maybe there is a solution for mac users,

cheers,

fux

hello,

I read some more information about the i2c interface (pic: basics I2C-Interface >>> in german only) and you have to put a resistor (1,8 k ohm) to each wire. Otherwise the programm will hang up. now I tried to make two arduino boards "talk" to each other. It still didn't work, but if I find a solution I will post it.

cheers,

fux

Haven't tried with the resistors yet, but I did just try compiling running XP as a virtual machine in OS X using Parallels.

Same results as just using OS X. I knew it would be too easy if that just worked.

FYI, if you want to use arduino in XP via Parallels, you need to disable the OS X FTDIUSBSerialDriver.kext found in System/Library/Extensions. A slight pain (because you have to renable it to use arduino later in OS X), but nothing horrific.

I have linux installed via parallels too... maybe I'll try that as well. Although fux's recommendation about the resistors sounds like a more likely solution... but it's less geeky and fun... and it also sounds like neillzero got it to work in xp without having any resistors hooked up. hmmm.

I'd be surprised if the operating system mattered. In general, the same thing ends up on the Arduino board regardless of your OS. Though you never know.

Maybe there are other differences between the setups that have worked and the ones that don't? Esp. in terms of how things were wired and which programs were running on which boards?

Haven't tried with the resistors yet

You should! The I2C bus won't work without them. Dedicated I2C hardware might already have them built in, but the Arduino definitely doesn't.

When you connect the board with the two wires (analog 4+5) plus one resistor for each wire to the 5+ of your board, the problem you descriped should be fixed. on XP my sketch doesn't hang up even if I leave the resistors, but on a mac I just got it runnin with the resistors (that's really weird). I think that you have to do this just on your "master" board, so for example if you use a second board as a "slave" you just have to put the two wires in the analog inputs 4+5, but I am not really sure about that. the "master_writer" sketch now works on my board, but the "slave_receiver" sketch on a second board hangs up.

sketch:

// Wire Slave Receiver
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

#include <Wire.h>

void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.receive(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.receive(); // receive byte as an integer
Serial.println(x); // print the integer
}

it seems that the function "receiveEvent" causes problems, because when one byte is received it hangs up. has anyone an idea, what could be the problem?

thanks,

fux

I'm using Mac OSX and I don't get the problem when I include Wire.h.

I am using an Arduino Ministamp, though, so that might be a difference.

However, when I do a Wire.begin(), it hangs.

I have pull-ups on the I2c bus (I'm using 10K's...not sure what values to use with the Arduino Mini). It hangs with or without pullups.

Also, I started reading the wire code and am concerned that it may not work at all with the Mini. Has anyone got I2c working with the Mini?

I have a working LCD on my setup and use it for debug. I'm trying to set up to talk to the Sparkfun real time clock device on I2C.

#include <stdio.h>
#include <LiquidCrystal.h>
#include <Wire.h>

LiquidCrystal lcd = LiquidCrystal();

int loopct = 0;

// based on SFE Pic code, the RT device address is 0xD0
#define RT_DEVICE_ADDRESS  0xD0

void setup( void ) 
{
  lcd.init();
  digitalWrite(13, HIGH);  // turn on debug LED  

  lcd.printIn("Wire.begin()");
        
  Wire.begin(); 
  
  lcd.clear();

  lcd.printIn("Wire init done");
}

In the case above, I see "Wire.begin()" but it never displays "Wire init done". It seems to hang in the Wire.begin().

So, I started looking through the wire and twi code. One of the things that concerns me is this:

void twi_init(void)
{
  // initialize state
  twi_state = TWI_READY;

  #ifdef ATMEGA8
    // activate internal pull-ups for twi
    // as per note from atmega8 manual pg167
    sbi(PORTC, 4);
    sbi(PORTC, 5);
  #else
    // activate internal pull-ups for twi
    // as per note from atmega128 manual pg204
    sbi(PORTD, 0);
    sbi(PORTD, 1);
  #endif

From what I can tell the internal pull-ups on an Atmega168 (arduino mini) is on PORTB. Given the comments in the code re: Atmega128 and this is the default if it's not an Atmega8, it makes me think the Wire interface has not yet been ported to work with the Mini.

I've put a scope on the Arduino mini ADC4 and ADC5 ports and never see any activity with Wire.begin(). These ports are not pinned out - I soldered a small female breakaway header to the ADC4 and ADC5 ports so I could access them.

Again, I have doubts that the Wire library and the Mini are currently compatible. Has anyone else researched this or succeeded with the I2C/TWI interface on the Mini?

But back to the thread - I can include Wire.h without a hang, but it is potentially because I'm using a Mini and Wire is hosed for an Arduino Mini (I suspect.)

here is my setup how I connect two boards over the i2c interface. note that the resistors goes from the two wires to the 5+ pin of the board. in this setup the left board is the "master" and the right one the "slave".

I think that you have to do this just on your "master" board

It doesn't matter where exactly you place the resistors between +5V and the I2C "backbone", it just needs to be done somewhere on the I2C bus.

Is it just me, or can you use a analog input also as a output? i did'nt know that. Or did you make a error and used analog pins in stead of digital pins?

This is driving me insane.

I'm just using the led_blink example included in the arduino download. Including Wire.h screws it up.

I've got the 1.8 kOhm resistors in place and I have the appropriate wires + power going to my sensor. Using an older USB arduino, but it looks like that's what fux was using from his pics and he got it to work without having NG.

I've tried it in OS X and XP.

I'll try to clean up my breadboard and post a pic, but if anybody else has any ideas I'm all ears. Thanks for all the help so far! - Charlie

ok, here it is. Again, I'm using this setup with the led_blink example included with Arduino... the only addition to the sketch is including the Wire header file, which causes the sketch to hang. The resistors are a little hard to see... but they are both 1.8 kOhm and going to the power bus from the SDA and SCL pins.

crap. I've tried to get it to work on three different computers (one Dell, one g5, one Intel Macbook), no luck. They all hang when I include the wire.h header.

At this point I have to assume it's a problem with my arduino (which has been around the block). I've ordered another one and hopefully that will make everything magically work. I'll let everybody know.

Victory! The new Arduino arrived today and immediately I was able to include Wire.h without it hanging (using the above breadboarded circuit).

Hopefully getting it to actually communicate with my sensor won't be as hard as just including the library was.

BIGBADOTIS (or anyone else)

Have you had any luck with Arduino and the HMC6352? I'm working on them now and I was hoping you had some code examples to share.

I'm new to Arduino and I2C so anything will help.

Thanx