Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 37

Thread: Teensy 3.6 not detecting MCP9808 sensor

Hybrid View

  1. #1

    Teensy 3.6 not detecting MCP9808 sensor

    Hi here is the code being used for this. Everytime I try to run the code it confirms code is on teensy board but cannot find the sensor. I tried this same sensor on an arduino Uno using same code and it works instantly. The pinout is pins 18 and 19 on teensy for the SCL and SDA and then on the Uno the pins are A4 and A5. What gives?

    #include <Wire.h>
    #include "Adafruit_MCP9808.h"

    // Create the MCP9808 temperature sensor object
    Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

    void setup() {
    Serial.begin(9600);
    while (!Serial); //waits for serial terminal to be open, necessary in newer arduino boards.
    Serial.println("MCP9808 demo");

    // Make sure the sensor is found, you can also pass in a different i2c
    // address with tempsensor.begin(0x19) for example, also can be left in blank for default address use
    // Also there is a table with all addres possible for this sensor, you can connect multiple sensors
    // to the same i2c bus, just configure each sensor with a different address and define multiple objects for that
    // A2 A1 A0 address
    // 0 0 0 0x18 this is the default address
    // 0 0 1 0x19
    // 0 1 0 0x1A
    // 0 1 1 0x1B
    // 1 0 0 0x1C
    // 1 0 1 0x1D
    // 1 1 0 0x1E
    // 1 1 1 0x1F
    if (!tempsensor.begin(0x18)) {
    Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct.");
    while (1);
    }

    Serial.println("Found MCP9808!");

    tempsensor.setResolution(3); // sets the resolution mode of reading, the modes are defined in the table bellow:
    // Mode Resolution SampleTime
    // 0 0.5C 30 ms
    // 1 0.25C 65 ms
    // 2 0.125C 130 ms
    // 3 0.0625C 250 ms
    }

    void loop() {
    Serial.println("wake up MCP9808.... "); // wake up MCP9808 - power consumption ~200 mikro Ampere
    tempsensor.wake(); // wake up, ready to read!

    // Read and print out the temperature, also shows the resolution mode used for reading.
    Serial.print("Resolution in mode: ");
    Serial.println (tempsensor.getResolution());
    float c = tempsensor.readTempC();
    float f = tempsensor.readTempF();
    Serial.print("Temp: ");
    Serial.print(c, 4); Serial.print("*C\t and ");
    Serial.print(f, 4); Serial.println("*F.");

    delay(2000);
    Serial.println("Shutdown MCP9808.... ");
    tempsensor.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
    Serial.println("");
    delay(200);
    }

  2. #2
    Junior Member
    Join Date
    Aug 2020
    Location
    Montreal
    Posts
    16
    I think SCL is pin 19 and SDA is pin 18. Is that what you have?

  3. #3
    Quote Originally Posted by Gavin Bryars View Post
    I think SCL is pin 19 and SDA is pin 18. Is that what you have?
    yes that's what I have but it's not detecting the sensor

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,642
    Maybe show picture of your wiring... AI assume you have 3.3v running to the VDD pin and GND...

    Also if in something like breadboard, are the pins soldered to board... (Hate to ask, but more than once this has happened)...

    First thing I would try would be to run the Scanner example sketch of the Wire library to make sure it sees it...

  5. #5
    yes I also checked power with volt meter and sensor is getting 3.3 volts here is a pic of it breadboarded. Pins have been soldered to sensor so weak connection isnt the issue. I'm lost on this. I even tried the same sensor on another 3.6 teensy and got the same result

  6. #6
    Junior Member
    Join Date
    Aug 2020
    Location
    Montreal
    Posts
    16
    Might want to take a look at this thread :

    https://forum.pjrc.com/threads/21680...ry-for-Teensy3

    In particular it says :

    The Teensy 3.6 internal pullup is essentially a short, and is unusable.

    I see there are external pullups on the Adafruit board and this can only make matters worse. If that's the problem, you need to disable the internal pullups. Don't know if you can do that with the standard Wire library but nox771 published a library (i2c_t3.h) with which you can (see thread above).

  7. #7
    so using that would I still leave the SDA and SCL hookups on pins 18 and 19

  8. #8
    Junior Member
    Join Date
    Aug 2020
    Location
    Montreal
    Posts
    16
    You can find the latest version of the library here :

    https://github.com/nox771/i2c_t3

    Once installed, open the basic_master example in the IDE under Examples->i2c_t2-master.

    It contains the following lines :

    Code:
        // Setup for Master mode, pins 18/19, external pullups, 400kHz, 200ms default timeout
        Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
        Wire.setDefaultTimeout(200000); // 200ms
    This should work in your setup.

  9. #9
    Just ran that and serial monitor did not print out any results so the teensy is not even connecting with the sensor as an I2C device so I am lost as in what to do.

  10. #10
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,642
    Quote Originally Posted by chandler54412 View Post
    Just ran that and serial monitor did not print out any results so the teensy is not even connecting with the sensor as an I2C device so I am lost as in what to do.
    Unclear here, did it hang and not print anything or did it say something like nothing found.

    Example run from T3.6 to board with SHT31...
    Code:
    I2C Scanner
    Scanning...
    Device found at address 0x44  (PCA9685, SHT3X)
    done
    
    Scanning...
    Device found at address 0x44  (PCA9685, SHT3X)
    done
    For simple tests like this, I normally get away with just using the built in Adafruit Resister, at least enough to test out...

    If Wire does not see it, than I would again double check wiring, likewise run simple test to make sure the wires are correct..

  11. #11
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,642
    Again a picture may help. And did the Wire library scanner program see your device?

  12. #12
    Quote Originally Posted by KurtE View Post
    Again a picture may help. And did the Wire library scanner program see your device?
    for some reason it is not letting me upload a photo on the replies. I ran the scanner and it serial printed nothing back so it is not even recognizing that there is a sensor plugged into the teensy.

  13. #13
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,642
    Are you using the Adafruit sensor or do you have link to the one you have?

  14. #14
    Quote Originally Posted by KurtE View Post
    Are you using the Adafruit sensor or do you have link to the one you have?
    yes I'm using the adafruit mcp9808 temperature sensor

  15. #15
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,642
    I have one on order... Probably won't have until end of week...


    But again showing a picture might help. Maybe see something like SCL/SDA lines reversed or pins off by one or???

    Sometimes when it does not make much sense, I end up using a simple program to double check my wires...
    One that myself and @defragster kept adding things to... HiLowTest

    Code:
    void setup() {
      Serial.begin(115200);
      while (!Serial && millis() < 4000 );
      Serial.println("Compile Time:: " __FILE__ " " __DATE__ " " __TIME__);
      Serial.printf("Num Digital Pins: %d\n", NUM_DIGITAL_PINS);
    
      testForShorts();
      
    }
    
    uint32_t cnt = 0;
    void loop() {
      cnt++;
        allPinTest( cnt );
    }
    
    uint32_t pinLast[NUM_DIGITAL_PINS];
    void allPinTest( uint32_t cnt ) {
      uint32_t ii, SET;
      Serial.print("PULLDOWN Start Vals:\n  ");
      SET = 0;
      Serial.print("PULLDOWN :: TEST to 3.3V\n  ");
      for ( ii = 0; ii < NUM_DIGITAL_PINS; ii++) {
        pinMode( ii, INPUT_PULLDOWN );
        delayMicroseconds( 5 );
        pinLast[ii] = digitalReadFast( ii );
        if (pinLast[ii]) {
          Serial.print("\nd#=");
          Serial.print( ii );
          Serial.print( " val=" );
        }
        Serial.print( pinLast[ii] );
        Serial.print(',');
      }
      Serial.println();
      Serial.println();
      while ( 1 ) {
        uint32_t jj, dd = 0, cc = 0, ee=4;
        cc = 0;
        for ( ii = 0; ii < NUM_DIGITAL_PINS; ii++) {
          jj = digitalReadFast( ii );
          if ( jj != pinLast[ii] ) {
            dd = 1;
            cc++;
            pinLast[ii] = jj;
            Serial.print("d#=");
            Serial.print( ii );
            if ( pinLast[ii] ) Serial.print( "\t" );
            Serial.print( " val=" );
            Serial.print( pinLast[ii] );
            Serial.print(',');
          }
          if ( cc > 1 && ee ) {
            Serial.println(">>> MULTI CHANGE !!");
            ee--;
          }
          if ( Serial.available() ) {
            while ( Serial.available() ) Serial.read();
            if ( 0 == SET ) {
              SET = 1;
              Serial.print("PULLUP :: TEST TO GND\n  ");
            }
            else {
              SET = 0;
              Serial.print("PULLDOWN :: TEST to 3.3V\n  ");
            }
            for ( ii = 0; ii < NUM_DIGITAL_PINS; ii++) {
              if ( 0 == SET )
                pinMode( ii, INPUT_PULLDOWN );
              else
                pinMode( ii, INPUT_PULLUP );
              delayMicroseconds( 20 );
              pinLast[ii] = digitalReadFast( ii );
              if (SET != pinLast[ii]) {
                Serial.print("d#=");
                Serial.print( ii );
                Serial.print( " val=" );
                Serial.println( pinLast[ii] );
              }
            }
          }
        }
        if ( dd ) {
          dd = 0;
          Serial.println();
          delay( 50 );
        }
      }
    }
    
    void testForShorts() {
      uint32_t ii;
      Serial.print("Quick Test for Shorts to adjacent pin");
      Serial.println("First pull pins down and see if the next one follows");
      for ( ii = 0; ii < NUM_DIGITAL_PINS-1; ii++) {
        pinMode( ii+1, INPUT_PULLDOWN );
        pinMode( ii, OUTPUT);
        digitalWrite(ii, HIGH);
        delayMicroseconds( 5 );
        if (digitalRead(ii+1)) {
          Serial.printf("%d:%d ", ii, ii+1);
        }
      }
      Serial.println("\n Now try Pull up and see if setting low follow");
      for ( ii = 0; ii < NUM_DIGITAL_PINS-1; ii++) {
        pinMode( ii+1, INPUT_PULLUP );
        pinMode( ii, OUTPUT);
        digitalWrite(ii, LOW);
        delayMicroseconds( 5 );
        if (!digitalRead(ii+1)) {
          Serial.printf("%d:%d ", ii, ii+1);
        }
      }
      Serial.println();  
    }
    Which I often run on boards. Hook up a jumper to GND. Start up sketch and serial monitor and then usually hit enter to go to the second test where you can check to take a pin to LOW...
    THen with my Jumper wire, I then probe pins I am interested in, and they should say something like Pin 19 LOW when you touch it or the like... Helps to find when I screw up and I miscounted and or forgot there was a GND pin or...

  16. #16
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,452
    Have you tried using external pullups on SCL and SDA - about 2k2? The Adafruit board has 10k pullups which may not be sufficient.

    Pete

  17. #17
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,847
    Quote Originally Posted by el_supremo View Post
    Have you tried using external pullups on SCL and SDA - about 2k2? The Adafruit board has 10k pullups which may not be sufficient.

    Pete
    2.2K is the minimum value needed for 3.3v bus systems. 4.7K is the minimum value needed for 5v systems. You can have larger resistors (like 10K). The larger the resistors (either a larger single resistor, or multiple smaller resistors on each device) can affect whether you can run I2C at higher speeds. Having longer wires, and a lot of devices on the I2C can also make things complicated, and for optimal performance you might need to disable some of the devices' pull-up resistors and use a custom resistor value.

    Typically, if you run the I2C scanner and it hangs, it is an indication that you need pull-up resistors.

    And of course, check your soldering. I recently was trying to add an Adafruit Feathering OLED 128x32 display, and it was flaky. I went through and resoldered the pins, and now it works great.

  18. #18
    Quote Originally Posted by el_supremo View Post
    Have you tried using external pullups on SCL and SDA - about 2k2? The Adafruit board has 10k pullups which may not be sufficient.

    Pete
    I have resistors but I have never used them so I am lost at how to wire them into the breadboard, can you tell me or show me how? I believe this will solve my problem but I am lost on where to mount the resistors. Thanks

  19. #19
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,847
    Quote Originally Posted by chandler54412 View Post
    I have resistors but I have never used them so I am lost at how to wire them into the breadboard, can you tell me or show me how? I believe this will solve my problem but I am lost on where to mount the resistors. Thanks
    For pull-up resistors, this post assumes you have through hole resistors and not SMT resistors that you have to solder.

    Assuming the Teensy is in a breadboard, take one of the resistor's wires and put it in pin 19. Connect the other end to 3.3v. Do the same for pin 18.

    On a classical breadboard with two lanes on each end, with the red and black colors, it is simplest if you connect one of the ground pins to the black power lane, and connect the 3.3v power to the red power lane. Connect the black power lanes on each side of the breadboard together, and connect the red lanes together (i.e. typically each side is not connected to the other side, so you need to join the two sides).

    Alternatively, once you have connected the separate resistors to pins 18 and 19, you can twist the two wires going into 3.3v, and just use one connection (you do the twist after the middle part which is the resistor).

    If you are using a Teensy 3.2, 3.5, or 3.6 (or older Teensy 3.0/3.1), be sure to connect the ground pin next to pin 0. If you have the Teensy on the breadboard with the USB port facting up, the ground pin is the first pin on the left side. Do not use the analog ground pin that is between the VIN and 3.3v pins (2nd pin on the right), this has some extra filtering for running analog devices. You could use the analog ground pin in this case, but it might prevent using it as intended. There might be a few places that want the digital ground and not the analog ground.

    If you are using a Teensy LC, 4.0, or 4.1, you can use either ground pin, since these Teensys do not have a separate analog ground.

    You could also use the 3.3v and ground pins at the end of the Teensy LC, 3.2, or 4.0 (or the similar pins with Teensy 3.5, 3.6, and 4.1 just before the SD drive).

    For just adding the pull-up resistors, you don't need to connect ground at all. But it is useful to use the power rails, so you have a power and/or ground source nearby. This makes it simpler to connect devices with 2 or 3 wires.

    Just for completeness, if you needed pull-down resistors instead of pull-up, you would connect the resistors to ground instead of 3.3v.

    A third reason for using resistors is to limit the current going through the wire. In this case, you must attach the resistor between the data pin and the device. Unlike pull-up or pull-down resistors, these resistors are not done in parallel. A common reason for using inline resistors is things like LEDs, that you want to limit the current to a particular value. For single point LEDs, I tend to use LEDs from Adafruit or Sparkfun that have the resistor built-in, so that I don't have to worry about adding the resistor later.
    Last edited by MichaelMeissner; 09-14-2020 at 07:57 PM.

  20. #20
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,452
    Maybe a picture will help.
    Click image for larger version. 

