Collect wires, a 1k or so resistor and an LED for a simple test. We'll wire up your LED light to the pi.
I try to use red for +5 and blue or green for ground. My green ground wire is grounded to pin 9 on the pi. My red wire goes to pin 11 where the voltage can be controlled by our code.
An LED has to have the polarity right or it won't turn on. The long leg of the LED should go to the resistor and then to the red wire. The short leg should go to the green wire.
For a high-level look, see the diagram of the 40-pin interface. The diagram below was made by the people at raspberry-pi-geek.com.
We will refer in the code to the physical pin number, but it is always useful to be able to look at a diagram to tell what is available to use on each pin. Anything marked "GPIO" is a general-purpose input/output pin that we can assign to output a signal to drive the LED. When the signal is HIGH, it will allow current to light the LED.
Once you have that, and as long as you are executing now inside ipython, you can click on the box below and hit shift-enter to run the script and watch it flash the LED.
import RPi.GPIO as GPIO
import time
from IPython.display import clear_output
# blinking function
def blink(pin):
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
return
print("using Raspberry Pi board pin numbers")
GPIO.setmode(GPIO.BOARD)
led_pin = 11
print("set up GPIO output channel")
GPIO.setwarnings(False)
GPIO.setup(led_pin, GPIO.OUT)
print("blinking the LED")
for i in range(0,6):
print("blink")
blink(led_pin)
GPIO.cleanup()
Did you see the output of the script appear under your code window? Did your LED flash its little heart out? We love being able to dig right in like this. It's spine-tingling fun the first time you see it.
Now click in the code above and try changing it before you hit shift-enter again. You could make it blink more quickly for example by shortening the sleep timer duration to 0.25.
It may look like a jumbled mess when you first encounter someone's code. Let's dig in and sort it out.
First, this is python code that uses libraries especially for pi. Python is a popular language for using the pi and it's also fairly easy to get started with.
The first lines with "import" make it clear what other libraries and functions we will call on. We need GPIO functions for activating the LED as well as time functions for pausing between flashes.
Next we create a function to blink the LED. It will set the pin to HIGH to illuminate it with current and LOW when we want it off. It will return after flashing the LED once.
In two steps, we tell the GPIO library we will use board pin numbering and we set "led_pin" to 11. With board pin numbering, this refers to the pin 11 on the physical connector. If you count your way up from the bottom, you can see in the diagram above this pin has the name "GPIO17". The 17 isn't really important to us, but we had to choose a pin that was a GPIO so we could use it for general purpose input/output (output specifically).
Pin 11 has to be designated for output next.
Finally we use a range to cycle through a loop to blink the light. It blinks once for each run through the loop from 0 to 5.
Go on to Learner lesson 2