You have blinked the built-in LEDs on your board. Now it's time to build your own LED circuits on your cyber:bot board's 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. In the picture below, 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 two LED circuits on your breadboard. In addition to the two LEDs, you will need two 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.
Parts Needed
(2) red LEDs
(2) 220 ohm resistors (red-red-brown)
(misc) Jumper wires
- Set the cyber:bot board's PWR switch to 0.
- Build two LED circuits, one each connected to P9 and P8, according to the schematic.
- Use the wiring diagram for your board, below, to double-check your work.
LED Test Script: pin_8_blink
This is the same test code that was used to blink the built-in P20 LED earlier in this tutorial, with the I/O pin number updated.
- In the micro:bit Python Editor, click Open, and then open pin_20_blink,
- Set the project's name to pin_8_blink, update the script so that it matches the one below, and then click Save.
- Click Send to micro:bit.
# pin_8_blink from cyberbot import * while True: bot(8).write_digital(1) sleep(2000) bot(8).write_digital(0) sleep(1000)
- On the cyber:bot board, set the power (PWR) switch to position 1.
- Verify that the LED connected to P8 should blinks.
- If the LED does not blink, check your wiring and try again. You might have the LED in backwards, or one of the leads or wires in the wrong socket.
- Once you have the P8 LED blinking, change bot(8)to bot(9), then re-run the code to test your P9 LED circuit.
- If you've verified that both your LED circuits are working, great! Continue on!
With the write_digital function, you have been using a 1 to connect the I/O pin to 3.3V, or 0 to connect to 0 V. However, you can also use True or False instead of 1 or 0. Let's try it.
- In the write_digital function calls, change (1) to (True) and (0) to (False).
bot(8).write_digital(True) sleep(2000) bot(8).write_digital(False) sleep(1000)
- Re-flash the script with the Send to micro:bit button.
- Verify that True and False work to blink the LED the same way as 1 and 0.
Try This
Let's take advantage of the programmable write_digital function. Paired with the for loop, it’s easy to make the LEDs light up one at a time.
- Set the project's name to flashing_LEDs, enter the script below, and then click Save.
- Click Send to micro:bit to flash the script into it.
- Verify that the script repeatedly blinks the P8 LED on/off and then blinks the P9 LED blinks on/off.
# flashing_LEDs from cyberbot import * while True: for pin_number in range(8, 10): bot(pin_number).write_digital(1) sleep(50) bot(pin_number).write_digital(0) sleep(50)
Your Turn
- Try changing the sleep times for different effects.
- Try adding a sleep call after the for loop.
- Try making a copy of the for loop, and pasting it after that sleep call. Then reverse its range from 9 to 7.
- Try modifying the program to make both of the LEDs blink at the same time.