R1BBIT · COMMUNITY

Join the Conversation

Ask questions, share projects, swap ideas.

64
Topics
70
Posts
53
Active
Notifications
Clear all

read data from an SCD41

 

 Serg
(@serg)
Active Member Registered
Joined: 4 months ago
Posts: 6
Topic starter  

Hi,

I’m trying to read data from an SCD41 sensor and display the values (CO2, temperature, humidity) on a 320x240 SPI screen using R1BBIT.

What is the recommended way to handle I2C sensor reading and screen output in R1BBIT scripts?

Any example would be greatly appreciated!



   
Quote
(@eugene-zhuk)
Active Member Registered
Joined: 4 months ago
Posts: 7
 

The best approach in R1BBIT is to separate the logic into three parts: initialization, data acquisition, and display update.

First, initialize both the I2C bus and the display in the setup section. Make sure the SCD41 is started in periodic measurement mode, since it doesn’t return data instantly.

Then, in your main loop, avoid reading the sensor continuously. The SCD41 provides new data примерно every 5 seconds, so it’s better to check if new data is ready before reading. This prevents unnecessary calls and improves stability.

For the display (320x240 SPI), don’t redraw the entire screen every cycle. Instead:

  • Clear only the areas where values change
  • Or update text in place if your display functions support it

A typical flow looks like this:

  1. Init I2C + SCD41
  2. Init LCD
  3. Loop:
    • Check if new data is ready
    • Read CO2 / temperature / humidity
    • Update only changed values on screen
    • Add a delay (1–2 seconds)

Also, consider adding simple thresholds (e.g., CO2 > 1000 ppm) and visual indicators — it makes the UI much more useful.

If R1BBIT supports timers or async events, it's even better to trigger sensor reads on interval instead of using a blocking loop.

This approach keeps your script responsive and avoids flickering on the display.



   
ReplyQuote
(@eugene-zhuk)
Active Member Registered
Joined: 4 months ago
Posts: 7
 

VAR CO2=0
VAR TEMP=0
VAR HUM=0
VAR TIMER=0

' --- INIT ---
I2C_INIT(21,22) ' SDA, SCL
SCD41_START() ' start periodic measurement

LCD_INIT()
LCD_CLEAR()

' --- LOOP ---
WHILE (1)

TIMER = TIMER + 1

' Read sensor every ~5 seconds
IF TIMER >= 5 THEN

IF SCD41_READ() THEN
CO2 = SCD41_CO2()
TEMP = SCD41_TEMP()
HUM = SCD41_HUM()

' Update only values (no full clear)
LCD_TEXT(10,20,"CO2: " + CO2 + " ppm ")
LCD_TEXT(10,60,"Temp: " + TEMP + " C ")
LCD_TEXT(10,100,"Hum: " + HUM + " % ")

' Air quality indicator
IF CO2 > 1000 THEN
LCD_TEXT(10,160,"Air: BAD ")
ELSE
LCD_TEXT(10,160,"Air: OK ")
ENDIF

ENDIF

TIMER = 0
ENDIF

DELAY(1000)

WEND



   
ReplyQuote
(@eugene-zhuk)
Active Member Registered
Joined: 4 months ago
Posts: 7
 

Why this works better:

  • SCD41 is read at its natural interval (~5s), not spammed
  • No full LCD_CLEAR() → no flickering
  • UI updates are lightweight and responsive
  • Easy to extend (graphs, colors, alerts)

If R1BBIT supports timers or events, you can replace the TIMER logic with a scheduled task — that would be even cleaner.

Also, for better UX, consider adding color indicators or thresholds instead of plain text.



   
ReplyQuote
Share: