I2C Device using teensy as the driver

Status
Not open for further replies.

sting

Member
I am trying to build an I2C device using a teensy 3.1 as the slave device. As I am figuring this out, the master is just the I2C-tools on a linux machine.

I have a simple skeleton copied from the sample, so I can try to figure out how to create the I2C device. It looks like this:

Code:
#include <Wire.h>

#define  REG_MAP_SIZE      16
#define DEVICE_ADDRESS   0x29

byte registerMap[REG_MAP_SIZE];

void setup()
{
  Wire.begin(DEVICE_ADDRESS);                // join i2c bus with address #4
  Wire.onRequest(requestEvent); // register event
  Wire.onReceive(receiveEvent); // register event
  //Serial.begin(9600);           // start serial for output
  //while (!Serial){
  //}
  //Serial.println("Foo: ");
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void requestEvent(){
  Serial.println("Request: ");
  for (int c = 0; c < (REG_MAP_SIZE-1); c++) {
    Serial.print("C: ");Serial.println(c);
    registerMap[c] = (byte)c; //registerMapTemp[c];
  }
  Wire.send(registerMap, REG_MAP_SIZE); 
  Serial.println("Sent: ");
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // 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
}
I can detect the device at the correct address using
i2cdetect -y -r 2

when I attempt to dump the register with
i2xdump -y 2 0x29 I get all of the same values

0x00 lots of them. I think I should get the 16 byte array;

I know from the print statement that the teensy is sending them.

I should probably mention I have the same setup, that I used to be sure that I could talk to the device, as I used for an adafruit I2C amplifier.
Am I doing something wrong?
 
Last edited:
Five things to check about i2c on teensy:
  • I2C on Teensy typically needs pull-up resistors between A4/A5 and 3.3v power. Usually 4.7K ohm is the preferred resistor to use.
  • The Wire/t3_i2c libraries automatically shift the device address left one bit to add the read/write flag. If you are going from the datasheet, you might need to shift the value right one bit. Given the device address is 0x29, I imagine it has already shifted the address.
  • Teensy i2c is 3.3v. If your device is 5v only, you will need to get bi-directional level shifters for i2c. There are various shifters around. My most recent shifter is the Pololu 4 channel logic level shifter, which is also fast enough for neopixels (possibly with 10K pull-down resistors): https://www.pololu.com/product/2595
  • I2c is not that tolerant of long wires.
  • If you need to set various settings or use the alternate i2c pins/buses, you need the alternate teensy i2c library: https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3

When I put an i2c device on my teensy, I always use this scanner to make sure I have the device connected, that I picked up from the Arduino forums:

Code:
// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}
 
Status
Not open for further replies.
Back
Top