Pico SD card voltage logger
Build it step by step
0 of 7 doneFormat the card
FAT32 on a computer, then put it in the module.
Wire the module
VCC to 3V3(OUT), GND to GND, SCK to GP18, MOSI to GP19, MISO to GP16, CS to GP17.
Wire the potentiometer
Ends to 3V3(OUT) and GND, middle to GP26.
Wire the button
GP14 to GND.
Save sdcard.py
On the Pico.
Run the program
The light comes on while it logs.
Turn the knob and wait a minute
Then press the button, remove the card and open log.csv.
How do you build a pico SD card voltage logger?
A Pico reads an analog input once a second and writes a CSV line with the time and the voltage to a microSD card module. You need Raspberry Pi Pico H, Micro SD card module (SPI, 3.3 V), Potentiometer 10 kΩ, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| SD module VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V (use a 3.3 V module) |
| SD module GND | Pico GND pin 38 | Common ground |
| SD module SCK | Pico GP18 pin 24 | SPI clock |
| SD module MOSI | Pico GP19 pin 25 | SPI data out |
| SD module MISO | Pico GP16 pin 21 | SPI data in |
| SD module CS | Pico GP17 pin 22 | Chip select |
| Potentiometer ends | Pico 3V3(OUT) pin 36 and GND pin 33 | Test voltage source |
| Potentiometer middle | Pico GP26 pin 31 | ADC0 input |
| Stop button | Pico GP14 pin 19 and GND | Closes the file safely |
Code
A microSD card module lets a Pico keep data after it is unplugged. The card is talked to over SPI, using a clock, a data line in each direction and a chip-select line. MicroPython can treat the card like a small disk once a driver file called sdcard.py is on the Pico, and the program mounts it as the folder /sd, after which it is used like any other file. Every second the Pico averages sixteen readings of the analog input, turns the number into volts, and writes one line of comma-separated text. Open the file later in a spreadsheet to draw a graph. Cards must be formatted as FAT32, and a card that is pulled out while a file is open can be damaged, so the program saves to the card every five lines and has a stop button that closes the file and unmounts the card, after which the light goes off. Inputs must stay between 0 and 3.3 volts. Use a divider for anything higher and never connect mains.
import time
import os
from machine import Pin, SPI, ADC
import sdcard # sdcard.py from the MicroPython drivers folder
spi = SPI(0, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
sd = sdcard.SDCard(spi, Pin(17))
os.mount(os.VfsFat(sd), "/sd")
adc = ADC(26)
button = Pin(14, Pin.IN, Pin.PULL_UP)
led = Pin(25, Pin.OUT) # the onboard light on a plain Pico
filename = "/sd/log.csv"
new_file = "log.csv" not in os.listdir("/sd")
f = open(filename, "a")
if new_file:
f.write("seconds,volts\n")
start = time.ticks_ms()
count = 0
led.value(1) # on while logging
while button.value() == 1:
total = 0
for _ in range(16):
total += adc.read_u16()
volts = (total / 16) * 3.3 / 65535
seconds = time.ticks_diff(time.ticks_ms(), start) / 1000
f.write("%.0f,%.3f\n" % (seconds, volts))
count += 1
if count % 5 == 0:
f.flush() # save to the card every five lines
time.sleep(1)
f.close() # button pressed: close the file safely
os.umount("/sd")
led.value(0)
print("Logging stopped, you can remove the card")Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start | Light on, no errors |
| Wait a minute | About sixty lines on the card |
| Turn the knob during the run | The volts column changes |
| Press the button | Light off and a message |
| Open log.csv on a computer | Seconds and volts columns |
If it does not work
OSError mounting the card
Check the wires, and that the card is FAT32 and 32 GB or smaller.
ImportError for sdcard
Save sdcard.py on the Pico.
Empty file
Press the stop button, or wait for the five-line save.
Garbage in the file
Re-format the card and avoid pulling it out while the light is on.
Voltage reads wrong
The input must be between 0 and 3.3 V, and the pot ends need 3.3 V and GND.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico SD card voltage logger?
Raspberry Pi Pico H, Micro SD card module (SPI, 3.3 V), Potentiometer 10 kΩ, Tactile push button 6 mm and Breadboard 830 Tie Points.
How long does the pico SD card voltage logger take to build?
About 150 minutes for a intermediate maker, in 7 steps.
How do I know the pico SD card voltage logger works?
Start: Light on, no errors. Wait a minute: About sixty lines on the card. Turn the knob during the run: The volts column changes.
OSError mounting the card: what should I check?
Check the wires, and that the card is FAT32 and 32 GB or smaller.
ImportError for sdcard: what should I check?
Save sdcard.py on the Pico.
Empty file: what should I check?
Press the stop button, or wait for the five-line save.
Garbage in the file: what should I check?
Re-format the card and avoid pulling it out while the light is on.
