Raspberry Pi 3 Tutorial 8 – GPIO Introduction with an LED
Difficulty level: Beginner
Approx reading time:
1. Raspberry Pi 3 model B
2. MicroSD card 8 or 16 GB (Class 4 and above) with Raspbian
3. Windows PC / Linux PC (Tested on Windows 10, Ubuntu 14.04 LTS)
4. Ethernet cable (Category 5 – also called Cat 5)
5. Micro USB cable
7. 5 mm LED (your favourite colour)
8. 220 Ohms Resistor (colour code: Red, Red, Brown)
9. 2 Male to Female(Red – power/GPIO pin, Black – ground) Jumper Wires
Way to go ->
1. Login to your Pi using PuTTy or your Ubuntu terminal.
3. From the menu on the top left open Python 3 (IDLE).
4. In IDLE, create a new file by clicking on File -> New File in the menu bar.
5. Save the file by clicking on File -> Save As… in the menu bar. The file could be saved on the Desktop. The name could be: LEDblink.py.
6. In the file LEDblink.py, write the following code with comments (line starting with “#”) for clear understanding and save (press Cntrl + S on your PC keyboard) the file.
7. Run the code by clicking on Run -> Run Module in the menu bar.
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) blinkCount = 3 count = 0 LEDPin = 8 # Setup the pin the LED is connected to GPIO.setup(LEDPin, GPIO.OUT) try: while count < blinkCount: GPIO.output(LEDPin, True) print("LED ON") sleep(3) GPIO.output(LEDPin, False) print("LED OFF") sleep(1) count += 1 finally: # Reset the GPIO Pins to a safe state GPIO.cleanup()
Hardware Connections ->
1. Raspberry Pi 3 GPIO Header.
2. LED connected to Pin 8 – BOARD notation (GPIO14 – BCM notation).