Path: Home => AVR-EN => Applications => Spotlight tn13   Diese Seite in Deutsch: Flag DE Logo
Spotlight small AVR applications

LED intensity and battery control for a spotlight with an ATtiny13
Hardware, mounting, use and software for a spotlight

Spotlight ATtiny13

Spotlight with an ATtiny13
  1. Properties
  2. Hardware
  3. Mounting
  4. Software
These pages as PDF (30 pages, 1.6 MB).

1 Features of the spotlight with ATtiny13

The hardware properties of the spotlight are:

Page top Properties Hardware Mounting Software

2 Hardware

2.1 Hardware of the PWM switched version

2.1.1 Electrical schematic of the switched version

Schematic of the spotlight The hardware consists of
  1. the controller part with the ATtiny13 and its In-System-Programming connection,
  2. the 22 constant current drivers with BC547 transistors (any small NPN type can be used) and the 26-pin connector to connect the LED rows on the front plate (the four pins on the corner are plus so the plug can be connected either way),
  3. the battery supply part and the 5 V supply derived from that via a medium power transistor and a regulator.

2.1.2 Switched constant current generation

The generation of the constant currents works with a pulse-width-modulated signal from the OC0A pin of the controller. Each PWM cycle lasts 853.3 µs, leading to a frequency of 1,172 Hz. The output pin OC0A is either for a single step of the 256 steps on (minimum intensity) or during up to 256 steps.

The PWM signal on OC0A switches, via a resistor of 220Ω and the two diodes 1N4148, a constant voltage of either 0.0 or 1.4 V. This voltage controls, via resistors of 220 Ω the bases of all 22 BC547 transistors (can be replaced by any small NPN type). If the constant voltage is at 1.4 V, the base voltage increases the current between emitter and collector until the voltage of the emitter is at 0.7 V, by that controlling this current. As the emitter resistor to ground is at 33 Ω, a current of around IE = 0.7/33*1000 = 21.21 mA results.

This constant current remains the same as long as the battery voltage is larger than the sum of all LED voltages of a row (Urow = 9 * 3.23 V = 29.07 V) plus 0.7 V emitter voltage plus 0.12 V collector/emitter saturation voltage, roughly 30 V. If the battery voltage is higher than 30 V, the voltage difference is consumed by the collector-emitter of the transistor. With the hardware as it is up to 43 V battery voltage can be driven in that way, until the transistors reach their maximum thermal load.

2.1.3 Controller operating voltage

From a nominal battery voltage of Ubatt = 9 * 4.3 V = 38.7 V (freshly charged) the operating voltage of 5 V for the controller cannot be produced by a standard voltage regulator such as a 7805, because its maximum input voltage (35 V) would be exceeded.

Therefore the battery voltage is first reduced to 7.5 V with a power transistor BD439 (any power NPN with 1 W and more can be used). This pre-regulator works with a Zener diode of 8.2 V, with Zener noise blocked by an electrolytic capacitor of 10 µF, and driven by a resistor of 6k8 with approximately 2.8 mA. This constant voltage of 8.2 V holds the base of the power transistor at that voltage and produces a voltage of U = 8.2 - 0.7 = 7.5 V on the emitter. As the power transistor consumes most of the thermal power, a small regulator such as a 78L05 can be used to regulate the 7.5 V down to 5 V.

2.1.4 The controller

The controller ATtiny13 performs the following tasks:
  1. It generates the 8 bit PWM signal on OC0A for the switched constant voltage for the current regulators.
  2. It measures the voltage on the manually adjusted potentiometer that regulates the intensity of the LEDs with its AD converter, software converts this voltage to a value of between 0 and 255 for the PWM.
  3. It measures the battery voltage, which is divided with resistors of 82k and 10k and smoothed by a capacitor of 10 nF, with its ADC. Software converts the measured voltage to drive the green/red LED as follows:
  4. The controller is connected with the ISP6 interface where it can be programmed within the running system without other modifications.

