top of page

Controlling the brightness of an LED with Raspberry Pi | PWM

Updated: Apr 5

Through this article, Controlling the brightness of an LED with Raspberry Pi using PWM, one would learn how the brightness of an led can be controlled using the PWM signal. Here #PWM stands for Pulse Width Modulation. This is a technique that can be used to control the speed of a #dc motor or the #brightness of an led or controlling the #servo motor.



Elements used

The image shows the list of components needed. Let's have a quick peek at each of them.


All components together
All components together

Raspberry Pi 3 Model B+

The Raspberry Pi is a series of small single-board computers. Here 3 Model B+ indicates the generation of the RPi board. The Raspberry Pi is also written as RPI or RPi or Raspberry Pie. The Raspberry Pi Model B+ is of the nearby size of a credit card.


Raspberry Pi 3 Model  B+
Raspberry Pi 3 Model B+

Bread Board:- A breadboard is a rectangular board with small holes in it which are used for connecting elements. The connections are not fixed and can be varied. In a breadboard, all the holes in one horizontal line are in series connection with each other. Breadboards are made from plastic and come in all shapes, sizes, and even different colors, the most common sizes are "full-size," "half-size," and "mini" breadboards.

Bread Board
Bread Board

Jumper wires:- Jumper wires are used to establish a connection between different elements in the circuit and #Raspberrypi board. There are two types of wire, male and female. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end.

Jumper Wires
Jumper Wires

Resistor:- A 1k resistor used in the circuit is shown below.

1K Ohm resistors
1K Ohm resistors

LED:- A Light Emitting Diode is a semiconductor device that emits light when current flows through it.

LED
LED

USB Cable

This cable is used to connect the RaspberryPi to the power supply. It also helps in the transmission of data.

USB Cable
USB Cable

Circuit Diagram

The circuit diagram is as shown below, where the cathode(negative) pin of the led is connected to the ground pin of the Raspberry Pi. Another end of the diode that is the anode(positive) end of the led is connected to one end of the resistor. And the other end of the resistor is connected to the #GPIO pin of the Raspberry Pi. Specifically, GPIO 18 which provides the PWM signals.



Download the code from here



Working


The Raspberry Pi's GPIO gives either 3.3 volts as maximum or zero volts, so the output is a square wave and in order to vary the brightness, we cannot get the voltage between 0-3.3 volts. Therefore here we use the concept of #duty cycle that is the on and off time of the signal and vary the brightness accordingly.



Voltage vs Time graph
Voltage vs Time graph

The figure above shows the voltage-time graph with a maximum voltage of 3.3v and a minimum voltage of zero. The thing which is to be noted here is that with the variation of the duty cycle the brightness of an led can be varied with the help of #Pulse Width Modulation(PWM).


As notable from the graph at 100% duty cycle the graph is almost constant whereas at 80% duty cycle in the graph a small T-off time is there, a time where the output is low. Similarly constantly changing the duty cycle changes the T-on and T-off time and thus we can change the brightness of the led.


The images are shown below which shows the variation in the brightness of the led.


Hardware Output 1
Hardware Output 1

Hardware Output 2
Hardware Output 2

Hardware Output 3
Hardware Output 3

Through this project, one would learn how to control the brightness of an led with the PWM signal. There are many other applications of PWM and with this knowledge, one can explore the same on a DC motor and see how to control the DC motor.

 

Visit the links below to explore more



2,068 views2 comments

Related Posts

See All
bottom of page