Building a Barcode Scanner System with Raspberry Pi

Learn how to create a complete barcode scanner system using Raspberry Pi. This beginner-friendly guide covers hardware setup, software installation, Python code for scanning, and potential applications in inventory and automation systems.

1. Introduction to Raspberry Pi Barcode Scanner System

A barcode scanner system enables devices to read printed barcodes and interpret them digitally. With the help of a Raspberry Pi and a USB barcode scanner, you can build a low-cost and efficient barcode scanning system used in inventory management, library systems, and automation.

2. Components Required

Hardware Components

  • Raspberry Pi 3/4 (with Raspbian OS installed)
  • USB Barcode Scanner (Plug-and-Play type)
  • Monitor, Keyboard, and Mouse
  • Internet Connection (for installing dependencies)

Software Components

  • Python 3
  • Libraries: `pyzbar`, `opencv-python`, `Pillow`
  • Optional GUI: Tkinter (for simple interface)

3. Understanding How It Works

The USB barcode scanner works like a keyboard input device. When a barcode is scanned, it sends the code as a string to the system. For camera-based scanners, the image is captured and decoded using libraries like OpenCV and PyZBar.

Modes of Scanning

  • Using a USB barcode scanner (keyboard emulator)
  • Using a Pi Camera or USB webcam with image decoding

4. Installing Required Python Libraries

5. Barcode Scanning with Camera and Python

  1. Connect a USB camera or Pi Camera.
  2. Capture frames continuously and scan for barcodes using PyZBar.
  3. Display the decoded barcode content.
import cv2from pyzbar.pyzbar import decodefrom PIL import Imagedef scan_barcodes():    cap = cv2.VideoCapture(0)    while True:        ret, frame = cap.read()        for barcode in decode(frame):            data = barcode.data.decode('utf-8')            print("Barcode: ", data)            pts = barcode.polygon            pts = [(pt.x, pt.y) for pt in pts]            cv2.polylines(frame, [np.array(pts)], True, (0,255,0), 2)        cv2.imshow('Barcode Scanner', frame)        if cv2.waitKey(1) & 0xFF == ord('q'):            break    cap.release()    cv2.destroyAllWindows()scan_barcodes()

6. Using a USB Barcode Scanner as Input

If you're using a USB barcode scanner that emulates keyboard input, you can capture the barcode using Python's input() or by monitoring key events.

print("Scan a barcode:")barcode = input("Barcode: ")print("You scanned:", barcode)

7. Applications of Barcode Scanner with Raspberry Pi

  • Inventory tracking system in small shops or warehouses
  • Library management (issue/return books)
  • Event check-in systems
  • Automated product verification in smart shelves
  • Retail POS systems

8. Troubleshooting Tips

  • Ensure the USB scanner is detected: `lsusb` command
  • Use `raspi-config` to enable the camera module
  • Install `libzbar0` if PyZBar throws decoding errors: `sudo apt install libzbar0`
  • Make sure OpenCV is correctly installed and camera index is valid

9. Extending the Project

  • Add a database (SQLite or Firebase) to store scanned barcodes
  • Integrate with Flask/Django for web-based inventory system
  • Create a GUI interface using Tkinter or PyQt
  • Trigger GPIO output based on barcode match (e.g., unlock a door)

10. Conclusion

  • You successfully created a basic barcode scanner using Raspberry Pi
  • Learned how to use both USB scanners and camera-based decoding
  • Understood Python-based image processing and user input handling
  • Discovered real-world applications and future extensions

11. References & Resources