I2C issues,

Jll6

New member
Hello, First post.. I'm having trouble getting 2 Teensy 4.1s to communicate with each other over I2C wire. I'm using example sketches master_writer and slave_reciever as is. The boards are wired directly together from pin 18-18 19-19, I've tried this with and without 4k7 pullups on the SCL and SDA lines with no difference. also with or without a common ground line between boards. I have monitored both lines with an oscilloscope all I see is every 500ms a single dip from 3.3v to zero which I am assuming is a start bit but then zero activity until the sketch transmits again, I've tried this swapping the boards from master to slave but with the same issue. I feel I am missing something very basic. Any suggestions?

thanks, john
 
Were you able to get this verified? I'm trying the same thing. I was able to get it to communicate with an arduino uno at one point, but I changed the code and have not been able to get back to that point. I'm trying two Teensy 4.1 boards as well wired as described above. Can someone help me figure out what I'm doing wrong? I threw some serial prints to make sure the code was being executed, but I keep getting "-1" on the receiving end.


Code:
Master
#include <Wire.h>
#include <WireIMXRT.h>
#include <WireKinetis.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  Serial.println("Begin Transmission");
  Wire.beginTransmission(9);

  Serial.println("Wire Write");
  Wire.write(0);
  
  Serial.println("End Transmission");
  Wire.endTransmission();
}


Slave
#include <Wire.h>
#include <WireIMXRT.h>
#include <WireKinetis.h>

int x = 0;

void setup() {

  Serial.begin(9600);
  Serial.println(x);
  delay(5000);
  Wire.begin(9);
  }

void loop() {

  x = Wire.read();
  Serial.println(x);
}
 
Last edited:
Sorry, maybe I am missing something, but with your example sketch:

Were you able to get this verified? I'm trying the same thing. I was able to get it to communicate with an arduino uno at one point, but I changed the code and have not been able to get back to that point. I'm trying two Teensy 4.1 boards as well wired as described above. Can someone help me figure out what I'm doing wrong? I threw some serial prints to make sure the code was being executed, but I keep getting "-1" on the receiving end.

Slave
Code:
#include <Wire.h>
#include <WireIMXRT.h>
#include <WireKinetis.h>

int x = 0;

void setup() {

  Serial.begin(9600);
  Serial.println(x);
  delay(5000);
  Wire.begin(9);
  }

void loop() {

  x = Wire.read();
  Serial.println(x);
}

Warning, I don't use Slave mode often... SO could be wrong, but I don't think this is the proper way for the slave to receive stuff...
That is I belive Wire.read();
will simply just read stuff out of a buffer.
Code:
	virtual int read(void) {
		if (rxBufferIndex >= rxBufferLength) return -1;
		return rxBuffer[rxBufferIndex++];
	}
Where I believe the data comes from the requestFrom()... code..

That is if you look at the Teensy Wire page: https://www.pjrc.com/teensy/td_libs_Wire.html

Instead I believe you need to setup handler functions using:
Wire.OnReceive(myReceiveHandlerFunction)
Wire.OnRequest(myRequestHandlerFunction)

Which the functions will be called when data is received or requested...

There are a couple of example sketches, that you might want to take a look at, including: Examples->Wire->slave_receiver
 
Sorry, maybe I am missing something, but with your example sketch:



Slave
Code:
#include <Wire.h>
#include <WireIMXRT.h>
#include <WireKinetis.h>

int x = 0;

void setup() {

  Serial.begin(9600);
  Serial.println(x);
  delay(5000);
  Wire.begin(9);
  }

void loop() {

  x = Wire.read();
  Serial.println(x);
}

Warning, I don't use Slave mode often... SO could be wrong, but I don't think this is the proper way for the slave to receive stuff...
That is I belive Wire.read();
will simply just read stuff out of a buffer.
Code:
	virtual int read(void) {
		if (rxBufferIndex >= rxBufferLength) return -1;
		return rxBuffer[rxBufferIndex++];
	}
