Skip to main content

Site Navigation

Your Account

Choose Language

Anything programming related to the Hexbright

34 Questions View all

Why is DPIN_PWR deactivated at initialization?

In the factory code as well as in the great hexbright library by David Hilton, I can see this snippet:

// We just powered on! That means either we got plugged

// into USB, or the user is pressing the power button.

pinModeFast(DPIN_PWR, INPUT);

digitalWriteFast(DPIN_PWR, LOW);

To me it looks wrong. We're first setting the pin mode to INPUT, i.e. DDB0 is set to 0 and then write LOW, i.e. PORTB0 is set to 0. According to the ATmega168 specs, this means that the pin is in Hi-Z state, i.e. it acts as if it was unconnected, so as soon as you release the switch, pin 1 on the MIC5353 must get low (through R18 and R19). Admittedly this is 20kΩ, so not a very reliable current sink, but since there's no other connection, the logical level should be low.

Am I wrong to assume that the code works only because initially the power switch is held (so the EN pin is driven high through D3) and when you release it, the discharge time for C19 is long enough (a few milliseconds) to let the Hexbright program turn DPIN_PWR on?

Answered! View the answer I have this problem too

Is this a good question?

Score 0
Add a comment

1 Answer

Chosen Solution

The hexbright runs code for about a half-second after the button is released before turning off. This is long enough for most programs to decide whether to be on or off, and if it should be on, which mode it should enter.

The initialization code doesn't know whether or not the flashlight /should/ be on, and leaving DPIN_PWR on LOW means that the light will automatically turn off if the program's conditions for staying on are not met.

Suppose someone starts charging the light. This will start executing whatever program is in place. If DPIN_PWR were set HIGH in initialization, the hexbright would stay on until it was explicitly turned off, even if no buttons had been pressed.

Because DPIN_PWR is LOW, the light will (by default) power down after the USB cable is disconnected.

Here is a sample program that turns /on/ when unplugged (an emergency light).

Was this answer helpful?

Score 0


Thank you for your answer, David! This explains perfectly why DPIN_PWR is left LOW. On the other hand, this means the code can be left out completely, because the initial value of DDRB0 and PORTB0 is always 0 after power-on or reset, and since the bootloader doesn't change them either, it must be already in the desired state when the Hexbright program starts executing.


Good to know :). I obviously don't know the register states on power on.


There was a bug where the light /wasn't/ turning off. This has been fixed. The light will currently run for 5 seconds before shutting off (unless told to stay on).


Add a comment

Add your answer

tesarikfamily will be eternally grateful.
View Statistics:

Past 24 Hours: 0

Past 7 Days: 0

Past 30 Days: 0

All Time: 119