Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Something wrong with my circuit

Status
Not open for further replies.

Chris_P

New Member
I have designed and built this circuit myself, with help from various people and I thought it was all going well until I was running some long tests and found a problem. Below is my circuit, please excuse my bad design as I learnt how to use Eagle as I did this and it probably isn't drawn the way it should be.

I have a transformer supplying 12.6VAC connected to IN_PW1 and IN_PW2.
**broken link removed**
Basically I have an Atmega16 with an HD44780 compatible LCD screen. On the diagram it just shows a connector to the LCD screen. The backlight on the LCD is controlled by the AVR. I have 2 momentary switches that are connected to interrupt pins on the AVR. I have an LDR for measuring light. And I have a 2N2222A transistor switching a 240V relay with a 12v coil. There is also an external oscillator to give me accurate timing. The switches are used to set an On Time and an Off Time and then it just switches the relay on and off continually from that point. The time is shown counting down on the LCD screen. The On Time and Off Time are held in EEPROM too. What I have found is that after running it for quite a few hours of running very short cycles, 5 seconds on and 10 seconds off, the LCD screen was blank and the relay was stuck on. The AVR did not register any switches either, like it has frozen. When I turn off the power and turn it back on it starts again OK, but some time later it freezes again. I am switching a fan in my tests.

I know my circuit can probably be designed much better than my attempts but I was wondering if anyone can see any obvious faults in the design that could cause the Atmega16 to freeze after running OK for quite a few hours?
 
Last edited:
isn't there a reset circuitry to be hocked to the RST pin...? (i am not sure... but this may be the problem)
 
The RST line should be hooked to VCC. If you use ISP, then it should have a 4k7 resistor, otherwise it's OK to just hardwire it.
 
Such a simple solution, I should never have ignored the error that Eagle gave me. Because it worked as it was I thought I didn't need to do anything with the reset. Wonder why it worked for hours before locking up? I have connected the RST to VCC and testing it again. Thanks :D
 
Last edited:
Reset lines usually have decoupling caps on them as well. I don't know of many applications where you need to trigger reset very fast so using a large capacitor will prevent noise from triggering resets
 
Mega AVR series already have internal pullup resistor on the RESET pin, so adding external reset resistor serve no good purpose.

On recent AVRs, the RESET pin is used also as "DebugWire" to communicate with the host software for debugging purposes and this preclude any connection of capacitor on this pin.

The OP's problem is most certainly not caused by "floating" reset pin, but more like software problem. Have the watchdog timer disabled or configured correctly? Is the main program looping correctly?

On most LCD modules I have used, the backlight LED(s) would draw more current(>100mA) than the AVR port pin can comfortably source/sink so it is better to use a transistor as a buffer.
 
eblc1388 said:
Mega AVR series already have internal pullup resistor on the RESET pin, so adding external reset resistor serve no good purpose.

On recent AVRs, the RESET pin is used also as "DebugWire" to communicate with the host software for debugging purposes and this preclude any connection of capacitor on this pin.

The OP's problem is most certainly not caused by "floating" reset pin, but more like software problem. Have the watchdog timer disabled or configured correctly? Is the main program looping correctly?

On most LCD modules I have used, the backlight LED(s) would draw more current(>100mA) than the AVR port pin can comfortably source/sink so it is better to use a transistor as a buffer.

Thanks for your advice. I was worried it may be a software problem. I have had no training at all in programing and have pieced the code together myself, so I am rather embarrassed about how poor most of my coding is. My code is very long so I will try and post the parts relevant to the timer here and hopefully you may see some obvious problem.

I don't think I have done anything with the Watchdog timer, will this cause a problem?

PHP:
// Defines
#define STOP_TIMER TCCR0 &= ~((1<<CS02) | (1<<CS01) | (1<<CS00))
// timer prescaler of 256
#define START_TIMER TCCR0 |= (1<<CS02)

// Timer 0 Overflow Interrupt handler, counts seconds
ISR(TIMER0_OVF_vect)
{
        // 256 ticks have gone by
         Ticks256++;

         // 1 second per 150 counts with timer prescaler of 256 and clock of 1
        if (Ticks256 == 150)
         {
                Ticks256 = 0;
                count_seconds--;
                // wait for run out
                if (count_seconds == 0)
                {
                      // my counting down and switching on and off happens here
                }
        }
}

// Main function
int main(void)
{
        // Configure device
        cli(); // disable interrupts just in case

         TCNT0 = 0x00;   // clear Timer/Counter
         START_TIMER;

         // enable interrupts on timer 0 overflow
         TIMSK  |= _BV(TOIE0);

        // Set MCU control register
        MCUCR |= (1<<ISC11) | (0<<ISC10) | (1<<ISC01) | (0<<ISC00);

         sei(); // enable interrupts

        // Loop forever; the interrupts will take it from here
        while(1)
        {
        }
}
With regards to the backlight, I will look at including a transistor in my circuit. What damage could be caused by me using the AVR port to sink the current? Will I damage the AVR?
 
Last edited:
Chris_P said:
Thanks for your advice. I was worried it may be a software problem. I have had no training at all in programing and have pieced the code together myself, so I am rather embarrassed about how poor most of my coding is. My code is very long so I will try and post the parts relevant to the timer here and hopefully you may see some obvious problem.

I don't think I have done anything with the Watchdog timer, will this cause a problem?

PHP:
// Defines
#define STOP_TIMER TCCR0 &= ~((1<<CS02) | (1<<CS01) | (1<<CS00))
// timer prescaler of 256
#define START_TIMER TCCR0 |= (1<<CS02)

// Timer 0 Overflow Interrupt handler, counts seconds
ISR(TIMER0_OVF_vect)
{
        // 256 ticks have gone by
         Ticks256++;

         // 1 second per 150 counts with timer prescaler of 256 and clock of 1
        if (Ticks256 == 150)
         {
                Ticks256 = 0;
                count_seconds--;
                // wait for run out
                if (count_seconds == 0)
                {
                      // my counting down and switching on and off happens here
                }
        }
}

// Main function
int main(void)
{
        // Configure device
        cli(); // disable interrupts just in case

         TCNT0 = 0x00;   // clear Timer/Counter
         START_TIMER;

         // enable interrupts on timer 0 overflow
         TIMSK  |= _BV(TOIE0);

        // Set MCU control register
        MCUCR |= (1<<ISC11) | (0<<ISC10) | (1<<ISC01) | (0<<ISC00);

         sei(); // enable interrupts

        // Loop forever; the interrupts will take it from here
        while(1)
        {
        }
}
With regards to the backlight, I will look at including a transistor in my circuit. What damage could be caused by me using the AVR port to sink the current? Will I damage the AVR?

it depends on the size of the LCD.. i've been making this on a 2 line LCD, Bright White backlight, w/out any problems
 
Chris_P said:
My code is very long so I will try and post the parts relevant to the timer here and hopefully you may see some obvious problem.

I can't see anything abnormal in the code posted but it seems you have placed all the timing values testing and handling inside the interrupt routine.

I would advise to try to place the testing of timing variable inside the main routine "while loop" so that the only job of the interrupt is to decrement the timing count variable. Wouldn't it be better to decrement a single variable which represent the total time required inside the interrupt handling routine, instead of working on two variables. Only in case of ASM programming, you'll need to do just that because of the 256 size limitation on each entity. In C, your variable can have very large value.

Chris_P said:
I don't think I have done anything with the Watchdog timer, will this cause a problem?

The watchdog timer is most probably disabled but you'll need to double check your compiler documentation.

If your present program can work for hours but sometime failed, it is possible that the AC load that you are switching has somehow induced interference to the AVR. Try driving the relay without any AC load to see if the program runs properly.

@ikalogic:
The AVR port pin can source/sink 20~25mA and if the LCD backlight required more current than that, then a transistor is needed. What is the nominal backlight LED current stated in that particular LCD datasheet?
 
eblc1388 said:
I can't see anything abnormal in the code posted but it seems you have placed all the timing values testing and handling inside the interrupt routine.

I would advise to try to place the testing of timing variable inside the main routine "while loop" so that the only job of the interrupt is to decrement the timing count variable. Wouldn't it be better to decrement a single variable which represent the total time required inside the interrupt handling routine, instead of working on two variables. Only in case of ASM programming, you'll need to do just that because of the 256 size limitation on each entity. In C, your variable can have very large value.
I'll do as you suggest. I am self taught and don't really know the best ways to do a lot of these things.