Where I believe the data comes from the requestFrom()... code..

That is if you look at the Teensy Wire page: https://www.pjrc.com/teensy/td_libs_Wire.html

Instead I believe you need to setup handler functions using:
Wire.OnReceive(myReceiveHandlerFunction)
Wire.OnRequest(myRequestHandlerFunction)

Which the functions will be called when data is received or requested...

There are a couple of example sketches, that you might want to take a look at, including: Examples->Wire->slave_receiver

Thanks for the help. I'll give this a try.
 
May I humbly request I2C slave support for Teensy 3.2 in the next 1.57 beta? My desire is to add a 3.2 as a slave in a 5V environment.

Thanks in advance for any consideration.

Bob
 
May I humbly request I2C slave support for Teensy 3.2 in the next 1.57 beta? My desire is to add a 3.2 as a slave in a 5V environment.

Thanks in advance for any consideration.

Bob
I could be wrong, but I believe that support has been in there for several years now. For example when the AVR and T3.x code was split into separate files back in 2017, I believe it
was in there before then.

Edit: More info up on the page: https://www.pjrc.com/teensy/td_libs_Wire.html
However note the page has not been updated for a long time, so things marked TLC and T3.x only probably include now T4.x
 
These posts give me hope that Teensy 4.1 can transmit on SCl2 at IO-24, and SDA2 at IO-25.
However, the posts do not actually show a sketch/includes that work.
I have had success with Teensy 3.5 SCL2 at IO-3, and SDA2 at IO4, to write Hello World to an I2C 1602 LCD. This is my goal with T4.1.
I see several suggestions such as change all Wire to Wire2, use Richard Gemmell's Teensy4_i2c and other suggestions. None of the ones I've seen actually show a working example.

Then I came across https://forum.pjrc.com/threads/67091-Are-SCL2-SDA2-functions-of-the-T4-1-usable in Post#6.
Here Paul Stoffregen seems to eliminate the prospect of this ever succeeding.
"Sadly, there is no way to do that. NXP simply did not provide a signal path inside the chip for the Wire SDA & SCL signals to route to pins 24 & 25 where the Wire2 SDA2 & SCL2 signals are."

Senior+ members, is Teensy 4.1 transmission of I2C via SCL2/SDA2 possible? If so, I'll continue my attempts. Maybe someone could provide an example sketch and include files.
If it is not possible, I will stop my efforts. Also, if not possible, it would be appropriate if PJRC would publish a corrected Pinout card. The current pinouts list the SCL2 and SDA2 giving the false hope that they are usable.
Paul is my Micro Controller Hero. I hope to get this clarified.
Richard
 
