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

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
    Montreal, Canada
    Posts
    2,755
    why you put wire.receive instead of wire.read for teensy?

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

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,372
    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:	148 
Size:	415.2 KB 
ID:	12621

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,372
    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:	143 
Size:	443.0 KB 
ID:	12623

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,880
    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!

  10. #10
    Junior Member
    Join Date
    Mar 2018
    Posts
    4

    question

    Quote Originally Posted by necrolancer View Post
    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!
    can you please show the code that you used, I'm having some problems trying to use a library that was built for arduino, but I want to use it with teensy 3.6

    Arduino code looks like this

    /*!
    * @file DFRobot_SHT20_test.ino
    * @brief DFRobot's SHT20 Humidity And Temperature Sensor Module
    * @n This example demonstrates how to read the user registers to display resolution and other settings.
    * Uses the SHT20 library to display the current humidity and temperature.
    * Open serial monitor at 9600 baud to see readings.
    * Errors 998 if not sensor is detected. Error 999 if CRC is bad.
    * Hardware Connections:
    * -VCC = 3.3V
    * -GND = GND
    * -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
    * -SCL = A5 (use inline 330 ohm resistor if your board is 5V)
    */

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

    DFRobot_SHT20 sht20;


    void setup()
    {

    Serial.begin(9600);

    Serial.println("SHT20 Example!");
    sht20.initSHT20(); // Init SHT20 Sensor
    delay(100);
    sht20.checkSHT20(); // Check SHT20 Sensor




    }

    void loop()
    {
    float humd = sht20.readHumidity(); // Read Humidity
    float temp = sht20.readTemperature(); // Read Temperature
    Serial.print("Time:");
    Serial.print(millis());
    Serial.print(" Temperature:");
    Serial.print(temp, 1);
    Serial.print("C");
    Serial.print(" Humidity:");
    Serial.print(humd, 1);
    Serial.print("%");
    Serial.println();
    delay(1000);
    }


    I'm totally new with teensy

  11. #11
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,588
    Use CODE tags (#) to retain indentation when you post your sketch to the forum. Can you attach a photo of your wiring. You need 4.7K pullups to 3v3 on pins 18 and 19 as shown in post #7. The SHT20 should be powered with 3v3 and GND. (if it's on a breakout board (provide part number for board), the breakout board may already have pullup resistors on SCL SDA). You don't need 300 ohm resistors as mentioned in comments in sketch.

  12. #12
    Senior Member
    Join Date
    Nov 2012
    Posts
    953
    OP also started a new thread on this https://forum.pjrc.com/threads/50216

    Pete

Posting Permissions

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