Only One Button? No Problem!

Sometimes less is more. This is especially true when dealing with microcontrollers with limited I/O pins. Even if you have lots of I/O, sometimes you are need to pack a lot into a little space. [Hugatry] was inspired by the simple interface found on a lot of flashlights: one button. Push it and it turns on. Push it again, and it switches modes. You cycle through the modes until you finally turn it back off. One button provides mutliple functions. The question is how can you use a power switch as an I/O device? After all, when you turn the power off, the microprocessor stops operating, right?

[Hugatry’s] answer is quite simple. He connects a resistor/capacitor network to an I/O pin (or multiple pins). When the processor turns on initially, the pin will read low and the capacitor will charge up. If you turn the power off, the CPU voltage will fall rapidly to zero, but the voltage on the capacitor will discharge slower. If you wait long enough and turn the power on, there’s no difference from that first power on event. But if you turn the power on quickly, the capacitor voltage will still be high enough to read as a logic one.

What that means is that the processor as part of its start up can detect that it was recently turned off and take some action. If it remembers the previous state in nonvolatile memory, you can have the code cycle through multiple states, just like a flashlight. You can see a video of the setup, below.

[Hugatry] included some simple Arduino code that illustrates the concept. However, the technique is simple enough that you can adapt it to other projects easily.

Think one button isn’t enough to do anything interesting? Think again. Then again, Amazon probably has a patent on things with one button.