This code (don't remember where I found it) will scan for I2C devices on Wire, Wire1 and Wire2 on a T4.1.

Code:
// i2c_scanner
// http://playground.arduino.cc/Main/I2cScanner
//
// 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 known.
// 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.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>

// BUGBUG::Should put this in WireIMXRT.h
#if defined(__IMXRT1062__)
#define WIRE_IMPLEMENT_WIRE
#define WIRE_IMPLEMENT_WIRE1
#define WIRE_IMPLEMENT_WIRE2
#endif
typedef struct {
  TwoWire *_wire;
  const char * wire_name;
  IMXRT_LPI2C_t * const port;
} wire_list_t;

wire_list_t wireList[] = {
  {&Wire, "Wire", &IMXRT_LPI2C1}
#if defined(WIRE_IMPLEMENT_WIRE1)
  , {&Wire1, "Wire1", &IMXRT_LPI2C3}
#endif  
#if defined(WIRE_IMPLEMENT_WIRE2)
  , {&Wire2, "Wire2", &IMXRT_LPI2C4}
#endif
#if defined(WIRE_IMPLEMENT_WIRE3)
  , {&Wire3, "Wire3"}
#endif
};
const uint8_t wirelist_count = sizeof(wireList) / sizeof(wireList[0]);


void setup(void)
{
  for (uint8_t wirelist_index = 0; wirelist_index < wirelist_count; wirelist_index++) {
    wireList[wirelist_index]._wire->begin();
  }
  Serial.begin(9600);
  while (!Serial);
  Serial.println(F("I2C Scanner\n"));
}


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

  for (uint8_t wirelist_index = 0; wirelist_index < wirelist_count; wirelist_index++) {
    IMXRT_LPI2C_t * const port = wireList[wirelist_index].port;
    Serial.print(F("Scanning("));
    Serial.print(wireList[wirelist_index].wire_name);
    Serial.println(F(")..."));
    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.
      wireList[wirelist_index]._wire->beginTransmission(address);
      error = wireList[wirelist_index]._wire->endTransmission();

      if (error == 0) {
        Serial.print(F("Device found at address 0x"));
        if (address < 16) {
          Serial.print("0");
        }
        Serial.print(address, HEX);
        Serial.print("  (");
        printKnownChips(address);
        Serial.println(")");

        nDevices++;
      } else if (error == 4) {
        Serial.print(F("Unknown error at address 0x"));
        if (address < 16) {
          Serial.print("0");
        }
        Serial.print(address, HEX);
        Serial.printf("MCR:%x MSR:%x, MIER:%x MDER:%x MCFGR0:%x MDMR:%x MCCR0:%x\n",
          port->MCR, port->MSR, port->MIER, port->MDER, port->MCFGR0, port->MDMR, port->MCCR0);
      }
    }
    if (nDevices == 0) {
      Serial.println(F("No I2C devices found\n"));
    } else {
      Serial.println(F("done\n"));
    }
  }
  delay(10000);           // wait 10 seconds for next scan
}


