Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@sh_o

Raspberry Pi でPWM信号使ってLEDの明るさを変更する

More than 1 year has passed since last update.

Raspberry Pi でPWM信号使ってLEDの明るさを変更する

PWM信号

周期的にオンとオフが切り替わる信号で、1波長の中でのオンの時間とオフの時間の割合を調整するで、
一連の流れの中での出力を変更できる信号

PWM.pngPWM2.png

Raspberry Pi とLEDを接続する

今回はたまたま家にあった赤色LEDをつかった。
電圧は Raspberry Pi からのPWMの3.3Vで足りるため、アノードにそのまま接続。
( 今回は18番のPINを使用 )
pi_GPIO_PWM.png
PWM_LED.png

PWMを出力するコードを書く

  • 以下のコードでは、10%の出力と100%の出力を約3秒ごとに2回繰り返す
import RPi.GPIO as GPIO
import time


def main():
    frequency = 200
    repeat_count = 600
    gpio_pin = 18

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(gpio_pin, GPIO.OUT)

    for i in [10, 100, 10, 100]:
        if 0 == i:
            on_time = 0
            off_time = (1 / frequency)
        else:
            on_time = (1 / frequency) * (i / 100)
            off_time = (1 / frequency) * (1 - (i / 100))

        for rc in range(repeat_count):
            GPIO.output(gpio_pin, True)
            time.sleep(on_time)
            GPIO.output(gpio_pin, False)
            time.sleep(off_time)

    GPIO.cleanup()


if __name__ == '__main__':
    main()
0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
sh_o

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?