Cellphone type power button

Hello, Greetings!

I'm working on a Hair brush project based on ATmega328. It is supposed to have a button which when pressed once will turn on the hair brush and set it to the least vibration mode, when the same key is pressed again the hair brush will be set on medium vibrating mode and when the same key is pressed thrice it will be set to highest vibrating mode. But if I long press and hold this button the hair brush should turn OFF. Something like cellphone. The hair brush is battery operated.

How this can be achieved? I have no idea where to start. I can handle the vibration mode stuff but this ON and delayed OFF thing, how to do it?

Thanks in advance

Look for button libraries; there are several that allow you to read a short vs. a long press. Combine this with a finite state machine and you've got the basics. Then implement in such a way that you don't use delay() as it will mess up responsiveness of the device to user input.

As to getting started: I'd say start by reading a button and distinguishing between long and short press; once you got that down, experiment with making a simple state machine that cycles through a couple of predefined states. Then combine the button and the state machine. Finally write the code for the relevant outputs (leds, motors etc) for each particular state.

1 Like

Remember, your motor will probably need a driver that supports your multiple modes.

every button or switch has two states,off and on.

when you press a button, changes from off to on.
when you release, it changes from on to off.

you can detect each state, and you can detect each change from one state to the other.

what I did was to use a timer similar to Blink Without Delay.

press (1st state change)
note the time
release (2nd state change)
check duration

if the release was less than 2 seconds, then ignore as it was not long enough.
if the release was over 2 seconds, then a new thing was done.

in my case, I also had an LED that would change after 2 seconds to let me know I had held more than 2 seconds.

Thank you for all of these wonderful and helpful inputs I'll try to implement them. But how do I disconnect the power and turn on the power to my hair brush with same switch/button? what type of hardware changes I need to make?

The power to the motor is not directly controlled by the button. Only a tiny signal comes from the button, telling the chip if the button is pressed or not. The logic in the program running on the chip controls the motor, that logic changes in response to the signal from the button.

As mentioned above, the chip cannot control the motor directly. A driver circuit/module is required between the atmega chip and the motor because the motor will require more power than the atmega's pins can provide.

hardware requires schematics
software requires code

if you are asking about a hardware issues, wiring, circuits, what chips, motor drivers, etc, you need to work up the bits needed.

microcontrollers use micro-electrics.

a switch says, I am open or I am closed
the micro-controller says, if the start switch is closed, then send a signal to the motor driver to run the motor.
the motor driver sees this low power signal and it allows high power to be sent to the motor.
an obvious solution which no-one has mentioned yet.

watch some videos on Arduino pump or Arduino fan or Arduino motor
take some time and follow the many tutorials on how to do the example sketches that are in the IDE. as you learn the basics, you will gain the knowledge to make this sort of project.

The problem you have selected to challenge is just a bit more complicated.

Cell phones are never really fully off. At least they are able to see and respond to an on/off button, and more than turn on, they wake up.

So in addition to hacking the idea of first, second and thrice button presses, and short and long, you'll have to manage the power with some extra ordinary circuitry and processor states.

I suggest you get a prototype working that just has an on/off switch, and a pushbutton for speed control one-two-three.

Then add long presses that simply light an LED so you know you are programmed correctly to see the long press, even if it doesn't turn off you device just yet.

Then look into low power and sleep modes, and how to get an Arduino to sleep soundly whilst waiting for that wakeup call.

Don't be intimidated, low power is fairly straight ahead if not a place for a total noob to start.

Read the link below as if you understood what he's talking about! It is a slog, but he has complete workable examples and good tips for how to fix up the hardware for low power:

HTH and please, step by step.


Seatch for arduino turn itself off with transistor.

You can have power to the arduino controled by a transistor.
The arduino itself controls the transistor.

You press and hold a button.
The button temporarily powers the transistor.
The arduino turns on
The arduino then maintains power to the transistor and from that point is in control
Later you press and hold the power button.
The arduino sees that and removes power from itself



first fish dragged out of the google pond has a nifty circuit that will work well, be sure to see the one where button status can be seen whilst the Arduino is powered.


This seems an easy way to implement do you have any reference for it, please?

This seems exactly what I wanted!
Thank you so much!

I'm going through it and I will get back here to let you know guys how it goes.

Once again thank you so much!

Just one query, Do I need to include driver transistor for the secondary N channel MOSFET which is driven by Arduino?

No. Both circuits on that page are complete and in agreement with advice given on these fora.

That is, just two MOSFETs, no need for a driver transistor in addition like you may have seen used with a relay.

The only comment I would make is that if size is important, you can find smaller MOSFETS, just

  • Be sure to pick logic level devices and

  • A P-FET that can handle your anticipated load.

Others may have part numbers handy.

The diodes in the 2nd circuit are not critical, use a switching diode 1N914 or 1N4148 - the diode he specs is fine also but larger unnecessarily.

Both the larger and the smaller diode will be useful at others times, so if you need a get a bunch don't fret. They are relatively inexpensive.

I'm still on a bunch of 100 1N914-alikes that I got, er, some time ago for a few dollars.


1 Like

Thank you so much Alto for breaking up the things simpler for me. I'm proceeding to implement that circuit already.

All the MOSFETS that I found were too big in terms of size and specs as well. So I decided to use SMD P Channel FET and SMD N channel FET. Would it make any difference? if I use FETs instead of MOSFETS?

I just have to give the schematics for this hair brush and the coding file so I won't be testing them. So, I guess this will be solution for my case.

Thank you so much all of you for taking time and giving all those timely and valuable inputs.

I can’t say about MOSFET vs. FET. Without going beyond my zone. :expressionless:

Surface mount devices are fine. If you are driving the hair brush vibrator (?) with the same FET, obvsly it will have to handle the max current of that in addition to the small current the control unit will need.

I am sure you do mean to test the software, somehow. I don’t think I’ve ever written something that didn’t need some fixing when it was entered and tested.

So you will need to fake the power on power off part.

Power on: that’s just reset or turn on the control unit with a switch. That’s what the program would experience with the real circuit attached.

Power off: to recognize the power off long push and prove that you have done, just use an LED and light it up to show when you circuit, if it was there, would turn things off.

I can’t leave without mentioning wokwi.com, my new favorite toy, which makes all kindsa testing much less painful than the normal cycle of development using a real Arduino.


1 Like

I checked the datasheets of those FETs and its more than 1 amps, I'm using philips shaver motor like this one:


Its 2.4V motor I'm not sure about its current ratings but I'm guessing looking at its size that it won't be more than 1 amp.

Your thoughts and views on this will be helpful please. Thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.