IC2 - Communication between two Teensys 4.1

JupiterMoll

Member
Hallo,

I'm triyng to send Data over IC2
from a Teensy 4.1 (Master) >>> to >>> Teensy 4.1 (Slave)

Both Teensys are grounded together and use SCL Pin : 19 and SDA Pin : 18.
The Wires are short.


For testing I' using this code examples from the Wire Library.

Master- Sender- Teensy 4.1

Code:
#include <Wire.h>

int led = 24;

void setup()
{
  pinMode(led, OUTPUT);
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop()
{
  digitalWrite(led, HIGH);    // briefly flash the LED
  Wire.beginTransmission(9);  // transmit to device #9
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();     // stop transmitting
  digitalWrite(led, LOW);

  x++;
  delay(500);
}

Slave - Reciver - Teensy 4.1

Code:
#include <Wire.h>

int led = LED_BUILTIN;
void receiveEvent(int howMany);
void setup()
{
  pinMode(led, OUTPUT);
  Wire.begin(9);                // join i2c bus with address #9
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  while(!Serial)
  {

  }
  Serial.println("RECIVER IC2");
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  digitalWrite(led, HIGH);       // briefly flash the LED
  while(Wire.available() > 1) {  // loop through all but the last
    char c = Wire.read();        // receive byte as a character
    Serial.print(c);             // print the character
  }
  int x = Wire.read();           // receive byte as an integer
  Serial.println(x);             // print the integer
  digitalWrite(led, LOW);
}

Sadly I got no output on the Reciver ?
Any ideas?

Thx.
 
You could try using the EasyTransferI2C library that is bundled with TeensyDuino: https://github.com/PaulStoffregen/Arduino-EasyTransfer. This is automatically installed so you don't have to download it. More info is available here: http://www.billporter.info/2011/05/30/easytransfer-arduino-library/.

But basically, you define a data structure for the data you want to transfer and the send it via I2C to another Teensy (doesn't have to be a T4) which has the receiver code on it to translate the data structure into the actual data.
 
Ok I testet this solution where Teensy 4 can act as slave .

https://github.com/Richard-Gemmell/teensy4_i2c.

Wire Slave Sender - Teensy 4.1

Code:
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Modified by Richard Gemmell Oct 2019

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// To use it, connect a master to the Teensy on pins 18 and 19.
//
// Consider using the I2CRegisterSlave class instead of Wire to
// create an I2C slave.

// Created 29 March 2006

// This example code is in the public domain.

#include <Arduino.h>
#include <i2c_driver.h>
#include <i2c_driver_wire.h>
//#include "EasyTransferI2C.h"

void requestEvent();

int led = 24;
struct TransmitData
{
  int32_t a;
  int32_t b;
  int32_t c;
  int32_t d;
  float e;
  float f;
  float g;
  float h;
};

TransmitData data;

void setup()
{
  pinMode(led, OUTPUT);
  Wire.begin(0x40);        // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
  Wire.setClock(1000000);
  data.a = 1;
  data.b = 2;
  data.c = 3;
  data.d = 4;
  data.e = 5.1;
  data.f = 6.1;
  data.g = 7.1;
  data.h = 8.1;

  Serial.begin(9600);
  while(!Serial)
  {

  }
  Serial.println("Hallo ");
  digitalWrite(led, HIGH); 


}

void loop()
{

   
  
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Serial.print("sending ("); 
  Serial.print(sizeof data);
  Serial.println(" bytes)");
  data.a = random(0);
  data.b = random(2);
  data.c = random(3);
  data.d = random(4);
  data.e = random(5.1);
  data.f = random(6.1);
  data.g = random(7.1);
  data.h = random(8.1);
  Wire.write((byte *)&data, sizeof data);
}


Wire Master Reader - Teensy 4.1

Code:
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Modified by Richard Gemmell Oct 2019

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device connected to pins 18 and 19.
//
// Consider using the I2CDevice class instead of Wire to read a sensor.

// Created 29 March 2006

// This example code is in the public domain.

#include <Arduino.h>
#include <i2c_driver.h>
#include <i2c_driver_wire.h>

int led = LED_BUILTIN;

struct TransmitData
{
  int32_t a;
  int32_t b;
  int32_t c;
  int32_t d;
  float e;
  float f;
  float g;
  float h;
};

TransmitData data;

void setup()
{
  pinMode(led, OUTPUT);
  Wire.begin();                         // join i2c bus

  Serial.begin(9600);                    // start serial for output
  while(!Serial)
  {

  }
}

void loop()
{
  Serial.print("requesting ("); 
  Serial.print(sizeof data); 
  Serial.print(" bytes)... ");
  if (Wire.requestFrom(0x40, sizeof data))
  {
    Wire.readBytes((byte*) &data, sizeof data);
    Serial.println("done");

    Serial.println(data.a);
    Serial.println(data.b);
    Serial.println(data.c);
    Serial.println(data.d);
    Serial.println(data.e);
    Serial.println(data.f);
    Serial.println(data.g);
    Serial.println(data.h);
  } 
  else {
    Serial.println("could not connect");
  }
 // delay(500);
}











[/CODE]
 
Back
Top