RGB LED color control with Raspberry Pi--python version

Source: Internet
Author: User

please refer to the other blog post for the circuit connection method: "Color control for RGB LEDs with Raspberry Pi--c language version"

Python code:

#!/usr/bin/env python#********************************************************* #File Name:rgb.py#author:jason Dai  #Date: 2015/02/04#*********************************************************import Rpi.gpio as GPIOimport timecolors = [0xFF0000, 0x00FF00, 0x0000FF, 0xffff00, 0xff00ff, 0x00ffff, 0xFFFFFF, 0x9400d3]pins = {' Pin_r ': One, ' pin_g ': ' Pin_b ' : Pins is a dictgpio.setmode (GPIO). BOARD) # Numbers GPIOs by physical locationfor I in Pins:GPIO.setup (Pins[i], GPIO. # Set pins ' mode is outputgpio.output (pins[i), GPIO. High) # Set pins to High (+ 3.3v) to off ledp_r = GPIO. PWM (pins[' Pin_r '), # set frequece to 2khzp_g = GPIO. PWM (pins[' Pin_g '), p_b = GPIO. PWM (pins[' Pin_b '), P_r.start (+) # Initial Duty Cycle = (LEDs off) P_g.start (+) P_b.start (+) def map (x, In_ Min, In_max, Out_min, Out_max): # Maps a number from one interval linearly to another, such as mapping a number between 0~100 to 0~255 return (x-in_min) * (out_max-out_min) /(In_max-in_min) + out_mindef SetColor (col): # for ExampLe:col = 0x112233r_val = (col & 0xFF0000) >> 16g_val = (col & 0x00FF00) >> 8b_val = (Col & 0x000  0FF) >> 0r_val = map (r_val, 0, 255, 0, +) # change a num (0~255) to 0~100.g_val = map (g_val, 0, 255, 0,) b_val = Map (b_val, 0, 255, 0, +) p_r.changedutycycle (100-r_val) # change duty cyclep_g.changedutycycle (100-g_val) P_B.C Hangedutycycle (100-b_val) try:while true:for col in Colors:setcolor (col) time.sleep (0.5) except Keyboardinterrupt:p_ R.stop () P_g.stop () P_b.stop () for I in Pins:GPIO.output (Pins[i], GPIO. High) # Turn off all Ledsgpio.cleanup ()
To run the program:

Python rgb.py


RGB LED color control with Raspberry Pi--python version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.