Analog input · Lesson 4 of 5· 15 min

Reading a dial

Why this matters

A button is on or off. Many things in the world are not: temperature, light, how far a dial is turned. These are analog values, and the Pico can read them.

The potentiometer

A potentiometer is a dial. It has three pins: one to 3.3 volts, one to GND, and the middle one, the signal, gives a voltage that changes as you turn it.

Open a tool

Wokwi simulator

Free, nothing to install. Works best on a laptop; on a phone you can still read the steps and the code below.

Opens wokwi.com in a new tab. Come back here when you are done.

Open Wokwi simulator

Step by step

  1. Add a potentiometer in Wokwi.

  2. Wire VCC to 3V3, GND to GND, and SIG to GP26. GP26 is one of the Pico's analog pins (ADC0).

from machine import ADC, Pin, PWM
import time

dial = ADC(26)
lamp = PWM(Pin(15))
lamp.freq(1000)

while True:
    value = dial.read_u16()     # a number from 0 to 65535
    lamp.duty_u16(value)        # the bigger the number, the brighter
    print(value)
    time.sleep(0.1)

Turn the dial in the simulator. The LED dims and brightens, and the numbers scroll in the window below the code.

What PWM does

PWM (pulse width modulation) switches the pin on and off very fast. If it is on most of the time, the LED looks bright; if it is on a little of the time, it looks dim. The same trick controls motor speed.

You’re reading for free. Sign in to save your progress, ask questions on any lesson, and earn a certificate when you finish.Sign in to keep my progress →

Lesson 4 of 5