Saturday, April 21, 2012

Push Button ON/OFF

21st April, 2012

Ever wondered how can you keep your circuit ON using a push button rather than a toggling switch?

just like in many instruments/ mobile phones etc

the following schematic shows you how,


The idea is that you use the push button to run the circuit normally, and then, very fast (in the beginning of your code if the cct contain a microcontroller) send a signal to close a switch (could be a relay or whatever)

Example on ATmega328

#include <avr/io.h>  
#include <util/delay.h>  
#include <USART.h>  
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))  
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) 

int main(void)  
{  
    init_usart();  
    SETBIT(DDRB,0);  //Output pin (ON/OFF)
    CLEARBIT(DDRB,1); //Input to change time
    SETBIT(PORTB,1);  //Enable Pull-Up resistor

    for (;;)  
    {  
        for (uint16_t i=30; i>0; i--)  //repeat 15 times going down, used as delay
        {  
            printf("i=%d\n",i);  //just to see whats the current delay value
            _delay_ms(100);  //give me time to release button
            while ((PINB & 0x02)==0x02)  //loop when I don't press button
            {  
                SETBIT(PORTB,0);   //ON
                _delay_ms(i);      //i value as delay
                CLEARBIT(PORTB,0); //OFF 
                _delay_ms(i);
            }  
        }  
    }  
    return 0;  
} 


Remember that when a person pushes a button and releases it, he usually takes quite long (in ms), which is an enough time for the MCU to run, turn the switch ON and keep itself alive :)








26th April, 2012

So I've tried to go with only MOSFETs instead of the bulky relay, I tried this earlier:




which did not succeed as I said, so today I tried this:





which was not successful as welll :S


What am I doing wrong?!!






No comments:

Post a Comment