Need help to fix stability issues for personal Atmega8 project

Hello,

I am creating temperature controller for breeding chickens with 6 temperature ranges. Also, by making this a digital solution, I am able to blink out current temperature code with single LED. It’s Atmega8 with Arduino 1.0 software.

Controler has thermoresistor, 6 position (2x6 in fact…) turn switch, and relay for outputting 220V into heat lamp.

Here are schematic and some photos:

Sadly, I have problem that after some time it freezes. Or code runs faster then it shoud. It’s unstable :frowning: .

For example, it runs so fast that yellow (status) led instead of blinking ones per second is “always” on. Meanwhile red “ON” LED, which in current test case should turn on every 2 seconds, blinks very fast (but “off” state is noticeable).

I have minimized program into as simple as this (this is only portion of code, there is no point to compile it…):

int i = 0;
int j = 0;
int x = 0;

void loop() 
{  
  //fake "controll" (relay, RED led) output every two seconds:
  x++;
  if (x == 20)
  {
    digitalWrite(12, 1);  
  }
  if (x == 40)
  {
    digitalWrite(12, 0);
    x = 0;
  }
  
  //yellow "status" blink every second
  i++;
  if (i > 20) i = 0;
  
  if (i < 10)
    digitalWrite(13, 1);
  else
    digitalWrite(13, 0);
    
  //thermostat functionality is disabled for testing purposes
  //controller.loop();
  
  j++;
  if (j == 300)
  {
    //also excluded
    //controller.blinkTemperature(13);  
    j = 0;
  }
  
  delay(100); 
}

…still, it’s the same. Could it be delay() problem? But why? Something with 16MHz resonator?

When it happens? Well, it looks like I have to attach load at 220V output (see schematic - its output of the relay), for example, small ~20W halogen lamp and wait for hours.

If it’s turned on WITHOUT load on 220 output - it does not crash. Well, at least I couldn’t repeat the crash yet… With 220V load it takes - I am not sure - maybe ~4 or more hours to crash.

I’ve added 0.1uF capacitors that was missing in original design to filter out “lightnings” that I thought was the cause of instability, but it didn’t helped…

I am stuck now, and I sincerely hope for your knowledge and experience :slight_smile:

EDIT: I forgot to mention very serious point: sometimes I have to re-upload sketch after crash, or it will not boot up!

Thank you.

You're wise to post a "minimal" sketch, rather than a whole bunch of code. I suspect, though, that you didn't test your reduced code to verify that it demonstrates the unexpected behavior that you're trying to resolve.

Try your own code, and see why I think that.

I noticed problem with my original code, then I have tried this reduced sketch I've posted and it's still the same.

Talkless:
I have tried this reduced sketch I've posted

Really - and the sketch you posted actually compiled and ran, did it? :wink:

PeterH:

Talkless:
I have tried this reduced sketch I've posted

Really - and the sketch you posted actually compiled and ran, did it? :wink:

I have posted loop()-only explicitly because I felt that generic setup() or includes it unnecessary it this story. If that's what you mean...

The codes seems simple can you drop a few parts off and run off internal rather than the xtal? Whats with all delay and x blah bits try setting a time and matching greater than that time.

Your smoothing cap seems far to small would like to see a 2200 uf or 4700 uf cap for smoothing that ripple. Did you turn on brown out detection in the fuses? As what your describing sounds exactly like what happens when it's not on and the power supply gets funky.

In truth, this is great for a first post. You made an effort to make it easier for the rest of us to help you with your code. I'm glad to see that.

Here's why I asked you to try your code:

  • As it is, it doesn't compile under the Arduino IDE, because it has no setup().
  • When I add a reasonable setup(), it runs as expected.

I added this code - a very straightforward setup() - between your declarations and your loop():

void setup() {
  pinMode(13,OUTPUT);
}

The code compiles. The LED on pin 13 flashes on for about one second, off for about one second, and then repeats for as long as I'm willing to watch. When I exchange pin 12 and pin 13, the LED flashes on for about two seconds, off for about two seconds, and so on. That looks like what I'd expect from the code. I can't help you with it, because I can't reproduce the unexpected behavior that you describe. For me, this code seems to work, with minimal, reasonable revisions.

I'm pleased to see that you posted a reduced version of your code. It's much easier to diagnose than a long program, and it makes it much more likely that we'll work on the actual problem with the code rather than spend time examining parts that already work.

To get even better responses, I'll suggest that you test the code that you post, to see that it compiles, and that it reproduces the symptoms that you're trying to fix. Or, at least tell us, "This is a portion of my code. This sample doesn't compile." Or something like that. Otherwise, you're likely to get several responses that say, "This doesn't compile," or, like the one you got from me: "This looks like it works."

silasmoeckel:
The codes seems simple can you drop a few parts off and run off internal rather than the xtal?

Ehm.. how do I use internal xtal?

silasmoeckel:
Your smoothing cap seems far to small would like to see a 2200 uf or 4700 uf cap for smoothing that ripple.