2.1.5 Possible variations

Several changes can be made to this basic hardware:

2.2 Hardware of the linear current-controlled LEDs

This version is nearly identical to the switched version, but the constant-current drivers are not switched but driven linearly with a constant voltage. This causes a few changes to the hardware and software.

2.2.1 Electrical schematic of the linear version

Schematic of the linear spotlight version This looks very similar to the switched version, with the following differences:
  1. The PWM output OC0A generates, with the resistor of 220 Ω and the electrolytic capacitor of 1000 µF a nearly constant voltage. This voltage only varies with the PWM's compare value (by that regulating the generated voltage) and by the switching of the PWM. The PWM in that case works at 4.688 Hz, the fast load- and unload-phases of the PWM cause only small changes in voltage (ripple, at 50% pulse width for example and a voltage of 2.08 V those are only +/- 1.8 mV).
  2. The generated constant voltage drives the bases of the current-regulating transistors BC547B (any small NPN with a hFE of typically 150 can be used) and generates a constant current via the emitter resistors of 180 Ω, in the example case with 50% pulse width of 7.96 mA.

LED current at different PWM values The LED current in mA is given by the equation
Iled = 0.088 * PWM value - 3.26

The lower part, where the LED current is zero, results from the base-emitter cut-off voltage, when the capacitor is below 0.65 V. At this voltages the transistors are not yet driving current. It is a matter of software to linearize this curve.

During the calculation of these values I realized that
  1. the sum of the 22 base currents plays a relevant role, as it unloads the capacitor slightly faster than it is loaded. Base current is roughly 2.8 mA at high LED current, while the PWM unload current (with OC0A in low state) at the highest PWM ratio is at -15.2 mA. The base current increases unloading slightly. To keep the base current small the transistor BC547B with a typical hFE of 150 was used.
  2. the voltage drops on the OC0A output due to current flow is also relevant. So the voltage of the OC0A output in high state is not at 5.0 V but 4.45 V only (with a minimum of 4,2 V), caused by the current to be delivered. In low state roughly 0,32 V instead of 0.0 V result, with a maximum of 0.62 V. Without this effect very different voltages would result.

Page top Properties Hardware Mounting Software

3 Mounting

3.1 Mounting the PWM switched components on a breadboard

Layout on a breadboard This is the layout for the switched version, as designed for a 80-by-50 mm breadboard that I have used.

Breadboard with components This is the breadboard view as built with all components mounted.

3.2 Mounting the linear version on a PCB

Layout of a PCB for the linear version This is the PCB layout for the linear version at a size of 80-by-50mm. Three bridges have to be soldered for a single sided PCB. Base resistors like in the switched version are not included, as the electrical schematic shows.

Component placement on the PCB, linear version This is the component placement on the PCB and the drill plan for the linear version.

The same PCB layout can also be used for the switched version, just replace the electrolytic capacitor of 1000 µF by two diodes and use 33 Ω resistors for the emitters.

In both versions the BD439 has to bear an increased thermal load. To reduce its temperature I screwed a small aluminium plate on it to increase heat dissipation. Further cooling measures are not necessary.

3.3 Front plate with LEDs

LED front plate Here the layout of the front plate with the LEDs. It was made from 2 mm acrylic glass plate of the size 295-by-250 mm. The horizontal distance between the LEDs is 30 mm, the vertical distance is 10 mm. The distance is sufficient, but take care when drilling the large holes.

Drilling starts with a 1.5mm, at low speed to avoid melting, and increases. It has some advantages if your drilling machine can rotate in inverse direction.

The nine LEDs of each row are connected by carefully angling the wires in 5 mm distance to the LED and connecting anodes with cathodes of the next LED.

The connection between the front plate with the LEDs and the breadboard is done with a 26-pin flat cable. The four corner pins are connected to the anodes of the 22 last LEDs in the rows, so it can be plugged in either way. The other 22 pins are each connected with the cathodes of the rows.

