# ex_2_3_06_button_to_led.py (จำลอง: https://wokwi.com/projects/469812700862565377)
from machine import Pin
from time import sleep
led = Pin(12, Pin.OUT)
button = Pin(4, Pin.IN) # Active-High: กด = 1
led.value(0) # pin-safety: เริ่มที่สถานะปิด
while True:
btn_state = button.value() # ช่วงที่ 1 อ่านสถานะปุ่ม
led.value(btn_state) # ช่วงที่ 2 ส่งสถานะไปคุม LED
print(f"Button: {btn_state} -> LED: {led.value()}") # ช่วงที่ 3 ยืนยันผลบน Shell
sleep(0.05) # หน่วง 100 ms ลดภาระ CPU