3. LEDs and a button¶
In this tutorial you turn an LED on and off, read a button, and then let the chip tell your program when the button is pressed. These are the building blocks of most firmware: outputs, inputs, and events.
Set up¶
Start from the traffic light example. Its circuit has the LEDs and the button this tutorial uses, wired the same way in Velxio and on a board:
microgoose new lamp --example velxio/traffic
You will replace the program in lamp/main.goose at each step, and keep
the circuit beside it in lamp/diagram.json.
Wire the LEDs and the button as in tutorial 1. This tutorial uses these pins:
| Part | ESP32 DevKit V1 | ESP32-S3-DevKitC-1 |
|---|---|---|
| red LED | GPIO25 | GPIO5 |
| yellow LED | GPIO26 | GPIO6 |
| green LED | GPIO27 | GPIO7 |
| button | GPIO14 | GPIO15 |
The programs use the ESP32's numbers. On the S3, change the numbers in
the let lines at the top of each one, and make the folder with
--target s3 added to the microgoose new command above.
Open the folder in VS Code (code lamp). Then, each time, press
Ctrl+Shift+B. In a terminal, the same is:
microgoose run lamp/main.goose --target esp32 # or --target s3
Each time, build: choose Terminal > Run Task... > MicroGoose: files for Velxio (velxio.dev) in VS Code, or in a terminal:
microgoose velxio lamp/main.goose
The first time, import velxio/lamp-circuit.zip with File > Import
project (or keep the traffic light's circuit open from tutorial 1: it
is the same). Then each time: Stop, File > Upload firmware with
velxio/lamp-esp32.bin, and Run.
Blink¶
import mcu;
let LED = 25; // the red LED: GPIO25
fn main() {
pin_output(LED);
print("blinking GPIO", LED);
while true {
pin_write(LED, 1); // on
delay_ms(500);
pin_write(LED, 0); // off
delay_ms(500);
}
}
The red LED blinks once a second.
pin_output(LED)makes the pin an output: the chip drives it high (3.3 V) or low (0 V).pin_write(LED, 1)drives it high. Current flows from the pin, through the resistor and the LED, to GND, and the LED lights.delay_ms(500)waits half a second. Other tasks on the chip, such as WiFi, keep running while it waits.
Try it: make the LED flash briefly, 50 ms on and 950 ms off. Then blink the yellow LED, GPIO26, instead.
Read the button¶
import mcu;
let LED = 25;
let BUTTON = 14;
fn main() {
pin_output(LED);
pin_input(BUTTON, PULL_UP); // reads 1 when nothing pulls it down
print("hold the button down");
var was = false;
while true {
let pressed = pin_read(BUTTON) == 0; // the button connects the pin to GND
pin_write(LED, if pressed { 1 } else { 0 });
if pressed != was {
print(if pressed { "pressed" } else { "released" }, " at ", millis(), " ms");
was = pressed;
}
delay_ms(10);
}
}
The LED is on while you hold the button down.
pin_input(BUTTON, PULL_UP)makes the pin an input, with a resistor inside the chip that pulls it up to 3.3 V. With the button up, the pin reads 1.- Pressing the button connects the pin to GND, so it reads 0. That is why
pressedispin_read(BUTTON) == 0. -
The loop looks at the button 100 times a second, and prints only when it changes:
hold the button down pressed at 23378 ms released at 23435 ms -
In Velxio, a quick click can come and go between two looks, and then the program never sees it. Hold the mouse button down a moment. Velxio can also show one press and release right after Run, before you touch anything. A board doesn't.
Let the button wake the program¶
Checking the button in a loop works, but the program is busy doing it, and a press shorter than one pass of the loop can be missed. The chip can watch the pin instead, and note each press as an event that the program waits for:
import mcu;
let LED = 25;
let BUTTON = 14;
fn main() {
pin_output(LED);
pin_input(BUTTON, PULL_UP);
pin_watch(BUTTON, FALLING, 20000); // an event on each press; 20 ms of bounce ignored
print("press the button: each press switches the LED");
var on: i32 = 0; // pin_write takes an i32
var presses = 0;
while true {
let source = event_wait(5000); // sleeps until an event, for up to 5 s
if source == BUTTON {
presses++;
on = 1 - on;
pin_write(LED, on);
print("press ", presses, ": LED ", if on == 1 { "on" } else { "off" });
} else {
print("no press in 5 s (", presses, " so far)");
}
}
}
Each press switches the LED on or off:
press the button: each press switches the LED
press 1: LED on
press 2: LED off
no press in 5 s (2 so far)
pin_watch(BUTTON, FALLING, 20000)asks the chip for an event each time the pin falls from 1 to 0: each press. A button's contacts bounce for a few milliseconds when they close, and would give several events for one press. The 20000 tells it to ignore changes for 20000 µs (20 ms) after each one.event_wait(5000)sleeps until an event comes, for at most 5 seconds, and returns where it came from: the button's pin number, or −1 when the time ran out.- The chip records each press when it happens, even while the program is busy elsewhere, so a short press isn't missed. Events wait in a queue until the program takes them. Interrupts has the details, and timers, which give events too.
Your turn¶
Make each press of the button light the next LED: red, then yellow, then green, then red again.
Hints: put the three pin numbers in an array, let LEDS: i32[3] = [25, 26,
27];. event_wait(-1) waits for an event with no time limit. % from
tutorial 2 brings a count back to 0.
One way to do it
import mcu;
let LEDS: i32[3] = [25, 26, 27]; // red, yellow, green
let BUTTON = 14;
fn main() {
for pin in LEDS { pin_output(pin); }
pin_input(BUTTON, PULL_UP);
pin_watch(BUTTON, FALLING, 20000);
var lit = 0;
while true {
for pin, i in LEDS { pin_write(pin, if i == lit { 1 } else { 0 }); }
print("GPIO", LEDS[lit], " is lit");
event_wait(-1); // no time limit: wait for the next press
lit = (lit + 1) % 3;
}
}
for pin, i in LEDS goes through the array with each element's index,
so exactly one LED, the one at index lit, is on.
What you did¶
- Drove an output pin, and read an input pin with a pull-up.
- Let the chip turn button presses into events, so the program sleeps until something happens.
Next, tutorial 4 reads a voltage, and uses PWM to dim an LED and turn a servo.