Name:	I2C_pullups_2009_5082.JPG 
Views:	8 
Size:	287.4 KB 
ID:	21726
    This shows a T4.1 on a breadboard, but note that I haven't yet soldered it to the header pins (a teensy MUST be soldered to the header pins to work at all).
    The two resistors are 2k2. One resistor is connected from the SDA pin to the + bus on the breadboard and the other resistor connects the SCL pin to the + bus.
    The red and black wires coming in from the top of the picture are the SDA and SCL wires from a separate board which has a Chronodot I2C realtime clock.
    The green and red wires coming in from the right are the power and ground pins from the Chronodot.
    The short red jumper wire connects the Teensy +3V pin to the breadboard's + bus.

    Pete

  21. #21
    Quote Originally Posted by el_supremo View Post
    Maybe a picture will help.
    Click image for larger version. 

Name:	I2C_pullups_2009_5082.JPG 
Views:	8 
Size:	287.4 KB 
ID:	21726
    This shows a T4.1 on a breadboard, but note that I haven't yet soldered it to the header pins (a teensy MUST be soldered to the header pins to work at all).
    The two resistors are 2k2. One resistor is connected from the SDA pin to the + bus on the breadboard and the other resistor connects the SCL pin to the + bus.
    The red and black wires coming in from the top of the picture are the SDA and SCL wires from a separate board which has a Chronodot I2C realtime clock.
    The green and red wires coming in from the right are the power and ground pins from the Chronodot.
    The short red jumper wire connects the Teensy +3V pin to the breadboard's + bus.

    Pete
    That was a great pic I ran the same setup of two pull up resistors on mine and it still gives me the could not find MCP9808 on the serial monitor so now I am truly lost. I am about to run an I2c scanner to see if it pulls anything up.

  22. #22
    Quote Originally Posted by chandler54412 View Post
    That was a great pic I ran the same setup of two pull up resistors on mine and it still gives me the could not find MCP9808 on the serial monitor so now I am truly lost. I am about to run an I2c scanner to see if it pulls anything up.
    The I2C scanner does not even pickup the sensor when scanning for it. However when I wire the sensor to an Arduino Uno it works no problem so it is clearly a teensy issue. I even swapped the teensy with another teensy 3.6 and it produced same error even after adding pull up resistors and I checked to make sure resistors were getting power. How else can I fix this?

  23. #23
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,452
    If the scanner doesn't find anything, can you post a photo that shows how things are wired?

    Pete

  24. #24
    Quote Originally Posted by el_supremo View Post
    If the scanner doesn't find anything, can you post a photo that shows how things are wired?

    Pete
    It wont let me post pic for some reason can I message you it?

  25. #25
    There is the picture hopefully you all can see it on your screen.

Posting Permissions

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