The cyber:bot board has a built-in Propeller microcontroller on its underside. The Propeller has 32 digital input/output pins, called I/O pins for short, that are designed to interact with circuits. These are referred to by number, P0 through P31.
Some of the I/O pins are connected to circuits built into the cyber:bot board, that your robot is already using. For example, P16 through P19 are connected to 3-pin headers, where the servo motors are plugged into P18 and P19. In this activity, we will use P20 and P21, which are connected to tiny built-in LED lights. Later on, we will build circuits on the breadboard, and connect them to the sockets alongside labeled P0–P15.
We'll experiment with the "O" (output) feature of an I/O pin with a program to turn the built-in LED circuit on and off.
- Locate the small LED light.
On the cyber:bot, it’s a small part just above the P20 label in the lower-right corner of your board.
Enter, save, and flash pin_20_blink.py to your micro:bit.
- Set the 3-position switch to 1, and watch the LED above P20.
# pin_20_blink.py from cyberbot import * while True: bot(20).write_digital(1) sleep(2000) bot(20).write_digital(0) sleep(1000)
How it works
The program begins with from cyberbot import *. This lets your script use bot(pin) functions from the cyberbot module, which send command codes to the Propeller microcontroller. From there, the Propeller uses its pre-programmed firmware to interact with circuits connected to its I/O pins.
The function call bot(20).write_digital(1) sets Propeller I/O pin P20 to "output high" which means it connects to its 3.3 V supply, as shown on the left side of the image below. The pin applies 3.3 V of electrical pressure to the LED circuit, causing electric current to pass through it and the light to turn on. After that, sleep(2000) makes the program do nothing for 2000 ms, which keeps the light on for 2 seconds.
Next, bot(20).write_digital(0). This sets P20 to output-low, which connects the pin to its 0 V ground supply voltage instead, as shown on the right side of the figure below. This takes away the electrical pressure, so the current stops flowing through the circuit and the light turns off. Another sleep(1000) makes the light stay off for one second.
Those four commands are in a code block in a while True: loop, which repeats endlessly, so the light keeps blinking.
Try this: pin_20_blink_fast.py
You can change the light's blink rate by changing the sleep function’s us argument. For example, to make the light blink a lot faster, significantly reduce the sleep time.
- Rename pin_20_blink.py to pin_20_blink_fast.py
- Change sleep(2000) and sleep(1000) to sleep(50).
- Save and flash the script, then watch the LED located at P20 — is it blinking faster now?
# pin_20_blink_fast.py from cyberbot import * while True: bot(20).write_digital(1) sleep(50) bot(20).write_digital(0) sleep(50)
- Expand your script to control the P21 light along with the P20 light.
- Modify your program so that it turns both lights on and off at about the same time.
- Modify your program so that whenever one light is on, the other is off.