Projects / Pico SD card voltage logger

Pico SD card voltage logger

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. PICOMICRO

    Format the card

    FAT32 on a computer, then put it in the module.

  2. PICO

    Wire the module

    VCC to 3V3(OUT), GND to GND, SCK to GP18, MOSI to GP19, MISO to GP16, CS to GP17.

  3. PICO

    Wire the potentiometer

    Ends to 3V3(OUT) and GND, middle to GP26.

  4. PICO

    Wire the button

    GP14 to GND.

  5. PICOMICRO

    Save sdcard.py

    On the Pico.

  6. PICO

    Run the program

    The light comes on while it logs.

  7. PICOMICRO

    Turn the knob and wait a minute

    Then press the button, remove the card and open log.csv.

In short

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

FromToWhy
SD module VCCPico 3V3(OUT) pin 36Power at 3.3 V (use a 3.3 V module)
SD module GNDPico GND pin 38Common ground
SD module SCKPico GP18 pin 24SPI clock
SD module MOSIPico GP19 pin 25SPI data out
SD module MISOPico GP16 pin 21SPI data in
SD module CSPico GP17 pin 22Chip select
Potentiometer endsPico 3V3(OUT) pin 36 and GND pin 33Test voltage source
Potentiometer middlePico GP26 pin 31ADC0 input
Stop buttonPico GP14 pin 19 and GNDCloses 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.

CheckExpected
StartLight on, no errors
Wait a minuteAbout sixty lines on the card
Turn the knob during the runThe volts column changes
Press the buttonLight off and a message
Open log.csv on a computerSeconds 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 →

Ask Lark AI

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.