Low Orbit Flux Logo 2 F

Raspberry Pi 4 - LED Blink

Before running this the code for this you will want to make sure the Python GPIO libraries are installed. If I remember correctly, I think they were already there and I actually didn’t need to install them on the latest version of Raspberry Pi OS.


sudo apt install python-rpi.gpio python3-rpi.gpio

The script will look like this. The basic idea is that it will loop forever and alternate between turning on and off with a one second delay.

blink.py
import RPi.GPIO as GPIO from time import sleep GPIO.setwarnings(False) # ignore warningas GPIO.setmode(GPIO.BOARD) # physical pin numbering GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # set pin 8 as output, initial to low (off) while True: GPIO.output(8, GPIO.HIGH) # HIGH / On sleep(1) # sleep 1 second GPIO.output(8, GPIO.LOW) # LOW / Off sleep(1) # sleep 1 second

Run your script like this:


python blink.py

LEDs:

NOTE: GPIO pin voltage is 3.3v

How it is connected:

GPIO pin 8 ==> [+] LED [-] ==> resistor ==> pin 6 (Ground)

We need to use a resistor to prevent too much current from flowing through the LED and burning it out. If we were to just plug in the LED without a resistor we could burn out the LED and potentially burn out the Raspberry Pi.

We’re going to assume our LED has the following:

We use Ohm’s law:

R=V/I

to find out how much resistance we will need:

(3.3-1.7)/20mA = 80 ohms

We need a resistor that can provide at least 80 ohms of resistance. It is OK if we provide more resistance than this but it will cause the LED to shine less brightly. In this example I use a 220 ohm resistor and it works great.