[teensy41] i2c problems

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
ryanGT
Posts: 24
Joined: Fri Jan 28, 2022 12:15 am

[teensy41] i2c problems

Post by ryanGT » Sat Mar 05, 2022 1:40 am

I am now having trouble getting started with i2c on the teensy 4.1. Based on this:

https://github.com/robert-hh/micropytho ... ickref.rst

I believe i2c hardware port 0 refers to pins 18 (sda) and 19 (scl). I have those pins along with ground and 3.3V connected to a level shifter with an Arduino Uno on the other side (GND, 5V, A4 (sda) and A5 (scl) on the Uno side). I have used this same setup with i2c and a pyboard and everything worked fine.

I am trying to run this simple upython code:

>>> from machine import I2C
>>> i2c = I2C(0, 400_000)
>>> i2c.scan()
[]
>>> i2c.writeto(0x04, b'12')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV

Since the scan returns nothing, it is not surprising that the writeto fails. As long as I have the pins right, I don't know what could be going wrong.

Any help appreciated.

Thanks,
Ryan

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: [teensy41] i2c problems

Post by Roberthh » Sat Mar 05, 2022 7:00 am

Most probably the pull-up resistors arte missing. PyBoard has 4k7 Ohm pull-up resistors on the board, Teensy does not have such. One remark: the Teensy port runs I2C only in master mode.

ryanGT
Posts: 24
Joined: Fri Jan 28, 2022 12:15 am

Re: [teensy41] i2c problems

Post by ryanGT » Sat Mar 05, 2022 3:10 pm

Thanks Robert. I would have never thought to check the pullups. I guess I have been spoiled by the devices I have used. This is my first time using a teensy.

For some reason, scan still doesn't work. Not sure if I would have to program my Arduino to follow some protocol there. But I can send a message and have it echoed back based on how I programmed the Arduino. So, it is working.

Thanks again,
Ryan

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: [teensy41] i2c problems

Post by Roberthh » Sat Mar 05, 2022 3:54 pm

During scan only the address is sent, and the device responds with ACK or NAK.

ryanGT
Posts: 24
Joined: Fri Jan 28, 2022 12:15 am

Re: [teensy41] i2c problems

Post by ryanGT » Sat Mar 12, 2022 4:51 pm

So, I forgot to include external pullup resistors in a custom arduino shield I made with a level shifter and everything worked fine. I think this is because my level shifter has pullup resistors in it (thought maybe 10K instead of 4k7):

https://learn.sparkfun.com/tutorials/bi ... d-overview

Am I interpreting that correctly? Are 10k pullups good enough between a Teensy 4.1 and an Arduino?

Thanks,
Ryan

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: [teensy41] i2c problems

Post by Roberthh » Sat Mar 12, 2022 4:58 pm

The one transistor level shifter used for I2C includes pull-up resistors. And 10k is fine for short wires and moderate speed. If you increase the wire length or the frequency, you will need lower pull-up resistors values.

ryanGT
Posts: 24
Joined: Fri Jan 28, 2022 12:15 am

Re: [teensy41] i2c problems

Post by ryanGT » Sat Mar 12, 2022 5:40 pm

Thanks again. So I pretty much got lucky.

As far as wire length, I'm talking two to three inches maximum as the teensy is plugged into a shield that's mounted on top of the Arduino. So it can only get 3 inches away worse case.

I think you're saying it would always be safer to have stronger pull-ups.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: [teensy41] i2c problems

Post by Roberthh » Sat Mar 12, 2022 7:06 pm

As long as it works it's fine. 10k is a common value at breakout boards. For a better decision, you would have to analyze the signals with an oscilloscope.

Post Reply