Driver for SCD30 CO2 Sensor

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
mnvk52
Posts: 11
Joined: Sat Sep 05, 2020 5:31 pm

Driver for SCD30 CO2 Sensor

Post by mnvk52 » Sun Jan 10, 2021 12:26 pm

Hey,
I want to read a SCD30 CO2 Sensor with an ESP32.
I found this driver: https://github.com/agners/micropython-scd30
Trying the example:

Code: Select all

import time
from machine import I2C, Pin
from scd30 import SCD30

i2cbus = I2C(1)
scd30 = SCD30(i2c, 0x61)

while True:
    # Wait for sensor data to be ready to read (by default every 2 seconds)
    while scd30.get_status_ready() != 1:
        time.sleep_ms(200)
    scd30.read_measurement()
doesn't work:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
NameError: name 'i2c' isn't defined
not a surprise because the variable i2c is not defined. How can I do that? I didn't find anything in the readme.

mnvk52
Posts: 11
Joined: Sat Sep 05, 2020 5:31 pm

Re: Driver for SCD30 CO2 Sensor

Post by mnvk52 » Sun Jan 10, 2021 12:32 pm

I found a solution

Code: Select all

i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)
Thanks for reading :)

mnvk52
Posts: 11
Joined: Sat Sep 05, 2020 5:31 pm

Re: Driver for SCD30 CO2 Sensor

Post by mnvk52 » Sun Jan 10, 2021 5:41 pm

I encountered an issue. I have modified the example and added a print of the values

Code: Select all

import time
from machine import I2C, Pin
from scd30 import SCD30

i2cbus = I2C(1)
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)
scd30 = SCD30(i2c, 0x61)

while True:
    # Wait for sensor data to be ready to read (by default every 2 seconds)
    while scd30.get_status_ready() != 1:
        time.sleep_ms(200)
    print(scd30.read_measurement())
After some time (seconds to minutes) there is an error:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
(870.4922, 22.77562, 34.85107)
(867.943, 22.80499, 34.83124)
(865.1123, 22.77562, 34.69849)
(862.2131, 22.77562, 34.69849)
(862.3291, 22.79164, 34.70001)
(860.758, 22.81834, 34.74426)
(859.4007, 22.79164, 34.75037)
(859.452, 22.80499, 34.80377)
(859.2345, 22.80499, 34.84497)
(858.7045, 22.79164, 34.82819)
(858.1632, 22.79164, 34.82819)
(857.4939, 22.77562, 34.7641)
(855.0312, 22.77562, 34.80072)
(854.8871, 22.77562, 34.81445)
(853.2128, 22.77562, 34.77631)
(852.5694, 22.76227, 34.77325)
(853.7568, 22.76227, 34.81445)
(853.0764, 22.76227, 34.80072)
(851.2052, 22.76227, 34.78699)
(850.1311, 22.76227, 34.76257)
(849.3401, 22.74891, 34.81293)
(848.5746, 22.77562, 34.85107)
(848.2946, 22.76227, 34.87701)
(848.6511, 22.76227, 34.92737)
(847.8842, 22.76227, 34.89075)
(848.0656, 22.76227, 34.92737)
(847.8646, 22.74891, 34.9411)
(847.7996, 22.77562, 34.97925)
(847.8042, 22.73289, 34.87396)
(846.7725, 22.77562, 34.81445)
(846.6022, 22.74891, 34.82361)
(846.6348, 22.77562, 34.90448)
(846.3162, 22.71954, 34.86023)
(845.8603, 22.71954, 34.79614)
(845.9128, 22.74891, 34.79919)
(844.8734, 22.71954, 34.82208)
(844.1621, 22.73289, 34.91364)
(842.8937, 22.70618, 34.91058)
(843.0404, 22.74891, 34.99146)
(843.6476, 22.71954, 34.92432)
(843.8562, 22.73289, 34.93805)
(844.2594, 22.71954, 35.31799)
(845.3938, 22.71954, 35.38208)
(846.0004, 22.69283, 35.34088)
(847.3644, 22.67681, 35.35309)
(848.2602, 22.69283, 35.20203)
(848.9667, 22.69283, 35.13794)
(849.6739, 22.70618, 35.07538)
(850.7389, 22.73289, 35.05249)
(852.0377, 22.69283, 35.04791)
(852.197, 22.70618, 34.97314)
(852.8499, 22.73289, 35.01587)
(853.2266, 22.71954, 35.06165)
(853.8171, 22.71954, 35.06165)
(853.2935, 22.69283, 35.03418)
(853.2643, 22.69283, 34.99756)
(853.115, 22.70618, 34.98688)
Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
  File "scd30.py", line 95, in get_status_ready
  File "scd30.py", line 161, in __read_bytes