void printKnownChips(byte address)
{
  // Is this list missing part numbers for chips you use?
  // Please suggest additions here:
  // https://github.com/PaulStoffregen/Wire/issues/new
  switch (address) {
    case 0x00: Serial.print(F("AS3935")); break;
    case 0x01: Serial.print(F("AS3935")); break;
    case 0x02: Serial.print(F("AS3935")); break;
    case 0x03: Serial.print(F("AS3935")); break;
    case 0x0A: Serial.print(F("SGTL5000")); break; // MCLK required
    case 0x0B: Serial.print(F("SMBusBattery?")); break;
    case 0x0C: Serial.print(F("AK8963")); break;
    case 0x10: Serial.print(F("CS4272")); break;
    case 0x11: Serial.print(F("Si4713")); break;
    case 0x13: Serial.print(F("VCNL4000,AK4558")); break;
    case 0x18: Serial.print(F("LIS331DLH")); break;
    case 0x19: Serial.print(F("LSM303,LIS331DLH")); break;
    case 0x1A: Serial.print(F("WM8731")); break;
    case 0x1C: Serial.print(F("LIS3MDL")); break;
    case 0x1D: Serial.print(F("LSM303D,LSM9DS0,ADXL345,MMA7455L,LSM9DS1,LIS3DSH")); break;
    case 0x1E: Serial.print(F("LSM303D,HMC5883L,FXOS8700,LIS3DSH")); break;
    case 0x20: Serial.print(F("MCP23017,MCP23008,PCF8574,FXAS21002,SoilMoisture")); break;
    case 0x21: Serial.print(F("MCP23017,MCP23008,PCF8574")); break;
    case 0x22: Serial.print(F("MCP23017,MCP23008,PCF8574")); break;
    case 0x23: Serial.print(F("MCP23017,MCP23008,PCF8574")); break;
    case 0x24: Serial.print(F("MCP23017,MCP23008,PCF8574")); break;
    case 0x25: Serial.print(F("MCP23017,MCP23008,PCF8574")); break;
    case 0x26: Serial.print(F("MCP23017,MCP23008,PCF8574")); break;
    case 0x27: Serial.print(F("MCP23017,MCP23008,PCF8574,LCD16x2,DigoleDisplay")); break;
    case 0x28: Serial.print(F("BNO055,EM7180,CAP1188")); break;
    case 0x29: Serial.print(F("TSL2561,VL6180,TSL2561,TSL2591,BNO055,CAP1188")); break;
    case 0x2A: Serial.print(F("SGTL5000,CAP1188")); break;
    case 0x2B: Serial.print(F("CAP1188")); break;
    case 0x2C: Serial.print(F("MCP44XX ePot")); break;
    case 0x2D: Serial.print(F("MCP44XX ePot")); break;
    case 0x2E: Serial.print(F("MCP44XX ePot")); break;
    case 0x2F: Serial.print(F("MCP44XX ePot")); break;
    case 0x33: Serial.print(F("MAX11614,MAX11615")); break;
    case 0x34: Serial.print(F("MAX11612,MAX11613")); break;
    case 0x35: Serial.print(F("MAX11616,MAX11617")); break;
    case 0x38: Serial.print(F("RA8875,FT6206,MAX98390")); break;
    case 0x39: Serial.print(F("TSL2561, APDS9960")); break;
    case 0x3C: Serial.print(F("SSD1306,DigisparkOLED")); break;
    case 0x3D: Serial.print(F("SSD1306")); break;
    case 0x40: Serial.print(F("PCA9685,Si7021")); break;
    case 0x41: Serial.print(F("STMPE610,PCA9685")); break;
    case 0x42: Serial.print(F("PCA9685")); break;
    case 0x43: Serial.print(F("PCA9685")); break;
    case 0x44: Serial.print(F("PCA9685, SHT3X")); break;
    case 0x45: Serial.print(F("PCA9685, SHT3X")); break;
    case 0x46: Serial.print(F("PCA9685")); break;
    case 0x47: Serial.print(F("PCA9685")); break;
    case 0x48: Serial.print(F("ADS1115,PN532,TMP102,LM75,PCF8591")); break;
    case 0x49: Serial.print(F("ADS1115,TSL2561,PCF8591")); break;
    case 0x4A: Serial.print(F("ADS1115")); break;
    case 0x4B: Serial.print(F("ADS1115,TMP102,BNO080")); break;
    case 0x50: Serial.print(F("EEPROM,FRAM")); break;
    case 0x51: Serial.print(F("EEPROM")); break;
    case 0x52: Serial.print(F("Nunchuk,EEPROM")); break;
    case 0x53: Serial.print(F("ADXL345,EEPROM")); break;
    case 0x54: Serial.print(F("EEPROM")); break;
    case 0x55: Serial.print(F("EEPROM")); break;
    case 0x56: Serial.print(F("EEPROM")); break;
    case 0x57: Serial.print(F("EEPROM")); break;
    case 0x58: Serial.print(F("TPA2016,MAX21100")); break;
    case 0x5A: Serial.print(F("MPR121")); break;
    case 0x60: Serial.print(F("MPL3115,MCP4725,MCP4728,TEA5767,Si5351")); break;
    case 0x61: Serial.print(F("MCP4725,AtlasEzoDO")); break;
    case 0x62: Serial.print(F("LidarLite,MCP4725,AtlasEzoORP")); break;
    case 0x63: Serial.print(F("MCP4725,AtlasEzoPH")); break;
    case 0x64: Serial.print(F("AtlasEzoEC")); break;
    case 0x66: Serial.print(F("AtlasEzoRTD")); break;
    case 0x68: Serial.print(F("DS1307,DS3231,MPU6050,MPU9050,MPU9250,ITG3200,ITG3701,LSM9DS0,L3G4200D")); break;
    case 0x69: Serial.print(F("MPU6050,MPU9050,MPU9250,ITG3701,L3G4200D")); break;
    case 0x6A: Serial.print(F("LSM9DS1")); break;
    case 0x6B: Serial.print(F("LSM9DS0")); break;
    case 0x70: Serial.print(F("HT16K33")); break;
    case 0x71: Serial.print(F("SFE7SEG,HT16K33")); break;
    case 0x72: Serial.print(F("HT16K33")); break;
    case 0x73: Serial.print(F("HT16K33")); break;
    case 0x76: Serial.print(F("MS5607,MS5611,MS5637,BMP280")); break;
    case 0x77: Serial.print(F("BMP085,BMA180,BMP280,MS5611")); break;
    case 0x7C: Serial.print(F("FRAM_ID")); break;
    default: Serial.print(F("unknown chip"));
  }
}

