Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:59 21 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CO2 sensing

Author Message
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 274
Posted: 03:23pm 10 Nov 2022
Copy link to clipboard 
Print this post

Another German video ;-)

https://youtu.be/FfbP-4b5n9o
 
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 274
Posted: 11:53am 11 Nov 2022
Copy link to clipboard 
Print this post

Here is the code to read out the CO2 sensor, if someone is interested.

The sensor is the SCD41, available as breakout from Pimoroni

Const I2C_ADDR = 98

Print "Initialize I2C ..."
SetPin GP0, GP1, I2C
I2C Open 100, 500

Print "Initialize sensor and start measuring ..."
I2C Write I2C_ADDR, 0, 2, &H36, &H46 : Pause 50
I2C Write I2C_ADDR, 0, 2, &H21, &HB1 : Pause 2

Function isDataAvailable()
 Local Integer buf(2) = (0,0,0)
 I2C Write I2C_ADDR, 0, 2, &HE4, &HB8 : Pause 2
 I2C Read I2C_ADDR, 0, 3, buf()
 isDataAvailable = ((buf(0) << 8) Or buf(1) And &HFFF) > 0
End Function

Sub getData CO2, Tmp, Hum
 Local Integer val, buf(8) = (0,0,0,0,0,0,0,0,0)
 I2C Write I2C_ADDR, 0, 2, &HEC, &H05 : Pause 1
 I2C Read I2C_ADDR, 0, 9, buf()
 CO2 = (buf(0) << 8) Or buf(1)
 val = (buf(3) << 8) Or buf(4)
 Tmp = (((21875 *val) >> 13) -45000) /1000
 val = (buf(6) << 8) Or buf(7)
 Hum = ((12500 *val) >> 13) /1000
End Sub

Dim integer i
Dim float CO2, T, H
For i=1 To 10
 Print "Waiting for data ..."
 Do : Pause 100 : Loop Until isDataAvailable()
 getData CO2, T, H
 Print "CO2 = " +Str$(CO2,5,0) +" ppm"
 Print "  T = " +Str$(T,-3,1) +Chr$(186) +"C"
 Print "  H = " +Str$(H,5,0) +"%"
 Pause 1000
Next i

Print "Stop measuring ..."
I2C Write I2C_ADDR, 0, 2, &H3F, &H86 : Pause 600
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025