TinyCircuits Forum

General Category => General Discussion => Topic started by: tinybuilder on March 04, 2014, 12:22:09 AM

Title: Communicating over I2C between 2 TinyDuino Stacks
Post by: tinybuilder on March 04, 2014, 12:22:09 AM
I figured I'd start a thread to discuss Communicating over I2C between 3 TinyDuino Stacks. I don't suggest trying this until further discussion and input is given by other members of the community.

To start with, 3 TinyDuino stacks, each with a TinyDuino processor module and a TinyDuino proto3 shield (a prototype shield 2 should also work too.)

From what I understand, the stacks should be wired as shown in this picture (http://www.tech-builders.net/tinyduino/i2cTDs.png). For the power source, I choose VBATT, but VCC or VIN may be more appropriate. Anyone have pros/cons for using something other than VBATT? I choose 1.5K pull up resistors but from what I understand, up to 47K can be used.

The I2C signals are the SCL (I2C Clock) and SDA (I2C Data) connections.

The code shown below, is based on the Instructables article I2C between Arduino's (http://www.instructables.com/id/I2C-between-Arduinos/?ALLSTEPS)

Master Code (runs on 1 of the stacks)
Code: [Select]
#include <Wire.h>

#define LED_PIN 13
byte x = 0;

void setup()
{
  Wire.begin(); // Start I2C Bus as Master
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

}
void loop()
{
 
  Wire.beginTransmission(9); // transmit to device #9
  Wire.send(x);              // sends x
  Wire.endTransmission();    // stop transmitting
  x++;
  if (x > 5) x=0;
  delay(900);
}

Slave Code (runs on 2 of the stacks)
Code: [Select]
#include <Wire.h>

#define LED_PIN 13

int x;

void setup() {
  Wire.begin(9);                // Start I2C Bus as a Slave (Device Number 9)
  Wire.onReceive(receiveEvent); // register event
 
  pinMode(LED_PIN, OUTPUT);
 
  digitalWrite(LED_PIN, LOW);
 
  x = 0;
}

void loop() {
  //If value received is 0 blink LED 1
  if (x == 0) {
    digitalWrite(LED_1, HIGH);
    delay(100);
    digitalWrite(LED_1, LOW);
    delay(100);
  }
  //If value received is 1 blink LED 2
  if (x == 1) {
    digitalWrite(LED_1, HIGH);
    delay(300);
    digitalWrite(LED_1, LOW);
    delay(300);
  }
}

void receiveEvent(int howMany) {
  x = Wire.receive();    // receive byte as an integer
}