Introduction: Raspberry Pi Tutorial: How to Blink an LED

LEDs (Light-emitting diode) can be found on many collors and sizes. This example shows the simplest thing you can do with Raspberry Pi to see physical output: it blinks an LED!

If you're new to Raspberry this tutorial will help you get started and make your first Raspberry Pi project!

Tutorial updates and more Raspberry Pi tutorials can be found here:

Step 1: What You Will Need - Hardware

For this tutorial you will need:

And of course any Raspberry Pi board with Raspbian OS installed.

Step 2: The Circuit

The connections are pretty easy, see the image above with breadboard circuit schematic.

Common leds have two pins. The positive end of a led (larger pin) is called anode, and the negative end is called cathode.

Note: Check GPIO pinout if you have an older RPi.

Step 3: The Python Code

In the program below, the first thing you do is to import the library for GPIO and sleep.

The next step is to initialize pin 18 as an output pin with GPIO.setup( ) function .

The While True loop runs over and over again, forever. In the main loop, you turn on or off LED with GPIO.output( ) function and "pause" the program for one seconds with sleep( ) function.

Download the code from here and open it with Thonny Python IDE or run it from terminal.

1. import RPi.GPIO as GPIO
2. from time import sleep
3. GPIO.setwarnings(False)
4. GPIO.setmode(GPIO.BCM)
5. GPIO.setup(18,GPIO.OUT)
6. while True:
7. .....GPIO.output(18,GPIO.HIGH)
8. .....print ("LED ON")
9. .....sleep(1)
10. ....GPIO.output(18,GPIO.LOW)
11. ....print ("LED OFF")
12. ....sleep(1)

Step 4: Well Done!

You have successfully completed our first Raspberry Pi "How to" tutorial and you learned how to use:

  • LED
  • comments in code
  • GPIO.setmode(), GPIO.setup(), GPIO.output(), print() and sleep() functions

I hope you liked this, let me know in the comments.

Here is a video in Greek Language: