In this tutorial, I am going to show you how to light a led with Raspberry PI and Python.
Before starting this tutorial, Raspberry PI is running with Raspbian. what you will need is:
Configure your hardware
Before you start, make sure you shut down and power off your Raspberry Pi to avoid shorting any circuits while working on wiring.
In your Raspberry Pi, please find the GND and GPIO4 pins. On Raspberry Pi, they are pin 25 and pin 7 on the GPIO pins. On Raspberry Pi3, they are pin 39 and pin 7.
Wire the actual hardware. Please connect the LED, the resistor GND and GPIO4 on the breadboard in the following order:
1. Connect GPIO4 to LED’s longer leg
2. Connect LED (shorter leg) to the register
3. Connect the other leg of the register to GND pin of Raspberry Pi
Specifically, the order of the LED’s longer or shorter leg is very important. If you connect the wrong side, the LED wont’ turn on.
Breadboard Wiring Steps
If this is the first time you use a breadboard, please make sure the breadboard has numbers and alphabets on columns and rows. We can specify a hole with
1. Attach the female connector of a jumper to GPIO4 male pin on Raspberry Pi.
2. Attach the male pin of the jumper to the hole 7-a of breadboard.
3. Attach the longer leg of LED to 7-d.
4. Attach the shorter leg of LED to 10-d.
5. Attach one leg of the resistor to 10-b.
6. Attach the other leg of the resistor to 14-b.
7. Attach the male pin of the jumper to 14-a of breadboard.
8. Attach the female connector of a jumper to GND male pin on Raspberry Pi.
9. Once you’ve set up the breadboard, it should look like this picture:
Now your hardware setting is ready.
You are now ready to switch the LED on. Turn on your PI and open the terminal window. Create a new text file smart_light.py.
Write the following code
import RPi.GPIO as GPIO
print “LED on”
print “LED off”
Save it with ‘control+x’.
Running the code
To run this code type:
sudo python smart_light.py
And that’s it! You are now able to turn an LED on and off.