Record QR Data Into CSV Format Using Raspberry Pi

This tutorial is suitable for QR Attendance System application.


After we are able to scan QR code using Raspberry Pi camera, now we want to record those data in CSV format. So, later we can open it using Microsoft Excel, Google Sheets or LibreOffice Calculator. Before that, please make sure you have read and developed following tutorial:


This video shows how to record QR data into CSV format using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python 3 sample program to scan and read QR code using Raspberry Pi camera, and stored the data into CSV format.

from gpiozero import LED, Button, Buzzer
import cv2
import re
import csv
led = LED(19)
sw1 = Button(21)
buzzer = Buzzer(26)
cap = cv2.VideoCapture(0)
detector = cv2.QRCodeDetector()
def sw1Pressed():
global sw1Press
sw1Press = True
sw1.when_pressed = sw1Pressed
sw1Press = False
print("Reading QR code using Raspberry Pi camera")
print("Press SW1 to scan.")
while True:
if sw1Press == True:
_, img =
data, bbox, _ = detector.detectAndDecode(img)
if bbox is not None:
for i in range(len(bbox)):
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
0, 0), thickness=2)
cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
if data:
sw1Press = False
data = data.split(",")
print("ID: " + data[0])
print("Name: " + data[1])
print("Class: " + data[2])
userScanned = False
with open('Database.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row["ID"] == data[0]:
buzzer.beep(0.1, 0.1, 1)
userScanned = True
if userScanned == False:
buzzer.beep(0.1, 0.1, 2)
with open('Database.csv', 'a') as csvfile:
fieldNames = ['ID', 'NAME', 'CLASS']
writer = csv.DictWriter(csvfile, fieldnames=fieldNames)
writer.writerow({'ID': data[0], 'NAME': data[1], 'CLASS': data[2]})
cv2.imshow("code detector", img)
if cv2.waitKey(1) == ord("q"):

view raw
hosted with ❤ by GitHub

Thank You


Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

1 thought on “Record QR Data Into CSV Format Using Raspberry Pi”

  1. Hi,

    I would like some help with this code. This is exactly what i need but i need a relay to be triggered and turn on for 10 seconds after the qr code is scanned and then turn off the relay.

    I’m a extreme newbie at coding and have been following your videos for a long time and have been purchasing alot from your store. I really need help with this particular code. Thanks alot

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

DIY Digital Alarm Clock Using REKA:BIT With Micro:bit
Display Internet Time (NTP) on micro:bit
DIY Interactive Robot Using REKA:BIT With Micro:bit
BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
Tutorials of Cytron Technologies Scroll to Top