AVR freezing and reseting due to motor (maybe)

Thread Starter

allahjane

Joined Sep 19, 2012
75
Hi there

I have an atmega8L-pu and a L293D motor driver connected to it. Both avr and motor driver use same power source (a 9v batt with 5v 7805 regulator)

also I have a LED that glows for 1 sec to denote a power on and then flashes to show power is available

program logic similar to a predefined path runner (not a problem)

So the problem is the Circuit runs correct and pins get high and low according to program logic but, as soon as I connect motors to the L293D motor driver the avr hangs after running correctly few seconds and resets (As shown by LED).

Then it restarts runs motor correctly for few sec then again hangs and restarts.. the cycle goes on repeating, there is no definite time between resets

I call it a HANG because the PORTS freeze to their output for a while before restarting

If I don't connect the motors to the driver and leaves the driver still connected and powered without VC(pin 8)connected then its fine but as soon a motor is connect the problem ocurrs

what could be the problem? an EM disturbance ? or power surge?
 

MrChips

Joined Oct 2, 2009
30,806
Most likely your power supply to the MCU is not filtered properly.

Here are some steps you can take in order of importance.

1) Put 0.1μF capacitor between Vcc and GND as close as possible to the pins of the MCU.

2) Put 10μF electrolytic capacitor between Vcc and GND feeding the MCU.

3) Isolate the Vcc feeding the MCU from the rest of the circuitry with a 100Ω resistor.

4) Put a large capacitor, about 4700μF electrolytic capacitor on the power feeding the motor.
 

kubeek

Joined Sep 20, 2005
5,795
Maybe just the feeble 9V battery gets too much load and the voltage sags under the brownout threshold? What MrChips said could cover that, but you might think about using some more powerful power supply.
 

ErnieM

Joined Apr 24, 2011
8,377
First thing to check is a "power surge" as suggested. I assume you don't have a scope or I'd say tie one on and see if you can capture any power supply issues.

Do you have anything beefier then a battery to try this?
 

BillO

Joined Nov 24, 2008
999
There are very, very few motors out there that could be adequately run using a tiny 9V rectangular battery. That might be your main problem. Just the Arduino and shield together are probably on the verge of being too much for it. What is the DC resistance of the motors and how many are you using?

As Mr. Chips says, the best way to run motors is off a separate supply from the controller, unless adequate capacity and filtering are used.

If I don't connect the motors to the driver and leaves the driver still connected and powered without VC(pin 8)connected then its fine but as soon a motor is connect the problem ocurrs
 

Thread Starter

allahjane

Joined Sep 19, 2012
75
Ok I have replaced the 9v rectangular battery with a 12v drycell battery pack (10x1.2 v cells) also I am now using bigger geared motors (due to requirements) which is rated at 12v 200mA

Now the problem is less frequent but it still exists , I will attach the caps as well like the user above mentioned..
please note the freeze state where all port pins freeze their state (1 or 0) for few secs


also, I don't think this is a power surge since the battery is powerful enough and motors wont slow down a single bit. Also adding caps may not solve the problem completely as it ocurrs even when motors are on in single direction constantly so feedback EMF doesn't seem to be a problem (corect me if I'm wrong)

Finally the logic is Interrupt driven so stack overflow (interrupts getting interrupted until memory full) may be a problem. But I would never rule out electronic errors

so here I'm posting the code please rectify the error (removed irrelevant parts
ignore syntax)

Rich (BB code):
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
short dataByte;


//sets output to 4 input of L293D
void setMotorExclusive(short lf,short rf,short lb,short rb){

    //pb3L,pb1R fwd
    //pb2L,pb0R back
    
    if(lf)
    PORTB|=(1<<PB3);
    else
    PORTB&=~(1<<PB3);
    
    
    if(lb)
    PORTB|=(1<<PB2);
    else
    PORTB&=~(1<<PB2);
    
    if(rf)
    PORTB|=(1<<PB1);
    else
    PORTB&=~(1<<PB1);
    
    
    if(rb)
    PORTB|=(1<<PB0);
    else
    PORTB&=~(1<<PB0);
}