3.4 Compact test equipment

Plug-in test board Test board To test the electronic and the software a small plug-in test board has been soldered with 22 ordinary LEDS and a 470Ω resistor. It can be plugged in on top of the breadboard or PCB.

The test board works from 15 V upwards. For a short time operation at 33.3 V is possible, having the transistors at full intensity (without PWM reduction) at approx. 430 mW heat power. The heat power of the resistors is at 210 mW and independent from the operating voltage. Those are getting really hot, so avoid touching them.

3.5 Power supply

Those who want to use the spotlight stationary can skip the batteries and can supply the spotlight with a power supply.

Schematic power supply spotlight This power supply unit produces the necessary voltage (roughly 31V under maximum load) and the necessary current (max. 660mA). Because 2x24V transformers are not available, the device works with a single coil transformer and a Graetz diode bridge.

Those who want to use the spotlight in both manners, mobile as well as stationary, can separate the batteries with a 1N4001 power diode, so that the attached power supply with the batteries switched on and at low LED intensity cannot feed current back into the batteries. Because the voltage of the power supply drops at currents well below 400mA, you can operate both without a problem. You should know that in that case the supply comes predominantly from the batteries and the power supply only delivers more current if the battery voltage drops to below 33V.

Power supply plug modes The external power supply can be plugged in with a 5mm supply plug. Because such plugs often provide a switch, you can use this to switch to the external supply when the plug is in. Depending from the polarity of the center pin either Plus or Minus are switched. With such a switched plug neither the diode is necessary nor are any concerns of interaction between the batteries and the power supply remain.

Voltage at full load This is the power supply's voltage at full load. The remaining ripple is removed by the constant-current-regulating transistors. There is enough over-voltage so that the regulation works perfect.

Voltage at zero load That would be the voltage at zero-load. The 40.7V would cause a heat production of 220mW at each transistor, which is not too much for small transistors. Because there is at least a load of 10mA for the voltage regulation and the ATtiny13's PWM this maximum case is not reached.

Spotlight power supply Spotlight power supply in a box The power supply can be mounted in a small box. Output voltage is on two 4mm plugs. The box needs some slots for air convection because the transformer gets rather hot under nominal load.

Page top Properties Hardware Mounting Software

4 Software

4.1 Download

The assembler source code for
  1. the switched version can be downloaded from here or can be viewed in the webbrowser here,
  2. the linear version can be downloaded from here or can be viewed in the webbrowser here.

The following shows how the software works.

4.2 Debug switches in the software

Two debug switches are built in at the start of the source code. Those switches ease simulation.
  1. debug_vtg: Here a simulated voltage can be defined and its conversion to the Compare B value of the TC0 can be simulated.
  2. debug_15V: This serves to test the software with an operating voltage from 12 to 18 V.
Both switches have to be at zero to assemble a working version.

4.2.1 Switch debug_vtg

Those who want to know and understand how the conversion of measured battery voltages to green/red LED PWM compare values works can set this switch and follow conversion, e. g. with avr_sim. The voltage to be converted can be given in the constant debug_voltage in mV. The voltage can be given with any number but should be within the display range (26 to 37 V, if the switch debug_15V is set to one then 11 to 18 V). Simulation of normal operation is a bit lengthy because 64 AD conversions last at least an hour simulation time.

4.2.2 Switch debug_15V

If your supply has only 12, 15 or up to 18 V, you can use the test equipment and the altered calculation method for the green/red LED.

4.2.3 Switch debug_pwm

This switch is only included in the linear version. In simulation it allows to skip the lengthy ADC conversion process by setting a desired potentiometer state as result of the AD conversion and to follow the PWM value calculation process directly.

4.3 The pulse width control

