• Usha Saha

Controlling the brightness of an LED with Raspberry Pi | PWM

Updated: Dec 1, 2020

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.

Circuit Diagram
Circuit Diagram

Codes


import RPi.GPIO as GPIO

The raspberry pi GPIO library is added. This is used for the setting of mode in which the GPIO works.

from time import sleep 

The sleep module is imported from the time library, this is basically used to set delay.l

led_pin = 12 

The led pin is initialized.

GPIO.setmode(GPIO.BOARD)

The mode is set up for the GPIO. Here the mode name is GPIO.BOARD.

GPIO.setup(led_pin, GPIO.OUT)

The led pin is setup as the output.

pwm = GPIO.PWM(led_pin, 100)  

The PWM function is initialized that is used to toggle the brightness of the led. The frequency of the signals is taken as 100Hz.

pwm.start(0)  

Next, the PWM signal is initialized to zero in the beginning.

while 1:         

A while loop is executed for an infinite time.

  try:
  for x in range(100):  
   pwm.ChangeDutyCycle(x) # Change duty cycle
   sleep(0.01)    

Under the while loop, a 'for' loop is executed with a range of 100. This means the value of x varies from 0-100 duty cycle. After the execution of the cycle, a delay of 10ms is added to monitor led light variation in a better manner. This for loop does a low to high function.

for x in range(100,0,-1):
            pwm.ChangeDutyCycle(x)
            sleep(0.01)

This "for" loop performs a reverse operation as compared to the previous one. That is it counts form high to low with a step of minus 1. The try cycle completes with the end of this loop.

pwm.stop() 

The PWM signal is made to stop.

GPIO.cleanup()

Lastly, the GPIO is cleaned up and assigned with zero.


Complete code

import RPi.GPIO as GPIO
from time import sleep 
led_pin = 12           
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led_pin, GPIO.OUT) 
pwm = GPIO.PWM(led_pin, 100)  
pwm.start(0)                    # Started PWM at 0% duty cycle
try:
    while 1:                  
        for x in range(100):  
            pwm.ChangeDutyCycle(x) # Change duty cycle
            sleep(0.01)         
            for x in range(100,0,-1):
            pwm.ChangeDutyCycle(x)
            sleep(0.01)
            except KeyboardInterrupt:
             pass        
             pwm.stop()      
             GPIO.cleanup()

Download the codes form here

pwm_led (Main)
.txt
TXT • 596B


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

Video explanation of the entire project for a better perception.

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

INTERFACING SERVO MOTOR WITH ARDUINO

Interfacing servo motor with RPi

Controlling Raspberry Pi's GPIOs using the Telegram bot

Interfacing DC motor with Arduino



309 views