Go Down

Topic: Need to reset arduino for i2c to work (Read 678 times) previous topic - next topic

WizenedEE

Hi all,
I'm trying to get two arduinos to communicate over i2c. I have it basically working, with the two transferring data both ways. However, when I first plug in the pair, they will not communicate until I reset the slave.

I connected the 5V, Gnd, A4, and A5 pins of an uno r2 and an uno r1 together and attached a 9v battery to one of them (pictures attached). On the "master", I put this sketch
Code: [Select]
// master.ino
#include <Wire.h>

void setup() {
    pinMode(13, OUTPUT);
    Wire.begin();
    do {
        Wire.requestFrom(12, 1);
    } while (Wire.available() < 1);
}

void loop() {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
}

and on the slave I put this sketch
Code: [Select]
#include <Wire.h>

uint8_t var[1];
void i2cRequest() {
    Wire.write((uint8_t *)var, 1);
}

void setup() {
    Wire.begin(12);
    Wire.onRequest(i2cRequest);
}
void loop() {}


The light on the master does not flash until I reset the slave by hitting the reset button; then it flashes. Resetting the master does nothing.

It seems to me the most likely culprit is a bug in the Wire libraries, since this sketch and the wiring is extremely simple and I've duplicated the results on several different wiring setups and sketches. I'm using a freshly-downloaded 1.0.3 IDE

PaulS

Code: [Select]
    do {
        Wire.requestFrom(12, 1);
    } while (Wire.available() < 1);

Now! I want the data now, dammit!

You are not giving the device a chance to respond.

Nick Gammon

Code: [Select]
do {
        Wire.requestFrom(12, 1);
    } while (Wire.available() < 1);


I don't see the point of this. Wire.requestFrom blocks until it gets a response. You don't need to put it in a loop like that.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

WizenedEE


Code: [Select]
do {
        Wire.requestFrom(12, 1);
    } while (Wire.available() < 1);


I don't see the point of this. Wire.requestFrom blocks until it gets a response. You don't need to put it in a loop like that.


Except it fails and Wire.available() returns 0 after a timeout.

Nick Gammon

My point is you may well write this:

Code: [Select]

    while (Wire.requestFrom(12, 1) == 0)
     {}


Wire.requestFrom waits until it has a response (or a NAK) and the length of that response is returned from Wire.requestFrom. So, you may as well test that.

I can't reproduce your results.

Connecting two Unos, if I reset the Master, a moment later the LED flashes, indicating it read from the Slave.

If I reset the Slave, nothing happens (because the Master has already entered loop), unless I hold the Slave in reset, reset the Master, and then release the Slave from reset.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

cjdelphi

i'd save time and effort and use softserial why do so many go for i2c/spi over uart?

Nick Gammon

SoftSerial has quite a few limitations because it is bit banged. It's fine if you need to print something on an attached printer, while checking for RFID cards (as I do). But if you need to do a number of things at once, then its limitations become more apparent. They may or may not matter in a particular application.

Both SPI and I2C use the hardware features to output quite fast, and receive incoming data using interrupts (I know SoftSerial uses interrupts too, but once it starts a byte it has to sit in a tight loop).

Your comment about "softserial" ... "over uart" doesn't make much sense on its own, because SoftSerial doesn't use the UART.

With a larger chip like the Mega you could indeed use multiple hardware serial ports, if your application required it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

cjdelphi

I was referring to the kind of communication used, to me recv/send is via 2 wires, eg hardware support pin 0 & 1


Go Up