For the intensity regulation and for the green/red operating voltage LED the timer TC0 is used. In the switched version the timer is clocked by the internal RC oscillator, divided by four, at 2.4 MHz, and by a prescaler of eight (300 kHz). When the timer reaches 255, it restarts with the next clock signal (1,171.875 Hz) and sets the OC0A and OC0B pins high, by that switching the base voltage of the constant-current transistors to 1.4 V. When the count reaches OCR0A or OCR0B, the pins OCR0A resp. OCR0B are cleared, the constant voltage goes down to zero and switches the transistor off.

The higher the compare value in OCR0A is, the longer the white LEDs on the front plate remain on and the higher is their intensity. This is linear: doubling the compare value doubles the intensity. As the LEDs are either driven fully with the constant current (at the beginning of the PWM cycle) or are not driven at all (when the compare has been reached) no partial current flows (that would alter the color of the LED).

In the linear version the controller is clocked by its default setting at 1.2 MHz. The prescaler of TC0 is 1, so the PWM frequency is 1.2 MHz / 256 = 4.6875 kHz. The PWM loads/unloads the electrolytical capacitor of 1000 µF. The voltage on the capacitor controls the constant current drivers.

The green/red LED for battery voltage control is operated differently. If the cathode of the green LED (= anode of the red LED) is on low, its intensity increases with increasing compare values in OCR0B. The higher the battery voltage, the higher is the compare value.

If the anode of the red LED (= cathode of green LED) is high the behavior reversed: the LED is on while the compare value is exceeded and off at the PWM cycle start. The higher the compare value the lower is its intensity. When converting the measured voltage to a red compare value this different behavior has to be considered.

The high PWM frequency of 1,172 Hz in the switched version was chosen to reduce patterning (moiré) which occurs at smaller frequencies. See the short examples here of those effects on a Fuji camera. Even half of that frequency still causes those patterns. The linear version does not show such patterns.

4.4 AD conversion measurements

Each compare match A of the timer TC0 starts an AD conversion. This is achieved by Because the ADIE bit in ADCSRA is also set, conversion completion leads to an interrupt. The interrupt service routine sets the T flag in the status register. Further reaction is performed outside the ISR, after the controller woke up from sleep and following the ISR execution.

Measurements start with the channel to which the potentiometer is attached. 64 measurements are performed and the results are added. After all 64 measurements have been summed up, the channel with the divided battery voltage is selected.

4.4.1 Measurements of the potentiometer voltage

In the switched version, the sum of the 64 measurements, its MSB, provides directly the compare value for the PWM, to be written to OCR0A.

In the linear version, the sum of the 64 measurements has to be multiplied. The multiplication factor is (256-cPwmMin). cPwmMin is finally added to bits 16 to 23 of the multiplication result and this byte is written to OCR0A.

4.4.2 Measurements of the battery voltage

When the timer TC0 reaches the compare value in OCR0A (at the time when the LEDs are switched off) the AD conversion is triggered. That means, the AD converter always measures the voltage at the end of the ON cycle.

As the sample&hold cycle of the ADC starts slightly delayed while the constant voltage is immediately switched off, the voltage for the ADC has to be slightly delayed. That is a task for a capacitor.

Filter on the ADC input The battery voltage is divided by the resistors 82k and 10k by the factor 9.2, so that the AD voltage input remains below the 5 V operating voltage of the ATtiny13. To delay the increase slightly a capacitor of 10 nF is attached. This combination of resistors and the capacitor causes a delay, but not too large.

The diagram shows that the voltage change on the ADC input following a switching from on (battery voltage low due to current flow) to off slowly changes but at last reaches the high state. Only if the ON pulse is very short, the measured voltage can be too high.

Sample-and-Hold delay and voltage change The following only applies for the switched version. In the linear version the current is constant, so no voltage changes have to be considered.

The ADC voltage change during the Sample-and-Hold delay of 1.5 ADC clock cycles is displayed here. Note that the axis is in µs. The difference in the measured voltage is rather small.

In all calculations the following conditions were used:

Page top Properties Hardware Mounting Software

Praise, error reports, scolding and spam please via the comment page to me.

©2018 by