In third article, we created a python script to send the IP address of Raspberry Pi on reboot to a telegram channel and scheduled it in crontab.
Now is the time to start exploring hardware side of Raspberry Pi. In this article we will control the LED (light emitting diode) via GPIO pins of Raspberry Pi by Python program.
We are using Raspberry Pi 3 B+ model.
I strongly recommend you to visit https://www.raspberrypi.org/documentation/usage/gpio/ for better understandig the GPIO pins of your Raspberry Pi.
Alternatively you can go to command prompt of your Raspberry Pi and run the command
pinout which will present you a handy reference of GPIO pins.
You can refer the instruction card received along with canakit.
Lets connect the components together.
- Connect pin 36 ( we will refer pins by literal sequence number, here pin 36 is GPIO pin 16 in above pic) with the live/voltage row on breadboard.
- Connect pin 6 (ground) with ground row on breadboard.
- Now connect a 220 ohm resistor with live row and positive terminal of LED.
- Connect the negative/ground terminal with ground row of breadboard.
- Use jumper wires for above connections. Refer my hand drawn diagram below for connections.
L and G stands for live and ground respectively.
Use below python code to control the LED.
import RPi.GPIO as GPIO import time PIN = 36 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(PIN, GPIO.OUT) #LED output pin while True: GPIO.output(PIN, 0) #Turn OFF LED time.sleep(1) GPIO.output(PIN, 1) #Turn ON LED time.sleep(1)
GPIO.BOARD, which means we are referring pin number by their position on board.
Try it out and if you are stuck anywhere feel free to comment.