Skip to main content

Hi 👋. I'm Richard Huf. I'm a Senior Frontend Developer, interested in design systems, CSS, accessibility, React, Typescript, and the open web.

Building circuits on a breadboard with the raspberry pi

I've been having a bit of a play with the Raspberry Pi Model 3B lately. Doing a bit of breadboarding, building some basic circuits, and gaining a bit of an understanding of basic electronics. It's been fun. Here's some random notes so far.

Handy commands

I ssh into my pi most of the time, as I find it easier than hooking up to a screen.

Useful libraries

Some simple scripts

  1. Flash a single led on pin 13 a fixed number of times.
import RPi.GPIO as GPIO
import time

LED_PIN = 13
SLEEP = 0.125
COUNT = 1
ITERATIONS = 20

try:
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(LED_PIN, GPIO.OUT)

    while COUNT <= ITERATIONS:
        print(COUNT)
        print("LED on")
        GPIO.output(LED_PIN, GPIO.HIGH)
        time.sleep(SLEEP)
        print("LED off")
        GPIO.output(LED_PIN, GPIO.LOW)
        time.sleep(SLEEP)

        COUNT += 1

    print("Program complete")

except NameError as e:
    print(e)
    GPIO.output(LED_PIN, GPIO.LOW)

finally:
    GPIO.cleanup()
  1. Flash a red and blue led in sequence, a fixed number of times
import RPi.GPIO as GPIO
import time

LED_BLUE = 26
LED_RED = 16
COUNT = 0
FLASHES = 10
DURATION = .1

try:
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(LED_BLUE, GPIO.OUT)
    GPIO.setup(LED_RED, GPIO.OUT)

    while COUNT < FLASHES:
        print("LED BLUE")
        GPIO.output(LED_BLUE, GPIO.HIGH)
        time.sleep(DURATION)
        GPIO.output(LED_BLUE, GPIO.LOW)

        print("LED RED")
        GPIO.output(LED_RED, GPIO.HIGH)
        time.sleep(DURATION)
        GPIO.output(LED_RED, GPIO.LOW)

        COUNT += 1

    COUNT = 0

    time.sleep(.5)

    while COUNT < 3:
        GPIO.output(LED_BLUE, GPIO.HIGH)
        GPIO.output(LED_RED, GPIO.HIGH)

        time.sleep(0.250)

        GPIO.output(LED_BLUE, GPIO.LOW)
        GPIO.output(LED_RED, GPIO.LOW)

        time.sleep(0.250)
        COUNT +=1

    print("Program complete")

except NameError as e:
    print(e)
    GPIO.output(LED_BLUE, GPIO.LOW)
    GPIO.output(LED_RED, GPIO.LOW)

finally:
    GPIO.cleanup()
  1. Control the brightness of an led with 'pulse width modulation'.
from gpiozero import PWMLED
from time import sleep

led1 = PWMLED(26)

while True:
    try:
        led1.value = 0
        sleep(1)
        led1.value = 0.5
        sleep(1)
        led1.value = 1
        sleep(1)
    except KeyboardInterrupt:
        led1.close()
        print("Ending program")
  1. Use pulse width modulation to play tones on a passive piezo buzzer.
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time

# Set GPIO numbering mode
GPIO.setmode(GPIO.BCM)
buzzer_pin = 26
GPIO.setup(buzzer_pin, GPIO.OUT)

# Create PWM object for frequency control
# PWM(pin, frequency)
p = GPIO.PWM(buzzer_pin, 1000)

try:
    print("Starting Buzzer...")
    p.start(50)  # Start with 50% duty cycle

    # Example: Play 3 different tones
    for f in [440, 660, 880]:
        p.ChangeFrequency(f)

        time.sleep(0.5)

finally:
    p.stop()
    GPIO.cleanup()
    print("Cleaned up.")
  1. Use 2x RGB LEDs to display a variety of colours. Uses the colorzero library. I attempted to use the Color.from_hls() function, and shift through a range of colors over time, but it quickly became more complex that my basic understanding of Python could handle.
