Raspberry Pi With Pwm Led Two Pushbuttons Breadboard Featured Image

How to control LED brightness on a Raspberry Pi using PWM

If you have fun making Flashing lights on Raspberry PiWait until you hear about its brightness control! In this guide, we work with a pair of buttons to adjust LED brightness on a Raspberry Pi.

Starting your first Raspberry Pi project? Here’s how to do it quickly Install Raspberry Pi OS on your device.

What does PWM do for an LEDصباح

PWM, or Pulse Width Modulation, is a method that artificially reduces the voltage output of the Raspberry Pi GPIO (General Purpose Input/Output) pins. It’s artificial, because you’re not really reducing the voltage and you just turn it on and off so fast that the total voltage becomes less than the real voltage you’re applying to it.

For a LED or light-emitting diode, increasing the total voltage makes it shine more, while decreasing it makes it dimmer. But since Raspberry Pi has no analog output, we use PWM to control LED brightness.

What will you need

  • 2 push buttons
  • 3 resistors (250-550Ω will work. Use a lower rating if the LED is too dark.)
  • 1 LED (any color)
  • the board
  • Crossing wires
  • Raspberry Pi (any model except Pi Pico)

How to use PWM to control LED brightness on a Raspberry Pi

In this instructable we use two buttons to make the LED light brighter or dimmer with PWM. Pressing the “brighter” button increases the PWM output, while pressing the “dimmer” button decreases it.

circuit preparation

  1. Let’s start with the LED. On the breadboard, place the LED and connect the resistor to one side. It does not matter which side the resistor is placed on.
  1. Connect the jumper next to its cathode. This will indicate number 11 on the Raspberry Pi. Add another jumper that leads to the blue rail on the breadboard, then add another jumper from that blue rail to pin 9 on the Raspberry Pi, which is GND.
The LED and resistor on the board are connected to the Raspberry Pi for the Raspberry Pi Pwm Led Guide

NB: To find the correct PIN on your Raspberry Pi, hold the board so that the GPIO pin tray is on the right. The top left pin should be pin 1, Pin 2 should be to the right, and Pin 3 should be below.

Raspberry Pi Pinout
  1. You will need to build the pushbuttons. Place the pushbuttons on the breadboard and add a resistor to one leg of each pushbutton. The other side of the resistor should lead to the blue rail of the breadboard.

advice: Want to learn more about pushbuttons? We have a complete guide dedicated to showing you how to do it Use pushbuttons with Raspberry Pi GPIO pins.

  1. Add jumper wires in parallel connection with the resistor and push button. Connect the other side of these terminals to pins 13 (the “brighter” button) and 15 (the “dimmer” button).
Push Buttons on Board with Led for Raspberry Pi Pwm Led Manual
  1. Use a jumper wire to connect the side pushbuttons to the red rail of the board.
Red jumper wires on the breadboard push-buttons for the Raspberry Pi Pwm Led Guide
  1. Connect the red rail to a 3.3V source on the Raspberry Pi, such as pin 1.

If Python is your programming language of choice, learn how Install and manage multiple versions of Python in Linux.

Blog Preparation

Using your favorite code editing tool, create a new file and save it with the name “rpi-lcdpwm.py.”

  1. Start with the code below, which gives you two ways to import modules on Python: one imports a file RPi.GPIO unit and lets you just call him GPIOThe second only imports a file sleep() function of the whole time lonliness.
import RPi.GPIO as GPIO
from time import sleep
  1. Mark the pin numbers to make it easier to change pins in case you change your mind later.
ledPin = 11
brightenButton = 13
dimButton = 15
  1. my choice: add the line GPIO.setwarnings(False) So that you can avoid the GPIO warning message when you start the script later.
  1. Set the pin selection method. BOARD It is a good choice for beginners, as it is easy to search for pins without having to go back to the pinout. The other way is BCM, which means “Broadcom”. This uses Broadcom numbers assigned to each pin, which may vary based on your Raspberry Pi model.
  1. Set the GPIO pins as input or output. We give up ledPin as an output pin and will always start its state as LOW. Set the next two lines brightenButton And the dimButton As the input pins that listen for button pushes. This should also be set as GPIO.PUD_DOWN To set it as pull-down resistors are used.
