AVR freezing and reseting due to motor (maybe)

Discussion in 'Embedded Systems and Microcontrollers' started by allahjane, Jan 15, 2013.

  1. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    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?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    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.
     
  3. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    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.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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?
     
  5. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    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.

     
  6. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    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)

    Code ( (Unknown Language)):
    1.  
    2. #include <avr/io.h>
    3. #include <util/delay.h>
    4. #include <avr/interrupt.h>
    5. short dataByte;
    6.  
    7. [COLOR=red]
    8. //sets output to 4 input of L293D[/COLOR]
    9. void setMotorExclusive(short lf,short rf,short lb,short rb){
    10.  
    11.     //pb3L,pb1R fwd
    12.     //pb2L,pb0R back
    13.    
    14.     if(lf)
    15.     PORTB|=(1<<PB3);
    16.     else
    17.     PORTB&=~(1<<PB3);
    18.    
    19.    
    20.     if(lb)
    21.     PORTB|=(1<<PB2);
    22.     else
    23.     PORTB&=~(1<<PB2);
    24.    
    25.     if(rf)
    26.     PORTB|=(1<<PB1);
    27.     else
    28.     PORTB&=~(1<<PB1);
    29.    
    30.    
    31.     if(rb)
    32.     PORTB|=(1<<PB0);
    33.     else
    34.     PORTB&=~(1<<PB0);
    35. }
    36.  
    37.  
    38. void processByte(){
    39.  
    40. short  byte=dataByte;
    41.  
    42. switch(byte){
    43. case 0x0f:dpadExclusive(1,0,0,0); [COLOR=Red]//dpadEclusive function sets LED (normal GPIO output)[/COLOR]
    44. setMotorExclusive(1,1,0,0);[COLOR=red]//sets output to motor driver[/COLOR]
    45. break;
    46. case 0x01:dpadExclusive(0,1,0,0);
    47. setMotorExclusive(0,0,1,1);
    48. break;
    49. case 0x10:dpadExclusive(0,0,1,0);
    50. setMotorExclusive(1,0,0,1);
    51. break;
    52. case 0xf0:dpadExclusive(0,0,0,1);
    53. setMotorExclusive(0,1,1,0);
    54. break;
    55. case 0xff:dpadExclusive(1,0,0,1);
    56. setMotorExclusive(0,1,0,0);
    57. break;
    58. case 0x1f:dpadExclusive(1,0,1,0);
    59. setMotorExclusive(1,0,0,0);
    60.  
    61. break;
    62. case 0xf1:dpadExclusive(0,1,0,1);
    63. setMotorExclusive(0,0,1,0);
    64. break;
    65. case 0x11:dpadExclusive(0,1,1,0);
    66. setMotorExclusive(0,0,0,1);
    67. break;
    68. default:
    69. setMotorExclusive(0,0,0,0);
    70. dpadAll(0);//turns of all LED
    71. };
    72.  
    73. }//end process
    74.  
    75. //starts UART
    76. void setReceiver(){
    77. UCSRB |= (1 << RXEN )|(1<<RXCIE);
    78. UCSRC |= (1<<URSEL)| (1 << UCSZ1 )|(1 << UCSZ0 );
    79. UBRRH = ( BAUD_PRESCALE>> 8);
    80. UBRRL = BAUD_PRESCALE ;
    81. }
    82. [COLOR=Red]
    83. int  main(){
    84. setupIO(); // sets GPIO pins using DDRx
    85. sei();
    86. setReceiver();
    87.  
    88. while(1){
    89.    
    90.     processByte();
    91. }
    92.  
    93. }
    94.  
    95.  
    96. ISR(USART_RXC_vect)
    97. { cli();
    98.     dataByte=UDR;
    99.     sei();
    100. }[/COLOR]
    101.  
    102.  
     
    Last edited by a moderator: Jan 16, 2013
  7. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    My motors are connected to run bi-directional so how should I attach the electrolytic cap (polarity) :confused:
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    The large cap is placed across the power supply, not the motor.
     
  9. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    Thanks My bad :p
     
  10. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    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.
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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.
     
  12. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    Truly said but I want to make my circuit compact. Can Caps help to eradiacate the problem to its roots?:confused:
     
  13. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    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
     
  14. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    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.
     
  15. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    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
     
  16. allahjane

    Thread Starter Member

    Sep 19, 2012
    75
    1
    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
     
Loading...