11 — Raspberry Pi 4 simulation

11

An interactive Raspberry Pi 4 circuit simulation you can run free in your browser on Velxio, by qwez.

Components

Sketch code

import RPi.GPIO as GPIO
import time

LED_PIN = 18
BUTTON_PIN = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

lamp_on = False
GPIO.output(LED_PIN, GPIO.LOW)

print("Press K4 to switch the LED", flush=True)

try:
    while True:
        # 按下时,输入由高电平变为低电平
        if GPIO.input(BUTTON_PIN) == GPIO.LOW:
            # 等待20毫秒,再次确认,消除按键抖动
            time.sleep(0.02)

            if GPIO.input(BUTTON_PIN) == GPIO.LOW:
                lamp_on = not lamp_on
                GPIO.output(LED_PIN, lamp_on)

                print(
                    "LED ON" if lamp_on else "LED OFF",
                    flush=True
                )

                # 等待松开,避免长按时连续切换
                while GPIO.input(BUTTON_PIN) == GPIO.LOW:
                    time.sleep(0.01)

                time.sleep(0.02)

        time.sleep(0.01)

except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

More projects by qwez