Voltmeter on an LCD: Raspberry Pi Pico 2 project with wiring and code
Make a voltmeter with the Raspberry Pi Pico 2. A potentiometer is on GP26, an analog input, and gives a voltage from 0 to 3.3 V. A 16 × 2 LCD with an I²C backpack is on GP4 (SDA) and GP5 (SCL). The first line already says "Voltmeter". On the second line show the voltage with two decimal places and the letter V, for example "1.65 V". Update it five times each second.
You will learn: The pico_i2c_lcd driver: I2cLcd(i2c, 0x27, 2, 16), putstr() and move_to(column, line). Turning an ADC reading (0 to 65535) into volts, and showing a number with a fixed count of decimal places using format().
Project 58 of 85 in the project gallery. Lesson 4 of 7 in the course Raspberry Pi Pico with MicroPython.
- The idea behind this page: how a microcontroller project works
- How to study this page
- Mistakes students make
- Key words
- What to learn first and next
The idea behind this page: how a microcontroller project works
A microcontroller project has three sides that must all be right: the parts, the wiring and the program. A microcontroller is a small computer on one chip. Its pins can read inputs, such as a button or a sensor, and switch outputs, such as an LED, a buzzer or a motor. The program, called a sketch on an Arduino, is the list of instructions that says what the outputs should do when the inputs change. Almost every project follows the same pattern: read an input, decide, then set an output, and repeat this many times a second.
A guided project gives you a goal, the parts, the wiring and starter code with gaps for you to complete. The simulator then checks your work by running it, the way a teacher would test a real circuit. The solution is not shown on this page, because working it out is how the ideas are learned.
This page is about one guided project: Voltmeter on an LCD. Read it with these ideas in mind.
How to study this page
- Read the goal at the top, then the parts table to see what each part is for.
- Study the wiring table and the picture: which leg of each part goes to which pin.
- Read the starter code line by line before you change it. Find the place where your own lines must go.
- Open the project in the simulator, complete the code and press Run. Read any message the check gives you: it says what is wrong and what to write.
- When it passes, change one thing (a pin, a delay, a limit) and predict what will happen before you run it again.
Mistakes students make
- Using a different pin in the code from the one in the wiring.The pin number in the sketch must match the pin the wire really goes to.
- Leaving out pinMode() in setup().A pin must be set as an input or an output before it is used, or it will not behave as expected.
- Changing many things at once.Change one thing, run, and see what happens. Then you know which change caused the result.
- Ignoring the check message.The message names the fault. Read it fully before trying again.
Key words
- Microcontroller
- A small, complete computer on one chip, with a processor, memory and input and output pins.
- Sketch
- The Arduino name for a program.
- Pin (of a microcontroller)
- A metal contact on a chip or board through which it reads an input or drives an output.
- Input
- A signal going into the microcontroller, for example from a button or a sensor.
- Output
- A signal coming out of the microcontroller, for example to an LED or a motor.
- Breadboard
- A board with rows of joined holes for building a circuit without soldering.
See every key word in the glossary
What to learn first and next
- Microcontroller simulator: how the simulator and its code editor work.
- Electronic components atlas: look up any part used here.
- Project gallery: the next project in teaching order.
- Circuit builder: see the currents and voltages in a circuit.
Parts you need
| Part | How many | What it does in this project |
|---|---|---|
| Raspberry Pi Pico 2 | 1 | Runs your Python program and powers the circuit. |
| LCD 16×2 with I²C backpack | 1 | The output you read. The first line holds the title and the second line shows the voltage. |
| Potentiometer | 1 | The voltage source to measure. Its middle leg gives 0 V to 3.3 V to GP26 as you turn the knob. |
| Breadboard and jumper wires | 1 | Hold the parts and join them without soldering. |
How to wire it
Put the parts on the breadboard, join the board's 5V and GND pins to the breadboard's power rails, then make these connections:
| Part | Connection |
|---|---|
| Potentiometer | wiper (middle leg) → GP26 |
| LCD 16×2 with I²C backpack | I²C address 0x27: SDA → GP4, SCL → GP5 |
In the Studio, the 💡 guide rings show where each wire goes, and the ✔ Check button tells you which connection is missing.
Starter code
Start from this Python program and write the missing lines. The comments say what each part has to do.
from machine import Pin, I2C, ADC
from pico_i2c_lcd import I2cLcd
import time
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
lcd = I2cLcd(i2c, 0x27, 2, 16) # bus, address, lines, columns
knob = ADC(26) # GP26 is ADC0
lcd.putstr("Voltmeter") # writing starts at column 0, line 0
while True:
reading = knob.read_u16() # 0 (0 V) to 65535 (3.3 V)
# 1. turn the reading into volts: volts = reading * 3.3 / 65535
# 2. move to the start of the second line: lcd.move_to(0, 1)
# the column comes first, then the line, and both count from 0
# 3. write it with two decimal places: lcd.putstr("{:.2f} V".format(volts))
time.sleep(0.2)
Build it step by step
- Open the project. Press Build it in the Studio. The parts are already in the tray.
- Place and wire the parts. Follow the wiring table above, or switch on the guide rings.
- Write the code. Complete the starter program in the Code panel. It is Python, written as text.
- Run it. Press ▶ Run and use the part's slider, button or switch to test it. The terminal panel shows what your program prints, and Stop works like Ctrl+C.
- Check it. Press ✔ Check: the simulator tests the circuit and the code and tells you exactly what to fix.
Questions students ask
What does the Voltmeter on an LCD project do?
Make a voltmeter with the Raspberry Pi Pico 2. A potentiometer is on GP26, an analog input, and gives a voltage from 0 to 3.3 V. A 16 × 2 LCD with an I²C backpack is on GP4 (SDA) and GP5 (SCL). The first line already says "Voltmeter". On the second line show the voltage with two decimal places and the letter V, for example "1.65 V". Update it five times each second.
What parts do I need for the Voltmeter on an LCD project?
An Raspberry Pi Pico 2, LCD 16×2 with I²C backpack, potentiometer, a breadboard and jumper wires.
How do I wire the Voltmeter on an LCD project?
Potentiometer: wiper (middle leg) → GP26; LCD 16×2 with I²C backpack: I²C address 0x27: SDA → GP4, SCL → GP5. Power comes from the board's 5V (or 3.3V) and GND pins along the breadboard rails.
What will I learn from the Voltmeter on an LCD project?
The pico_i2c_lcd driver: I2cLcd(i2c, 0x27, 2, 16), putstr() and move_to(column, line). Turning an ADC reading (0 to 65535) into volts, and showing a number with a fixed count of decimal places using format().
Can I build this without real hardware?
Yes. The project runs in the free Science Tinker microcontroller simulator in your browser: place the parts, wire them, write the sketch and press Run. The ✔ Check button tests your circuit and code and tells you what to fix. Nothing to install and no account needed.
Where is the finished code?
The page gives the starter sketch so that you write the important lines yourself. If you are stuck, the 💡 Solution button in the Studio shows a complete, working sketch that you can run and study.