Forum Rule: Always post complete source code & details to reproduce any issue!
Page 26 of 26 FirstFirst ... 16 24 25 26
Results 626 to 640 of 640

Thread: New I2C library for Teensy3

  1. #626
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    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\SHT31 test_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\SHT31 test_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\SHT31 test_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\SHT31 test_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\SHT31 test_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\SHT31 test_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.

  2. #627
    Senior Member
    Join Date
    Mar 2013
    Location
    Austin TX
    Posts
    412
    Quote Originally Posted by Kedar View Post
    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>
    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.

  3. #628
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    Quote Originally Posted by nox771 View Post
    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?

  4. #629
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,971
    @Kedar - you've come to a second thread asking questions that could have stayed on the other thread ...https://forum.pjrc.com/threads/54229...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

  5. #630
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    Quote Originally Posted by defragster View Post
    @Kedar - you've come to a second thread asking questions that could have stayed on the other thread ...https://forum.pjrc.com/threads/54229...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.

  6. #631
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,971
    Quote Originally Posted by Kedar View Post
    @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 ...

  7. #632
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    Quote Originally Posted by defragster View Post
    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

  8. #633
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,797
    Quote Originally Posted by Kedar View Post
    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.

  9. #634
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    @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

  10. #635
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,971
    @MM put that in post #2 on the other thread - I just linked back to that again on the other thread …

  11. #636
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    @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

  12. #637
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,797
    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:

  13. #638
    Junior Member
    Join Date
    Nov 2018
    Posts
    17
    @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

  14. #639
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    2,766
    if your worried about corrosion you could always brush on a thin layer of die electric grease

  15. #640
    Senior Member
    Join Date
    Nov 2012
    Location
    Salt Lake City, UT, USA
    Posts
    252

    Generic open library for I2C Multiplexer PCA9548

    Quote Originally Posted by MichaelMeissner View Post
    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.
    Click image for larger version. 

Name:	I2C_net_PCA9600_SALT_2018-11-03 14-39-08.png 
Views:	8 
Size:	27.0 KB 
ID:	15079

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •