Teensy 3.6 I2C Problems

Status
Not open for further replies.

necrolancer

New member
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);
}
 
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.
 
I don't have my physical set up with me right now, but here is basically how I wired it.
Temp Teensy Wiring.png
 
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.
 
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!
 
question

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
 
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.
 
Status
Not open for further replies.
Back
Top