10/7/2018
Introduction
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!
Let's get started!
If you're new to Raspberry this tutorial will help you get started and make your first Raspberry Pi project!
Let's get started!
Video in Greek language
What you will need - Hardware
For this tutorial you will need:
|
The Circuit
The connections are pretty easy, see the image above with breadboard circuit schematic.
The 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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #Import library for GPIO and sleep import RPi.GPIO as GPIO from time import sleep #Stop the warnings GPIO.setwarnings(False) #Set the LED on pin 18 as an output GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT) #Loop while True: GPIO.output(18,GPIO.HIGH) print ("LED ON") sleep(1) GPIO.output(18,GPIO.LOW) print ("LED OFF") sleep(1) |
Download the code from here and open it with Thonny Python IDE or run it from terminal.
|
|
Now start making your own modifications to the code. For example you can add a second led or change the sleep time.
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
Video in Greek language
I hope you liked this, let me know in the comments.