Getting started with a Raspberry Pi can be an exciting experience. It has never been easier for beginners to get started with both programming and DIY electronics.
One easy project is to create a simple circuit with two LEDs and control one of them with code. Here’s how to do it!
Before you get started, you need to make sure your Pi has an operating system installed. Installing Raspbian via NOOBS is the fastest way to get started.
Boot up your Pi and connect it to a screen, mouse, and keyboard like you would a normal desktop computer. Alternatively, you can connect to your Pi via SSH. to save extra wires. We’ll show you how to drive LEDs, whichever method you choose.
Once you’re sure your Raspberry Pi is booting up properly, turn it off again while building the circuit so you don’t damage your Pi.
With the Raspberry Pi you will need:
- 2 x LEDs
- 2 resistors (220 ohm to 1 k ohm)
- Connecting cables
If you received your Raspberry Pi with a starter kit, you probably already have everything on this list. Now let’s build our circuit.
Simple LED Circuit
Set up your components as shown in this Fritzing diagram:
This circuit does two things. Pins 5v and GND Pi join power layout tires.
Note. For a better understanding of what a layout is and how it works, check out our crash course on what a layout is and how it works.
The two power buses are connected at the end and the line runs from positive power bus to positive (anode) side of the lower LED. negative the side of the LED is connected to a resistor which is connected back to the power line GND .
The top LED is wired differently. The line runs from contact 12 (GPIO18) Raspberry Pi to the positive side of an LED that goes through a resistor and back to the bus GND . Pin 12 is also GPIO18, as strange as it sounds, our guide to pins Raspberry Pi GPIO pins Raspberry Pi GPIO pins help you figure it out!
It doesn’t matter which direction you mount the resistors, but it’s important to position the LEDs correctly. Luckily, it’s easy to tell which side is which:
Once everything is set up, it should look something like this:
Note that I’m using an external Wi-Fi dongle here, only needed if you’re suffering from the curse of weak Wi-Fi!
Make sure everything is set up correctly, then boot up your Raspberry Pi. An LED connected directly to the 5V pin should turn on immediately. The other LED is the one you will control from the code.
Method 1: Python via IDLE
If you are using your Raspberry Pi in desktop mode, open the application menu in the top left corner of the screen and navigate to Programming > Python 3 (IDLE) . This will open a Python shell. If you are using SSH mode, instructions are provided later in this article.
The Raspbian operating system comes with Python already installed. Python is the perfect programming language for beginners and there are many great sites out there. there to help you get started. We’ll create a short Python together, though if you’d rather get the script ready, you can copy the code from Pastebin.
You can program directly into the shell, but it would be nice to create a program that you can save and use again. Open a new file by clicking File > New File .
You are about to create a simple sketch blinking, which will turn the LED on and off. First you need to import RPi.GPIO and temporary modules.
import RPi.GPIO as GPIO import time
Import to as GPIO saves you from having to type RPi.GPIO every time and you need the module time for the delays between turning on and off the LED. Now configure the GPIO pin.
GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) ledPin = 12 GPIO.setup(ledPin, GPIO.OUT)
Configure GPIO pins to use numbering BOARD and set GPIO alerts to false. Don’t worry if you don’t understand this at this stage! Then install LedPin to pin 12 (GPIO18) of your Pi. Finally set ledPin to OUTPUT . The pin is now ready to drive the LED.
Creating an LED Flash
By creating a cycle for you can control the number of blinks of the LED. Enter the following code, indented the same way.
for i in range(5): print("LED turning on.") GPIO.output(ledPin, GPIO.HIGH) time.sleep(0.5) print("LED turning off.") GPIO.output(ledPin, GPIO.LOW) time.sleep(0.5)
This for loop is executed five times and each time it will print in the python shell what does it do before changing output 12 to HIGH turn on the LED, and then SHORT turning off the output. The program automatically exits.
Save your program and then select » Run» > «Run Module» in the editor menu. Your LED should flash five times!
Congratulations! You have created your first GPIO program!
Method 2: Python over SSH and Nano
If you are connected to your Raspberry Pi via SSH, you can create this program from the command line. Create a new script in Nano by typing: