running program freezes randomly

Hello guys,
i wrote an incubator program which maintain desired athmosphere inside of it. i designed that with 2 Temp. sensor (NTC), one humidity sensor (DHT11) , 4 relay and microsd card module.

i wrote few different version of that because program doesnt reliable. that stuck and freeze randomly. i thought that is because of feeding voltage than i tried 12v, 5v (USB) and both (12v-5v) feed. or problem to read sd card.

i was testing incubator controlling unit (without apply on incubator). that was working good for few days (supported via usb). but that freaks when i apply on incubator. some day i found it stuck when heater on which fried eggs, sometimes stuck when humidifier on that cause %95 to damage DHT sensor.
during this crazy moment (about 2 months) i have made lots of research but couldnt realize what am i doing wrong. i am get tired to check incubator is ok or boiling my eggs.

i would like to know what am i doing wrong! here is my code( sdcard included and simple one without sdcard).

i m looking forward to read your replies.
thanx a lot.

smart one( i think that but it couldnt proof that :slight_smile: kulucka_v3_manuelzamanMEGA_sdcard_kosan.ino

simple one kulucka_v3_manuelzamanMEGA_basit.ino

kulucka_v3_manuelzamanMEGA_basit.ino (25.3 KB)

kulucka_v3_manuelzamanMEGA_sdcard_kosan.ino (34.5 KB)

If it worked on the bench but not when controlling the incubator, it sounds as though you have an electrical issue - perhaps spikes from your relays turning off. How is everything wired up?

Hi unsal,

I tried to compile your ocde but you are using a different LiquidCrystal_I2C.h-library with different methods.
Where does this library come from?
How about using the hd44780.h library?

Pretty long code. it would be very hard for me to analyse your code with variable names written in your native language.
Take a look if there exists a subforum with your native language. Or translate everything to english.
I estimate there are only a few persons who enjoy doing this translation for you.

If you use a terminal-software that can save the received messages into a file on your hardsisk this will enhance your possabilities to search for the bug. In this way you could add a lot of serial debug-output. It doesn't matter if this file grows to a size of 100 Mbyte. If the debug-output does clearly state where the output was sended from you can trace back to the exact point. Of course this needs some analysing how to create debug-output in such a way that finding extreme situations can be done by searching for specific words or expressions and not reading through 10000nds lines of log-output

How about a second arduino that runs a pretty small program that does nothing more than measuring temperature and humidity and can give an alarm and shutoff your heating or humifier if values go beyond allowed limits?

Any professional device that uses a heater has security add-ons to shut down the device in case of a major malfunction
where the main control-unit fails.

If this second arduino is connected to a free IO-pin of your first you could log the status of this IO-pin and in case of an alarm indictated through the IO-pin-status changed this will give hints what is happening.

As a general advice: any code should be developed in a certain manner: Add one thing and then test for all circumstances that can occur and even test for circumstances you don't believe that they can occur.
If some if-conditions do testing for a value-range even test if the values go below or above the expected value-range and add precautions if the values go out of the expected range.

I prefer using onewire-sensors type DS18B20 over NTCs. They are at medium costs (2 Euro per piece) and avoid all the difficulties than can occur with using analog temperature-sensors with EMV. They have an accuracy of 0.5°C.
Though they have their own limits.
You can't use them with very long wires (exceeding 5m) or if you want to you have to add electronic ´s to make the bus-communication reliable.

You have defined some function but there is still pretty much code inside the main loop()
I prefer dividing the code in pretty small functional units. I define a function for each functional thing and this function does only one thing. If there is a second, thrird, forth,... etc. thing it will be defined it its one function.
Then every single function will be tested throroughly even for the craziest sitautions until it is working reliable.
Then and only then I go on with the next small function. In the end this approach safes a lot of time.

Determing the temperature needs a lot of complex calculations but you do no range-checking of the result for plausability. This might hide a bug. But I don't know.

For the use of NTCs I would prefer a table with ADC-values and their corresponding temperature-values over a such a complex calculation. This table hasn't be too big. maybe in 5K-steps. Inbetween a linear approximation will be precise enough. If these NTCs are very inlinear throw them away and replace them with something like a PT1000 or KTY.
As I already wrote I prefer digital onewire-sensors DS18B20.

best regards Stefan

wildbill:
If it worked on the bench but not when controlling the incubator, it sounds as though you have an electrical issue - perhaps spikes from your relays turning off. How is everything wired up?

wildbill:
If it worked on the bench but not when controlling the incubator, it sounds as though you have an electrical issue - perhaps spikes from your relays turning off. How is everything wired up?

First of all, Thank you very much Wildbill to share your idea . i am thinking same too. because everything was fine when i plug arduino via usb (phone charger), but lcd screen fade a bit when relay triggered. i thought that lack of energy (couse relay started to consume energy). even all, "control unit" worked fine for 3 days. it didnt freezed. i didnt see any problematic clue on log file too. than i thought , "maybe the consume of current increase when i plug on incubator" then i feed relay via external 5v, and supply arduino on both way at the same time (via 12v and 5v usb). that time there werent any blink on screen but that stuck again. i thing the main reason relay. when it triggered (for ex. rotator works every 2 hrs. mean 7200 sec. system frozen few times when counter 7199 sec. if not freeze at that time arduino freaked out and created meaningles counter time ,7200 became million something)
so, i am attaching circuit diagram and a log file.

thnx again

unsal1309:
First of all, Thank you very much Wildbill to share your idea . i am thinking same too. because everything was fine when i plug arduino via usb (phone charger), but lcd screen fade a bit when relay triggered. i thought that lack of energy (couse relay started to consume energy). even all, "control unit" worked fine for 3 days. it didnt freezed. i didnt see any problematic clue on log file too. than i thought , "maybe the consume of current increase when i plug on incubator" then i feed relay via external 5v, and supply arduino on both way at the same time (via 12v and 5v usb). that time there werent any blink on screen but that stuck again. i thing the main reason relay. when it triggered (for ex. rotator works every 2 hrs. mean 7200 sec. system frozen few times when counter 7199 sec. if not freeze at that time arduino freaked out and created meaningles counter time ,7200 became million something)
so, i am attaching circuit diagram and a log file.

thnx again

StefanL38:
Hi unsal,

I tried to compile your ocde but you are using a different LiquidCrystal_I2C.h-library with different methods.
Where does this library come from?
How about using the hd44780.h library?

Pretty long code. it would be very hard for me to analyse your code with variable names written in your native language.
Take a look if there exists a subforum with your native language. Or translate everything to english.
I estimate there are only a few persons who enjoy doing this translation for you.

If you use a terminal-software that can save the received messages into a file on your hardsisk this will enhance your possabilities to search for the bug. In this way you could add a lot of serial debug-output. It doesn't matter if this file grows to a size of 100 Mbyte. If the debug-output does clearly state where the output was sended from you can trace back to the exact point. Of course this needs some analysing how to create debug-output in such a way that finding extreme situations can be done by searching for specific words or expressions and not reading through 10000nds lines of log-output

How about a second arduino that runs a pretty small program that does nothing more than measuring temperature and humidity and can give an alarm and shutoff your heating or humifier if values go beyond allowed limits?

Any professional device that uses a heater has security add-ons to shut down the device in case of a major malfunction
where the main control-unit fails.

If this second arduino is connected to a free IO-pin of your first you could log the status of this IO-pin and in case of an alarm indictated through the IO-pin-status changed this will give hints what is happening.

As a general advice: any code should be developed in a certain manner: Add one thing and then test for all circumstances that can occur and even test for circumstances you don't believe that they can occur.
If some if-conditions do testing for a value-range even test if the values go below or above the expected value-range and add precautions if the values go out of the expected range.

I prefer using onewire-sensors type DS18B20 over NTCs. They are at medium costs (2 Euro per piece) and avoid all the difficulties than can occur with using analog temperature-sensors with EMV. They have an accuracy of 0.5°C.
Though they have their own limits.
You can't use them with very long wires (exceeding 5m) or if you want to you have to add electronic ´s to make the bus-communication reliable.

You have defined some function but there is still pretty much code inside the main loop()
I prefer dividing the code in pretty small functional units. I define a function for each functional thing and this function does only one thing. If there is a second, thrird, forth,... etc. thing it will be defined it its one function.
Then every single function will be tested throroughly even for the craziest sitautions until it is working reliable.
Then and only then I go on with the next small function. In the end this approach safes a lot of time.

Determing the temperature needs a lot of complex calculations but you do no range-checking of the result for plausability. This might hide a bug. But I don't know.

For the use of NTCs I would prefer a table with ADC-values and their corresponding temperature-values over a such a complex calculation. This table hasn't be too big. maybe in 5K-steps. Inbetween a linear approximation will be precise enough. If these NTCs are very inlinear throw them away and replace them with something like a PT1000 or KTY.
As I already wrote I prefer digital onewire-sensors DS18B20.

best regards Stefan

Thank you Stefan,
i tested code at home before install on incubator. everything was fine for 3 days. than i decited to apply on it. but that freeze sometimes few mins later, maybe an hour. i guess relay send back some hidden voltage to couse problem. i have attached few log , basic code (converted descriptions in english) and lcd library which i used.

thanks again.

LOGGER.TXT (961 KB)

LOGGER.TXT (961 KB)

kulucka_v3_manuelzamanMEGA_basit.ino (25.8 KB)

lcd library is Newliquidcrystal_1.3.5
i can not load here because of 2mb limit :frowning:

log1.txt (491 KB)

log2.txt (961 KB)

unsal1309:
lcd library is Newliquidcrystal_1.3.5
i can not load here because of 2mb limit :frowning:

If there is a clean web host, then just provide a link using link tags.

i am attaching circuit diagram

I don't see it.

Your log shows some temperaturevalues.
This is of almost no use for a deep analysis.
For a deep analysis you would have to take time to add debug-output every few lines of code
so that the moment of freezing canbe narrowed down.
Each debugout needs his own number to be really sure where the output came from.

If this freezing occurs at randomly times I have two ideas what might cause the problems:

Array-boundary outside allowed numbers.
Do a very careful checking of the array-boundaries

This liquidChristal-library claims to be faster than the original
I would change back to the standard-library

electrical problems: Do the relay-boards include freewheeling diodes to prtect against inductive overvoltages?
With some very cheap chinese products I had big quality problems. So I don't use them anymore.

completely isolating the Arduino elecronics from the rest through using optocouplers.
signaltransmission with optocopuplers is based on light. So even the input for the relais is electrically isolated against the arduino.

You have to provide much more information about your hardware
What Arduino-board are you using?
What powersupply are you using?
How is everything wired? Provide a schematic and or very detailed pictures of the wiring. The pictures must exactly and easy to see all IO-Pin-numbers on both ends of each wire

To narrow down the problem disconnect all relais, heaters etc. connect only one thing
then test. If it runs OK add next thing test etc. etc. etc.

best regards Stefan

StefanL38:
Your log shows some temperaturevalues.
This is of almost no use for a deep analysis.
For a deep analysis you would have to take time to add debug-output every few lines of code
so that the moment of freezing canbe narrowed down.
Each debugout needs his own number to be really sure where the output came from.

If this freezing occurs at randomly times I have two ideas what might cause the problems:

Array-boundary outside allowed numbers.
Do a very careful checking of the array-boundaries

This liquidChristal-library claims to be faster than the original
I would change back to the standard-library

electrical problems: Do the relay-boards include freewheeling diodes to prtect against inductive overvoltages?
With some very cheap chinese products I had big quality problems. So I don't use them anymore.

completely isolating the Arduino elecronics from the rest through using optocouplers.
signaltransmission with optocopuplers is based on light. So even the input for the relais is electrically isolated against the arduino.

You have to provide much more information about your hardware
What Arduino-board are you using?
What powersupply are you using?
How is everything wired? Provide a schematic and or very detailed pictures of the wiring. The pictures must exactly and easy to see all IO-Pin-numbers on both ends of each wire

To narrow down the problem disconnect all relais, heaters etc. connect only one thing
then test. If it runs OK add next thing test etc. etc. etc.

best regards Stefan

Hi Stefan,
i have no idea relay has freewheeling diodes or not. i am feeding whole system via 5v 1a standard phone charger. now i am editing code to analize part by part. thanx

i m using this relay. https://www.amazon.com/JBtek-Channel-Module-Arduino-Raspberry/dp/B00KTEN3TM
Arduino model - Arduino Mega 2560 R3
library Newliquidcrystal_1.3.5 WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

i m using this relay. https://www.amazon.com/JBtek-Channel-Module-Arduino-Raspberry/dp/B00KTEN3TM

How is it wired?

If you want complete optical isolation, connect "Vcc" to Arduino +5 volts but do NOT connect Arduino Ground. Remove the Vcc to JD-Vcc jumper. Connect a separate +5 supply to "JD-Vcc" and board Gnd. This will supply power to the transistor drivers and relay coils.

See the "optical isolation" section of
http://arduinoinfo.mywikis.net/wiki/ArduinoPower#OI

so we can see

What is that bizarre circuit on A14 and A15? It makes no sense.

aarg:
so we can see

What is that bizarre circuit on A14 and A15? It makes no sense.

those are NTC (termistor) connection as temp sensor . 5K NTC with 5K resistor.

cattledog:
How is it wired?

If you want complete optical isolation, connect "Vcc" to Arduino +5 volts but do NOT connect Arduino Ground. Remove the Vcc to JD-Vcc jumper. Connect a separate +5 supply to "JD-Vcc" and board Gnd. This will supply power to the transistor drivers and relay coils.

See the "optical isolation" section of
ArduinoPower - ArduinoInfo

Thank you, i have tried this method but i used ardunio's GND . i see that i was wrong. i m going to do that on right way.

Your voltage-dividers NTC with resistor are highly unlinear. It might be that at lower or higher temperatures the change of your analogRead becomes very small or with all your calculations with log etc ends up on strange values

Did you ever check your NTC sensors what results your calculations inside the code for highest and lowest possible temperatures give as result?

That's an important point to check

I suggest replacing these NTCs with DS18B20-sensors.
They have a digital interface called onewire
Three Pins one data-pin connected to an IO-Pin of the arduino GND and Vcc

there are ready to use libraries for DS18B20 sensors

best regards Stefan

Hi all can I make a suggestion. Based on the 3D Filament drier I made I was getting corrupted LCD displays and OLED displays and sometimes total freezing of the arduino. I was using a 60W filament bulb as heater and strongly suspected the relay module for interference. However after placing several diodes and a capacitor to try to clean the voltages I found that the interference was being picked up by the combined Humidity and Temperature sensor module. Once fitted with a ferrite core over the wires I no longer have this problem. Hope it helps.