How to use hardware serial and I2C on Teensy 3

Status
Not open for further replies.

mat

New member
Hi,

I am just starting with the Teensy 3 board but I have a couple of problem. The board seems to work correctly as really simple programs are working (reading and writing digital pins value, controlling servos with the Servo library).

However I am incapable of using any advanced capability of the board like the Hardware serial or the I2C bus.

For example, I am trying to interface with an URM37 V3.2 range sensor. It is correctly set for TTL mode and I am using the arduino library provided on this page (dfrobot.com). I adapted the library to use the HardwareSerial rather than an instance of SoftwareSerial and the rest of the code is very generic and should not pose any problem on the ARM processor of the Teensy. And I am just running the example program provided with the library.

However I am not able to get any measurement (that is I never get any input on the serial interface, whatever I am sending as output). The sensor is working correctly (I tested it on an arduino uno board with the same code) and I tested a lot of different set-up, like direct connexion of the RX and TX lines, connexion through a level converter to get the 3V of the board being 5V on the sensor (although the sensor is supposed to work at 3V), powering the sensor with 3V or with 5V, trying on every serial interface of the board, etc. and I am running low on other ideas.

Does someone have an idea on what could I be doing wrong or what should I try to do ?

I also have the same problem with the I2C bus where I try to communicate with an IIC LCD1602 but the display does not react to any command (it works though with an arduino board). But this is less important than the serial interface for now.

Thanks for your help!
 
For example, I am trying to interface with an URM37 V3.2 range sensor. It is correctly set for TTL mode and I am using the arduino library provided on this page (dfrobot.com). I adapted the library to use the HardwareSerial rather than an instance of SoftwareSerial and the rest of the code is very generic and should not pose any problem on the ARM processor of the Teensy. And I am just running the example program provided with the library.

There are 5 examples with that library.

Not knowing exactly which one you're using, I looked at DistanceByHardwareSerial.ino. The main issue it has is use of "Serial" to talk to the sensor. On Teensy "Serial" is the USB virtual serial. You want "Serial1". You'll need to change every "Serial" to "Serial1", to make it communicate as hardware serial on pins 0 and 1.

Also, that library has a copy of the AVR-only SoftwareSerial. Just delete those 2 files. Teensy 3.0 has a SoftwareSerial that works if you use a pair of pins that are a real serial port. It's meant to allow using libraries that are hard-coded to depend on SoftwareSerial.


I also have the same problem with the I2C bus where I try to communicate with an IIC LCD1602 but the display does not react to any command (it works though with an arduino board).

The most common problem is the pullup resistors. Teensy 3.0 requires you to use real pullup resistors on both SDA and SCL.
 
The Teensy 3 is a 3.3V device. Connecting 5V (e.g. TTL) to it could destroy the chip.

I hope to have been careful enough (e.g. by using a level converter), and I just tested again with a board that I did not use until now with the same result, so I don't exactly know what is happening.

Paul, thanks for your answer, I did adapt the library to use one of the Serial port of the board. To simplify, here is a self contained example of code that I tried (which block on the "reading command part" when executed):
Code:
uint8_t command[4] = { 0x22, 0x00, 0x00, 0x22 };
char response[4];

void setup() {
  Serial.begin(9600);
  Serial.println("Starting");
  Serial.flush();
  Serial1.begin(9600);
  delay(1000);
}

void loop() {
  Serial.println("Sending command...");
  Serial.flush();
  Serial1.write(command, 4);
  Serial1.flush();
  Serial.println("Reading result:");
  Serial.flush();
  for (int i = 0; i < 4; i++) {
    while (!Serial1.available()) { };
    Serial.print(Serial1.read());
    Serial.print(" ");
  }
  Serial.println("");
  Serial.flush();
  delay(1000);
}

My hope was that someone would have the same sensor (or a similar one) and could tell me if they manage (and how) to interface it with the teensy.

for the I2C I used pullup resistors as per the information on the Wire library page, so here also if someone have a similar component I would be interested to know how they managed to use it.

Thanks!
 
Status
Not open for further replies.
Back
Top