Does not run after closing serial monitor, Co2 sensor with LCD

Hi friends
I want to use Co2 sensor Sensirion I2C SCD4x, I used the sample code after adding library of sensor and connect Liquid crystal LCD with I2C to MKR1010. It shows Co2, Humidity and temp every 5 seconds on LCD, but after closing Arduino software (closing serial monitor), it stops working and showing updates on LCD.
it not working just with power supply and needs to connect PC and running serial monitor software.

Can you please help me,
Code is in below , or you can see sample code of Sensirion I2C SCD4x here

my code

#include <SensirionI2CScd4x.h>
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
SensirionI2CScd4x scd4x;

void printUint16Hex(uint16_t value) {
}

void setup() {
  lcd.init();  // initialize the lcd
  Serial.begin(115200);
  while (!Serial) {
   delay(100);
     }

  Wire.begin();
  uint16_t error;
  char errorMessage[256];
  scd4x.begin(Wire);

  // stop potentially previously started measurement
  error = scd4x.stopPeriodicMeasurement();
  if (error) {
    errorToString(error, errorMessage, 256);
      }

  uint16_t serial0;
  uint16_t serial1;
  uint16_t serial2;
  error = scd4x.getSerialNumber(serial0, serial1, serial2);
  if (error) {
    errorToString(error, errorMessage, 256);
  }
  // Start Measurement
  error = scd4x.startPeriodicMeasurement();
  if (error) {
    errorToString(error, errorMessage, 256);
  }
}

void loop() {
  uint16_t error;
  char errorMessage[256];
  delay(5000);

  // Read Measurement
  uint16_t co2;
  float temperature;
  float humidity;
  error = scd4x.readMeasurement(co2, temperature, humidity);
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);  //position
  lcd.print("Co2:   T:   H:%");
  lcd.setCursor(0, 1);
  lcd.print(co2);
  lcd.setCursor(5, 1);
  lcd.print(temperature);
  lcd.setCursor(11, 1);
  lcd.print(humidity);
}

To troubleshoot it isnt working, I'd like to know if the onboard LED is blinking. Could you blink the onboard LED?

<<<<turn on on board LED here
  delay(5000);
<<<<<turn off on board LED here
  

does the onboard LED blink?

It isnt working is a terrible issue description. Now we got to figure out by playing 1000 questions what it is.

2 Likes

LED is blinking and board is ON, I think code stops somewhere at middle of code.
The 5 second delay is because the sensor reads co2 every 4 or 5 seconds

I just added some lines to the original sensor library samples file for showing data on LCD.
If you want, you can see that file.

Yes of course.

This loop will block program execution, if the Arduino is not connected to the serial monitor.

Just replace it with something like delay(2000);

1 Like

Yes, that loop was a problem, thank you so much.

Its working now :relaxed:

Yes, that loop was a problem, thank you so much.

Its working now :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.