Build it step by step
0 of 7 doneMake parts.csv
Four columns.
Install the libraries
qrcode and Pillow.
Run make
Open one PNG.
Scan a label
With a phone camera.
Print and stick one
Check it is readable.
Try add and list
Update the stock.
Back up the file
To a second computer.
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
| From | To | Why |
|---|---|---|
| Pi Zero | Power supply, SD card and Wi-Fi | A small home server |
| Label printer or ordinary printer | Print the PNG labels from any computer | Output |
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.
| Check | Expected |
|---|---|
| make | One PNG per part |
| Scan the label | Opens the datasheet |
| add R10K 5 | Count goes up by five |
| list | Shows 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 →
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.