GPIO.setup(ledPin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(brightenButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dimButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  1. Let’s declare PWM. pwmLEDPin It is a variable that is easy to write GPIO.PWM(ledPin, 100) Later, the .start(0) The command starts the PWM process. We can now change the output ledPin using PWM.
pwmLEDPin = GPIO.PWM(ledPin, 100)
pwmLEDPin.start(0)
  1. Duty cycle is the percentage of time a pin is active during the pulse wave. Here, we set the duty cycle to 100% first. We had a fairly lengthy discussion on this topic in our area A guide to using servo motors with a Raspberry Pi, If you are interested.
dutyCycle = 100
GPIO.output(ledPin, GPIO.HIGH)
  1. For the iterative part, we put a file while A loop that works almost forever.
  1. At the beginning of this iteration cycle, we update the duty cycle.
pwmLEDPin.ChangeDutyCycle(dutyCycle)
  1. Let’s program what brightenButton Do. When the raspberry detects electricity passing through the pin to brightenButtona message will appear saying “BrightenButton is HIGH”, which adds 5 to the current value of the duty cycle until it reaches 100.
if GPIO.input(brightenButton) == GPIO.HIGH:
                print("brightenButton is HIGH")
                if dutyCycle < 100:
                        dutyCycle += 5
                        sleep(0.25)
                else: dutyCycle = 100
  1. When programming a file dimButton You do the opposite, decreasing the value by 5 until it reaches 0.
elif GPIO.input(dimButton) == GPIO.HIGH:
                print("dimButton is HIGH")
                if dutyCycle > 0:
                        dutyCycle -= 5
                        sleep(0.25)
                else: dutyCycle = 0

final code:

import RPi.GPIO as GPIO
from time import sleep
 
ledPin = 11
brightenButton = 13
dimButton = 15
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(brightenButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dimButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
pwmLEDPin = GPIO.PWM(ledPin, 100)
pwmLEDPin.start(0)
dutyCycle = 100
GPIO.output(ledPin, GPIO.HIGH)
 
while True:
        pwmLEDPin.ChangeDutyCycle(dutyCycle)
        if GPIO.input(brightenButton) == GPIO.HIGH:
                print("brightenButton is HIGH")
                if dutyCycle < 100:
                        dutyCycle += 5
                        sleep(0.25)
                else: dutyCycle = 100
        elif GPIO.input(dimButton) == GPIO.HIGH:
                print("dimButton is HIGH")
                if dutyCycle > 0:
                        dutyCycle -= 5
                        sleep(0.25)
                else: dutyCycle = 0

make it work

First, you will need a terminal. You can use the built-in Raspberry Pi station or Controlling a Raspberry Pi through SSH on a separate computer. Through the terminal, you must go to the Python script directory and enter python3 rpi-ledpwm.py or the name of the file you used.

Sometimes the LED will appear to be blinking. The PWM frequency would probably be too low, if that was the case. You can increase the frequency by increasing the number in pwmLEDPin = GPIO.PWM(ledPin, 100) until the flicker is gone.

If you find transitions likable, cut back on time sleep(0.25) inside the loop. It does get faster the more you lower it, though, so don’t lower it too much.

advice: Prefer Arduino instead? complete here A guide to get you started on Arduino projects.

Frequently Asked Questions

What is the minimum frequency that I can create with a Raspberry Pi?

The lowest frequency you can set on your Raspberry Pi is 10 Hz. The device cannot support anything less than that.

What is the maximum frequency that I can create with a Raspberry Pi?

The highest frequency you can set on your Raspberry Pi is 8000 Hz.

Can pulse waves be converted into sound waves?

yes. Using a piezoelectric element, it is quite possible to convert pulse waves into sound waves that you can hear. In this case, modulating the frequency changes the pitch, while modulating the duty cycle adjusts the volume.

All photos by Terenz Jomar Dela Cruz.

Terence Jomar Dela Cruz

Terenz is a hobbyist who specializes in robotics trying to build the coolest robot the world has ever seen. He could already have done it if he hadn’t been too busy burning LEDs as a second hobby.

Subscribe to our newsletter!

Our latest lessons delivered straight to your inbox

#control #LED #brightness #Raspberry #PWM

Leave a Comment

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