New I2C library for Teensy3

HI,

Im trysint to read from two SHT31 sensors, but keep getting errors using this library, can anyone please advise?

Code:
#include <i2c_t3.h>
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <SPI.h>
#include <SoftwareSerial.h>

  SoftwareSerial lcd = SoftwareSerial(0, 7);  //lcd tx pin = 7
//#include <i2c_t3.h>

Adafruit_SHT31 sht31a = Adafruit_SHT31();
Adafruit_SHT31 sht31b = Adafruit_SHT31();

void setup() {
  Serial.begin(9600);
 
Wire.begin(I2C_MASTER, 0x44, 33, 34);
Wire1.begin(I2C_MASTER, 0x45, 37, 38);
 
  SPI.begin(); // Init SPI bus
  while (!Serial)
    delay(10);     // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("SHT31 test");
  if (! sht31a.begin(0x44)) {    // first sensor with default address
    Serial.println("Couldn't find SHT31 #1");
    while (1) delay(1);
  }

  if (! sht31b.begin(0x45)) {    // second sensor with secondary address
    Serial.println("Couldn't find SHT31 #2");
    while (1) delay(1);
  }
  lcd.begin(9600); //initializes lcd
    lcd.write(0xFE); //screen size
  lcd.write(0xD1);
  lcd.write(16);
  lcd.write(2);
}


void loop() {
  float t = sht31a.readTemperature();
  float h = sht31a.readHumidity();
  float t2 = sht31b.readTemperature();
  float h2 = sht31b.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t);
  } else { 
    Serial.println("Failed to read temperature");
  }
  delay (100);
   if (! isnan(t2)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t2);
  } else { 
    Serial.println("Failed to read temperature");
  }
  
  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
 lcd.write(0xFE);
      lcd.write(0x58);
         lcd.print("Hum. % = "); lcd.println(h);
    
  } else { 
    Serial.println("Failed to read humidity");
  }
  delay (100);
    if (! isnan(h2)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h2);
 lcd.write(0xFE);
      lcd.write(0x58);
         lcd.print("Hum. % = "); lcd.println(h2);
    
  } else { 
    Serial.println("Failed to read humidity");
  Serial.println();
  delay(1000);
  }
}








In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/Wire.h:26:0,

from C:\Users\kedar\Dropbox\code\SHT31test_teensy\SHT31test_teensy.ino:16:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireKinetis.h:215:16: error: conflicting declaration 'TwoWire Wire'

extern TwoWire Wire;

^

In file included from C:\Users\kedar\Dropbox\code\SHT31test_teensy\SHT31test_teensy.ino:1:0:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\i2c_t3-master/i2c_t3.h:989:15: note: previous declaration as 'i2c_t3 Wire'

extern i2c_t3 Wire;

^

In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/Wire.h:26:0,

from C:\Users\kedar\Dropbox\code\SHT31test_teensy\SHT31test_teensy.ino:16:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireKinetis.h:218:16: error: conflicting declaration 'TwoWire Wire1'

extern TwoWire Wire1;

^

In file included from C:\Users\kedar\Dropbox\code\SHT31test_teensy\SHT31test_teensy.ino:1:0:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\i2c_t3-master/i2c_t3.h:991:19: note: previous declaration as 'i2c_t3 Wire1'

extern i2c_t3 Wire1;

^

In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/Wire.h:26:0,

from C:\Users\kedar\Dropbox\code\SHT31test_teensy\SHT31test_teensy.ino:16:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Wire/WireKinetis.h:221:16: error: conflicting declaration 'TwoWire Wire2'

extern TwoWire Wire2;

^

In file included from C:\Users\kedar\Dropbox\code\SHT31test_teensy\SHT31test_teensy.ino:1:0:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\i2c_t3-master/i2c_t3.h:994:19: note: previous declaration as 'i2c_t3 Wire2'

extern i2c_t3 Wire2;

^

Error compiling for board Teensy 3.5.
 
HI,

Im trysint to read from two SHT31 sensors, but keep getting errors using this library, can anyone please advise?