eblc1388 said:
The watchdog timer is most probably disabled but you'll need to double check your compiler documentation.
I put in a bit of extra code to disable it anyway.
eblc1388 said:
If your present program can work for hours but sometime failed, it is possible that the AC load that you are switching has somehow induced interference to the AVR. Try driving the relay without any AC load to see if the program runs properly.
I have a feeling this is probably the problem. A friend of mine first had the problem when he was switching a solenoid. I got the problem switching a fan, but I just ran it for about 20 hours switching a low powered light with no problem. I have probably put components in bad places. I tried to minimise the board size and just put things wherever I could fit them. This is my board at the moment. I was thinking of putting an MOV on the main supply to the transformer and the relay. Would this help? Is there something else I can do to minimise interference on this board? I really appreciate the help with this.
**broken link removed**
 
If this is the first time you have tried to actually layout a PCB, I must say you have done surprisingly well. There are some minor suggestions for you to consider.

1. the 0V trace is too thin and too little and it is not good for overall stability. Usually the 0V common occupied quite some space on the board and they also acts as ground plane. Your AVR can go up to 16MHz clock and it is in effect an RF circuit.

2. one of the AC connections from the relay is too close to the AVR pin (IDC connector pin16) and electrical leakage could be a problem. I would suggest you rotate the 7805 counterclockwise to get some board space and move the AVR and LCD IDC connector up a bit so that you have larger separation between AC connection and the IDC pin16. Since you are using both PCB sides for load current carrying, the trace width can also be made a tiny bit smaller to help separation.

3. the rectified 12.6V AC would give some 17.6V DC peak voltage to the 7805 input and also the relay coil. This might be too high for the relay. Measure actual voltage and check relay spec.

4. the addition of a MOV might or might not help and I cannot say for sure. It would not hurt for sure.

5. if switching inductive load cause problems, then you would need to look into better electrical shielding using metal enclosure or in extreme cases optical isolation between relay circuit and AVR.

6. look into unused AVR pins and make sure that they are configured as OUTPUT if possible. Usually I would set them to HIGH. In your case, an extra 10K to VCC and 0.1uF capacitor to 0V on the AVR reset pin might also help.
 
Mega AVR series already have internal pullup resistor on the RESET pin, so adding external reset resistor serve no good purpose.
In a noisy environment, 'helping' the weak internal pullup has often been recommended. I recommend it. It certainly does no harm.
 
ikalogic said:
the ATMEGA16L-8PU only goes to 8 Mhz....
I am using ATMEGA16-16PU being clocked by a 9.8304Mhz oscillator.

I am also looking at all of the points eblc1388 made and am going to change the whole design. Pity I already had a bundle of pcb's made.
 
Thought I'd post about my progress, maybe someone else will learn something from my mistakes too. I stupidly built quite a few of these before I discovered the problem so I am attempting to fix these ones without having to rebuild them completely.

eblc1388 said:
If this is the first time you have tried to actually layout a PCB, I must say you have done surprisingly well. There are some minor suggestions for you to consider.

1. the 0V trace is too thin and too little and it is not good for overall stability. Usually the 0V common occupied quite some space on the board and they also acts as ground plane. Your AVR can go up to 16MHz clock and it is in effect an RF circuit.
Don't think there is much I can do about that on my existing boards but I will widen it on my new ones.

eblc1388 said:
2. one of the AC connections from the relay is too close to the AVR pin (IDC connector pin16) and electrical leakage could be a problem. I would suggest you rotate the 7805 counterclockwise to get some board space and move the AVR and LCD IDC connector up a bit so that you have larger separation between AC connection and the IDC pin16. Since you are using both PCB sides for load current carrying, the trace width can also be made a tiny bit smaller to help separation.
As eblc1388 suggested in a PM I cut this trace as close to the relay as I could and soldered the wire directly to the relay.

eblc1388 said:
3. the rectified 12.6V AC would give some 17.6V DC peak voltage to the 7805 input and also the relay coil. This might be too high for the relay. Measure actual voltage and check relay spec.
I found the peak DC voltage was high but when the relay was switched it dropped a bit to be just inside the specs of the relay. So hopefully this doesn't cause a problem. I will use a zener diode on my next design.

eblc1388 said:
4. the addition of a MOV might or might not help and I cannot say for sure. It would not hurt for sure.
I added one to the main supply just to be safe.

eblc1388 said:
5. if switching inductive load cause problems, then you would need to look into better electrical shielding using metal enclosure or in extreme cases optical isolation between relay circuit and AVR.
I am pretty sure it is switching inductive loads that is causing the problem. It works fine when switching no load or very small loads but I need it to be able to switch anything that can be plugged into a 10A 240V socket including AC units, pump motors etc.

