Snap Circuit – Basic LED Control

Snap Circuit – Basic LED Control

WARNING

Make sure you pay close attention to the details, as failure to do so could result in your Raspberry Pi suffering damage and invalidating the warranty. We cannot be held responsible for any damage resulted with incorrectly following the guide.

Description

The Raspberry Pi is very good at talking to many different electrical components with the help of digital input and outputs, serial and with Bluetooth and Wi-Fi. LEDs are great indicators to notify if a specific criteria is met – Such as a new email has been received, code has finished compiling or just to simply let you know if it will rain today.

 

Preparation

What we need

  • Raspberry Pi (A Raspberry Pi 2 or 3 is best for this)
  • Snap to Pi starter kit
  • Snap Circuit base plate
  • 1 – 3 Snap bridge
  • 1 – D1 Red LED
  • 1 – D2 Green LED

Setting up the Raspberry Pi

Attach the Raspberry Pi to the Snap to Pi plate, to do this can be found here. Place the Raspberry Pi on your Snap Circuit base plate and power up the Raspberry Pi, if this is the first time using your Raspberry Pi, it is best to read this page first to familiarise yourself with your Raspberry Pi.

The Circuit

Firstly place the D1 and D2 LEDs on the base plate. These will go first as the 3 snap bridge sits on top.

Now to connect the Snap to Pi wires between the Raspberry Pi and circuit.

Carefully count the number of pins on the Raspberry Pi before attaching any wires, be sure to double and triple check that the wire matches the correct pin!

The Python code

With the Raspberry Pi booted and showing the desktop, open Terminal by either clicking the Black computer screen icon on the task bar, or click Pi Menu (start button), followed by Accessories and then Terminal.

1-start-idle

In this box type in sudo idle. This will open IDLE for typing our Python code with permission to control the GPIO pins.

Click File and then New File;

2-new-file

In the window that pops up, click File, Save As and give it a name to remember

Type in the following code in to this window, pay attention to capitals and spacing;

from gpiozero import LED
from time import sleep

red = LED(4)
green = LED(17)

while True:
    red.on()
    sleep(1)
    green.on()
    sleep(1)
    red.off()
    sleep(1)
    green.off()
    sleep(1)

Now double check the code to make sure no mistakes were made, otherwise the code might run and not give the expected results, or present you with an error.

When you are happy, click Run, and then Run Module.

Immediately the LEDs will start to turn on and off in sequence. As this was created with a while True: loop, this will run forever until told to stop.

To actually stop the script, just hold CTRL and press C.

What is happening

The Python code will run in a forever loop, the first line after the imports and setting up is red.on(). Self explanatory – It turns the red LED on, and keep it on for 1 second with the help of sleep(1). Reading through the rest of the code, next it turns the green LED on, for another second before turning the LEDs off again. Once it reaches the end, the loop begins again.

The LEDs where set up with the GPIO BCM numbering for GPIO 4 (pin number 7) and green with GPIO 17 (pin number 11). More LEDs can be added by choosing a spare GPIO pin – Which there are plenty of.

 

Downloads

PDF Version