void processByte(){

short  byte=dataByte;

switch(byte){
case 0x0f:dpadExclusive(1,0,0,0); //dpadEclusive function sets LED (normal GPIO output)
setMotorExclusive(1,1,0,0);//sets output to motor driver
break;
case 0x01:dpadExclusive(0,1,0,0);
setMotorExclusive(0,0,1,1);
break;
case 0x10:dpadExclusive(0,0,1,0);
setMotorExclusive(1,0,0,1);
break;
case 0xf0:dpadExclusive(0,0,0,1);
setMotorExclusive(0,1,1,0);
break;
case 0xff:dpadExclusive(1,0,0,1);
setMotorExclusive(0,1,0,0);
break;
case 0x1f:dpadExclusive(1,0,1,0);
setMotorExclusive(1,0,0,0);

break;
case 0xf1:dpadExclusive(0,1,0,1);
setMotorExclusive(0,0,1,0);
break;
case 0x11:dpadExclusive(0,1,1,0);
setMotorExclusive(0,0,0,1);
break;
default:
setMotorExclusive(0,0,0,0);
dpadAll(0);//turns of all LED
};

}//end process

//starts UART
void setReceiver(){
UCSRB |= (1 << RXEN )|(1<<RXCIE);
UCSRC |= (1<<URSEL)| (1 << UCSZ1 )|(1 << UCSZ0 );
UBRRH = ( BAUD_PRESCALE>> 8); 
UBRRL = BAUD_PRESCALE ;
}

int  main(){
setupIO(); // sets GPIO pins using DDRx
sei();
setReceiver();

while(1){
    
    processByte();
}

}


ISR(USART_RXC_vect) 
{ cli();
    dataByte=UDR;
    sei();
}
 
Last edited by a moderator:

Thread Starter

allahjane

Joined Sep 19, 2012
75
Most likely your power supply to the MCU is not filtered properly.

Here are some steps you can take in order of importance.

1) Put 0.1μF capacitor between Vcc and GND as close as possible to the pins of the MCU.

2) Put 10μF electrolytic capacitor between Vcc and GND feeding the MCU.

3) Isolate the Vcc feeding the MCU from the rest of the circuitry with a 100Ω resistor.

4) Put a large capacitor, about 4700μF electrolytic capacitor on the power feeding the motor.
My motors are connected to run bi-directional so how should I attach the electrolytic cap (polarity) :confused:
 

BillO

Joined Nov 24, 2008
999
It won't hurt to place .1uF ceramic disc or mlcc capacitors across the motor winding as well. This will help reduce the spikes and EMI caused when the motor windings are switched by the brushes/commutators.
 

ErnieM

Joined Apr 24, 2011
8,377
Since you have two batteries would it be very hard to power the controller off the 9V and the motor off the 12V? That helps isolate glitches due to the motor from the controller.
 

Thread Starter

allahjane

Joined Sep 19, 2012
75
Since you have two batteries would it be very hard to power the controller off the 9V and the motor off the 12V? That helps isolate glitches due to the motor from the controller.
Truly said but I want to make my circuit compact. Can Caps help to eradiacate the problem to its roots?:confused:
 

Thread Starter

allahjane

Joined Sep 19, 2012
75
4) Put a large capacitor, about 4700μF electrolytic capacitor on the power feeding the motor.
Please mention the voltage rating as well

anyway Thanks a lot

Nice suggestion I had a 1000uF 16V electrolytic capacitor for time being so I attached it to the L23D9's motor power supply input (pin 8 & gnd)

and POOF the rebooting was gone .. but there's another problem now the circuit gets paranoid for few seconds (2-4) while motors are running where the pin output states start switching randomly very fastly and then the system recovers and keeps on going correctly before it happens again

. This happens randomly and is frequent but at least it wont cause that freeze and restart

the random output conditions generally occur when motors are running and some times even in off state

I hope its the side effect of using a 1000uf cap instead of 4700uf
 

MrChips

Joined Oct 2, 2009
30,806
You would know the voltage rating, no me.
What is the voltage output of your power supply? Use that and multiply it by 1.5 minimum.
 

Thread Starter

allahjane

Joined Sep 19, 2012
75
A BIG THANKS TO ALL

Especially to Mr. Chip for providing those capacitor ratings

Now I have a 4700uF 25v cap attached to the motor driver power supply and no problems at all

The circuit works like a charm

Cheers :D

And thanks to all again
 

Thread Starter

allahjane

Joined Sep 19, 2012
75
How should I add [SOLVED] in front of this thread's name so if any other noob like me can know this thread has a working solution
 
Top