ATmega8 struck after few loops

I have done small incubator based on below program. My display always get struck with temp and humidity value after few times execution of while loop.

Means first few minutest it is displaying “Hatching…” after few seconds it displays “temp:xx.xx c humi: xx.xx %”. After sometime LCD keep displaying only temp and humidity (no Hatching word coming in my lcd and also temperature not maintained in my incubator box). Microcontroller is not getting loop after few loops.

Attached the DHT11 library which I have used for this and attached pics of my circuit.

My observations -

  1. Whenever it struck the temperature is always greater than 37.50
  2. After hard reset, means switching off and on the power button, microcontroller works as expected for next few loops.

Please help me on this issue. :confused:

Note : I have edited my F_CPU value as below. Uploading my program by Arduino IDE by setting 8Mhz internal clock speed. Used USBasp as a programmer.

const int DHTLIB_TIMEOUT = (F_CPU/16 * 40000);
#include <dht.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define DHTPIN 8 // PB0 pin in atmega8
dht DHT;
LiquidCrystal_I2C lcd(0x27, 16, 2);  // 0x27 0x3F

int ledPin = 9; 

void setup() {
  pinMode(ledPin, OUTPUT);

  // initialize the LCD


void loop() {
  int chk = DHT.read11(DHTPIN); //This wake up the sensor and read the values.

  switch (chk)
    case DHTLIB_OK:
        float t = DHT.temperature;
        float h = DHT.humidity;

        lcd.setCursor(0, 0);
        lcd.print(" C");
        lcd.setCursor(0, 1);
        lcd.print(" %");

        if (t >= 37.50)  {
          digitalWrite(ledPin, LOW);
          digitalWrite(ledPin, HIGH);
        lcd.setCursor(0, 1);
        lcd.print("Checksum Error");
        lcd.setCursor(0, 1);
        lcd.setCursor(0, 1);

dht.cpp (4.55 KB)

dht.h (1.94 KB)

I am new to electronics. I got struck in this issue. Kindly show some light on this?

Any help much appreciated.

Don't know if the library is faulty so that's another option. But from


  1. Whenever it struck the temperature is always greater than 37.50

and the fact I don't see a led but I do see a relay (why on earth is it called a led then :wink: ) I just assume you trash the power.

  1. so what is power the stuff?
  2. And why don't I see a capacitor across the power?

PS, with all the cheap Pro Mini's and Nano's it's just not worth it to mes with stand alone ATmega's on a breadboard...

Thanks Septillian for your reply.

Apologies for code written as led. It is a bulb connected with my relay.

Breadboard sourced up by 5v using adapter. It was tested by multimeter. My bulb was powered by required volt( my case it is 240 volt).

Where do you want me to plug the capacitor. Please let me know pF value as well pls. I am very new to electronics.

As per the datasheet, 100nF as close as possible to the IC.

And what adapter? Do you also call up the garage without telling what make and model you have? :wink:

But maybe you need a bit more decoupling for the Arduino PSU. But that's a bit hard when you just have a single 5V supply.

Once again thanks for your reply. I have only 22nF 25v electrolytic keltron capacitor handy. Will buy 100nF on tomorrow, Since here 10.45PM :slight_smile:

I have two questions.

  1. Can I use 22nF 25V electrolytic capcitor for decoupling ?

  2. Kindly correct me on connections
    AREF need to connect with 100nF(Close to AVR) and other end need to connect with Gnd. Rest all connections same. Kindly correct me on this. Attached the pics of my capacitor connection.

After going though few forums and documentations I have understood that, decoupling capacitor will help to decouple the AVR from 5V power supply to avoid the noise in AVR.

Apologies for not being more clear on Adapter. I have used 5v DC500mA adapter to device which powers up in breadboard. The device is in picture which I have bought in local shops. This device power up 5 v to breadboard which I have tested in the multi-meter.


Buy 100nF ceramics, like a ton :smiley: They are the most universal cap ever and every IC likes one. On a breadboard, I usually place it directly on top of the IC.

Aref, I don’t know from the top of my head… Do you use analogRead()? Otherwise it doesn’t matter.

And is the adapter connected to that YwRobot board also 5V? That is to little for that board! It needs 6,5V at least. If you just have a 5V adapter, connect it to the ATmega etc directly.

And for better decoupling you can supply power to the ATmega via a (Schottky) diode and add (besides the 100nF you should always place) a >=100uF cap. This drops the voltage to the Arduino slightly but not a problem in most cases.

Don’t supply the relay via the diode. The diode decouples the supply from the ATmega because the relay can’t drain the cap because of the diode. I would supply the LCD via the diode as well (aka, same power as ATmega) so it’s decoupled from the relay as well. The LCD can crash as well :wink: