Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Teensy 3.6 I2C Problems

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    4

    Teensy 3.6 I2C Problems

    Hello! This is my first time using a Teensy product and have previously worked with Arduino.
    I'm having trouble using the teensy to read a simple temperature reading from this temperature sensor.
    http://www.mouser.com/ds/2/302/PCT2075-1127732.pdf

    I was able to get it working on an Arduino Uno with the exact came code setup. Is there anything wrong with my code for my Teensy 3.6???
    I am using 4.7k pullup resistors and am connected to pin 18 & 19 for SDA & SCL.
    Output for Arduino: 68 (for Fahrenheit)
    Output for Teensy 3.6: 0

    ARDUINO CODE
    Code:
    #include <Wire.h>
    
    int address1 = 72;
    
    void setup() 
    {
      Serial.begin(9600);
      Wire.begin();
        
      Serial.println("Begin!");
    }
    
    void loop() 
    {
      int c, Fahrenheit;
    
      Wire.beginTransmission(address1);
      Wire.write(0);
      Wire.endTransmission();
      Wire.requestFrom(address1, 2);
      
      c = Wire.read();
      
      Fahrenheit = (1.8)*c + 32;
      
      Serial.print("Temperature: ");
      Serial.println(Fahrenheit);
      delay(1000);
    }
    TEENSY 3.6 CODE:
    Code:
    #include <Wire.h>
    
    int address1 = 72;
    int pin_SDA = 18; 
    int pin_SCL = 19; 
    
    void setup() 
    {
      Serial.begin(9600);
      Wire.begin();
      Wire.setSDA(pin_SDA);
      Wire.setSCL(pin_SCL);
      delay(1000);
      Serial.println("Begin!");
    }
    
    void loop() 
    {
      int c;
    
      Wire.beginTransmission(address1);  
      Wire.write(0);  
      Wire.endTransmission();  
      Wire.requestFrom(address1, 2);
      
      c = Wire.receive();
      
      Serial.print("Temperature: ");
      Serial.println(c);
      delay(1000);
    }

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Canada
    Posts
    834
    why you put wire.receive instead of wire.read for teensy?

  3. #3
    Senior Member
    Join Date
    Dec 2016
    Location
    Canada
    Posts
    834
    your also requesting 2 bytes instead of 1, better fix that.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,236
    Quote Originally Posted by necrolancer View Post
    Is there anything wrong with my code for my Teensy 3.6???
    Your code looks fine.

    However, you might try printing the return from Wire.requestFrom() in both programs. Like this:

    Code:
      c = Wire.requestFrom(address1, 2);
      Serial.print("requestFrom gave ");
      Serial.println(c);
    Output for Teensy 3.6: 0
    My guess is you will see requestFrom returning 2 on Arduino and 0 on Teensy.

    The problem is very likely in the wiring. Maybe you could show us photos of how you've actually connected both of them? Maybe we'll be able to see a wiring problem you've overlooked? (it happens all the time here... we're pretty good at helping with wiring when we can see good photos)


    FWIW, Wire.read() and Wire.receive() do the same thing. A very long time ago, early versions of Arduino had Wire.receive() but not Wire.read(). Arduino changed from receive to read. Since that day, many years ago, Teensy has supported both. Very little old code still lingers on website with Wire.receive(), since it no longer works on regular Arduino boards. I've considered removing it from Teensy, but there doesn't seem to be any harm in supporting both, other than perhaps minor confusion in threads like this.

  5. #5
    Junior Member
    Join Date
    Jan 2018
    Posts
    4
    I don't have my physical set up with me right now, but here is basically how I wired it.
    Click image for larger version. 

Name:	Temp Teensy Wiring.png 
Views:	8 
Size:	415.2 KB 
ID:	12621

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,236
    Maybe this is a misunderstanding about how to connect the 4.7K resistors?

    The SDA & SCL signals from the chip connect directly to Teensy. Then you connect a resistor between each signal and 3.3V. The resistors are not supposed to be wired between the Teensy and the other chip.

  7. #7
    Junior Member
    Join Date
    Jan 2018
    Posts
    4
    So this is how it should be wired?
    Click image for larger version. 

Name:	Temp Teensy Wiring.png 
Views:	7 
Size:	443.0 KB 
ID:	12623

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    5,177
    Yes, that will pull the SDA and SCL lines up to 3.3V

  9. #9
    Junior Member
    Join Date
    Jan 2018
    Posts
    4
    I got it working! Thank you all for your help in helping me solve my problem. I guess I wasn't sure how the resistors were set up with the Teensy for I2C. Thank you!

Posting Permissions

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