How to use various IC2 busses? Teensy 4.1

Status
Not open for further replies.

laptophead

Well-known member
I am using an IMU and a Lidar on the same bus and they play nice for most of the time.
But sometimes the Lidar is showing zero for the readings.... The problem is so intermittent is hard to troubleshoot.

I could move one of the devices on another IC2 bus, lets see what happens.

How do I write that?

HTML:
#include <Wire.h>     // Arduino standard I2C/Two-Wire Library
//#define I2Cclock 400000
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

#define BNO055_SAMPLERATE_DELAY_MS (10)
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);

unsigned long previousMillis = 0;

#include <TFMPI2C.h>  // TFMini-Plus I2C Library v1.5.0
TFMPI2C tfmP;         // Create a TFMini-Plus I2C object 16 (0x10)

void setup()
{
    Serial.begin( 115200);   // Initialize terminal serial port
             // Initialize printf library.
    delay(20);

    Wire.begin();            // Initialize two-wire interface
    Wire.setClock( 400000);  // Set I2C bus speed to 'Fast' if
                               // your Arduino supports 400KHz.

    Serial.printf( "\r\nTFMPlus I2C Library 1.5.0\r\n");  // say 'hello'

    // Send some example commands to the TFMini-Plus
    // - - Perform a system reset - - - - - - - - - - -
    Serial.printf( "System reset: ");
    if( tfmP.sendCommand( SOFT_RESET, 0))
    {
        Serial.printf( "passed.\r\n");
    }
    else tfmP.printReply();
    // - - Display the firmware version - - - - - - - - -
    Serial.printf( "Firmware version: ");
    if( tfmP.sendCommand( GET_FIRMWARE_VERSION, 0))
    {
        Serial.printf( "%1u.", tfmP.version[ 0]); // print three single numbers
        Serial.printf( "%1u.", tfmP.version[ 1]); // each separated by a dot
        Serial.printf( "%1u\r\n", tfmP.version[ 2]);
    }
    else tfmP.printReply();
    // - - Set the data frame-rate to 250 - - - - - - - - -
    Serial.printf( "Data-Frame rate: ");
    if( tfmP.sendCommand( SET_FRAME_RATE, FRAME_100))
    {
        Serial.printf( "%2uHz.\r\n", FRAME_100);
    }
    else tfmP.printReply();


Serial.println("Orientation Sensor Test"); Serial.println("");

  /* Initialise the sensor */
  if(!bno.begin())
  {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
   
  delay(1000);

  /* Use external crystal for better accuracy */
  bno.setExtCrystalUse(true);
   
  /* Display some basic information on this sensor */
  //displaySensorDetails();
    delay(500);            // And wait for half a second.
}

// Initialize data variables
int16_t tfDist = 0;       // Distance to object in centimeters
int16_t tfFlux = 0;       // Signal strength or quality of return signal
int16_t tfTemp = 0;       // Internal temperature of Lidar sensor chip

// = = = = = = = = = =  MAIN LOOP  = = = = = = = = = =
void loop()
{

  
  if (millis() - previousMillis >= 10) {
    // save the last time you blinked the LED
    previousMillis = millis();
    
    tfmP.getData( tfDist, tfFlux, tfTemp); // Get a frame of data
    if( tfmP.status == TFMP_READY)         // If no error...
    {
        Serial.printf( "Dist:%04icm ", tfDist);   // display distance,
        //Serial.printf( "Flux:%05i ",   tfFlux);   // display signal strength/quality,
        //Serial.printf( "Temp:%2i%s",  tfTemp, "°C" );   // display temperature,
        Serial.printf( "\r\n");                   // end-of-line.
    }
    else tfmP.printFrame(); // Otherwise, display error and data frame


  sensors_event_t event;
  bno.getEvent(&event);
    Serial.print(F("Orientation: "));
  Serial.print((float)event.orientation.x);
  Serial.print(F(" "));
  Serial.print((float)event.orientation.y);
  Serial.print(F(" "));
  Serial.print((float)event.orientation.z);
  Serial.println(F(""));

  }
    
    

}
 
If for example you wish to use the Wire pins on pins 16 and 17, then you simply subsitute which object you are talking to.

That is instead of using the Wire object, you use the Wire1 object, Dito for pins 24, 25 you use the Wire2 object.

Note: a lot of libraries are hard coded to only use the Wire object, but some have been updated to allow different ones.

Example the Adafruit BNO055. You would change your constructor to something like: Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire1);
 
Status
Not open for further replies.
Back
Top