How to Changing the Color of  RGB LED in Raspberry pi

You want to control the color of an RGB LED. Today I show you How to Changing the Color of RGB LED in a Raspberry pi step by step complete process

Required Component connect  LED Raspberry Pi :

1.Raspberry pi
 
 

This book will help you to gain more knowledge of Raspberry pi  Software and Hardware Problems and Solutions

Raspberry Pi Cookbook

Circuit diagram connects RGB LED Raspberry Pi:

shows how you can connect your RGB LED on a breadboard. Make sure that the LED is the correct way around; the longest lead should be the second lead from the top of the breadboard. This connection is called the common cathode, as the negative connections (cathodes) of the red, green, and blue LEDs within the LED case have all their negative sides connected together to reduce the number of pins needed in the package. [caption id="attachment_557" align="alignnone" width="249"]Using a RGB LED with a Raspberry Pi
Using an RGB LED with a Raspberry Pi

 Code

from tkinter import *
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
pwmRed = GPIO.PWM(18, 500)
pwmRed.start(100)
pwmGreen = GPIO.PWM(23, 500)
pwmGreen.start(100)
pwmBlue = GPIO.PWM(24, 500)
pwmBlue.start(100)
class App:
     def __init__(self, master):
         frame = Frame(master)
         frame.pack()
         Label(frame, text='Red').grid(row=0, column=0)
         Label(frame, text='Green').grid(row=1, column=0)
         Label(frame, text='Blue').grid(row=2, column=0)
         scaleRed = Scale(frame, from_=0, to=100,
         orient=HORIZONTAL, command=self.updateRed)
         scaleRed.grid(row=0, column=1)
         scaleGreen = Scale(frame, from_=0, to=100,
         orient=HORIZONTAL, command=self.updateGreen)
         scaleGreen.grid(row=1, column=1)
         scaleBlue = Scale(frame, from_=0, to=100,
         orient=HORIZONTAL, command=self.updateBlue)
         scaleBlue.grid(row=2, column=1)
     def updateRed(self, duty):
         pwmRed.ChangeDutyCycle(float(duty))
     def updateGreen(self, duty):
         pwmGreen.ChangeDutyCycle(float(duty))
     def updateBlue(self, duty):
         pwmBlue.ChangeDutyCycle(float(duty))
root = Tk()
root.wm_title('RGB LED Control Raspberry pi')
app = App(root)
root.geometry("300x250+0+0")
root.mainloop()
When You run this code you can see like this interface and you can control the red, green, and blue channels of the LED
  RGB GUI RAspberry pi 
 The type of RGB LED used here is a common cathode. If you have the common anode type, then you can still use it, but connect the common anode to the 3.3V pin on the GPIO connector. You will then find that the slider becomes reversed, so a setting of 100 becomes off and 0 becomes full on. When you are selecting an LED for this project, LEDs labeled diffused are best because they allow the colors to be mixed better.

If you want to know more about raspberry pi then click on the link below

RASPBERRY PI TUTORIALS FOR BEGINNERS

No comments

Theme images by Dizzo. Powered by Blogger.