It finds a DS3231 and its onboard EEPROM on Wire2.
Code:
I2C Scanner

Scanning(Wire)...
No I2C devices found

Scanning(Wire1)...
No I2C devices found

Scanning(Wire2)...
Device found at address 0x57  (EEPROM)
Device found at address 0x68  (DS1307,DS3231,MPU6050,MPU9050,MPU9250,ITG3200,ITG3701,LSM9DS0,L3G4200D)
done
SCL and SDA on Wire2 have 4k7 pullups.

Pete
 
P.S. I also use the standard one-bus I2C scanner and defined Wire to be Wire2. That also finds the ds3231 and the EEPROM.

Pete
 
Thank you el_supremo. The i2c_scanner I had only scans for SCL/SDA not SCL/SDA2.

My test uses:
Teensy 4.1, SCL2 at I0-24, SDA2 at IO-25.
No external pullup resistors. Also works with 2.2k pullups to 3.3V on the T4.1 SCL/SDA2. Perhaps because my wires are only 150mm long. I'll use pullups.
I2C Module with Supply Voltage :2.5-6V
Results - Works:
"Scanning(Wire2)...
Device found at address 0x27 (MCP23017,MCP23008,PCF8574,LCD16x2,DigoleDisplay)
done"

After reading https://forum.pjrc.com/threads/67091...he-T4-1-usable in Post#6. I was about to give up.

You have given me hope of succeeding in my goal of displaying "Hello World" using:
- Teensy 4.1 SCL/SDA2
- I2C Module from http://www.hiletgo.com/ProductDetail/1949416.html also available from https://www.amazon.com/dp/B00VC2NEU8?psc=1&ref=ppx_yo2ov_dt_b_product_details
Note: The I2C Module Specifies Supply Voltage :2.5-6V
- LCD 3.3V http://www.hiletgo.com/ProductDetail/2169991.html also available from https://www.amazon.com/dp/B079M3GWFG?psc=1&ref=ppx_yo2ov_dt_b_product_details

Software:
Arduino 1.8.19
Teensyduino 1.56
Wire.h from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire
LiquidCrystal_I2C.h JohnRickman version. https://github.com/johnrickman/LiquidCrystal_I2C which I modified to use Wire2

This sketch works with Teensy 3.5 on SCL2 pin3, and SDA2 pin 4. Not with Teensy 4.1 on SCL2 pin 24 and SDA2 pin 25. Which is what I need.
It requires the #include <Wire.h> which is in the IDE's library folder.
And #include "LiquidCrystal_I2C.h" // save LiquidCrystal_I2C.h and .cpp in this sketches folder. It is not the default version. I modified LiquidCrystal_I2C.cpp to use Wire2.
Code of my modified .cpp is shown after the sketch.

Sketch HelloWorld_T35.ino:
Code:
/*
      HelloWorld_T35.ino
      7/2/22 Works.  Only on T3.5. Why not on T4.1?
      Displays "Hello World" on top line and "Hello There" on bottom line.
      Uses:
      Teensy 3.5. https://www.pjrc.com/store/teensy35.html
      I2C Interface Module, specifies Supply Voltage 2.5-6V. http://www.hiletgo.com/ProductDetail/1949416.html
      HD44780 1602 LCD Module,specifies 5V. http://www.hiletgo.com/ProductDetail/1915543.html
      Works on Teensy 3.5 SCL2 on  pin 3 and SDA2 on pin 4.

      Software:
      Arduino 1.8.19
      Teensyduino 1.56
      Wire.h from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire
      LiquidCrystal_I2C.h JohnRickman version.  https://github.com/johnrickman/LiquidCrystal_I2C

      Hardware:
      - Teensy 3.5 from https://www.pjrc.com/store/teensy35.html
        Also available from https://www.digikey.com/en/product-highlight/s/sparkfun/teensy-3-5-development-boards
      - I2C Module from http://www.hiletgo.com/ProductDetail/1949416.html    Note: Specifies Supply Voltage :2.5-6V
      
*/
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1