OSError: [Errno 110] ETIMEDOUT
After that I can restart it, but it's resulting with the same error.
Any ideas?

kertxo
Posts: 3
Joined: Fri Apr 30, 2021 12:17 pm

Re: Driver for SCD30 CO2 Sensor

Post by kertxo » Fri Apr 30, 2021 12:35 pm

I also facing the same error.
Im using SoftI2C to set I2C

Code: Select all

i2cbus = SoftI2C(sda=Pin(21), scl=Pin(22))
I tryed with uPy 1.14 and 1.15 with same results. If I print an i2c scan previously I got that there are no i2c devices.

I tried with Arduino libraries and there is no problem.

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

Re: Driver for SCD30 CO2 Sensor

Post by Roberthh » Fri Apr 30, 2021 1:17 pm

a) did you connect the SEL pin of the sensor to GND?
b) the baud rate must not exceed 50000. The default is 100000. So try a lower baud rate, like 10000.
c) according to the data sheet the internal pull-up of the sensor is 45k. That works only with short cables

kertxo
Posts: 3
Joined: Fri Apr 30, 2021 12:17 pm

Re: Driver for SCD30 CO2 Sensor

Post by kertxo » Fri Apr 30, 2021 6:57 pm

Thank you for replying so quickly
  • SEL pin is connected to GND
  • Baudrate setted to 10000
  • I use 20cm Duponts
I tried with 3.3v and 5v at VCC

Last error i got is --> OSError: [Errno 19] ENODEV .No device found, if i run i2c.scan() before sendind a command i get an empty list, and no recover its possible until next reset.

Code: Select all

(698.3709, 26.81049, 39.1037)
(700.5651, 26.78111, 39.21967)
(700.5568, 26.79713, 39.32343)
(700.5651, 26.78111, 39.32343)
(699.6648, 26.79713, 39.32343)
(698.6852, 26.81049, 39.26544)
(704.0288, 26.81049, 40.1123)
(714.1973, 26.79713, 40.79437)
(726.7838, 26.78111, 40.7196)
(741.3965, 26.79713, 40.23132)
(772.8082, 26.75441, 39.81018)
(825.2788, 26.78111, 39.62097)
(811.5271, 26.75441, 39.49738)
(805.622, 26.73838, 39.51263)
(813.2113, 26.75441, 39.43939)
(799.8799, 26.76776, 39.38293)
(798.508, 26.75441, 39.36462)
(788.3013, 26.76776, 39.38293)
(784.2977, 26.75441, 39.33716)
Traceback (most recent call last):
  File "main.py", line 24, in <module>
  File "main.py", line 20, in main
  File "scd30.py", line 86, in read_measurement
  File "scd30.py", line 163, in __read_bytes
  File "scd30.py", line 160, in __write_command
OSError: [Errno 19] ENODEV
MicroPython v1.15 on 2021-04-18; ESP32 module with ESP32
Sometimes hold running 1 minute and others 30 minutes. Its frustrating.

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

Re: Driver for SCD30 CO2 Sensor

Post by Roberthh » Fri Apr 30, 2021 7:10 pm

So you are getting responses, but then it breaks after a while. That means the hardware set-up is fine, with SDA, SCL, GND and Vcc for the connection. Sorry, I did not recognize that initially. It is not clear to me how your code looks, but there are also statements from a user mnvk52, which seems to have the same problem. So it may be that the sensor locks up. Do you have any means to analyze the protocol, like a simple 10$ logic analyzer? That would help.

kertxo
Posts: 3
Joined: Fri Apr 30, 2021 12:17 pm

Re: Driver for SCD30 CO2 Sensor

Post by kertxo » Fri Apr 30, 2021 9:09 pm

The readings of the SCD30 are part of a bigger system, but at this moment im testing it by the simplest way, almost the same code as mnvk52

Code: Select all

from machine import Pin, SoftI2C
from scd30 import SCD30
import utime

def main():

    i2cbus = SoftI2C(sda=Pin(21), scl=Pin(22), freq=10000)
    print(i2cbus.scan())

    readyPin = Pin(23, Pin.IN, Pin.PULL_UP)
    
    scd30 = SCD30(i2cbus, 0x61)
    
    while True:
        
        while readyPin.value() != 1:
            utime.sleep_ms(200)
        print(scd30.read_measurement())
             

if __name__ == "__main__":
    main()

Sorry i havent got a logic analyzer and i dont know how to use it.

At this moment its running fine since my last reply, about 2.5 hours. Its the by far the longest run.

Edit: 9h running. First time achieved this running times.

Post Reply