Projects / Component bag QR labeller

Component bag QR labeller

Intermediate1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi Zero

    Make parts.csv

    Four columns.

  2. Pi Zero

    Install the libraries

    qrcode and Pillow.

  3. Pi Zero

    Run make

    Open one PNG.

  4. Pi Zero

    Scan a label

    With a phone camera.

  5. Pi Zero

    Print and stick one

    Check it is readable.

  6. Pi Zero

    Try add and list

    Update the stock.

  7. Pi Zero

    Back up the file

    To a second computer.

In short

How do you build a component bag QR labeller?

A Pi makes QR labels for parts bags that link to datasheets and your stock count. You need Raspberry Pi Zero 2 W, SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 15 W USB-C power supply. It takes 7 steps and about 90 minutes.

Level
Intermediate
Time
About 90 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Pi ZeroPower supply, SD card and Wi-FiA small home server
Label printer or ordinary printerPrint the PNG labels from any computerOutput

Code

Make a spreadsheet of your parts with four columns, an ID such as R10K, a name, the link to the datasheet, and how many you have, save it as parts.csv and copy it to the Pi. The script turns each row into a small PNG label with a QR code that opens the datasheet when scanned with a phone camera, plus the ID and name in text. Print the PNGs on any printer, or on a label printer by sending the images to it, and stick one on each bag or drawer. To keep the count, the same script adds or removes a number from a part, which you can run over SSH, or later from a small web page. Keep the file in a folder backed up to another computer, because a corrupt SD card would lose it. The QR holds only a public link or a part ID, so it is safe to put on a label, but never put passwords or personal information in a QR code. Raspberry Pi OS (Bookworm or newer) with Python 3. The default font is small; for bigger text, load a TrueType font from /usr/share/fonts with ImageFont.truetype.

# labels.py: makes one QR label per part from parts.csv (columns: id,name,datasheet_url,qty).
#   python3 labels.py make          creates labels/ID.png for each part
#   python3 labels.py add ID 5      adds 5 to a part's stock count (use a negative number to remove)
#   python3 labels.py list          shows the stock
# Install first:  sudo apt install python3-qrcode python3-pil
import csv, sys, os
import qrcode
from PIL import Image, ImageDraw, ImageFont

FILE = "parts.csv"

def load():
    with open(FILE, newline="") as f:
        return list(csv.DictReader(f))

def save(rows):
    with open(FILE, "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=["id", "name", "datasheet_url", "qty"])
        w.writeheader(); w.writerows(rows)

def make():
    os.makedirs("labels", exist_ok=True)
    font = ImageFont.load_default()
    for r in load():
        qr = qrcode.make(r["datasheet_url"] or r["id"]).resize((220, 220))
        label = Image.new("RGB", (600, 260), "white")      # about 50 x 22 mm at 300 dpi
        label.paste(qr, (20, 20))
        d = ImageDraw.Draw(label)
        d.text((260, 40), r["id"], fill="black", font=font)
        d.text((260, 80), r["name"], fill="black", font=font)
        label.save("labels/%s.png" % r["id"])
        print("made", r["id"])

cmd = sys.argv[1] if len(sys.argv) > 1 else "list"
if cmd == "make":
    make()
elif cmd == "add":
    rows = load()
    for r in rows:
        if r["id"] == sys.argv[2]:
            r["qty"] = str(int(r["qty"]) + int(sys.argv[3])); print(r["id"], "now", r["qty"])
    save(rows)
else:
    for r in load():
        print(r["id"], r["name"], r["qty"])

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
makeOne PNG per part
Scan the labelOpens the datasheet
add R10K 5Count goes up by five
listShows the current stock

If it does not work

Label too small to scan

Increase the QR size or print larger.

KeyError

Check the column names in the CSV.

ModuleNotFoundError

Install the libraries with apt.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the component bag QR labeller?

Raspberry Pi Zero 2 W, SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 15 W USB-C power supply. Raspberry Pi 15 W USB-C power supply: Use micro-USB for the Zero.

How long does the component bag QR labeller take to build?

About 90 minutes for a intermediate maker, in 7 steps.

How do I know the component bag QR labeller works?

make: One PNG per part. Scan the label: Opens the datasheet. add R10K 5: Count goes up by five.

Label too small to scan: what should I check?

Increase the QR size or print larger.

KeyError: what should I check?

Check the column names in the CSV.

ModuleNotFoundError: what should I check?

Install the libraries with apt.