#include <Wire.h> // save Wire.h and .cpp in the IDE's library folder
#include "LiquidCrystal_I2C.h"  // save LiquidCrystal_I2C.h and .cpp in this sketches folder.

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars by 2 line display

void setup()
{
  Serial.begin(115200);     // Teensyduino IDE Serial Monitor
  Serial.println("Running"); // Indicate sketch is running on the serial monitor

  pinMode(LED_BUILTIN, OUTPUT);
  // blink LED three times to indicate sketch is running
  for (int x = 0; x < 3; x++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(300);
    digitalWrite(LED_BUILTIN, LOW);
    delay(300);
  }
  Wire2.begin(0x27); //Teensy 3.5 SCL2 on IO-3 and SDA2 on IO-4

  lcd.init();   // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("Hello World");
  lcd.setCursor(3, 1);
  lcd.print("  Hello There");
  delay(10000);
  digitalWrite(13, LOW);
}

void loop() { }

Liquid_Crystal_I2C.cpp
I modified 3 lines. Perhaps it works with T3.5 but not T4.1 because I did something wrong.
Near Line 9
//#define printIIC(args) Wire.write(args)
to #define printIIC(args) Wire2.write(args)

Near Line 257
//Wire.beginTransmission(_Addr);
to Wire2.beginTransmission(_Addr);

and Near Line 259
//Wire.endTransmission();
to Wire2.endTransmission();

LiquidCrystal_I2C.cpp
Code:
// Based on the work by DFRobot
// https://github.com/johnrickman/LiquidCrystal_I2C
#include "LiquidCrystal_I2C.h"
#include <inttypes.h>
#if defined(ARDUINO) && ARDUINO >= 100

#include "Arduino.h"

//#define printIIC(args)    Wire.write(args)
#define printIIC(args)  Wire2.write(args)

inline size_t LiquidCrystal_I2C::write(uint8_t value) {
    send(value, Rs);
    return 1;
}

#else
#include "WProgram.h"

//#define printIIC(args)    Wire.send(args)
#define printIIC(args)  Wire2.send(args)

inline void LiquidCrystal_I2C::write(uint8_t value) {
    send(value, Rs);
}

#endif
#include "Wire.h"



// When the display powers up, it is configured as follows:
//
// 1. Display clear
// 2. Function set: 
//    DL = 1; 8-bit interface data 
//    N = 0; 1-line display 
//    F = 0; 5x8 dot character font 
// 3. Display on/off control: 
//    D = 0; Display off 
//    C = 0; Cursor off 
//    B = 0; Blinking off 
// 4. Entry mode set: 
//    I/D = 1; Increment by 1
//    S = 0; No shift 
//
// Note, however, that resetting the Arduino doesn't reset the LCD, so we
// can't assume that its in that state when a sketch starts (and the
// LiquidCrystal constructor is called).

LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)
{
  _Addr = lcd_Addr;
  _cols = lcd_cols;
  _rows = lcd_rows;
  _backlightval = LCD_NOBACKLIGHT;
}

void LiquidCrystal_I2C::init(){
    init_priv();
}

void LiquidCrystal_I2C::init_priv()
{
    Wire.begin();
    
    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
    begin(_cols, _rows);  
}

