Atmel Tiny driving Mosfet to PWM Motor

Discussion in 'General Electronics Chat' started by Leigh, Sep 15, 2010.

  1. Leigh

    Thread Starter Member

    Feb 11, 2010
    20
    3
    There are many posts about this I know. I have a circuit that works, but is unreliable. I have a Tiny circuit that I use for many projects with outputs and a status LED. I have set up Timer/Counter 0 in PWM mode, and the output of this circuit drives the mosfet as per the diagram. The PWM is used to slow the motor (obviously). Whenever the motor is turned on and off the status LED goes on and off.

    The control program is turning on the motor for a few seconds, then turning it off for a few seconds, repeatedly. Sometimes the motor does not turn off, but the status LED goes off, indicating that the program has done it's bit. Why might the motor sometimes stay on. It speeds up instead, indicating that it is not being controlled by the PWM. I am happiest in digital environment, so motors and mosfets don't have to try hard to confuse me.

    Any help is appreciated.
     
  2. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    There are several short comings in your design.

    1. Wrong MOSFET selected. The STP16NF06 MOSFET needs at least 10V gate-source voltage to fully turns ON but the Tiny AVR port can only provide about 4.8V max.

    2. The gate resistor 100K is too high, virtually useless in this case.

    3. The Tiny port pin output capacity is only in the ~20mA range and cannot drive the MOSFET ON/OFF fully at required PWM frequency. You'll need to use a current booster to drive it properly.

    The following is a circuit designed by one of our senior members to address the problem. You'll also need to use a "logic level" MOSFET in your design, these will turn fully ON with gate voltage of 4.5V or less.

    [​IMG]
     
  3. Leigh

    Thread Starter Member

    Feb 11, 2010
    20
    3
    Thank you for the comments and advice. I was lucky that my circuit worked at all.. actually no, if it hadn't worked at all I might have been forced to re-visit my design earlier.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Code ( (Unknown Language)):
    1. /*******************************************************************
    2. ** Program to control Dave Bell's robotic dog
    3. **  with status LED on pin11, PD6 and MX PWM on pin14, PB2 / OC0A
    4. *******************************************************************/
    5. #include "DavesDog.h"
    6.  
    7. void init(void) {
    8.     //init Watchdog, reset on watchdog ovfl, reset time =~ 2 s (seems to need 1<<WDIE??)
    9.    WDTCSR |= (1<<WDE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0); // | (1<<WDIE)
    10.    //init progFlags & globals
    11.    initFlags_Globals();
    12.     //prep Port D for LED O/P
    13.    DDRD = (1<<StatusLED);
    14.   //prep Port B for MX control currently on Pin 0, Port B
    15.    DDRB |= (1<<MX_PWM);  
    16.  
    17.   //prep for PWM.. need to use MX_START & MX_STOP in program to run
    18.    TCCR0A = (1<<COM0A1) | (1<<WGM00); //phase correct, o/p OC0A
    19.    OCR0A = 0x20;      //default speed, half
    20.   //enable Global Interrupts
    21.    asm("sei");
    22. }
    23.  
    24. int main(void) {
    25.    init();
    26.    flashFor(10);
    27.    delay(100);
    28.    while(1) {
    29.     wdt_reset();
    30.   /*  PORTB |= (1<<MX_PWM); //On
    31.     delay(1);
    32.     PORTB &= ~(1<<MX_PWM); //Off
    33.     delay(1);*/
    34.     MX_START;
    35.     delay(100);
    36.     MX_STOP;
    37.     delay(500);
    38.    }
    39. }
    40.  
    41.  
    42. void delay(long x) {
    43.      for(int i = 0; i < x; ++i) {
    44.      wdt_reset();
    45.      _delay_ms(10);  // Wait for about .01 sec times x cycles
    46.      }
    47. }
    48.  
    49.  
    50. void flashFor(int tenths) {
    51.     PORTD |= (1<<StatusLED); //On
    52.     //PORTA |= (1<<PA2); //temp to test using RESET as output
    53.     delay(tenths);    //delay
    54.     PORTD &= ~(1<<StatusLED); //Off
    55.     //PORTA &= ~(1<<PA2); //temp to test using RESET as output
    56. }
    57.  
    58. void initFlags_Globals(void){
    59.       ;
    60. }  
    61.  
    I have tried to do a bit of cleanup to your code and place it between
    Code ( (Unknown Language)):
    1.  tags to aid in making it readable.
    2.  
    3. hgmjr
     
Loading...