メーカーのサポートページに載っていたproject3を改造して、Grove Shield for Pi Pico V1.0でボタン操作でLEDセンサーを点灯させてみました。
メーカーのサポートページは下記のURLです。
Grove Shield for Pi Pico V1.0の初期設定は、「連休前に買った、Grove Shield for Pi Pico V1.0で温湿度センサーとOELDディスプレイ(project 2)を動かしてみました。」の記事をご確認ください。
まず、LEDセンサーをD16にボタンセンサーをD18に差し込み、PicoをPCへ接続します。

PicoをPCへ接続したら、Tonny起動してインタプリタとして「MicroPython(Raspberry Pi Pico)」を選択します。

新規作成を選択後、project 3のメインプログラムを修正して起動します。
今回は下記のようにコードを修正しました。
from machine import Pin
button = Pin(18, Pin.IN, Pin.PULL_UP)# button connect to D18
button.irq(lambda pin: InterruptsButton(),Pin.IRQ_FALLING)#Set key interrupt
led = Pin(16, Pin.OUT)#led connect to D16
tmp = 0
'''Key interrupt function, change the state of the light when the key is pressed'''
def InterruptsButton(): #button input
global tmp
tmp = ~tmp
led.value(tmp)
while True:
pass
無事に動けば、ボタンでLEDのオンオフを切り替えることができます。

Comments