Operating a Simple Switch and LED on the Raspberry Pi

GPIO, as may have been explained in other tutorials, stands for General Purpose Input/Output and a GPIO pin can be set high (taking the value 1) by connecting it to a voltage supply, or set low (taking the value 0) by connecting it to ground. The Raspberry Pi can set the pin to take either value and treat it as an output, or it can detect the value of the pin and treat it as an input.

The Raspberry Pi's pin header looks like this:

Operating a Simple Switch and LED on the Raspberry Pi

There are twenty-six pins in total: three power supply pins, 3V3 (3.3V), 5V0 (5.0V) and GND (0V); 6 DNC (do not connect) pins; and seventeen GPIOpins. Some of these seventeen pins have alternative functions as well, but we won't dwell on those now.

Let's build a simple circuit with a switch and LED. You will need:
– 1 × Working Raspberry Pi with an Internet connection
– 1 × Breadboard + jumpers
– 4 × Male/female jumper cables
– 1 × LED
– 1 × Pushbutton switch
– 1 × 270Ω resistor
– 1 × 10kΩ resistor

A breadboard has several key features. It has red and blue lines which demarcate the holes belonging to the supply voltage rails (which will be 3.3V for our purposes since the GPIO pins operate at 3.3V) and ground rails (GND) respectively. The holes in the same group are linked via connections inside the breadboard.

The rest of the pin holes can be grouped into segments of 5 shown in the black boxes. The holes in each segment are linked together, but the segments are not connected to one another.

It is recommended to link up all the supply voltage and ground rails as shown below for easy wiring later on.

GPIO10 is used as an output. When it is set low, the LED will be turned on, and vice-versa when it is set high. GPIO8 is used as an input, so when the pushbutton switch is pressed the pin is set high.

So, what can we actually do? Lots!

Connect the 3.3V and GND pins of the Raspberry Pi to the corresponding rails on the breadboard using the male/female jumper cables.

Connect the LED along with the 270Ω resistor and wire them to the 3.3V rail according to the circuit diagram. You will need to refer to the technical datasheet (this is only for the specific LED used in this tutorial) to determine which pin should be connected to the voltage supply (positive rail) and which should be connected to the pin (negative). From the datasheet, the shorter pin is labelled ‘anode’ and should be connected to the positive rail. The datasheets are usually found on the product page for electronic components.

Add in the switch and the 10kΩ resistor. Note that the pushbutton switch has 4 pins and you will have to decide which two pins to connect to the pin header by referring to its technical datasheet.

If you find that the switch doesn’t fit into the breadboard well, straighten the pins using a pair of pliers a little before inserting it into the board.

Now, connect the LED to GPIO10 pin of the Raspberry Pi pin header and the switch to GPIO8 pin.

Now, we'll write some code in Python to trigger the turning on of the LED upon pressing the switch. First, we need to install a Python module, RPi.GPIO, to control the GPIO pins for the Raspberry Pi.

Type the following lines in LXTerminal to do so:
sudo apt-get install python-rpi.gpio

Now, open a text editor and type the following code. This will program the switch and LED, and the LED will be turned on when the switch is pressed. To run the code, type python your_filename.py in a terminal.

# Import the required module.
import RPi.GPIO as GPIO
# Set the mode of numbering the pins.
GPIO.setmode(GPIO.BOARD)
# GPIO pin 10 is the output.
GPIO.setup(10, GPIO.OUT)
GPIO pin 8 is the input.
GPIO.setup(8, GPIO.IN)
# Initialise GPIO10 to high (true) so that the LED is off.
GPIO.output(10, True)
while 1:
    if GPIO.input(8):
        GPIO.output( 10, False)
    else:
        # When the button switch is not pressed, turn off the LED.
        GPIO.output( 10, True)

Operating a bicoloured LED on the Raspberry Pi

Next we'll replace the normal LED with a bicoloured LED. You will need:
– 1 × Working Raspberry Pi with an Internet connection
– 1 × Breadboard + jumpers
– 5 × Male/female jumper cables
– 1 × Bicoloured LED
– 1 × Pushbutton switch
– 1 × 270Ω resistor
– 1 × 10kΩ resistor

GPIO10 controls the green LED, GPIO12 controls the red LED, and the switch remains the same.

  1. Swap the LED in the previous circuit with a bicoloured LED in the following manner. For the bicoloured LED, we need to check the technical datasheet to determine how to connect the pins. In this case, the central pin is the anode pin and should be connected to the power rail via a resistor. The remaining longer pin corresponds to the red LED and should be connected to GPIO12 (bottom) while the last shorter pin should be connected to GPIO10 (top).
  2. 2Connect the LED pins to the GPIO pins on the Raspberry Pi pin header using two jumper cables.

Now, let's have code that changes the state of the LED every second when the button is pressed:

# Import the required modules.
import RPi.GPIO as GPIO
import time
# Set the numbering sequence of the pins, then set pins ten and twelve to output, and pin eight to input.
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(8, GPIO.IN)
# Turn both of the LEDs off.
GPIO.output(10, True)
GPIO.output(12, True)
# The SwitchState variable is 1 if the button is pressed, and 0 otherwise. LEDState is 0 when off, 1 when red, and 2 when green.
SwitchState = 0
LEDState = 0
Operating a Simple Switch and LED on the Raspberry Pi Schemetic
while 1:
    if GPIO.input(8)
# When the LED is off, keep the green LED off, turn the red one on, then change the state of the LED to reflect that it is red, and wait one second.
        if LEDState == 0
            GPIO.output(10, True)
            GPIO.output(12, False)
            LEDState = 1
            time.sleep(1)

For more detail: Operating a Simple Switch and LED on the Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top