top of page

LED Brightness Control with Shift Registers

LEDs (Light-Emitting Diodes) have become a popular choice for lighting solutions due to their efficiency, longevity, and versatility. However, controlling the brightness of LEDs can be challenging. In this article, we will explore the concept of LED brightness control with a 74HC595 shift register, understand how they work, and learn how to implement them in a circuit.



Wiring a 74HC595 Shift Register to an Arduino

This section gives a basic overview of how to wire the 74HC595 shift register to an Arduino. If you're looking for detailed instructions, look at this article, where I cover the basics.


Required Materials

  • Arduino

  • 74HC595 Shift Register

  • 8 x LEDs

  • 8 x 220Ω Resistors

  • Breadboard

  • Jumper wires


Below is a table of the connections that need to be made from the shift register to the Arduino.

Shift Register

Arduino

OE (Output Enable)

Digital Pin 3

SER (Serial Data)

Digital Pin 4

RCLK (Register Clock)

Digital Pin 5

SRCLK (Shift Register Clock)

Digital Pin 6

SRCLR (Shift Register Clear)

5V (VCC)

VCC (Power Supply)

5V (VCC)

GND (Ground)

Ground (GND)

Q0-Q7 (Output Pins)

LEDs

Ensure that the output enable pin (OE, pin 13 on the shift register) is connected to pin 3 on the Arduino and not directly tied to the ground rail. Below is a picture of the schematic and a wiring diagram.

Schematic of 74HC595 shift register wired to eight LEDs
Diagram of 74HC595 shift register wired to eight LEDs and an Arduino Uno

Arduino Code for 74HC595 LED Brightness Control


In the Arduino IDE, create a new sketch and define the following variables:

The constant values are the pins that send data to and control the shift register. The numbers should match the pin used on the physical microcontroller.


Next, we need to set the pins to outputs. We can accomplish this using the "pinMode" function within the "setup" function.


After setting up the pins, we need to create two functions that can send data to the shift register and another that can control the brightness of the LEDs. Below are two functions that can accomplish this.


updateShiftRegister

Responsible for shifting data into the shift register. Here's a breakdown of how it accomplishes this:

  1. It begins by setting the "latchPin" to LOW. This action drops the latch pin to GND, preparing the shift register to receive new data.

  2. The "shiftOut" function then writes the data to the shift register

  3. Once the data has been shifted into the shift register, the "latchPin" is set to HIGH. This action pushes the data from the shift register's internal storage to the output pins, thereby updating the state of the LEDs connected to those pins.


setBrightness

Controls the brightness of the LEDs. It uses the "analogWrite" function to achieve this. Here's how it works:

  1. The brightness value is passed as an argument to the function. This value represents the desired brightness level, ranging from 0 (completely off) to 255 (fully on).

  2. Inside the function, the "analogWrite" function controls the outputEnablePin. The "analogWrite" function creates a pulse-width modulation (PWM) signal to quickly turn the output pins on/off, giving the allusion that the LEDs are dimming.

  3. 255 - brightness: The brightness value is subtracted from 255 to invert it. This is because the OE pin is an active low, meaning that a LOW signal enables the outputs, while a HIGH signal disables them. So, when you want the LEDs to be at max brightness, you have to write a 0 (GND) to the OE pin, and when you want the LEDs entirely off, you have to write a 1 (+5v).


Looking back at the "setup" function, let's use the "updateShiftRegister" function to turn all eight LEDs on.

We use the value of 255 because every bit in the byte of data we send to the shift register will have a value of 1.


Now that all LEDs are on once power is supplied to the Arduino, we can change the LEDs' brightness in the "loop" function. Below is an example of dimming the LEDs until they're off and then slowly brightening them until they're at 100% brightness.

Here's how the code works:

  1. The code enters a for loop that counts down from 255 to 0 using the variable "i" as the loop counter. On each iteration, the loop changes the brightness of the LEDs using the "setBrightness" function. We can see every attainable brightness level by passing in the variable "i" to the "setBrightness" function. After setting the brightness, the program pauses briefly to prevent the lights from dimming too fast.

  2. Once the LEDs a completely off (first for loop completed), the program enters another for loop that counts up from 0 to 255 again using the variable "i" as the loop counter. This loop goes through the same process as the first for loop, only in reverse.




Completed Code




567 views0 comments
bottom of page