Mission: Control an LED circuit using a Raspberry Pi to create a blinking LED & blinking LED with a switch
- Build a basic LED circuit
- Connect the circuit to the Raspberry Pi
- Setup Scratch 2 so that it can control the Raspberry Pi’s GPIO output
- Construct a program that will control the LED, making it blink on and off
- Bonus round
What you will need
- 2x Male to Female Jumper Cables – I chose Red & Black, but any will do.
- 1x LED – Green or Red, whatever you fancy!
- 1x 330 Ohm (330R) 0.25W Resistor
Part 1: Build a basic LED circuit
1. Put the Male pin of the first jumper cable (Black in the image) into any slot on Column 1 (Column 1, Row L in the image)
2. Put one leg of the 330 OHM resistor into any slot on Column 1 (Column 1, Row G in the image)
3. Put the second leg of the 330 OHM resistor into another Column (Column 7, Row G in the image)
4. Place the anode (aka “The Long Leg”) of the LED, into the same column as the second leg of your resistor (Column 7, Row I in the image)
5. Place the cathode (aka “The Short Leg”) of the LED into another column that hasn’t been used (Column 10, Row I in the image)
6. Put the male pin of the second jumper cable (Black in the image) into the same column as the "Short Leg" of the LED (Column 10, Row L)
What you have just built and how it will work:
1. The first jumper cable will connect the positive GPIO pin on the Raspberry Pi to the breadboard
2. This positive feed will be carried to the resistor, limiting the amount of power being carried to the LED.
3. Power will pass through the LED, and the negative side of the circuit will pass through the second jumper cable, to the ground pin of the Raspberry Pi.
Part 2: Connect the circuit to the Raspberry Pi
1. Connect the jumper on the positive side of the circuit (the side with a resistor), to GPIO 25.
2. Connect the jumper on the positive side of the circuit (after the LED) to GPIO 6 (third pin down).
Part 3: Setup Scratch 2 so that it can control the Raspberry Pi’s GPIO output
1. Open Scratch 2 by clicking on the Raspberry Pi Start Menu, navigating to Programming and then selecting Scratch 2.
2. To control the Raspberry Pi’s GPIO pins, we first need to add the GPIO extension. Select “More Blocks” from the “Scripts” tab and click on “Add an Extension”.
3. You will be presented with two options. Double-click on “Pi GPIO” to install the extension.
4. Now that you have the GPIO extension installed, you will gain access to the GPIO control blocks and can start constructing the program.
Part 4: Construct a program that will control the LED, making it blink on and off
1. We first need to state how we are going to start our program. Select “Events”, then click and drag the “When Green Flag Clicked” block to the canvas section on the right.
2. Next, we’ll need to create a loop, so that the LED will blink on and off multiple times. Navigate to the “Control” blocks, then click and drag a “Repeat” block over to the canvas, snapping it to the bottom of the “When Green Flag Clicked” block.
Note: You can change the number of times the loop repeats by clicking the number field on the block.
3. To make our LED blink, we must start by making it turn on. Select the “More Blocks” section, and drag a “Set GPIO… to output…” block into the repeat loop.
4. If you followed Part 2, your LED should be connected to GPIO 25 on the Raspberry Pi. Open the dropdown menu and select GPIO 25 on the “Set gpio… to output…” block, so that the program knows to control the pin that the LED is connected to.
5. So that you can see the LED being turned on and off, you’ll need to create a gap between the LED turning on and off. Under the “Control” section, drag a “Wait… secs” block to the loop, so that it is under the “Set GPIO to output high” block.
Note: You can choose how long you want the LED to remain on for, by clicking on the number in the “Wait” block.
6. With the LED told to turn on for 1 second, next we want the LED to turn off. Drag another “Set gpio…to output…” block over to the loop, under the “Wait…secs” block. Select gpio 25 again, but this time, open the second dropdown and select “output low”.
7. We now want to tell the LED to remain off for a set amount of time, before turning on again. Add a “Wait…secs” block under the “set gpio 25 to output low” block.
8. Now that you’ve got a completed circuit, you can test it by clicking the green flag icon.
You should now have a flashing LED! Stop the program by clicking the red Stop button, next to the green flag.
- Check each part of the code first, making sure it matches the code in the images
- Check you’re connected to the right GPIO connections
- Check that the circuit is wired correctly
Part 5: Bonus round
You’ve succeeded in creating a flashing LED circuit… now lets make it a little smarter.
1. Drag a “Stop” block over from the “Control” block section. Place it below the “Repeat” loop, and tell the loop to repeat 10 times. That way, once you’ve started the program, it will flash the LEDs on and off 10 times, and stop automatically!
2. Why not add a switch into the circuit, so you can only see the LED flash when you are pressing a button?
- Start with the circuit from Part 1
- Move the LED so the positive side is in Column 7, Row H and the negative is in Column 11, Row H
- Move the second jumper, so that the pin is in Column 13, Row G
- Plug a switch in, so that it has two pins in Column 11, and two in Column 13
- Start the program again and see if the LED turns on and off when you hold the button down. Try rotating the switch if it doesn’t work!