Struggling to use SCD41 sensors with ESP32 and an I2C multiplexer #12883
-
Hello there, as my first micropython project I'm trying to create a set of sensors to monitor my building ventilation system. I have been able to test the sensors individually and they work OK. I've also been able to plug them into the multiplexer in different pins and detect them so I'm happy that's working. I've re-done the breadboard wiring a few times now... Attempt 1 at multiplexing was with this method, sending the channel instructions to the multiplexer then trying to talk to the SCD41. from machine import Pin
from machine import I2C
from time import sleep
import scd4x
i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=100000)
i2c.writeto(0x70, b'\x80') #setting the multiplexer channel here
sensor = scd4x.SCD4X(i2c)
print("Sensor detected: ", [hex(i) for i in sensor.serial_number])
print("Resetting sensor.")
sensor.factory_reset()
#print("Reset complete")
#sensor.self_test() #Gives OSError: [Errno 116] ETIMEDOUT
print("Starting measurement.")
sensor.start_periodic_measurement()
while True:
if sensor.data_ready:
print("Result")
print(sensor.temperature)
print(sensor.relative_humidity)
print(sensor.CO2)
else:
try:
print(sensor.serial_number)#This function won't work if the sensor is on and running in periodic_measurement mode
print("Sensor has switched off. Switching on.")
sensor.start_periodic_measurement()
print("Sensor restarted")
i2c.scan()
except:
pass
sleep(1) In the REPL I get: Sensor detected: ['0xa1', '0x85', '0xb7', '0x7', '0x3b', '0xc1'] #so it can interrogate the sensor.
Resetting sensor.
Starting measurement.
Warning: exception chaining not supported #Expecting this, means the sensor is doing periodic measurement and won't give a serial number
Warning: exception chaining not supported
Warning: exception chaining not supported
Warning: exception chaining not supported
(161, 133, 183, 7, 59, 193) #Being able to output a serial number means measurement has stopped?
Sensor has switched off. Switching on.
Sensor restarted
Warning: exception chaining not supported #Back in business...
Warning: exception chaining not supported
(161, 133, 183, 7, 59, 193) #Again?
Sensor has switched off. Switching on.
Sensor restarted
Warning: exception chaining not supported #And this repeats. Every 3 loops the sensor stops measuring.
Warning: exception chaining not supported
(161, 133, 183, 7, 59, 193) Attempt 2 was with the code from the last post in this thread on the old micropython forum. import tca9548a_test #the I2C gets started up in this thing
import scd4x
from time import sleep
tca=tca9548a_test.TCA9548A()
tca.switch_channel(7)
sensor=scd4x.SCD4X(tca.bus)
print("sensor detected: ", [hex(i) for i in sensor.serial_number]) #check we're seeing a sensor
sensor.factory_reset()
#sensor.self_test() #Gives OSError: [Errno 116] ETIMEDOUT
sensor.start_periodic_measurement()
while True:
if sensor.data_ready:
print("Temperature", sensor.temperature)
print("Humidity", sensor.relative_humidity)
print("CO2", sensor.CO2)
else:
print("Waiting")
sleep(0.5) And in the REPL I get: sensor detected: ['0xa1', '0x85', '0xb7', '0x7', '0x3b', '0xc1'] #OK we can see the sensor then.
Waiting
Waiting
Waiting #But we can't read values from it? Help! I've run out of ideas. There's a bit more example code for this multiplexer in C, but I'd set out to use this as an opportunity to learn python and I'd like to stick to that. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Try perform some tests using directly I2C commands ( Based in linked example You can write You own driver , also remember about physical limitation of i2C bus length vs speed and at beginning test all in shot length wiring (to eliminate/minimize physical limitation errors) |
Beta Was this translation helpful? Give feedback.
-
You could use SoftI2C, which works with every GPIO pin pair. |
Beta Was this translation helpful? Give feedback.
I think I've tried SoftI2C on the firebeetle earlier and I couldn't get that or HardI2C to work on any pins other than default 21 and 22. My guess was that there's other outputs on the board labelled SCL and SDA. If they're connected to 21 and 22 then changing the I2C pins would make the other outputs behave in an odd way.
Either way, SoftI2C worked where HardI2C did not. I can now read one of the two SCD4x sensors via the multiplexer, which is something to build on. Thank you.