The circuit is based around the Digispark Attiny85 module firmware controlling the high power white LED via enabling/disabling the joule thief circuit.
During normal operation the Digispark Attiny85 module outputs a digital high via PB1 causing transistor Q2 to conduct. Thus providing a path to ground for the primary winding of the joule thief coil via resistor R7 rather than to the base of transistor Q1. Hence, the oscillation normally that would occur in transistor Q1 and the joule thief coil is inhibited. Therefore, the white LED is not illuminated. Details for how the joule thief circuit operates are available at wikipedia (1)
This also means if the Digispark Attiny85 module is powered down (or outage of the 'mains' 5V DC supply) the joule thief circuit will operating independantly and illuminate the white LED using the 1.5V battery (i.e., act as "backup" night light in event of power outage). Switch SW2 disconnects the battery to the joule thief circuit.
The Digispark Attiny85 module firmware continuously checks the HC-SR04 ultrasonic module for a return signal that is within the preset distance. See Attiny85/HC-SR04 project for more detail. If a return signal is found, pin PB1 is output digital low and hence the joule thief circuit oscillation is enabled and the white LED is illuminated. The firmware initiates a timer which will cut off the white LED after the preset time has expired.
Toggle button SW1 inconjunction with the state of the light dependant resistor (LDR) Z2 is used to accept user input to set the on-time duration of the white LED and the "sensitivity" of the ultrasonic sensor.
When the toggle button is depressed and the LDR is in the "light" (i.e., above the preset limit), a firmware timer is initiated and for each second the toggle button is held-on the on-time duration of the white LED is increased in steps of one minute, to a maximum of five minutes after which cycles back to one minute. The white LED is flashed rapidly the corresponding number of flashes to indicate to the user the currently selected on-time duration, which is selected by simply releasing the toggle button.
Simiarily, when the toggle button is depressed but the LDR is in the "dark" (i.e., blocked by a finger or similar) the distance within which the ultrasonic sensor will report a return signal is increased in steps of 25cm.
This function of the LDR inconjunction with the toggle button in providing user input is ancillary to the main purpose of the LDR (although was necessary due to the limited number of I/O pins available on the Digispark Attiny85 module). The main purpose of the LDR is to enable the Digispark Attiny85 module to assess light conditions and only enable the white LED if the ultrasonic sensor is tripped, and ambient conditions are 'dark' i.e., night-time. The 'darkness' is preset via resistor R6. R6 could be replaced by a potentiometer if it is desired to allow this to be altered by the user.
The choice of using the Digispark Attiny85 module to enable digital control of the joule thief and operation of the HC-SR04 ultrasonic sensor also means 5V DC power for the circuit is conveniently available from standard phone chargers via the micro-USB port.
An advantage of using the Digispark Module is that the firmware can be written and uploaded using the Arduino IDE. The code is available
and the following is a high-level overview:
Central to the firmware is the NewPing Arduino Library, this enables easy initialisation and reading the ultrasonic sensor.
The main loop consists of first checking the various inputs, that is, the state of the LDR and toggle switch and then reacting appropriately, depending if the LED is currently on or off.
If the LED is currently on, that is, the sensor was previously tripped a timer is checked to determine if the LED should remain illuminated or not.
If the LED is not illuminated, the ultrasonic sensor is checked, and if a signal within the set range is detected, the LED on status is changed, as long as the LDR is below the set limit, i.e., it is dark.
Finally, if the toggle button has been pressed, after some software debouncing, either the LED on time, if the LDR is in the dark, or the ultrasonic sensor range if the LDR is in the light,
is set according to how long the user holds down the toggle switch.
And then the main loop repeats.