lucullusTheOnly / TinyWire

Composite Master and Slave I2C library for Atmels ATTiny microcontrollers
108 stars 26 forks source link

Two Attiny85s NACKing 5 out of 10 times after turning the power on #27

Open oomek opened 4 years ago

oomek commented 4 years ago

I've been trying to link 2 Attiny85s, but the connection is very unreliable. When I connect the power for both boards at the same time I always get NACK, when I connect Slave first, wait until it boots ( 5 seconds Digispark Attiny85 ) I get communication half the time. I have 2k pull up resistors.

Here is the sketch I'm using:

#include <Arduino.h>
#include "TinyWire.h"

#define MASTER 1

uint8_t array[32];
uint8_t address = 0x00;

void blink(int time, int n)
{
    for (int i = 0; i < n; i++)
    {
        digitalWrite(1, HIGH);
        delay(time / 2);
        digitalWrite(1, LOW);
        delay(time / 2);
    }
}

#if MASTER

void setup()
{
    pinMode(1, OUTPUT);
    blink(200, 3);
    TinyWire.begin();
    for ( int i = 0; i < 32; i++)
        array[i] = i;
}

void loop()
{
    TinyWire.beginTransmission(0x46);
    TinyWire.send(address);
    for ( int i = 0; i < sizeof(array); i++)
        TinyWire.send(array[i]);

    if ( TinyWire.endTransmission() != 0)
        blink (200, 5);
    else
        blink(1000, 1);
}

#else //SLAVE

void receiveEvent(int howMany)
{
    byte c = 0;
    while (TinyWire.available() > 0)
        c = TinyWire.read();
    blink(500, 1);
}

void setup()
{
    pinMode(1, OUTPUT);
    blink(200, 3);
    TinyWire.begin(0x46);
    TinyWire.onReceive(receiveEvent);
}

void loop()
{
    delay(1);
}

#endif
oomek commented 4 years ago

It seems like the slave is causing that issue when begin() is called. It pulls down SCL and SDA for a moment for some reason. image