Control LEDs using raspberry pi

#1

Hello,
I want to control brightness of 6 LEDs using RPi, Please help me i am a beginner here,

0 Likes

#2

Hi Raj,
You can set up a circuit like this one (but with 6 LEDs):
https://www.circuito.io/app?components=9443,11372,11372,11372,200000

Then, use this test code to set PWM value for a LED (example on pin 12, add for the rest of them):

import RPi.GPIO as GPIO   # Import the GPIO library.
import time               # Import time library

GPIO.setmode(GPIO.BOARD)  # Set Pi to use pin number when referencing GPIO pins.
                      # Can use GPIO.setmode(GPIO.BCM) instead to use 
                      # Broadcom SOC channel names.

GPIO.setup(12, GPIO.OUT)  # Set GPIO pin 12 to output mode.
pwm = GPIO.PWM(12, 100)   # Initialize PWM on pwmPin 100Hz frequency

# main loop of program
print("\nPress Ctl C to quit \n")  # Print blank line before and after message.
dc=0                               # set dc variable to 0 for 0%
pwm.start(dc)                      # Start PWM with 0% duty cycle

try:
    while True:                      # Loop until Ctl C is pressed to stop.
        for dc in range(0, 101, 5):    # Loop 0 to 100 stepping dc by 5 each loop
            pwm.ChangeDutyCycle(dc)
            time.sleep(0.05)             # wait .05 seconds at current LED brightness
            print(dc)
        for dc in range(95, 0, -5):    # Loop 95 to 5 stepping dc down by 5 each loop
            pwm.ChangeDutyCycle(dc)
            time.sleep(0.05)             # wait .05 seconds at current LED brightness
            print(dc)
except KeyboardInterrupt:
    print("Ctl C pressed - ending program")

pwm.stop()                         # stop PWM
GPIO.cleanup()                     # resets GPIO ports used back to input mode

The code was taken from https://www.mbtechworks.com/projects/raspberry-pi-pwm.html

0 Likes

#3

Thanks mate for this.

0 Likes

#4

As i am using this to make a test bench so i have to operate i on 12v supply. What should i do??

0 Likes

#5

You can’t power the Pi from 12V, you can however power the LEDs from the Pi without needing another power supply I believe.

0 Likes