Skip to content

4. A knob, a dimmer and a servo

In this tutorial you read a knob, which gives a voltage between 0 and 3.3 V, and use it to set an LED's brightness and a servo's angle. Along the way you meet the ADC, which measures voltages, and PWM, which switches a pin on and off fast enough to act like a voltage in between.

Set up

microgoose new knob --example velxio/dimmer

This circuit has a potentiometer (the knob), an LED and a servo.

Part ESP32 DevKit V1 ESP32-S3-DevKitC-1
potentiometer: middle pin GPIO34 GPIO4
potentiometer: outer pins 3V3 and GND 3V3 and GND
LED, through a 220 Ω resistor to GND GPIO18 GPIO5
servo: signal (orange or yellow wire) GPIO19 GPIO6
servo: power (red) and ground (brown or black) VIN and GND 5V and GND

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 knob). Then, each time, press Ctrl+Shift+B. In a terminal, the same is:

microgoose run knob/main.goose --target esp32    # or --target s3

A servo takes more current than a USB port gives some boards; if the board resets when the servo moves, power the servo from its own 5 V supply, with its ground joined to the board's GND.

Each time, build: choose Terminal > Run Task... > MicroGoose: files for Velxio (velxio.dev) in VS Code, or in a terminal:

microgoose velxio knob/main.goose

The first time, import velxio/knob-circuit.zip with File > Import project. Then each time: Stop, File > Upload firmware with velxio/knob-esp32.bin, and Run. To turn the knob while the program runs, drag it with the mouse.

Read the knob

import mcu;

let KNOB = 34;      // the potentiometer's middle pin: GPIO34

fn main() {
    print("the ADC has ", adc_bits(), " bits: readings 0 to ", (1 << adc_bits()) - 1);
    while true {
        let raw = adc_read_raw(KNOB);
        let mv = adc_read_mv(KNOB);
        print("raw ", raw, ", ", mv, " mV");
        delay_ms(500);
    }
}

Turn the knob and watch the numbers follow it. From one end of the knob to the other, in Velxio:

the ADC has 12 bits: readings 0 to 4095
raw 0, 142 mV
raw 2614, 2248 mV
raw 4095, 3134 mV
  • The knob is a potentiometer: its middle pin gives a voltage between those of its outer pins, 0 V and 3.3 V, depending on where it is turned.
  • The ADC (analog-to-digital converter) measures that voltage. adc_read_raw gives the reading as the ADC counts it, 0 to 4095 on the ESP32. adc_read_mv gives it in millivolts.
  • The ESP32's ADC doesn't reach the very ends. It reads raw 0 for anything below about 140 mV, and raw 4095 for anything above about 3.1 V. A board's numbers differ a little from these, since each chip has its own calibration.
  • Not every pin has an ADC. On the ESP32, use GPIO32 to GPIO39; on the S3, GPIO1 to GPIO10. Those keep working while WiFi is on.

Dim an LED

An output pin is either on or off. To make an LED glow at half brightness, PWM (pulse-width modulation) switches it on and off thousands of times a second, on half the time. Your eye sees the average.

import mcu;

let KNOB = 34;
let LED = 18;

fn main() {
    let top = (1 << adc_bits()) - 1;
    print("LED PWM at ", pwm_start(LED, 5000), " Hz");
    while true {
        let share = adc_read_raw(KNOB) as f32 / top;     // 0.0 to 1.0
        pwm_duty(LED, share * share * 0.999);   // squared: closer to how eyes see brightness
        delay_ms(20);
    }
}

The LED brightens as you turn the knob up.

  • pwm_start(LED, 5000) starts a PWM wave on the pin, 5000 times a second, and returns the rate the hardware could make.
  • pwm_duty(LED, 0.25) keeps the pin on a quarter of each period.
  • The knob's position becomes a number from 0.0 to 1.0, share. Squaring it makes the low end of the knob finer, which is closer to how eyes see brightness than a straight line.
  • Velxio shows an LED at exactly 100% as off, so the program stays just under, at 99.9%. A board doesn't need that.

Turn a servo

A hobby servo turns to an angle set by the pulses on its signal wire: a pulse every 20 ms, and the pulse's length sets the angle. About 544 µs is 0°, and about 2400 µs is 180°.

import mcu;

let KNOB = 34;
let LED = 18;
let SERVO = 19;

fn main() {
    let top = (1 << adc_bits()) - 1;
    pwm_start(LED, 5000);
    print("servo PWM at ", pwm_start(SERVO, 50), " Hz");     // a pulse every 20 ms
    var shown = -1;
    while true {
        let share = adc_read_raw(KNOB) as f32 / top;
        pwm_duty(LED, share * share * 0.999);
        let us = 544 + (1856 * share) as i32;     // 544 us is 0 degrees, 2400 us is 180
        pwm_pulse_us(SERVO, us);
        let degrees = (share * 180.0 + 0.5) as i32;
        if degrees != shown {
            print("servo at ", degrees, " degrees (", us, " us pulses)");
            shown = degrees;
        }
        delay_ms(20);
    }
}

The servo's arm follows the knob, and the LED with it. Turning the knob from one end, to a little past the middle, to the other end:

servo PWM at 50 Hz
servo at 180 degrees (2400 us pulses)
servo at 115 degrees (1728 us pulses)
servo at 0 degrees (544 us pulses)
  • pwm_start(SERVO, 50) makes 50 pulses a second: one every 20 ms.
  • pwm_pulse_us(SERVO, us) sets each pulse's length in microseconds, instead of a share of the period.
  • Servos differ: if yours buzzes at either end, narrow the range, for example 1000 to 2000 µs.

The program you have now is close to the bundled velxio/dimmer example, which knob/main.goose started as. Compare them.

Your turn

Make the servo sweep by itself from 0° to 180° and back, taking two seconds each way, while the knob still sets the LED.

Hint: a for loop of 100 steps, 20 ms apart, takes two seconds.

One way to do it
import mcu;

let KNOB = 34;
let LED = 18;
let SERVO = 19;

fn step(share: f32) {
    let top = (1 << adc_bits()) - 1;
    let knob = adc_read_raw(KNOB) as f32 / top;
    pwm_duty(LED, knob * knob * 0.999);
    pwm_pulse_us(SERVO, 544 + (1856 * share) as i32);
    delay_ms(20);
}

fn main() {
    pwm_start(LED, 5000);
    pwm_start(SERVO, 50);
    while true {
        for i in 100 { step(i / 100.0); }        // 0 to 180 degrees
        for i in 100 { step(1 - i / 100.0); }    // and back
        print("swept there and back at ", millis(), " ms");
    }
}

What you did

  • Measured a voltage with the ADC.
  • Used PWM two ways: a duty cycle for brightness, and a pulse length for a servo's angle.

Where to go next

  • Hardware API: every function of import mcu;, including the UART, I2C and SPI buses that most sensors and displays use, and CAN.
  • Networking: join WiFi, serve a web page, publish to MQTT, and update the board over the air.
  • The other examples: microgoose new NAME --example NAME with threads, boots, analog, pwm, interrupts, serial, web, mqtt and more (see Getting started).