named_colors.py
named_colors = [
  "aliceblue",
  "antiquewhite",
  "aqua",
  "aquamarine",
  "azure",
  "beige",
  "bisque",
  "black",
  "blanchedalmond",
  "blue",
  "blueviolet",
  "brown",
  "burlywood",
  "cadetblue",
  "chartreuse",
  "chocolate",
  "coral",
  "cornflowerblue",
  "cornsilk",
  "crimson",
  "cyan",
  "darkblue",
  "darkcyan",
  "darkgoldenrod",
  "darkgray",
  "darkgreen",
  "darkgrey",
  "darkkhaki",
  "darkmagenta",
  "darkolivegreen",
  "darkorange",
  "darkorchid",
  "darkred",
  "darksalmon",
  "darkseagreen",
  "darkslateblue",
  "darkslategray",
  "darkslategrey",
  "darkturquoise",
  "darkviolet",
  "deeppink",
  "deepskyblue",
  "dimgray",
  "dimgrey",
  "dodgerblue",
  "firebrick",
  "floralwhite",
  "forestgreen",
  "fuchsia",
  "gainsboro",
  "ghostwhite",
  "gold",
  "goldenrod",
  "gray",
  "green",
  "greenyellow",
  "grey",
  "honeydew",
  "hotpink",
  "indianred",
  "indigo",
  "ivory",
  "khaki",
  "lavender",
  "lavenderblush",
  "lawngreen",
  "lemonchiffon",
  "lightblue",
  "lightcoral",
  "lightcyan",
  "lightgoldenrodyellow",
  "lightgray",
  "lightgreen",
  "lightgrey",
  "lightpink",
  "lightsalmon",
  "lightseagreen",
  "lightskyblue",
  "lightslategray",
  "lightslategrey",
  "lightsteelblue",
  "lightyellow",
  "lime",
  "limegreen",
  "linen",
  "magenta",
  "maroon",
  "mediumaquamarine",
  "mediumblue",
  "mediumorchid",
  "mediumpurple",
  "mediumseagreen",
  "mediumslateblue",
  "mediumspringgreen",
  "mediumturquoise",
  "mediumvioletred",
  "midnightblue",
  "mintcream",
  "mistyrose",
  "moccasin",
  "navajowhite",
  "navy",
  "oldlace",
  "olive",
  "olivedrab",
  "orange",
  "orangered",
  "orchid",
  "palegoldenrod",
  "palegreen",
  "paleturquoise",
  "palevioletred",
  "papayawhip",
  "peachpuff",
  "peru",
  "pink",
  "plum",
  "powderblue",
  "purple",
  "rebeccapurple",
  "red",
  "rosybrown",
  "royalblue",
  "saddlebrown",
  "salmon",
  "sandybrown",
  "seagreen",
  "seashell",
  "sienna",
  "silver",
  "skyblue",
  "slateblue",
  "slategray",
  "slategrey",
  "snow",
  "springgreen",
  "steelblue",
  "tan",
  "teal",
  "thistle",
  "tomato",
  "turquoise",
  "violet",
  "wheat",
  "white",
  "whitesmoke",
  "yellow",
  "yellowgreen"
]
#!/usr/bin/python3

from gpiozero import RGBLED, Button
from signal import pause
from random import randint
from colorzero import Color
from named_colors import named_colors

LED_1_RED_PIN = 24
LED_1_GREEN_PIN = 13
LED_1_BLUE_PIN = 21

LED_2_RED_PIN = 25
LED_2_GREEN_PIN = 18
LED_2_BLUE_PIN = 26

button = Button(16)
led1 = RGBLED(LED_1_RED_PIN, LED_1_GREEN_PIN, LED_1_BLUE_PIN)
led2 = RGBLED(LED_2_RED_PIN, LED_2_GREEN_PIN, LED_2_BLUE_PIN)

def pulse():
    color1 = named_colors[randint(0, len(named_colors) - 1)]
    color2 = named_colors[randint(0, len(named_colors) - 1)]
    print("LED 1, {}".format(color1))
    print("LED 2, {}".format(color2))
    led2.color = (1, 1, 1)
#    led1.pulse(on_color=Color(color1), fade_in_time=1, fade_out_time=1, n=None)
#    led2.pulse(on_color=Color(color2), fade_in_time=1, fade_out_time=1, n=None)

def off():
    led1.off()
    led2.off()

try:
    button.when_pressed = pulse
    button.when_released = off

    pause()
except KeyboardInterrupt:
    pass

finally:
    print("Shutdown")

⚡️🔋👨🏻‍💻