The idiot in question being... me!
It's been ages since I did any real electronics. Most of my work involves software and pre-assembled bits of kit. I thought that it was time I reacquainted myself with the joys of electricity 🙂
Because I'm fundamentally lazy, I purchased the all-in-one Raspberry Pi 2 kit from Vilros.
Lots of LEDs, some buttons, a nice case, all the cables, resistors, and all sorts of bits and bobs. Including a breadboard!
What's A Breadboard?
I remember - from school - that a breadboard is a.... thing... that lets you... electricity?
Honestly, it wasn't until I saw this diagram from Adafruit that it all finally clicked into place:
I followed a sample tutorial on how to wire in an LED to be permanently on. Basically +3.3v → long leg of LED. Short leg of LED → 330Ω resistor → Ground.
Ok, that was easy. Now lets make it flash on command!
The wiring is fairly similar, but we use a controlable GPIO pin instead.
GPIO pin 21 → long leg of LED. Short leg of LED → 330Ω resistor → Ground.
Here's what it looks like:
And here's the Python code to switch it on, then off.
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) led = 21 GPIO.setup(led, GPIO.OUT) # Switch on GPIO.output(led, 1) # Switch off GPIO.output(led, 0)
Ok, so, next thing - how to get information in to the Pi?
I was confused about exactly how the push switch worked - why were their four pins?
I read the O'Reilly Raspbery Pi Tutorial and became enlightened.
So, lets create a thingumy which lights up an LED when a button is pressed.
The LED will be the same as the above. The button will be wired as:
GPIO pin 19 → Button A. Button B → Ground.
And the code is:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # Pin 19 will sense for button pushing button = 19 GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) # The LED led = 21 GPIO.setup(led, GPIO.OUT) while True: input_state = GPIO.input(button) # Sense the button if input_state == False: print('Button Pressed') time.sleep(0.2) # Switch on LED GPIO.output(led, 1) else : # Switch off LED GPIO.output(led, 0)
I'm not entirely sure why
if input_state == False: is the logic for the button being pressed. Seems like it ought to be
True, no? Anyway, there's a long discussion about all the ways sensors can be read on the raspberry-gpio-python pages.
Well, there you have it. An utterly simple guide to getting started. I just hope future-me finds this useful!