Brightness control (PWM) for LED driver or bicycle headlight

PWM dimmer for MK ATmega8, battery-powered, and charge indication.

The article is intended for persons with some knowledge of radio electronics, namely:

  • what is a microcontroller and how to flash it,
  • what is PWM regulation,
  • what is led driver.

The project was conceived for installation on a bicycle. How it all began. My friends and I often participated in night bike rides, so we needed a headlight for the bike. Well, I didn’t want to put an ordinary flashlight ... I needed something more functional. For example, with a “small / medium / maximum” brightness adjustment, and since it was planned to use a lithium-ion battery as a power supply, a charge level indicator was also needed. I saw a lot of similar projects on the Internet, but somehow they did not suit me. For example, I came across projects of PWM dimmers, but they either did not have a charge level indicator, or the charge level indicator was on 1 ... 3 LEDs, but I did not like such a little information content. Well, do it, do it, and I took up the assembly of my project. So, as a charge indicator, I take 10 LEDs, or rather, I take an LED "column", like this:

I ordered this LED "column" in an online store (there are no radio stores in our city), so it will arrive only in a couple of weeks. Instead, I temporarily put 10 ordinary LEDs.

As a control microcontroller, I used ATmega8 (or ATmega328), since this MK has an ADC, with which I organized the measurement of the battery charge level. Also, this MK has a sufficient number of pins (and we want to connect as many as 10 LEDs). This microcontroller is common in radio stores, and is relatively cheap - in the range of 50 ... 100 rubles, depending on the greed of the store and the type of case.

To understand how the device works, let's look at the block diagram:

This article describes only what concerns the PWM controller (left side of the block diagram), and you choose the LED driver and the LED itself to your taste, the one that suits you best. The ZXSC400 driver suits me, so I will consider it as an example.

The PWM controller must be connected to an LED driver that has a dimming function (DIM, PWM, etc.), such as the ZXSC400. You can use any other suitable driver, as long as it supports PWM brightness control, and is powered by the same battery that powers the PWM controller. For those who do not know what an LED driver is, I will explain: a driver is needed so that the LED glows equally brightly both when the battery is charged and when the battery is dead. In other words, the LED driver maintains a stable current through the LED.

Typical wiring diagram for the ZXSC400 LED driver:

The power of this circuit must be connected to the power of our PWM controller, and the PWM output from the controller must be connected to the “STDN” input of the ZXSC400 driver. The output "STDN" just serves to adjust the brightness using a PWM signal. In a similar way, you can connect a PWM controller to many other LED drivers, but this is a separate issue.

Device operation algorithm. When power is applied, the MK displays the battery charge level for 1 second (on an LED scale of 10 LEDs), then the LED scale goes out, the MK goes into power saving mode, and waits for control commands. I did all the control on one button in order to pull fewer wires on the bike. When the button is held for more than 1 second, the PWM controller turns on, a signal with a duty cycle of 30% (1/3 of the LED brightness) is applied to the PWM output. When the button is held again for more than 1 second, the PWM controller turns off, no signal is sent to the PWM output (0% duty cycle). When the button is pressed briefly, the brightness switches from 30% - 60% - 100%, and the battery charge is displayed for 1 second. Thus, a single press changes the brightness of the LED, and a long press turns the LED on/off. To test the performance of the PWM controller, I connected a regular LED to its output, but I repeat once again - solely for the purpose of testing performance. In the future, I will connect the PWM controller to the ZXSC400 driver. The operation of the device is shown in more detail and clearly in the video (link at the end of the article).

The following diagram also shows the brightness adjustment process:

What to do if these brightness values ​​\u200b\u200bare not satisfied? For example, you want it to be like this: 1%, then 5%, then 100%. I have considered this option as well. Now the user can set these three brightness values ​​to whatever he wants! To do this, I wrote a small program that, based on the desired values, generates a file for EEPROM firmware. Having flashed this file into the microcontroller, the brightness will change accordingly to the desired ones. I am attaching a screenshot of the program window:

If you do not flash the EEPROM file, then the brightness values ​​\u200b\u200bwill remain "default" - 30%, 60%, 100%. A properly assembled device does not need to be configured. If desired, you can only adjust the minimum, average, and maximum brightness at your discretion. The program and instructions for use are at the end of the article.

Select the battery to be used. I used a Li-ion battery due to its prevalence and cheapness. But in the circuit, I provided a jumper J1, with which you can choose what we use as power.

If jumper J1 is in the "1" position, then one Li-ion battery is used. If jumper J1 is in position "2", then three ordinary AAA/AA/C/D batteries connected in series are used. Jumper J1 is necessary for the correct display of the battery charge level, since the operating voltage of a Li-ion battery is approximately in the range of 3.3 ... 4.2 V, and for conventional batteries, the operating voltage is approximately 3.0 ... 4.5 V. I have attached the battery voltage correspondence tables with the indicator readings at the bottom of the article.

Indicator LEDs. The LEDs that display the battery charge level can be anything. You can adjust their brightness within a small range by changing the value of the current-limiting resistor R1. To display the charge level, a dynamic indication is used, due to which energy savings are achieved, since only one LED is lit at a time. You can also watch the video about the indication of the battery charge level (link at the end of the article).

The microcontroller can be either ATmega8 or ATmega328. Both of these microcontrollers are compatible in the location of the contacts, and differ only in the content of the "firmware". I used an ATmega328 since I had this MK in stock. In order to reduce power consumption, the microcontroller is powered by an internal 1 MHz RC oscillator. The microcontroller program was written in environment 4.3.6.61 (or 4.3.9.65).

The circuit uses a TL431 reference voltage source chip. With its help, a good accuracy of measuring the battery voltage is achieved. Power is supplied to the TL431 from the PC1 pin of the microcontroller through the resistor R3. The supply voltage to the TL431 occurs only during the indication of the charge level. After the indication LEDs go out, the supply voltage is cut off, saving battery power. The TL431 chip can be found in unusable computer power supplies, in broken cell phone chargers, in switching power supplies from laptops and various electronic equipment. I used TL431 in SOIC-8 package (smd option), but TL431 is more common in TO-92 package, so I made several PCB options.

About emulation in the program "". The project in Proteus does not work correctly. Due to the fact that the ATmega8 model does not wake up, and also with brakes, a dynamic indication is displayed. If, after starting the project, immediately hold down the button so that the PWM controller turns on, then everything works. But it’s worth turning off the PWM controller by holding the button again, as the MK will go to sleep and will not wake up again (until the project is restarted). I don't attach the project in Proteus. Who wants to play - write, I will send the project to Proteus.

Main technical characteristics:

  • Supply voltage at which operability is guaranteed: 2.8 ... 5 volts
  • PWM signal frequency: 244Hz
  • Frequency of dynamic indication of the scale of 10 LEDs: 488 Hz (per 10 LEDs) or 48.8 Hz (per LED)
  • Number of brightness modes cycled through: 3 modes
  • Ability to change the brightness of each mode by the user: Yes

Below you can download firmware for MK ATmega8 and ATmega328

Shutov Maxim, Velsk

List of radio elements

Designation Type Denomination Quantity NoteShopMy notepad
U1 MK AVR 8-bit

ATmega8-16PU

1 To notepad
U2 Reference IC

TL431ILP

1 To notepad
Resistors
R1, R2 Resistor constant SMD 1206

330 ohm

2 To notepad
R3 Resistor constant SMD 1206

1 kOhm

1 To notepad
R4 Resistor constant SMD 1206

10 kOhm

1 To notepad
R5 Resistor constant SMD 1206

47 kOhm

1 To notepad
Resistor constant SMD 1206