26 thoughts on “Only One Button? No Problem!

    1. Having a separate high and low mode can be nice for when you don’t want to be blinded while walking to the bathroom at night. But I’ll agree that beyond that they’re all just annoying.

      1. Being seen is one place where where it dumb to stingy. If the battery is disposable or coin cell throw the lite out and get with some luminosity. If you strobe then have as much as the peak in a steady light. A strobe alone should be illegal, it’s harder to get a fix when everything is moving.
        With the multi-mode on flashlights it is a conspiricy to wear out the switch much faster, which is allready not Murphy proof.

        1. I often drive behind other cyclists and strobe is annoying as hell, couldn’t agree more. Here’s the thing, there’s at least one LED always on, they just cycle from left to right and then back from right to left. The mode I use is not that. In terms of light intensity over time it’s same as 1 LED being always on instead of 3, which seems to be enough, given the angle reflectors on the light (at least for me). I could send you a short video if you’re interested, would love to hear a driver’s opinion about this.

    2. When it’s the cycling type, yes. But if it’s implemented well it’s quite useful. My current EDC is a Zebralight SC62w and it lets me easily get to three distinct modes from off with more available from there. From off: click for high, hold-release for low, double click for medium. Very simple.

    3. You can take it to extremes… like one bicycle tail light I had, it used one button to cycle between about 8 different modes including off.

      Turning the bloody thing off was an exercise in frustration as you’d be hitting the button repeatedly, then accidentally skip over the off position and have to start again.

      It’s fine if there are only about 4 modes and there’s a separate mechanism (e.g. long press) to turn it off. The Ay-Up headlights I have do this, and while it can be tricky getting the timing right for the “off” state (off there is ~1 sec, longer is a battery check), it doesn’t cause the same frustration.

    4. On my everyday carry tactical flashlight…
      First press: Blindingly bright beam that can be seen for miles/kilometers.
      Second press: Blindingly bright ≈10-15 Hz strobe that can be seen for miles/kilometers.
      Third press: Low power mode, perfect for a night walk with the dog.
      Ten presses really fast: Changes the settings so the first press is low power mode and second press is blindingly bright.

      Most of my favorite ‘torches’ have multiple settings. It isn’t that difficult to figure out.
      And if the button wears out on the expensive one, I call and they send a free replacement tail cap; no questions asked. Though I’ve had it for 3 years and it still functions flawlessly. :)

  1. When you’re using a microcontroller, the button on a GPIO pin can trigger an IRQ to wake a device from deep sleep, or trigger a mode change, with one mode being enter deep sleep. No need to write to NVM, really.

  2. Another way of approaching this problem:

    That lets you handle the button pushes completely in software and needs just two pins, one to detect the state of the switch, the other to control the BJT that holds the power on. You can have it so on power-on-reset, it delays latching the input power MOSFET and turning things on proper, saving battery life, if that’s needed.

    I use a similar arrangement to this on a bicycle horn: only the power output from the MOSFET feeds through a 12-9V switchmode regulator to provide a stable 9V to the amplifier circuits.

  3. Do this carefully, many micros have ESD protection in the form of clamping diodes and you can raise havoc (or even release magic blue smoke) by inadvertently powering the micro through the I/O pin once VDD goes lower than the voltage drop of the protection diodes relative the voltage on the cap. Best to put a current limiting resistor between the cap and the pin.

  4. Neat trick! I’ve seen something similar in Elektor years ago, instead of using a capacitor on a gpio pin, the cap was used to power the mcu. Switching on and off, the ultra low power mcu still ran while the gpio pin was connected to the power supply with little capacitance using a diode iirc. Duration of pulses and number of pulses can be used as a trigger for different actions like switching multiple lights or dimming. Can even be used with cmos logic ic’s like a 4bit counter.

    I own a scuba dive light that uses the trick mentioned in the article, long press to turn it on/off, short press to cycle between 3 brightness levels and one sos flash mode. That would be very handy indeed if you drift out into the ocean, night falls and the super bright flashlight can be seen for miles for search and rescue personell.

  5. Reminds me of the way the Apple ][ and later the PC used to read the joystick potentiometers, by charging a capacitor and then dumping it across the variable resistor and timing how long it took to go below a certain level using a comparator. It was all done using a few GPIO.

  6. Atari did something this on the 32 in one cartridge for the VCS when they rereleased it. There was an RC circuit that fed into the input of a binary counter. The counter addressed a different program in the ROM so whenever you turned the power off if you turned it back on again within about 10 second you got the next game on the cartridge.

  7. Trying to squeeze the most capability out of the least control options is part of the fun of designing with microcontrollers. User-interface haiku.

    There’s a fairly common frequency counter circuit for radios, using a PIC 16F628, that uses just one button to scroll through the possible offset values (local oscilator) – short push scrolls through the options, long push selects. It works well.

    For our boat I built a simple one button dimmer for some interior LED strip lighting. Short push cycles through full, mid, minimum brightness, and long push fades gradually while held.

  8. Is there a good cheap way to use a single pushbutton to turn a load on and off without a micro? I have used this circuit to try to turn on a simple LED light with minimal success:

    The issues include the LEDs acting as photodiodes and providing adequate voltage to turn the circuit back on when it is in the off state, and any amount of “low” impedance like just touching the circuit also turns it on.

  9. I did something a bit like that in a project. I wasnt using a micro and needed to turn a load on or off with a push button. This is not the most elegant solution but it works. Use a cmos low power binary counter (4024 or something). Your push button provides the clock signal (dont forget to add debounce circuitry) and your load is driven by Q1 output. Effectively you are using a T-type bistable and you can toggle that I/O line on or off when you press the push button.

    1. That’s a good idea, but I didn’t mention I wanted to have a very small bistable latching switch, which is why I was trying to do it with discretes. I previously had a version of the circuit that used a JK flip flop, and it worked fine, but I was trying to get smaller. Then I ended up using a CAT874 from ON Semi, and it was very tiny, but the 10 second delay to turn the circuit on was deemed unacceptable. I got the discrete circuit that I linked above to work ok, but it turns on way too easily – just about anything turns it back on. I was hoping someone knew of a simple circuit that could do this. Do cheap flashlights really use microcontrollers?

  10. If you only need a dimmer function you just need to make the dimming value inversely proportional to the nonlinear time between power on cycles when it is below the threshold level that indicates full brightness. off.on off..on off…on off….on etc. the longer you wait the brighter it gets. You could drop in a 555 and a buzzer to give you an auditory clue as to when to switch on again but that gets tricky as the capacitor has to power the 555 while also discharging at a rate that can be meaningful to the MCU. Or you could simply have the light dim rather than turn off instantly so if you turn it on again it holds the current level, unless that level dropped to zero in which case it goes back to full.

    Anyway, it is a nice little hack, and it suggests a few extra fun things to try, the sort of HAD article I really enjoy, thanks Al.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.