void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
    if (lines > 1) {
        _displayfunction |= LCD_2LINE;
    }
    _numlines = lines;

    // for some 1 line displays you can select a 10 pixel high font
    if ((dotsize != 0) && (lines == 1)) {
        _displayfunction |= LCD_5x10DOTS;
    }

    // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
    // according to datasheet, we need at least 40ms after power rises above 2.7V
    // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
    delay(50); 
  
    // Now we pull both RS and R/W low to begin commands
    expanderWrite(_backlightval);    // reset expanderand turn backlight off (Bit 8 =1)
    delay(1000);

      //put the LCD into 4 bit mode
    // this is according to the hitachi HD44780 datasheet
    // figure 24, pg 46
    
      // we start in 8bit mode, try to set 4 bit mode
   write4bits(0x03 << 4);
   delayMicroseconds(4500); // wait min 4.1ms
   
   // second try
   write4bits(0x03 << 4);
   delayMicroseconds(4500); // wait min 4.1ms
   
   // third go!
   write4bits(0x03 << 4); 
   delayMicroseconds(150);
   
   // finally, set to 4-bit interface
   write4bits(0x02 << 4); 


    // set # lines, font size, etc.
    command(LCD_FUNCTIONSET | _displayfunction);  
    
    // turn the display on with no cursor or blinking default
    _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
    display();
    
    // clear it off
    clear();
    
    // Initialize to default text direction (for roman languages)
    _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
    
    // set the entry mode
    command(LCD_ENTRYMODESET | _displaymode);
    
    home();
  
}

/********** high level commands, for the user! */
void LiquidCrystal_I2C::clear(){
    command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
    delayMicroseconds(2000);  // this command takes a long time!
}

void LiquidCrystal_I2C::home(){
    command(LCD_RETURNHOME);  // set cursor position to zero
    delayMicroseconds(2000);  // this command takes a long time!
}

void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){
    int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
    if ( row > _numlines ) {
        row = _numlines-1;    // we count rows starting w/0
    }
    command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}

