1

In the process of learning and working on a larger project, I started by adding an OLED display (128x64, SPI). I wrote a simple script to update every 5 seconds and then display the number of seconds on both the OLED and the Serial port. This worked just fine and I even let it run for more than a day. Then I added a DHT22. This is where the issues started coming in. The Arduino Mega (clone) is powering both devices. I have a 10K resistor across the VCC and signal pins of the DHT22. The code should now update both the OLED and the serial port with the number of seconds and the temperature and humidity. This works, for awhile, and then the whole program freezes for some odd reason. The time before freezing is also at random intervals.

During my research it was suggested to add a 10uF capacitor across the ground and VCC. So I added a 10uF 50v cap and the results are still the same. Originally I had Serial.println(String(..... in the code, but I replaced all that to not use String(). I tried adding Serial.flush() at the end of the display code, but equal results.

Additional research also suggested waiting 30 seconds before trying to read the DHT22. I added this 30 second wait at the end of the setup function, but same results. I've tried checking for NaN, not checking for NaN and nothing has seemed to correct this issue.

I've also tried the program without the OLED. Same results. I've tried the program without the Serial output, but same results.

What is going on here and does anyone have any suggestions as to how to correct this?

Currently this whole thing is rigged up on a breadboard if that helps.

Below is the current code I'm trying:

#include <Arduino.h>

#include <SPI.h>
#include <Wire.h>

#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Adafruit Libraries for the OLED display
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Setup the OLED variables
#define OLED_MOSI   9 // D1 on the OLED
#define OLED_CLK   10 // D0 on the OLED
#define OLED_DC    11 // DC on the OLED
#define OLED_CS    12 // CS on the OLED
#define OLED_RESET 13 // RES on the OLED
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

// Global variables
unsigned long currentMillis;
long previousMillis = 0;
#define interval 5000

float humidity;
float fahrenheit;

void setup(){
  // Start the serial port transmission
  Serial.begin(9600);

  // Start the OLED
  display.begin(SSD1306_SWITCHCAPVCC);

  dht.begin();

  // Clear the memory of the OLED
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(1,0);
  display.println("Madgayrah");
  display.display();

  while(millis()<2000);

  display.setCursor(0,16);
  display.println("Arduino");
  display.display();



  while(millis()<5000);
//  while(millis()<30000);

  Serial.println("");
  display.clearDisplay();
}


void loop() {
  currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    poll_and_display(currentMillis);
  }
}

void poll_and_display(long currentMillis){
    read_dht22();

    Serial.println(previousMillis/1000);

    if(!isnan(humidity) && !isnan(fahrenheit)){
      Serial.print(fahrenheit, 1);
      Serial.print("* ");
      Serial.print(humidity, 0);
      Serial.println("%");
    }

    display.clearDisplay();

    display.setCursor(0,0);
    display.println(currentMillis/1000);
    if(!isnan(humidity) && !isnan(fahrenheit)){
      display.setCursor(0,16);
      display.print(fahrenheit, 1);
      display.print("* ");
      display.print(humidity, 0);
      display.print("%");
    }
    display.display();

    Serial.println("");

    Serial.flush();
}

void read_dht22(){
    humidity = dht.readHumidity();
    fahrenheit = dht.readTemperature(true);

//    while (isnan(humidity) || isnan(fahrenheit)) {
//      unsigned long tempEnteredMillis = millis();
//      while (millis() - tempEnteredMillis < 2500);
//      
//      humidity = dht.readHumidity();
//      fahrenheit = dht.readTemperature(true);
//
//      Serial.print("Result from NaN loop = ");
//      Serial.print(fahrenheit, 1);
//      Serial.print("* ");
//      Serial.print(humidity, 0);
//      Serial.println("%");
//    }
}

UPDATE: After continuing to research on this I discovered that Arduino has a Watchdog Timer, or at least SHOULD have, and this SHOULD detect hang ups and restart the Arduino. So I tried a hardware version of the Watchdog Timer and a software version, but neither one seemed to work and/or correct the hang up/freeze issue. Below are the links I used for the software and hardware versions.

https://circuits4you.com/2018/01/24/tutorial-on-arduino-watchdog-timer-setup/

https://blog.frogslayer.com/creating-an-arduino-watchdog-timer/

I then ran across another post about a different solution that I have implemented and it seems to be working, but I want to give it some more time just to be sure. More updates to come.

1

Here is the solution. After searching last night I ran across this post https://forum.arduino.cc/index.php?topic=517608.new . Turns out, I was using the Adafruit DHT library and that seems to be what the issue was. I switched over to this https://github.com/markruys/arduino-DHT DHT library and it's been running for some 14 hours now with NO issues. Looking back through the Serial output I don't even see where it returned a NaN, not even once. I'm so excited to have this corrected and hopefully this will help out someone else stuck with this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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