You have blinked the built-in LEDs on your board. Now it's time to build your own LED circuits on your Propeller prototyping area.
Introducing the LED
A diode is an electrical part that only lets electricity flow through it in one direction. A light-emitting diode (LED) emits light when current passes through it. You have to make sure to connect an LED the right way for it to light up. If you plug an LED in backwards it will not hurt it, but it will not emit light.
An LED has two terminals: the anode and the cathode. The anode lead is labeled with the plus-sign (+), and it is the wide part of the triangle in the schematic symbol. The cathode lead is labeled with a minus-sign (-), and it is the line across the point of the triangle in the schematic symbol.
You can tell the anode and cathode apart by the shape of the LED’s plastic case. Look closely — it’s mostly round, but there is a small flat area near the cathode lead. Also note that the LED’s leads are different lengths. Usually, the shorter lead is connected to the cathode.
Always check the LED’s plastic case. Sometimes the leads have been clipped to the same length, or a manufacturer does not follow this convention.
Example LED Circuits
Let's build three LED circuits on your breadboard. In addition to the three LEDs, you will need three resistors. Unlike the LEDs, the resistors do not have positive and negative leads, so you don't have to worry about plugging them in backward. Resistors resist the flow of electrical current. Each one has a value that tells how strongly it resists current flow, measured in ohms, often noted by Greek letter omega: Ω. Take a look at that Resistor Color Codes page to see how its bands indicate its value.
- (3) LEDs; red, yellow, and green if you have them
- (3) 220 ohm resistors (red-red-brown)
- (misc) Jumper wires
- Disconnect power to your prototyping area.
- Build three LED circuits, one each connected to P10, P9, and P8, according to the schematic.
- Use the wiring diagram for your board, below, to double-check your work.
LED Test Code
This is the same test code that was used to blink the built-in P26 LED earlier in this tutorial, with the I/O pin number updated.
- Log into your BlocklyProp account, and make a new project for your board.
- Connect power to your prototyping area.
- Build and save the project shown below, which blinks the LED connected to P8.
- Click the Run Once button, and verify that your P8 LED is indeed blinking. (If it is not, check your wiring and try again. You might have the LED in backwards, or one of the leads or wires in the wrong socket.)
- Set the make PIN blocks to use 9, then re-run the code to test your P9 LED circuit.
- Set the make PIN blocks one more time to use 10, then re-run the code to test your P10 LED circuit.
If you've verified that all three of your LED circuits are working, great! Continue on!
Did You Know?
The make PIN block comes in two versions. The original one we have been using has a drop-down list for selecting the I/O pin number. The programmable version accepts a number value block, a use variable block, or any expression that resolves to a number.
Let's take advantage of the programmable make PIN blocks. Paired with the repeat item loop, its easy to make them light up one at a time in order.
- Make a new project for your board.
- Build, save, and run the project shown below. The LEDs should light up one at a time, in order.
- Try changing the pause times for different effects.
- Try adding a pause block after the repeat item loop.
- Try making a copy of the repeat item loop, and pasting it after that pause block. Then reverse its repeat item range from 10 to 8.
Perhaps you want the LEDs to light up out of order! Instead of using the repeat item block, you could use the random operator to choose which LED should light up each time through the loop. It's available under Operators > Number.
- Modify the code using the random block to select which LED to light. Be sure to match the variable names!
Save your code, and click the Run Once button.
- Once that is working, add two more set variable and random blocks to control each pause block. Choose whatever number range you like for the pause times.
- Test-drive your changes, and adjust the pause times until you like the effect.
Can you imagine using this technique to make twinkling stars, scary eyes, or fireflies in a project?