Wow, I thought it's enough. Sadly, I don't have oscilloscope to check it. I will try to add bigger cap then, thanks.

silasmoeckel:
Did you turn on brown out detection in the fuses?

Sorry, I do not follow what you wanted to say.

tmd3:
For me, this code seems to work, with minimal, reasonable revisions.

Well exactly, I showed uber-simple example that it would exclude software bug possibility. It's too simple to cause problem, at least it seems so.

It's something with hardware design, at least I believe that, but I am not experienced enough for resolving it by myself.

Thanks for criticism by the way.

Do you still want to fix it?

tmd3:
Do you still want to fix it?

Well of course, what kind of question is that? :slight_smile:

I’ll try to add bigger capacitor as soon as I get access to my welding equipment, and test again.

I couldn't tell where you were wanting to go from here. It looks like you're now suspecting a power quality issue, rather than a code problem, and you're going to try some hardware changes.

Good luck.

tmd3:
It looks like you're now suspecting a power quality issue, rather than a code problem, and you're going to try some hardware changes.

Yes, and I have to learn about "brown out protection fuses" also.

I've found some new info from this post:

I am using a 9V wall adapter, and apparently what was happening was that when I unplug it, it has a capacitor that gradually loses its charge, so the voltage it supplies to the voltage regulator gradually (meaning many, many milliseconds!) falls to zero. I confirmed in the ATMEGA8 data sheet, and elsewhere, that this can cause the ATMEGA8 to execute instructions incorrectly, and thereby overwrite program memory.

So there are more and more proof for that my power (sometimes) goes down into some critical region... and then bad stuff happens.

Hi Talkless,

Looking at your schematic and I feel sure AGND needs connecting to GND & AVCC needs connecting to 5V.
You don't need the 5V to AREF if your using internal reference voltage for A/D conversion.

Could you please explain the Vin and Vout to the ATmega8. Are you using analogue input to check supply voltage?

Riva:
Could you please explain the Vin and Vout to the ATmega8. Are you using analogue input to check supply voltage?

Earlier I've used not precise 7805 and so I measured Vin, Vout of voltage divider and then calculated thermoresistor value (and so temperature) despite voltage regulators bad precision.

But analog values were wrong. So I changed voltage regulator into LM317 and set it to 5.00V, but it's still wrong.... Then I connected AREF and it started to give good values. I guess all these problems came because I simply didn't connect AGND and AVCC..? :blush:

Vin is obsolete now I guess...

I guess all these problems came because I simply didn't connect AGND and AVCC..?

AVCC & AGND not being connected would most probably have caused that problem.

From the ATmega8 guide

The ADC has a separate analog supply voltage pin, AVCC. AVCC must not differ more than ±0.3V from VCC. See the paragraph “ADC Noise Canceler” on page 195 on how to connect this pin. Internal reference voltages of nominally 2.56V or AVCC are provided On-chip. The voltage reference may be externally decoupled at the AREF pin by a capacitor for better noise performance.

Riva:
From the ATmega8 guide

The ADC has a separate analog supply voltage pin, AVCC. AVCC must not differ more than ±0.3V from VCC. See the paragraph “ADC Noise Canceler” on page 195 on how to connect this pin. Internal reference voltages of nominally 2.56V or AVCC are provided On-chip. The voltage reference may be externally decoupled at the AREF pin by a capacitor for better noise performance.

Thank you. I should really reed entire datasheet…

It also suggest to add LC (100nF + 10uH) circuit for AVCC, but I guess I’ll skip that step since my that tiny board is so filled up already… I have noticed that even on original Uno board analog values wiggle a bit, so I wrote AproximateBuffer<class T,char N> to get average of last N values (software cap? :smiley: ) with price of some delay that is not a problem in current project.

Currently I have only enabled Brownout protection (I’ll add bigger cap later) with higher voltage threshold and yet it works OK.

Electronics is fun, new field with new stuff to learn :slight_smile:

So far the problem persists :~ .

I've connected 4700uF (tried various capacities) cap at my rectifier and also 10uF at Vadj of voltage regulator, as recommended in LM317 datasheet in order not to amplify ripple.

I have enabled brown out protection - program is not corrupting anymore, though delay() still goes wild after some time (after few hours or so): LED blinks at much faster rate then it should.

I have uploaded DSO Nano Quad oscilloscope screenshot with rather strong ripple at the time relay is turned on. It's edge trigger on power supply before regulator (19-16VDC) for about 18V (when it starts to go down).

Ripples goes for about 300uS. Here blue graph shows 5v for the ATmega - it swings from 3.8V up to 6.3V! And it's even before power supply voltage starts to go down noticeably (yellow graph)...

Any ideas on that? Could that be my problem? Circuit has 0.1 ceramic caps already, but do I need some other kind?

Several people have had weird problems that turned out to be caused by crosstalk between cable bundles.
Now the ATmega is wired up correctly (did you update the schematic) it looks like ripple noise may be corrupting the MCU.
Are all your cables neatly bound together or laying next to each other? If so then try separating them and see if this helps.