1

I have been recently working on a website that takes input from the arduino ie- temperature and humidity. But there is a fatal problem that I've encountered, the serial monitor spews out some garbage with some actual readings. Is there a way to eliminate this?

 ╰─λ cat /dev/ttyACM0
T0°C
Humidity: 39.00%
00%
Temperature: 32.70°C
Humidity: 39.00%
Temperature: 32.70°C
Humidity: 39.00%
Temperature: 32.70°C
Humidity: 39.00%
00%
Temperature: 32.70°C
Humidity: 39.00%
Temperature: 32.70°C
Humidity: 39.00%
Temperature: 32.70°C
Humidity: 39.00%
idit9.00Tempure:70°Humi: 39
emperatu32.7
Huty: 0%
idit9.00Tempure:70°umidity.00%Tratu32.7
idit9.00Tempture.70�Humi: 39
empere: 0°Cmidi39.0
atur°C
Hum9.00

code-

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 4

#define DHTTYPE    DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  Serial.begin(9600);
  sensor_t sensor;
  dht.begin();
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  delay(delayMS);
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
}

board - Arduino Uno (R3)

7
  • 1
    Welcome to Arduino:SE. Without seeing your code and hardware setup, we have no way to answer your question. Commented Oct 3, 2022 at 5:48
  • @sempaiscuba thanks for correcting me. I have edited it now, let me know if there are some further corrections. Commented Oct 3, 2022 at 5:57
  • I would print something unique in setup() that makes it evident in the output listing that it is only running once.
    – timemage
    Commented Oct 3, 2022 at 6:04
  • What is the actual value of delayMS? The calculation looks fishy, since sensor is not initialized. The value should be around 2500ms.
    – PMF
    Commented Oct 3, 2022 at 6:33
  • 1
    Do you have ModemManager installed on your system? Could you check that nobody else is messing with the serial port? Like this: sudo fuser -v /dev/ttyACM0. Commented Oct 3, 2022 at 7:28

1 Answer 1

0

As has been noted in the comments, you are trying to read the sensor at a rate that is too fast.

Try changing this line:

delayMS = sensor.min_delay / 1000;

to:

delayMS = sensor.min_delay = 1000;

or:

delayMS = sensor.min_delay = 3000;

This will read the sensor at a slower rate and should give you clean output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.