Skip to main content

LED & Button Control

The blade has two digital LEDs mounted in series. A square yellow LED is located on the top of the board, and a green LED is located on the edge. The LEDs can be controlled via GPIO18.

info

You'll need to install the Adafruit_Blinka and the NeoPixel library.

from time import sleep
import board
import neopixel
import RPi.GPIO as GPIO

# LED strip configuration:
LED_COUNT = 2 # Number of LED pixels.
LED_PIN = board.D18 # GPIO pin connected to the pixels.
LED_BRIGHTNESS = 1 # Brightness of the LED pixels

# Button configuration:
BUTTON_PIN = 20 # GPIO pin connected to the button.

# Setup the button pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Create NeoPixel object with appropriate configuration.
pixels = neopixel.NeoPixel(LED_PIN, LED_COUNT, brightness=LED_BRIGHTNESS, auto_write=False)

# Generate a random color.
def random_color():
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

# Set the color of the entire strip.
def set_color(color):
for i in range(LED_COUNT):
pixels[i] = color
pixels.show()

print('Press the button to change colors. Press Ctrl-C to quit.')

try:
while True:
button_state = GPIO.input(BUTTON_PIN)
if button_state == GPIO.LOW:
# Button is pressed
color = random_color()
set_color(color)
# Wait for the button to be released
while GPIO.input(BUTTON_PIN) == GPIO.LOW:
sleep(0.01)
sleep(0.1)
except KeyboardInterrupt:
# Cleanup on exit
GPIO.cleanup()

Edge Button

The button is connected to GPIO20 in the Raspberry PI compute module and can be customized as you like. Can be pressed with a left touch of the Blade latch.

Example Script

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button to GPIO20

try:
while True:
button_state = GPIO.input(20)
if button_state == False:
print('Button Pressed...')
time.sleep(0.2)
except:
GPIO.cleanup()