Skip to content

Controlling LED Brightness with PWM: Dimming Lights with a Gentle Touch :)

Recently, I’ve been tinkering again with embedded systems, as you might know. Electronics can sometimes be mind-boggling but dealing with such intricate details is also quite enjoyable, honestly. Recently, in a project, I needed to adjust the brightness of LEDs. You know, not a blinding light in a totally dark environment but a softer lighting more suitable for the ambiance, right? That’s exactly the case. My first thought was of course PWM. Those who are at least somewhat familiar with PWM can probably guess how useful it is. If you don’t know, don’t worry, we will go into details soon.

PWM, which stands for Pulse Width Modulation, is simply a method to control the average power by adjusting the on and off durations of a signal. In electrical circuits, it is widely used for controlling motor speeds or LED brightness. For example, think of a LED. Instead of being constantly on at full brightness, it flickers on and off very quickly, such that our eyes cannot perceive this. By adjusting this flickering duration, we can control how bright the LED appears. Isn’t that fantastic?

So, how will we do this? Usually, with microcontrollers like Arduino, it’s very easy. Microcontrollers have pins that output PWM signals, and by sending specific commands to these pins, we can set the desired brightness level. For example, in Arduino UNO, pins 3, 5, 6, 9, 10, and 11 provide PWM output. By using these pins, you can control the LED’s brightness with a value between 0 and 255. Zero means completely off, and 255 means full brightness.

Meanwhile, there are a few things to pay attention to when adjusting brightness. First, do not exceed the maximum current of your LED. If you provide too much current, the LED can burn out, which is undesirable. Usually, a resistor is placed in front of the LED to limit the current. When calculating the value of this resistor, you need to consider the LED’s operating voltage and current, as well as the PWM output voltage of the microcontroller. You can find ready-made calculators online for this purpose, I usually check those. For example, you can search for: LED resistor calculation. With such simple calculations, you can prevent damage to the circuit.

The second point to consider is the frequency of the PWM signal. At higher frequencies, the LED appears to be continuously lit, while at lower frequencies, flickering might be more noticeable. Generally, Arduino’s default PWM frequency is sufficient for most applications, but for some sensitive tasks, it may be necessary to change this frequency. However, most of the time, default settings are enough, so diving into details isn’t always necessary, in my opinion.

Now let’s look at the code part. Let’s see how to do this with a simple Arduino code. Suppose we connect the LED to pin 9 and want to control its brightness. Here is the code:

// WRONG EXAMPLE CODE: Only sets a single brightness level. // In this code, brightness cannot be adjusted, the LED is always the same. const int ledPin = 9; // LED connected pin

void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output }

void loop() { // Brightness at maximum analogWrite(ledPin, 255); delay(1000); // wait 1 second // Turn off the LED analogWrite(ledPin, 0); delay(1000); // wait 1 second }

As seen above, the code just turns the LED on and off. No brightness adjustment. Naturally, this is not what we want. Now let’s look at the correct, functional code. We will set up a loop that gradually increases and then decreases the brightness. This way, we can see the LED flickering more clearly, even observe the flickering effect 🙂

Here’s a more functional code that adjusts brightness:

// CORRECT EXAMPLE CODE: Brightness gradually adjusted. const int ledPin = 9; // LED connected pin

void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output }

void loop() { // Increase brightness from 0 to 255 for (int brightness = 0; brightness <= 255; brightness += 5) { analogWrite(ledPin, brightness); // Set LED brightness delay(30); // Small delay for smoother transition }

// Decrease brightness from 255 to 0 for (int brightness = 255; brightness >= 0; brightness -= 5) { analogWrite(ledPin, brightness); // Set LED brightness delay(30); // Small delay for smoother transition } // Pause before repeating the cycle delay(500); }

With this code, the LED’s brightness will gradually increase, reach full brightness, then decrease again gradually. This cycle will repeat indefinitely. You can experience different brightness levels visually. Try it out, I think you will like it 🙂 By the way, you can further develop this code. For example, connect a potentiometer (adjustment knob) to manually control the brightness. Or, you can control the brightness by sending commands from a computer via serial communication. That makes things even more fun.

In conclusion, PWM control of LED brightness is a very simple yet highly useful technique. You can use this method to create different effects or just achieve more pleasant lighting in your electronics projects. The key point is to connect everything correctly and pay attention to the code. Even simple circuits can give very satisfying results. You can find many resources online on this topic, and if you search for Arduino PWM LED brightness, many videos and articles will come up.

Recently, I used this technique in a friend’s project, and it really changed the atmosphere of the environment. Small details, but they make a big difference. Believe me, sometimes the simplest solutions are the most effective ones. Anyway, try it yourself and see how it turns out.

Ultimately, PWM is one of the foundational techniques in embedded systems. It is not only used for controlling LED brightness but also for motor control and sound production, among many other areas. Knowing this technique well will make your future projects easier. Remember, every big project starts with small steps.

Now, go ahead and try these codes. Maybe you will make a small bedside lamp, who knows? Or use it in a hobby project. I personally love small but impactful things. It’s practical and educational. Oh, and by the way, while writing this article, I just remembered—I might also write about motor speed control with PWM someday. Because PWM works similarly for motors too.

I hope this article was helpful for you. If you have any questions, don’t hesitate to ask—I’ll try my best to assist. Sometimes things in technical details can be confusing, which is normal. The important thing is to keep trying. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.