概要
DavinciキットのRGB LEDを試してみる。オリジナルのコードを少し変えて、3色をまとめて扱うするクラスを作ってみた。また、徐々に明るさを変化させてみた。
回路
RGB LEDのピン配置は以下のようになっており、最も長いピンがGNDでRed-GND-Green-Blueの順に配置されている。
回路図は以下の通り。
コマンドラインで以下を実行して、接続を確認。
1 2 3 4 5 6 7 8 9 10 11 12 |
>>> import RPi.GPIO as GPIO >>> GPIO.setmode(GPIO.BCM) →GPIOのモード設定 >>> GPIO.setup(17, GPIO.OUT) →以下、R/G/Bのピン設定 >>> GPIO.setup(18, GPIO.OUT) >>> GPIO.setup(27, GPIO.OUT) >>> GPIO.output(17, GPIO.HIGH) →赤LEDテスト >>> GPIO.output(17, GPIO.LOW) >>> GPIO.output(18, GPIO.HIGH) →緑LEDテスト >>> GPIO.output(18, GPIO.LOW) >>> GPIO.output(27, GPIO.HIGH) →青LEDテスト >>> GPIO.output(27, GPIO.LOW) >>> GPIO.cleanup() →リソース解放 |
基本コード
辞書でR/G/Bのピン番号を設定し、周波数を100HzにしてLEDの発色を確認。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import RPi.GPIO as GPIO import time # Define GPIO pins to connect each color of LED led = {'red': 17, 'green': 18, 'blue': 27} # Setup GPIO GPIO.setmode(GPIO.BCM) for pin in list(led.values()): GPIO.setup(pin, GPIO.OUT) # Prepare PWMs for 3 colors with frequency frequency = 100 pwm_r = GPIO.PWM(led['red'], frequency) pwm_g = GPIO.PWM(led['green'], frequency) pwm_b = GPIO.PWM(led['blue'], frequency) # Define closing function def destroy(): for pin in list(led.values()): pwm_r.stop() pwm_g.stop() pwm_b.stop() GPIO.cleanup() # Light LED for 3 seconds pwm_r.start(20) pwm_g.start(70) pwm_b.start(40) time.sleep(3) destroy() |
クラス化と色変化
3色のLEDをまとめて扱うクラスを定義。
- コンストラクターでピン番号と周波数を指定してインスタンスを生成
start
メソッドでPWMの作動開始changeDutyCycle
メソッドでデューティー比を変更設定stop
メソッドでPWM停止とGPIOのリソース解放
定数COLORSで定義した色を順次発色させている。カラーコードからデューティー比への変換は1つの関数で一括して行っている。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import RPi.GPIO as GPIO import time # Color cord series to light LED in order COLORS = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF] # Function to convert from 2-bytes hexadecimal to 0-100 percantage def color_code_to_duty_cycle(color_code): b_cycle = (color_code & 0x0000FF) / 255 * 100 g_cycle = ((color_code & 0x00FF00) >> 8) / 255 * 100 r_cycle = ((color_code & 0xFF0000) >> 16) / 255 * 100 return [r_cycle, g_cycle, b_cycle] # Class to treat three colors LED collectively class RgbLed: # Constructor with pin numbers and frequency def __init__(self, red_pin, green_pin, blue_pin, frequency): # Dictionary of color name and GPIO pin number self.pins = {} self.pins['red'] = red_pin self.pins['green'] = green_pin self.pins['blue'] = blue_pin # Set GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Setup pins for output for pin in list(self.pins.values()): GPIO.setup(pin, GPIO.OUT) # Generate PWM for 3 colors and assigne to pins self.pwm_r = GPIO.PWM(self.pins['red'], frequency) self.pwm_g = GPIO.PWM(self.pins['green'], frequency) self.pwm_b = GPIO.PWM(self.pins['blue'], frequency) # Method to start PWM def start(self): self.pwm_r.start(100) self.pwm_g.start(100) self.pwm_b.start(100) # Method to change duty cycles of 3 colors def changeDutyCycle(self, r, g, b): self.pwm_r.ChangeDutyCycle(r) self.pwm_g.ChangeDutyCycle(g) self.pwm_b.ChangeDutyCycle(b) # Method to stop PWM and release resources def stop(self): self.pwm_r.stop() self.pwm_g.stop() self.pwm_b.stop() GPIO.cleanup() # Execution start led = RgbLed(17, 18, 27, 100) try: led.start() while True: for color in COLORS: # Convert R/G/B color level to percentage [r, g, b] = color_code_to_duty_cycle(color) # Change duty cicles and wait led.changeDutyCycle(r, g, b) time.sleep(1) except KeyboardInterrupt: led.stop() |
色と輝度の変化
R/G/Bを順に徐々に明滅させる例。カラーコードや変換関数は使わず、直接各色のデューティー比を0~100~0に変化させている。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import RPi.GPIO as GPIO import time # Class to treat three colors LED collectively class RgbLed: # Constructor with pin numbers and frequency def __init__(self, red_pin, green_pin, blue_pin, frequency): # Dictionary of color name and GPIO pin number self.pins = {} self.pins['red'] = red_pin self.pins['green'] = green_pin self.pins['blue'] = blue_pin # Set GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Setup pins for output for pin in list(self.pins.values()): GPIO.setup(pin, GPIO.OUT) # Generate PWM for 3 colors and assigne to pins self.pwm_r = GPIO.PWM(self.pins['red'], frequency) self.pwm_g = GPIO.PWM(self.pins['green'], frequency) self.pwm_b = GPIO.PWM(self.pins['blue'], frequency) # Method to start PWM def start(self): self.pwm_r.start(100) self.pwm_g.start(100) self.pwm_b.start(100) # Method to change duty cycles of 3 colors def changeDutyCycle(self, r, g, b): self.pwm_r.ChangeDutyCycle(r) self.pwm_g.ChangeDutyCycle(g) self.pwm_b.ChangeDutyCycle(b) # Method to stop PWM and release resources def stop(self): self.pwm_r.stop() self.pwm_g.stop() self.pwm_b.stop() GPIO.cleanup() # Execution start led = RgbLed(17, 18, 27, 100) try: led.start() # Cyclicly change the color and blightness of LED while True: for r in range(0, 100): led.changeDutyCycle(r, 0, 0) time.sleep(0.01) for r in range(100, 0, -1): led.changeDutyCycle(r, 0, 0) time.sleep(0.01) for g in range(0, 100): led.changeDutyCycle(0, g, 0) time.sleep(0.01) for g in range(100, 0, -1): led.changeDutyCycle(0, g, 0) time.sleep(0.01) for b in range(0, 100): led.changeDutyCycle(0, 0, b) time.sleep(0.01) for b in range(100, 0, -1): led.changeDutyCycle(0, 0, b) time.sleep(0.01) except KeyboardInterrupt: led.stop() |