Code:
[COLOR=#ff0000]#include <i2c_t3.h>[/COLOR]
#include <Arduino.h>
[COLOR=#ff0000]#include <Wire.h>[/COLOR]
#include "Adafruit_SHT31.h"
#include <SPI.h>
#include <SoftwareSerial.h>

You cannot include both i2c_t3 and Wire libs in the same project because they define the same objects. If you want to use i2c_t3, you need to replace all
#include <Wire.h>
with
#include <i2c_t3.h>

including any sub-libraries that use it, for instance inside the Adafruit_SHT31.h file.
 
You cannot include both i2c_t3 and Wire libs in the same project because they define the same objects. If you want to use i2c_t3, you need to replace all
#include <Wire.h>
with
#include <i2c_t3.h>

including any sub-libraries that use it, for instance inside the Adafruit_SHT31.h file.



Tried this and now i get no errors but still no reading from the second sensor. Am i missing a pull up resistor on one of the lines?
or do i need to add the addresses 0x44 and 0x45 elsewhere?
 
@Kedar - you've come to a second thread asking questions that could have stayed on the other thread ...https://forum.pjrc.com/threads/54229-Teensy-3-5-using-2-SHT31-sensors-simultaneously

The above full code wasn't shown there … I just noted that before this post …

Also on that thread I pointed out the need for proper pullups … now asked here. - Also other points on your thread there not yet addressed …

Having multiple thread posts doesn't help
 
@Kedar - you've come to a second thread asking questions that could have stayed on the other thread ...https://forum.pjrc.com/threads/54229-Teensy-3-5-using-2-SHT31-sensors-simultaneously

The above full code wasn't shown there … I just noted that before this post …

Also on that thread I pointed out the need for proper pullups … now asked here. - Also other points on your thread there not yet addressed …

Having multiple thread posts doesn't help

@defragster, I found the link about the i2c_t3 library here and asked the question here, Havent been posting at random.
P.s. The whole code has been posted in that thread on the first comment i made, although i did not use # to quote the code but its all there, please check the beginning of the thread.
I appreaciate your help on this.
 
@defragster, I found the link about the i2c_t3 library here and asked the question here, Havent been posting at random.
P.s. The whole code has been posted in that thread on the first comment i made, although i did not use # to quote the code but its all there, please check the beginning of the thread.
I appreaciate your help on this.

re post #629 above … even the whole code in the 'other thread' first post does not show using two versions of i2c software as the post here does.

As nox771 pointed out using both isn't right … back on the other thread ...
 
re post #629 above … even the whole code in the 'other thread' first post does not show using two versions of i2c software as the post here does.

As nox771 pointed out using both isn't right … back on the other thread ...

Yes, I have changed that, still to no luck on the read out.
I dont know how the addresses of the two different SHT31 sensors can be set. Tried the corrects but no use.
Thanks
 
Yes, I have changed that, still to no luck on the read out.
I dont know how the addresses of the two different SHT31 sensors can be set. Tried the corrects but no use.
Thanks

If you have the Adafruit SHT31, the tutorial page describes the pinouts:

In particular, on the first SHT31, you do not connect the pin named 'ADDR' and it will have address 0x44. On the second SHT31, you connect the 'ADDR' pin to 3.3v, and it will have address 0x45. You connect the same wires to both SHT31's (SCL, SDA, ground, 3.3v). When you use the program, you have two variables, one that you use the begin method with 0x44, and the other you use the begin method with 0x45. As we have been trying to say, i2c is a shared bus system. You hook them up all together using the same cabling, and the address configuration will sort out which device is being used at a time.

There are times when you need to use multiple i2c buses. But in this case, start with the simple i2c bus of both SHT31's on the same bus.

If you don't have the Adafruit SHT31, you need to read the datasheet for the device that you bought to determine how to set the address.
 
@MichaelMeissner, Just saw that you can change the address by doing what you recommended. I will try it and update you guys.
Again Im sorry., Im new to I2C bus and this sensor in general, I appreciae your patience.

Thanks,
K
 
@MM it worked!!
Connecting the ADR pin to 3.3 Vin changed the address to 0x45. I defined this as per your specifications in the code. But this mean we can read only 2 sensors at a time?

Thanks a ton @MM, @ Defragster!!

Below is the code for you guys to see and the results:




/***************************************************
This is an example for the SHT31-D Humidity & Temp Sensor

Designed specifically to work with the SHT31-D sensor from Adafruit
----> https://www.adafruit.com/products/2857


Code:
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <SPI.h>
// #define lcd Serial2
#include <SoftwareSerial.h>

  SoftwareSerial lcd = SoftwareSerial(0, 7);  //lcd tx pin = 7
//#include <i2c_t3.h>

Adafruit_SHT31 sht31a = Adafruit_SHT31();
Adafruit_SHT31 sht31b = Adafruit_SHT31();

void setup() {
  Serial.begin(9600);
 Wire.begin();
Wire.setSDA(A15);// A15 SCL
Wire.setSCL(A14);//A14 SDA

 
  SPI.begin(); // Init SPI bus
  while (!Serial)
    delay(10);     // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("SHT31 test");
  if (! sht31a.begin(0x44)) {    // first sensor with default address
    Serial.println("Couldn't find SHT31 #1");
    while (1) delay(1);
  }

  if (! sht31b.begin(0x45)) {    // second sensor with secondary address
    Serial.println("Couldn't find SHT31 #2");
    while (1) delay(1);
  }
  lcd.begin(9600); //initializes lcd
    lcd.write(0xFE); //screen size
  lcd.write(0xD1);
  lcd.write(16);
  lcd.write(2);
}


void loop() {
  float t = sht31a.readTemperature();
  float h = sht31a.readHumidity();
  float t2 = sht31b.readTemperature();
  float h2 = sht31b.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t);
  } else { 
    Serial.println("Failed to read temperature");
  }
  delay (100);
   if (! isnan(t2)) {  // check if 'is not a number'
    Serial.print("Temp 2*C = "); Serial.println(t2);
  } else { 
    Serial.println("Failed to read temperature");
  }
  
  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
 lcd.write(0xFE);
      lcd.write(0x58);
         lcd.print("Hum. % = "); lcd.println(h);
    
  } else { 
    Serial.println("Failed to read humidity");
  }
  delay (100);
    if (! isnan(h2)) {  // check if 'is not a number'
    Serial.print("Hum.2 % = "); Serial.println(h2);
 lcd.write(0xFE);
      lcd.write(0x58);
         lcd.print("Hum.2 % = "); lcd.println(h2);
    
  } else { 
    Serial.println("Failed to read humidity");
  Serial.println();
  delay(1000);
  }
}
SHT31 test
Temp *C = 21.22
Temp 2*C = 21.90
Hum. % = 65.46
Hum.2 % = 37.07
Temp *C = 21.22
Temp 2*C = 21.89
Hum. % = 65.46
Hum.2 % = 37.70
Temp *C = 21.20
Temp 2*C = 21.90
Hum. % = 65.48
Hum.2 % = 37.75
Temp *C = 21.22
Temp 2*C = 21.90
Hum. % = 65.50
Hum.2 % = 37.47
Temp *C = 21.20
Temp 2*C = 21.90
Hum. % = 65.52
Hum.2 % = 37.06
 
@MichaelMeissner yes saw the multiplexer breakout board. I might try it. I also found out this sensor has a onboard heater function so as to prevent any condensation. This may come in handy to prevent any biases due to condensation and subsequent corrosion.

Thanks
 
Generic open library for I2C Multiplexer PCA9548

The Adafruit device can only have 2 addresses. Perhaps other devices can have more addresses, perhaps not.

There are ways to do deal with this (using different i2c buses, using i2c multiplexers, and switching the pins for the SCL/SDA). Each of these methods have their own features and problems.

One method is the i2c multiplexer, such as:

FYI we have written a generic PCA9548 (and same part from other vendors such as TI) I2C MUX library which assumes i2c_t3. It's being used in a commercial system deployed across the US so it is extensively tested. The repo includes several tests. We make a board with several possible temperature and humidity sensors on it, some examples are for those and others just test the MUX itself. It's a cool part but has some quirks such as being able to turn on all outputs at the same time if you so wish... IIRC we didn't base our library on others since the ones we looked at all had issues such as hard coded addresses. Ours supports a system with multiple MUXes each with multiple similar devices on each branch. We have tested it with 6-7 TMP275 temperature sensors on multiple branches. We used RJ12 connectors with 6C cable and a PCA9600 buffer between our main board with Teensy on it and the MUX board(s). The outputs of the MUX board are not buffered so they are limited to 8-10 feet of modular phone cable. We used modular phone cable (stranded wire) since it is good enough for this, it's cheap, and can't be mixed up with Ethernet connectors. We use a specific pinout to isolate lines as best we can so there is minimal SDA/SCL crosstalk.

Since I mentioned the pinout, here's what it is. This is the section of our controller board which runs to the outside world, such as the MUX board which has a matching buffer. When powering downstream devices the 5V jumper is obviously closed. The buffer is transparent to the library and I should probably post something about that to another thread.
I2C_net_PCA9600_SALT_2018-11-03 14-39-08.png
 
All - I uploaded a new release on GitHub:
https://github.com/nox771/i2c_t3

It applies some fixes and changes the license to MIT. I would edit the top-post accordingly, but for some reason the edit button has disappeared, and I can no longer modify it (anyone know what that is about?)
 
(anyone know what that is about?)

Change was made some months back to keep posts from being lost - edit period ends in 2 hours. Sr+'s can edit - I'll put the link and note in post one - if you want revise first post in some way - drop it below and it will get moved up or replaced if you leave a note.
 
Edit by proxy is a bit too cumbersome. I think at this point just edit the top post so it refers all future updates/downloads to GitHub (there is already a link at the top of first post, just emphasize that is the download location). All the description and API is still current, so that info is ok. Also remove the .zip attachment on first post as that one is an older release.
 
Hello,
in my project i want to use the sensors MPL3115A2,IIS2MDC and VL53L0X.According to the datasheet the sensor MPL3115A2 has a 4MHz maximum clock frequency in the i2c communication, the IIS2MDC 3.4MHz and the VL3L0X 400kHz.
I'm thinking of using the library i2c_t3 and two channels i2c,first channel 18-19(sda0-scl0) and second channel 56-57(sda3-scl3) with the following initializations:


Wire.begin(I2C_MASTER, ... , 400000); // 400kHz

Wire3.begin(I2C_MASTER, ... , 3000000); // 3MHz


On the first channel will be connected the sensor VL53L0X and in the second channel the sensors MPL3115A2 and IIS2MDC.It will work?
Or i could connect all sensors to a channel??
 
Hello,
in my project i want to use the sensors MPL3115A2,IIS2MDC and VL53L0X.According to the datasheet the sensor MPL3115A2 has a 4MHz maximum clock frequency in the i2c communication, the IIS2MDC 3.4MHz and the VL3L0X 400kHz.
I'm thinking of using the library i2c_t3 and two channels i2c,first channel 18-19(sda0-scl0) and second channel 56-57(sda3-scl3) with the following initializations:


Wire.begin(I2C_MASTER, ... , 400000); // 400kHz

Wire3.begin(I2C_MASTER, ... , 3000000); // 3MHz


On the first channel will be connected the sensor VL53L0X and in the second channel the sensors MPL3115A2 and IIS2MDC.It will work?
Or i could connect all sensors to a channel??

Please any help.
 
Different speeds means different I2C busses. Changing the I2C data rate "on the fly" might result in timing problems.
If I were you, and if it's really needed to go with such high speeds, I'd put the faster devices on the primary I2C bus with 3.4MHz and the slower on one of the secondary busses with 400kHz.

The rest is "just try it out" (prototyping is never a bad thing) and, especially for the high speed bus, check the SDA and SCL signals with a high BW (>=100MHz) oscilloscope which will allow you to optimize the values of the pull-up resistors (required) and the series resistors (sometimes needed, depends on your pcb layout and wiring inductance) to get clean signal ramps without ringing (the keyword is "impedance matching").

Although the IIS2MDC and the MPL3115A2 can do I2C in the MHz range, this is not mandatory, it's just a maximum. You should calculate the required bandwidth for your application (data volume x update rate) and check if they could not be operated at a lower I2C speed. Lower speed means always less problems. In case you find that everything together would fit into the 400kHz bandwidth, you might consider packing all on one single I2C bus.
 
That's exactly what I tried, but stupid me forgot to select the Teensy 3.1 board in the Arduino IDE. I usually work with Visual Micro for Arduino inside Visual Studio (where the spoofing worked anyway).

Note the additional typedef I use in the spoofing Wire.h
Code:
#ifndef _WIRE_SPOOF_I2C_T3_
#define _WIRE_SPOOF_I2C_T3_

#include <i2c_t3.h>

typedef i2c_t3 TwoWire;

#endif

This allows for code that handles I2C buses as objects (i.e. multi bus capable libs such as the ones I am currently developping).

I think this is a feasible pattern after all that works without touching any third party libs.

Thanks again,
Mark

Hi - old post I know - but I'm trying to do teh samer think but with teh DigoleSerialDisp libray displays.

Any idea of this method should work with teh digole library too?

Using the std wire lib - I had this line
Code:
DigoleSerialDisp mydisp1(&Wire, '\x27');
 
Hi - old post I know - but I'm trying to do teh samer think but with teh DigoleSerialDisp libray displays.
Any idea of this method should work with teh digole library too?
Using the std wire lib - I had this line
Code:
DigoleSerialDisp mydisp1(&Wire, '\x27');

This thread is still correct for problems involving this library.

I am unfamiliar with DigoleSerialDisp library. It may work by simply swapping the include line:
Code:
#include <i2c_t3.h>
instead of
Code:
#include <Wire.h>
But it must be done in all places in the code, including in the DigoleSerialDisp header file if it has such an include.

If it doesn't work you will need to supply more information and example code.
 
Back
Top