Go Down

Topic: Interfacing Arduino Due with ATtiny85 via I2C (Read 162 times) previous topic - next topic


For a Project I need to interface a Due with an ATtiny via I2C, and I just can't get it working at all.

As for the ATtiny Core, I'm using ATtinyCore
I2C on the ATtiny is realized by tinyWireS

Let's consider this simple example given by Ladvien

As for the Attiny as a Slave:
Code: [Select]
// the 7-bit address (remember to change this when adapting this example)
#define I2C_SLAVE_ADDRESS 0x4

// Get this from https://github.com/rambo/TinyWire
#include <TinyWireS.h>

// The default buffer size, Can't recall the scope of defines right now
#define TWI_RX_BUFFER_SIZE ( 16 )

//Character variable used to echo data back.
char chrSendData;

//Variables used in getting and parsing data.
char rxChrData; //Receives the data.
char rxString[12];  //Varbiable for holding one string of data.
int rxIndex = 0; //Used to index rxString.

//Integer for holding the pwm value received from master.
int pwmValA;

void requestEvent(){ 

//Handles receiving i2c data.
void receiveEvent(uint8_t howMany)
    if (TinyWireS.available()){ 
      if (howMany < 1)
      {   // Sanity-check
      if (howMany > TWI_RX_BUFFER_SIZE)
      {   // Also insane number

      if (!howMany)
      {   // This write was only to set the buffer for next read
      {   //Gets i2c data.
          rxChrData = TinyWireS.receive();
          //Places the characters in an array one at a time.
          rxString[rxIndex] = char(rxChrData);
          //Increment the data array.
          //If a stop character is read, parse the char array and convert it to a single integer. 
          if (rxChrData == ':'){
              //This is a low memory form of parsing the char array into an intger
              pwmValA = int(100*rxString[2]+10*rxString[3]+rxString[4]);
              //Prints the parsed value.
              //Writes the parsed value to pin 1 (PB1).
              analogWrite(1, pwmValA);
              //Resets the char array index.
              rxIndex = 0; 

void setup()
    pinMode(1, OUTPUT); // OC1A, also The only HW-PWM -pin supported by the tiny core analogWrite
    //Sets up the onReceive function (what we do if we get stuff).
    //Sets up the onRequest function (what we do if asked to send something).

void loop()
//Detects a stop sending command.

//Puts the data we got into a variable to send back for error checking.
chrSendData = char(rxChrData);


And for the Master:
Code: [Select]
#include <Wire.h>
#define I2C_SLAVE_ADDR  0x4
volatile boolean haveData = false;
volatile int value =0;

char chrPrint;
char charPrintOld;

char txChar;
int txIndex = 0;

void setup()
  Wire1.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output

void loop()

  byte lb;
  if (Wire1.available()) {
    //    hb=Wire1.read();
  char txStringChar[4] = {0, 5, 5, ':'};
  if (txIndex <= 4){
      txChar = char(txStringChar[txIndex]);
  else {
   txIndex = 0;
  Wire1.beginTransmission(4); // transmit to device #44 (0x2c)
                             // device address is specified in datasheet
  Wire1.write(txChar);         // sends value byte 
  Wire1.endTransmission();     // stop transmitting

As for further Information, the ATtiny utilizes a external 16Mhz crystal, the level shifting is done by two MOSFET's. I do know that the level does work, since I have absolutely no problems with an Arduino Uno Compatible C-Control board run with 3.3V logic.

The shematics can be found in an attachement, of course the tiny and the Due share the same ground and I already tried different pullup resistor values and Wire as well as Wire1 to no result so far. Since I'm relatively new to electronics I have no Idea how to progress from here, and my main point is trying to understand what is actually happening and if somebody can help me learn I'm eternally grateful.

Go Up