// Turn the display on/off (quickly)
void LiquidCrystal_I2C::noDisplay() {
    _displaycontrol &= ~LCD_DISPLAYON;
    command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal_I2C::display() {
    _displaycontrol |= LCD_DISPLAYON;
    command(LCD_DISPLAYCONTROL | _displaycontrol);
}

// Turns the underline cursor on/off
void LiquidCrystal_I2C::noCursor() {
    _displaycontrol &= ~LCD_CURSORON;
    command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal_I2C::cursor() {
    _displaycontrol |= LCD_CURSORON;
    command(LCD_DISPLAYCONTROL | _displaycontrol);
}

// Turn on and off the blinking cursor
void LiquidCrystal_I2C::noBlink() {
    _displaycontrol &= ~LCD_BLINKON;
    command(LCD_DISPLAYCONTROL | _displaycontrol);
}
void LiquidCrystal_I2C::blink() {
    _displaycontrol |= LCD_BLINKON;
    command(LCD_DISPLAYCONTROL | _displaycontrol);
}

// These commands scroll the display without changing the RAM
void LiquidCrystal_I2C::scrollDisplayLeft(void) {
    command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
void LiquidCrystal_I2C::scrollDisplayRight(void) {
    command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}

// This is for text that flows Left to Right
void LiquidCrystal_I2C::leftToRight(void) {
    _displaymode |= LCD_ENTRYLEFT;
    command(LCD_ENTRYMODESET | _displaymode);
}

// This is for text that flows Right to Left
void LiquidCrystal_I2C::rightToLeft(void) {
    _displaymode &= ~LCD_ENTRYLEFT;
    command(LCD_ENTRYMODESET | _displaymode);
}

// This will 'right justify' text from the cursor
void LiquidCrystal_I2C::autoscroll(void) {
    _displaymode |= LCD_ENTRYSHIFTINCREMENT;
    command(LCD_ENTRYMODESET | _displaymode);
}

// This will 'left justify' text from the cursor
void LiquidCrystal_I2C::noAutoscroll(void) {
    _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
    command(LCD_ENTRYMODESET | _displaymode);
}

// Allows us to fill the first 8 CGRAM locations
// with custom characters
void LiquidCrystal_I2C::createChar(uint8_t location, uint8_t charmap[]) {
    location &= 0x7; // we only have 8 locations 0-7
    command(LCD_SETCGRAMADDR | (location << 3));
    for (int i=0; i<8; i++) {
        write(charmap[i]);
    }
}

// Turn the (optional) backlight off/on
void LiquidCrystal_I2C::noBacklight(void) {
    _backlightval=LCD_NOBACKLIGHT;
    expanderWrite(0);
}

void LiquidCrystal_I2C::backlight(void) {
    _backlightval=LCD_BACKLIGHT;
    expanderWrite(0);
}



/*********** mid level commands, for sending data/cmds */

inline void LiquidCrystal_I2C::command(uint8_t value) {
    send(value, 0);
}


/************ low level data pushing commands **********/

// write either command or data
void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
    uint8_t highnib=value&0xf0;
    uint8_t lownib=(value<<4)&0xf0;
       write4bits((highnib)|mode);
    write4bits((lownib)|mode); 
}

void LiquidCrystal_I2C::write4bits(uint8_t value) {
    expanderWrite(value);
    pulseEnable(value);
}

void LiquidCrystal_I2C::expanderWrite(uint8_t _data){                                        
    //Wire.beginTransmission(_Addr);
    Wire2.beginTransmission(_Addr);
    printIIC((int)(_data) | _backlightval);
    //Wire.endTransmission();   
  Wire2.endTransmission();   

}

void LiquidCrystal_I2C::pulseEnable(uint8_t _data){
    expanderWrite(_data | En);    // En high
    delayMicroseconds(1);        // enable pulse must be >450ns
    
    expanderWrite(_data & ~En);    // En low
    delayMicroseconds(50);        // commands need > 37us to settle
} 


// Alias functions

void LiquidCrystal_I2C::cursor_on(){
    cursor();
}

void LiquidCrystal_I2C::cursor_off(){
    noCursor();
}

void LiquidCrystal_I2C::blink_on(){
    blink();
}

void LiquidCrystal_I2C::blink_off(){
    noBlink();
}

void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){
        createChar(char_num, rows);
}

void LiquidCrystal_I2C::setBacklight(uint8_t new_val){
    if(new_val){
        backlight();        // turn backlight on
    }else{
        noBacklight();        // turn backlight off
    }
}

void LiquidCrystal_I2C::printstr(const char c[]){
    //This function is not identical to the function used for "real" I2C displays
    //it's here so the user sketch doesn't have to be changed 
    print(c);
}


// unsupported API functions
void LiquidCrystal_I2C::off(){}
void LiquidCrystal_I2C::on(){}
void LiquidCrystal_I2C::setDelay (int cmdDelay,int charDelay) {}
uint8_t LiquidCrystal_I2C::status(){return 0;}
uint8_t LiquidCrystal_I2C::keypad (){return 0;}
uint8_t LiquidCrystal_I2C::init_bargraph(uint8_t graphtype){return 0;}
void LiquidCrystal_I2C::draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len,  uint8_t pixel_col_end){}
void LiquidCrystal_I2C::draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len,  uint8_t pixel_row_end){}
void LiquidCrystal_I2C::setContrast(uint8_t new_val){}

I have read several suggestions for using Teensy 4 and Teensy 4.1 with SCL/SDA2 to an I2C LCD. But they all seem to drift off topic.
I have not had success with any of the suggestions.They certainly have not provided evidence of a sketch that works and specific #include files.
"Try this. Try that. Try this other way..." I need to see a sketch that works. Not guesses at what might work.

el_supremo, or anyone else, can you help?
 
Last edited:
In init_priv you need to change Wire.begin() to Wire2.begin().
Also, remove Wire2.begin(0x27) from the setup function - it will be done by init_priv.

Pete
 
Back
Top