Reading Input from a Barcode Scanner using Python, a Raspberry Pi and Serial Port

Lev Ilmer
3 min readAug 1, 2023

Interacting with hardware such as a barcode scanner has become increasingly straightforward due to the evolution of Python and it’s many open-source libraries. In this article we will in particular focus on how to use Python, along with the PySerial library, to interact with a barcode scanner connected to a Raspberry Pi.

1. Prerequisites

  1. A Barcode Scanner: This could be any standard scanner, I’m using a mini scanner bought off Aliexpress
  2. A Raspberry Pi: You can use any computer, I am using a Raspberry Pi
  3. Python: Anything newer than Python 3.6, I am using Python 3.10
  4. PySerial: This is the library we will use to communicate with the reader
YHD-M300C

2. Setting Up the Barcode Scanner

For this tutorial we will be using the scanner in serial mode. Most scanners typically come with a manual full of barcodes that when scanned change the device settings, though it’s possible yours will default to serial mode.

--

--