Single switch on/off

Thread Starter

geoffers

Joined Oct 25, 2010
475
Hi all,
I'm making a project using a PIC18f2523, I would like to be able to turn it, on/off and control it use a single switch. Up to now when using a PIC I have allways either used the weak pull ups in the pic or put external pullups on and used any control switch to ground the pin. Is there a good reason not to use a pull down resistor? I could then use a single pole push switch and a power tranistor 'held' open by the pic. After a long press the pic could turn everything off? My push button could then via a diode be used to turn the transitor on until the pic holds the transistor when its running? Is this the best way to go about it?
Thanks Geoff
 

t06afre

Joined May 11, 2009
5,934
The worst ting is a floating input. That is a no-no. But choosing pull-up or down is much up to the designer. At least then you do not have to turn any stone in order to reduce current consumption. Remember also in a micro-controller setting it do not matter much if a input signal is active high or low
 
Last edited:

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thank you, I was considering putting the pic to sleep, however I have other components, lcd etc. that would need turning off so I have to put a transistor somewhere!
Cheers Geoff
 

ErnieM

Joined Apr 24, 2011
8,377
I'm making a project using a PIC18f2523, I would like to be able to turn it, on/off and control it use a single switch.
Yeah, me too. See below.

Up to now when using a PIC I have allways either used the weak pull ups in the pic or put external pullups on and used any control switch to ground the pin. Is there a good reason not to use a pull down resistor?
Nope, either a pull-up or pull down leaves the pin in a defined state. Just don't let an input float without any defined voltage on it.

My push button could then via a diode be used to turn the transitor on until the pic holds the transistor when its running? Is this the best way to go about it?
I would need to see your schematic to comment on your scheme. Here's one I am currently using:




This is a one-off battery powered timer to run a pump. It needs to be on for a few minutes, and I would like to be able to field set the minutes (as the field is in the middle of a harbor). SW1 is a pushbutton that does several things. Press it and the device turns on, press again it turns off. Press and hold puts into "program mode" so you can change the timeout (which gets saves in EEPROM).

U1 can be most any PIC for this app. U2 is a low dropout regulator with an enable input (pin 3 marked EN). If EN goes above 1.4V the regulator turns on. EN is normally pulled down. Pressing SW1 drives EN high to about a diode drop above 5V.When the PIC starts to run it uses it's own GP5 pin to keep U2 on, until such a time as the PIC turns itself off. While on, the button can still be pressed and sensed at GP3.

Just note the circuit is still "in the mail" as the parts are on order but the breadboard isn't built. I've always trusted my measurements more then my calculations.

(Schematic is done in Kicad, hopefully I've not modified a lib you don't have.)
 

Attachments

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thanks,
I'm after somthing similar, this is what I was thinking of doing?
on-off.gif
Hope its readable! Took me a minuite or two to figure out how to get it on here! I think your method looks more elegant! My circuit is battery powerd, so the only concern I might have is as the battery gets low, using the potential divider and diode on the switch as you are might lead to it not being able to pull the input of the pic high enough in my circuit? Do I have the basic idea right on my schematic if not the values?
Thanks again Geoff
 

Markd77

Joined Sep 7, 2009
2,806
In a circuit like this it matters whether you use a pull up or pull down resistor.
<ed>Ignoring the fact that the regulator wouldn't work as intended: </ed>
If in ErnieM's circuit the switch was moved to be in parallel with R3, the PIC would be powered through the GP3 pin <ed> internal </ed> protection diode with VDD removed (mislabeled on the PIC).
I've done that before, the PIC carries on running, sometimes erratically.
If it was any pin apart from MCLR, I'd be tempted to add a 3.3V zener to guarantee the voltage at the pin was safe, it would also help with larger battery voltage ranges.
 