eblc1388 said:
6. look into unused AVR pins and make sure that they are configured as OUTPUT if possible. Usually I would set them to HIGH. In your case, an extra 10K to VCC and 0.1uF capacitor to 0V on the AVR reset pin might also help.
I made changes to the code and configured all unused pins as OUTPUT and set them HIGH. On the existing boards I couldn't find a way of putting a resistor to VCC but I have soldered a 0.1uF cap from the reset pin to 0V.

A couple are being trialed again now on high loads. Fingers crossed.
 
I need to get some new boards made up just in case my quick fixes don't work so I thought I'd post my changes and see if anyone can find fault, BEFORE I go and have them made.

I have not tested any of these changes on a breadboard yet so I may have made some mistakes. In the schematic I made these changes:-
1/ I put in a 2N2222A transistor to switch the LCD backlight on and off.
2/ On the AVR reset pin I put a 10K resistor to VCC and a 0.1uf Cap to 0V.
3/ I put a 1W 12V zener diode in the unregulated 12V DC supply to switch the relay. Not sure if I put this around the right way.
4/ I took the relay completely off the board and have put a 4N28 optoisolator instead. I have some of these already so I might as well use them. Not sure about my wiring of these though. Yet to test it. EDIT: I had to add a transistor to get it working.

If anyone can point out any faults in these changes or anything else I would appreciate it.
**broken link removed**

On the pcb I have implemented the changes from the schematic. I have also doubled the width of the 0V trace around most of the board. I put the 4n28 and diodes as far away from the AVR as I could. How does that look?
**broken link removed**
 
Last edited:
Chris_P said:
Thought I'd post about my progress, maybe someone else will learn something from my mistakes too. I stupidly built quite a few of these before I discovered the problem so I am attempting to fix these ones without having to rebuild them completely.


Don't think there is much I can do about that on my existing boards but I will widen it on my new ones.


As eblc1388 suggested in a PM I cut this trace as close to the relay as I could and soldered the wire directly to the relay.


I found the peak DC voltage was high but when the relay was switched it dropped a bit to be just inside the specs of the relay. So hopefully this doesn't cause a problem. I will use a zener diode on my next design.


I added one to the main supply just to be safe.


I am pretty sure it is switching inductive loads that is causing the problem. It works fine when switching no load or very small loads but I need it to be able to switch anything that can be plugged into a 10A 240V socket including AC units, pump motors etc.


I made changes to the code and configured all unused pins as OUTPUT and set them HIGH. On the existing boards I couldn't find a way of putting a resistor to VCC but I have soldered a 0.1uF cap from the reset pin to 0V.

A couple are being trialed again now on high loads. Fingers crossed.

wish all members were like you :) thx
 
Well I have had some advice about my schematic, and a list of mistakes I have possibly made. One was about the opto having a common ground with the rest of the circuit and effectively bypassing the isolation. Do I need to have 2 completely seperate power supplies to use an optoisolator properly?
 
Chris_P said:
Well I have had some advice about my schematic, and a list of mistakes I have possibly made. One was about the opto having a common ground with the rest of the circuit and effectively bypassing the isolation. Do I need to have 2 completely seperate power supplies to use an optoisolator properly?

It depends what you're using it for, if it's for mains isolation then you obviously mustn't have anything connected across the isolation barrier.
 
Chris_P said:
I need to get some new boards made up just in case my quick fixes don't work so I thought I'd post my changes and see if anyone can find fault, BEFORE I go and have them made.

You should test out your new design on breadboard before committing for new PCB fabrication. They cost quite a lot of money for a few samples.

Chris_P said:
1/ I put in a 2N2222A transistor to switch the LCD backlight on and off.

In so doing you have forgotten to include the current limiting resistor for the LCD backlight. The resistor value= (5-LED_V) / backlight_current, where LED_V & current depends on color of backlight and is given by LCD datasheet.

Chris_P said:
3/ I put a 1W 12V zener diode in the unregulated 12V DC supply to switch the relay. Not sure if I put this around the right way.

You have the zener diode connected backwards in your schematic. Why 12V so high? You would only need to drop about 6V.

Chris_P said:
4/ I took the relay completely off the board and have put a 4N28 optoisolator instead. I have some of these already so I might as well use them. Not sure about my wiring of these though. Yet to test it. EDIT: I had to add a transistor to get it working.

I would have advised you to use an optical TRIAC MOS3020 to drive a 240V coil AC relay directly if you want the go the full optical route. Doing it your way with 4N28 does not give much advantage against interference.

You have to try out the new design to see how it performs with inductive loads.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top