Control the led brightness (PWM output) using RPi

shape
shape
shape
shape
shape
shape
shape
shape
blog-details
April, 2020

INSTALL DEPENDENCY:

  1. Check any Python package installed in RPi
  2. Open terminal paste below code.
python
python3
  • If python, python3 and GPIO library of python is Not installed copy the below code and paste in terminal.

 

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python
sudo apt-get install pyhton3
sudo apt-get install python-rpi.gpio python3-rpi.gpio
  • Analog output basically PWM (Pulse Width Modulation) output.
  • For more Details about PWM go through below calculation for how it produce variable voltage in GPIO pin of RPi using PWM method.

 

  • led.GPIO.PWM(3,100) #Set  PWM output wih 100Hz Frequency
  • 100Hz means 100cycles per sec.

REQUIRED COMPONENTS

  1. Raspberry Pi (setup)- 1pic
  2. Micro USB cable
  3. Resistor 220E – 1pic
  4. LED (3.5mm) -1pic
  5. Breadboard- 1pic
  6. Jumper wire – as per requirement

 

CIRCUIT CONNECTION:

 

CODE:

 

# Tested code by Robogenesis
import RPi.GPIO as GPIO 
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) #Board pin numbering
GPIO.setup(3, GPIO.OUT)  #Led is connected to Pin 3
led = GPIO.PWM(3,100) # set PWM output with 100Hz frequency

try :
    x=0
    while True :
            print ("increment")
            for x in range (0,100,1) :
                    led.ChangeDutyCycle(x)
                    time.sleep(0.02)
                    
            print ("Decrement")
            time.sleep(1)
            for x in range (100,0,-1) :
                    led.ChangeDutyCycle(x)
                    time.sleep(0.02)
            led.start(0)
except :
    led.stop()
    GPIO.cleanup()

 

OUTPUT:

Hava a Good Day!! smiley

Comments

Leave a Reply