Last edited:

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thanks for the repley,
Could you elaborate a bit, I'm quite new to this and am learning as I go! When should you use a pull up and when should you use a pull down? Would it be the regulator in my circuit that wouldn't work correctly or in Ernies? The alternative I had considerd would be to put the PIC to sleep and use the port B interrupt to wake it up. I thought I could then switch the rest of the circuit on and off with a transistor driven by the PIC. According to the data sheet this PIC uses 100nA when is asleep so a battery should last ages!
Thanks Geoff
 

ErnieM

Joined Apr 24, 2011
8,377
Thanks,
I'm after somthing similar, this is what I was thinking of doing?
View attachment 45433
Hope its readable! Took me a minuite or two to figure out how to get it on here! I think your method looks more elegant! My circuit is battery powerd, so the only concern I might have is as the battery gets low, using the potential divider and diode on the switch as you are might lead to it not being able to pull the input of the pic high enough in my circuit? Do I have the basic idea right on my schematic if not the values?
Thanks again Geoff
Yeah I can see where you are going (it's readable!) but I see a problem: It is true the switch will turn on Q1 when pressed, and allow each push to be detected at RA2. The bad news is RA1 will not keep Q1 on. Here's why:

Q1 is an emitter follower powering the PIC. (Aside: you should bypass it's output). Now "emitter follower" is a general term, in the real world the emitter is a diode drop below the base. So with a 5V supply the button turns the PIC on with 4.3V, which later will drive Q1... then releasing the button drops the base with 4.3V, so the emitter output drops to 3.6V, so RA1 drops... and the whole thing turns off pretty darn quick.

If I were doing this with transistors I might try something like so:



Either the button or RA1 turns on the NPN, which turns on the PNP. As the PNP should be saturated POWER = the 5V - 0.1V, so we don't loose much thru the transistor. RA2 can see the button presses too.

The resistor values aren't critical. About 5K for everything should be good for a 10mA or so load. If your load is much bigger then the resistor between transistors has to drop: 500 ohms for a 100mA load should be good leaving the others back at 5K.

Any more load then I actually have to calculate things (which I will do if you tell me the max load current expected).
 

Attachments

Markd77

Joined Sep 7, 2009
2,806
I was writing about ErnieM's circuit as an example. All my comments were about that.
I hadn't really looked at yours as it's a bit hard to read. If that's a 78L05 regulator then it draws around 3mA with no output current, so the PIC sleep current is negligible compared to that.
If you are shopping around for a new regulator, you may as well get one with an enable input.
 

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thanks Guys,
Yes it is a 7805 reg, its becoming clear that I'm going to have to use a different regulator, I should have read the data sheet for the 7805, I didn't realise it would draw that much current with no load, my battery would be flat in a week without the thing being turned on! These things are always more complicated than I expect! Any advice on the best regulator to use? My circuit is still growing at the moment but I'm geussing its going to be drawing 500mA at a absolute maximum!
Cheers Geoff
 

ErnieM

Joined Apr 24, 2011
8,377
Half an amp is a large amount of current to get from a battery. If that is truly your requirement (meaning you have a breadboard you're measured and the measurement makes sense when compared to your paper estimate) then you should be considering a switching buck regulator. A linear regulator works by literally burning off the extra voltage, while a switcher converts power at one voltage to another voltage with good efficiency.

Example: A switcher may easily be 90% or better efficient. If you use a linear regulator to convert 9V to 5V it is at best 5/9 = 55% efficient, and even that can't be reached in the real world.

You may find the V7805-500-SMT suitable to use.
 
Last edited:

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thanks Ernie,
I must admit thats a guestimate for the absolute maximum, its probably going to be closer to 200-250mA peak, but I thought it maybe better to over spec it than under. That regulator does look like it should do the job. I think I may try to use your method, if I had a 9v battery and 5v supply, do you know what range of voltages would still enable a high input to a pin? ie, from the data sheet I found that any input pin can have +0.3v above vss on it. I couldn't find what the lowest voltage is that would still be 'seen' as a 1 by the pin was though? The alterative I did think of was to use a pin set as a comparator for the input but that does seem overcomplicated? Basicaly I'd like the highest range of battery voltages I can so the input still works as the battery goes flat?!
Cheers Geoff
 

ErnieM

Joined Apr 24, 2011
8,377
...if I had a 9v battery and 5v supply, do you know what range of voltages would still enable a high input to a pin?
Do you mean the "pin" on the PIC18f2523? Wow... they actually did not spec that, did they? <grin> Generally I like to aim for < 1V for a zero in, and > (Vdd-1V) for a one in. That always gives some good noise immunity for CMOS.

One common trick is to use the button to overdrive an input thru a large (10K or higher) resistor, with another resistor to ground. With the button open the resistor to ground acts as a pull-down ensuring a reliable zero. Close the button and the series resistor drives the input ESD diode to give a reliable one. The electrical characteristics do hint this is OK given they spec "Input clamp current" at 20mA. Pick a resistor to give a lot less current then this and you are fine. I've even done this to sense when the AC line is crossing zero connecting the AC thru just a large (500K or so) resistor. (But don't tell anyone I told you that here as it's a violation of our TOS.)

On my schematic I'm not using that trick as I'm using the MCLR input which A) can handle higher voltages as it's the programming voltage pin, and B) it does not have an ESD diode (since it's the prog voltage pin). However, it is cleaner way to do it and I may switch pins to do just that (once I am thru debugging and get back two pins on my 8 pin device).

The V7805 needs any voltage above 1.4V to turn the regulator on, and below 1.1V to shut it off.
 

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thanks again,
I'm thinking this is going to be the best way for me to do it. From both of your posts I get the impression a bit of voltage 'range' on the pic input can be accomadated? I'm unsure as to what range of voltage a 9v battery will give as it goes flat? Guess it would vary from battery to battery. I see the min voltage on the regulator is 6v so it would be a 3v range at the most?
Cheers Geoff
 

Thread Starter

geoffers

Joined Oct 25, 2010
475
Thanks again,
Do apretiate you looking at this. Just so I'm clear on this, its ok to 'overdrive' the input on the pic as long as the current stays under 20mA? So with 9v and a 10k resistor I would be putting 0.9mA on the input? Would I then need a about 100k pulling the pin to ground? The pull down resitor at that value would also drop the voltage by 0.9v wouldn't it? Finaly, the regulator control voltage first applied by the switch and then the pic, if this is above 5v I think its ok as far as the regulator concernd but will the pic do anything funny if its output pin is momentarly raised above 5v when the switch is used ? A diode between the pic and regulator would solve that I suppose but if its not need theres no point in putting one there! Sorry if some of my questions are basic!
Cheers Geoff
 

ErnieM

Joined Apr 24, 2011
8,377
Just so I'm clear on this, its ok to 'overdrive' the input on the pic as long as the current stays under 20mA?
Yes it's OK just as long as the input pin has that ESD diode to VDD: Not every input pin does. Microchip does have an App Note on doing just this (sorry no link on hand).

So with 9v and a 10k resistor I would be putting 0.9mA on the input? Would I then need a about 100k pulling the pin to ground? The pull down resitor at that value would also drop the voltage by 0.9v wouldn't it?
That is all correct. 0.9mA from a battery is a lot of current so I would look to scale that back just to keep more juice in the battery. If I was buying fresh parts (and not trying to reuse just my parts on hand) I would get a 400K and a 500K resistor to get 5V from 9V.


Finaly, the regulator control voltage first applied by the switch and then the pic, if this is above 5v I think its ok as far as the regulator concernd but will the pic do anything funny if its output pin is momentarly raised above 5v when the switch is used ?
That's a bit more complicated then can be described with just words, I would need your schematic to comment completely.

I could not find an upper limit to the R-78AA5.0-0.5SMD ON/OFF pin; typically they can go the full input range but it would be nice to see that in print.

On the PIC I/O side I would aim to overdrive the pin with voltage, but at a very small current so the initial power on press doesn't have the ability to fully power the PIC with the unregulated voltage off the button.
 
Top