How to read from a i2c sensor and print on a i2c display when both use different init pins

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Hello-world
Posts: 13
Joined: Mon Mar 11, 2019 2:23 pm

How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by Hello-world » Wed Mar 20, 2019 4:55 am

Hi everyone,

Let me start off by saying im new to MicroPython so please pardon any noobie-ness. I am trying to read values from a bno055 IMU (9DOF) sensor and display them on the built in ssd1306 OLED on my Heltec ESP32 (non-lora model).

Heres my question:
It appears that to print text on the ssd1306 I need to init i2c via:

Code: Select all

  rst = Pin(16, Pin.OUT)
  rst.value(1)
  scl = Pin(15, Pin.OUT, Pin.PULL_UP)
  sda = Pin(4, Pin.OUT, Pin.PULL_UP)
  i2c = I2C(scl=scl, sda=sda, freq=450000)
  oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)
however to read values from the bno055 and get them in the REPL I use:

Code: Select all

import bno055
from machine import I2C, Pin
i2c = I2C(-1, Pin(22), Pin(21))
My goal is to use the ssd1306 and the bno055 inside the same sketch but since the i2c's are initiated using different pins, does that mean I need to initialize i2c within specific functions (i.e. one used for reading the imu and one used for printing to the screen) or do I have any way to pass multiple i2c scl and sda pins within a single i2c init. Looking at the MicroPython docs, it doesnt seem that this is allowed however....

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

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by Roberthh » Wed Mar 20, 2019 6:39 am

you can use the same SDA and SCL pin for both devices. They are selected by the device address, which is part of the communication. Only the display needs another Pin for Rst. You also do not have to init the Pin for I2C separately. That is done in creating the I2C() instance, which also only has to be done once for both devices. If you want to run the devices at different frequencies, then you have to call i2c.init(freq) before each interaction with the respective device. But it is more convenient to got for a frequency that matches both the display and the sensor.

Hello-world
Posts: 13
Joined: Mon Mar 11, 2019 2:23 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by Hello-world » Wed Mar 20, 2019 2:19 pm

Roberthh wrote:
Wed Mar 20, 2019 6:39 am
you can use the same SDA and SCL pin for both devices. They are selected by the device address, which is part of the communication. Only the display needs another Pin for Rst. You also do not have to init the Pin for I2C separately. That is done in creating the I2C() instance, which also only has to be done once for both devices. If you want to run the devices at different frequencies, then you have to call i2c.init(freq) before each interaction with the respective device. But it is more convenient to got for a frequency that matches both the display and the sensor.

Thanks for the reply. So if my board uses different i2c pins for the oled than the pins I used to connect the IMU sensor then I just need to attach the IMU to the pins that the oled uses for SDA and SCL and I should be good?

Weird thing is when I do a scan using the pins my IMU is attached onto, I see two device addresses, wonder what might be causing it to see something else.

Thanks again

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by fstengel » Wed Mar 20, 2019 4:57 pm

What addresses do you see? The bno055 chip should answer 0x28 or 0x29, and the SSD1306 0x3c or 0x3d

Hello-world
Posts: 13
Joined: Mon Mar 11, 2019 2:23 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by Hello-world » Thu Mar 21, 2019 4:13 am

fstengel wrote:
Wed Mar 20, 2019 4:57 pm
What addresses do you see? The bno055 chip should answer 0x28 or 0x29, and the SSD1306 0x3c or 0x3d
Using this code:

Code: Select all

import bno055
from machine import I2C, Pin

i2c = I2C(-1, Pin(22), Pin(21)) #, timeout=1000)

s = bno055.BNO055(i2c)

go = True
c = 1

while go:
  time.sleep(9)
  print (i2c.scan())
  print(s.euler())
  c += 1
  print('Count is:', c)
  if c == 20:
    go = False
I see i2c.scan() print a few different addresses each time the loop is executed:

Code: Select all

>>> 

Ready to download this file,please wait!
.......
download ok
exec(open('./imu.py').read(),globals())
[19, 40]
[40]
(0.25, 2.3125, 1.125)
Count is: 2
[51]
Trs.py", line  _partial
  File "bno055.py", line 154, in _registers
OSError: [Errno 110] ETIMEDOUT
>>> 
Looks like addresses fluctuate between [19,40], [40], [51], [41]


I don't have anything else connected to the ESP32 other than the bno055. The OLED was built in.


the ETIMEDOUT error is something im also trying to resolve but I can save that for another day

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by fstengel » Thu Mar 21, 2019 12:38 pm

Weird. The scan should consistently show 40 (or 41) not a mix of 40, 41 and none of the others.

Hello-world
Posts: 13
Joined: Mon Mar 11, 2019 2:23 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by Hello-world » Thu Mar 21, 2019 2:36 pm

fstengel wrote:
Thu Mar 21, 2019 12:38 pm
Weird. The scan should consistently show 40 (or 41) not a mix of 40, 41 and none of the others.
Any idea what I can try to fix it? I guess I can lock the bno055 into one address (?)

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by fstengel » Thu Mar 21, 2019 5:56 pm

That's the weird bit: normally the bno's address should be 0x28 or 0x29, depending on the state of its COM3 pin. If you are using the adafruit package, this pin is accessed through the ADR pin and defaults to COM3 low and 0x28 as the I2C address. So first things first: check if your bno is properly wired...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by dhylands » Thu Mar 21, 2019 6:36 pm

well 0x28 = 40 and 0x29 = 41.

The numbers reported by i2c.scan are decimal.

Make sure that you have a good ground connection between your devices and the processor.

Hello-world
Posts: 13
Joined: Mon Mar 11, 2019 2:23 pm

Re: How to read from a i2c sensor and print on a i2c display when both use different init pins

Post by Hello-world » Thu Mar 21, 2019 8:20 pm

dhylands wrote:
Thu Mar 21, 2019 6:36 pm
well 0x28 = 40 and 0x29 = 41.

The numbers reported by i2c.scan are decimal.

Make sure that you have a good ground connection between your devices and the processor.
Will do! Thank you

Post Reply