1

I want to display the DHT22 temperature and humidity sensor in a 3 digit 7 segments display. It's always showing the sensor value and after 2 or 3 sec the display turns off, and then the display turns on, and so on.
I need help to understand where is the mistake on the code to make the display always turned ON.

#include <SevSeg.h>
#include <EEPROM.h>
#include "DHT.h"
#define COMMON_ANODE 0
#define DHTPIN A0     // what pin we're connected to

#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value
SevSeg sevseg;

void setup(){

byte numDigits = 3;
byte digitPins[] = {1,2,3};
byte segmentPins[] = {4,5,6,7,8,9,10,11};
sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
 dht.begin();
}

void loop(){
   temp= dht.readTemperature();

   sevseg.setNumber(temp,1);
   sevseg.refreshDisplay();
}  
1

A good way to avoid display flicker is to only update the display when needed. The display update is what causes it to flicker. If you change your main loop as below, the display will only update when needed (when the current reading is different than the last reading). It's probably also a good idea to only read the temperature every 2 seconds or so which can be controlled with a simple delay().

float temp, last_temp = 0;

void loop() {
    temp = dht.readTemperature();
    if (temp != last_temp)
    {
        sevseg.setNumber(temp,1);
        sevseg.refreshDisplay();
    }
    last_temp = temp;
    delay(2000);  
}
0

You have a multiplexed display. Multiplexed displays have to be constantly updated, or they display the last displayed digit (if there was one) for an extended period.

The reading from the DHT will take time. That time will interrupt the display updates causing it to stop for a while as it reads.

Why does it do it every few seconds? All I can suggest is that the DHT library you are using limits the actual readings to every few seconds - and between those times it just returns the previously stored value.

To get around it you should consider doing your display updates from within a timer interrupt (assuming your DHT library doesn't disable interrupts).

  • Many thanks Majenko, can you send a sample code to solved – nawfal ahmed Sep 25 '18 at 18:51
  • many thanks,i would like to share my idea which i try it this week with all my friends, i just repair DHT22 library and decrease the delay time in, and then the 7 segments blinking every 2 seconds will disappear . – nawfal ahmed Oct 8 '18 at 16:34

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.