Post by @michael

Photo of CozIR Ambient CO2 sensor, Raspberry Pi Pico 2 W on a breadboard and a plastic box with jumper wires, all placed on a laminated pinout diagram
Photo of CozIR Ambient CO2 sensor, Raspberry Pi Pico 2 W on a breadboard and a plastic box with jumper wires, all placed on a laminated pinout diagram
Replying to @michael
michael
@michael
Public en edited

@michael

The sensor came with the following Python demo script. First step is to rewrite that for MicroPython.

#!/usr/bin/python3

# Demo script to measure CO2 with a Cozir Ambient sensor

import serial
import time

# Serial port setup
ser = serial.Serial("/dev/ttyU0")	# OpenBSD
print("Serial connected")

# Polling mode
ser.write(bytes("K 2\r\n", "utf-8"))
ser.flushInput()
time.sleep(1)
print("COZIR CO2 sensor ready\n")

# Discard first measurement
ser.write(bytes("Z\r\n", "utf-8"))
time.sleep(.01)
discard = ser.read(10)
time.sleep(.5)

# Measure
while True:
    ser.write(bytes("Z\r\n", "utf-8"))
    time.sleep(.01)
    resp = ser.read(10)
    resp = resp[:8]
    fltCo2 = int(resp[2:])
    print("CO2 (ppm) = " + str(fltCo2))
    time.sleep(1)
Replying to @michael
michael
@michael
Public en edited
Photo of Raspberry Pi Pico connected to CozIR Ambient CO2 sensor with jumper wires
Photo of Raspberry Pi Pico connected to CozIR Ambient CO2 sensor with jumper wires

@michael

MicroPython version of CozIR Ambient demo script:

from machine import UART, Pin
import time

uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))

uart1.write(b'K 2\r\n')
uart1.flush()
time.sleep(1)
print("COZIR CO2 sensor ready\n")

uart1.write(b'Z\r\n')
time.sleep_ms(100)
discard = uart1.read(10)

while True:
    uart1.write(b'Z\r\n')
    time.sleep_ms(10)
    resp = uart1.read(10)
    resp = resp[:8]
    fltCo2 = int(resp[2:])
    print("CO2 (ppm) = " + str(fltCo2))
    time.sleep(1)

Upload script to Pico and run:

(.venv) $ mpremote pico cp cozir.py :cozir.py + exec "import cozir.py" 
cp cozir.py :cozir.py
COZIR CO2 sensor ready

CO2 (ppm) = 802
CO2 (ppm) = 802
CO2 (ppm) = 810
CO2 (ppm) = 808
^C(.venv) $ 

#MicroPython #RaspberryPiPico #CO2 #sensor