Pages: [1]   Go Down
Author Topic: Need to reset arduino for i2c to work  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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:
#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


* rover5pic-small.jpg (3677.18 KB, 3600x2700 - viewed 7 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://gammon.com.au/i2c
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My point is you may well write this:

Code:
    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.
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: