When the clerk scans our groceries, or when we visit the self-scan kiosk, barcodes have been part of our daily routine for decades. Developed as a machine readable form of input, barcodes are loosely based upon Morse Code, using alternating thick and thin bars of black and white to represent values. We take them for granted in the 21st century, but the humble barcode revolutionized 20th century commerce.
SB Components latest HAT, the $50 RPI Barcode Reader HAT is exactly that, a Raspberry Pi HAT with a built-in DE2120 barcode reader. The HAT features a 1.14 inch TFT display, and a GPIO passthrough for HATs and breadboard projects.
Armed with a stack of barcodes to test, including some retro PlayStation 1 games we tested RPI Barcode Reader HAT on our Raspberry Pi 4, and on our Windows 10 PC.
Barcode Reader HAT Specifications
|Barcode Scanner||DE2120 2D Scan Engine [pdf]|
|Display||1.14 inch TFT|
|Communications||40 pin GPIO and Micro USB|
|UPC-A, UPC-E, EAN-8, EAN-13, Code 128, GS1-128, Code 39, Code 93, Code 11, Interleaved 2 of 5, Matrix 2 of 5, Industrial 2 of 5, Codabar, MSI, GS1 DataBar, Datalogic 2 of 5|
|QR Code, Data matrix, PDF 417, Micro PDF 417, Aztec Code|
|Dimensions||65 x 56mm|
Using the Barcode Reader HAT
As this is a HAT, it fits directly on top of all 40 GPIO pins and is powered via the GPIO. The GPIO is broken out on top of the HAT and that means we could stack another board on top, something that we will test later.
We tested the Barcode Reader HAT with an early version of the Python library and provided SB Components with feedback on the issues that we faced. Suffice to say the Python library is functional and easy to use, but a number of typos did slow down our progress. With the typos identified, overcome and reported we finally got to use the board and it works rather well.
The base of the Python library is all about interacting with the DE2120 scanner module which can spit out data via a serial / UART connection. The scanner can be reprogrammed to act as a keyboard, outputting data as if it were a keyboard via the micro USB port which means it can be used with another computer.
We did test the Barcode Scanner HAT away from the Raspberry Pi. Using a special barcode to configure the reader as a USB keyboard, we connected the HAT to our Windows 10 PC and started scanning random barcodes, all of which appeared in our text editor window.
We chose to use the scanner as a serial device and after working through the example applications we got a grasp of how it worked. Data is output as bytes, essentially a string of text starting with b followed by the barcode data contained in ‘’, the final character /r is a carriage return.
After grabbing a bunch of PlayStation 1 games from our attic, we set to creating a demo to learn more about the Barcode Reader HAT. We used the barcode from two games as our test. A conditional test would check the data read by the barcode scanner and compare it against the two known barcodes. If there was a match, the name of the game is printed to the Python Shell. We added an extra few lines to show a screenshot of the game on the 280 x 135 resolution screen. We had to manually edit the image to match the screen resolution exactly, if there is even a pixel difference, the code will error. We created three images, two for the games and another using our logo. Our logo is displayed while the scanner waits for input. We ran the code and lined up our first game, pressed the scan button and our barcode was read, printing the name of the game to the shell, and showing the screenshot on the screen. After a moment the screenshot is replaced with the Tom’s Hardware logo, waiting for another barcode to be scanned.
We branched out our test and used an HC-SR04+ ultrasonic sensor, connected to the GPIO via the breakout. The purpose of the HC-SR04+ was to trigger the barcode scanner without touching the unit. The Python library features an ultrasonic module which abstracts the code, making it easy to use the sensor. We successfully integrated the sensor into the project and could handsfree scan our games and see the screenshots on the screen.
Buoyed by our GPIO success we connected our favortie Raspberry Pi HAT, Pimoroni’s Explorer HAT Pro to the GPIO header, and we can confirm that the Explorer HAT is accessible. There is a but though, the tall header pins are just not tall enough to provide clearance over the barcode scanner. We added another GPIO header, providing us with a few more milimeters of clearance. We ran through Pimoroni’s test.py file and all of the functions were available. We then mixed a little Explorer HAT code into our games scanner test code so that a certain color of LED lit and a motor would spin as we scanned a game. With only four lines of code this worked as we expected, and this means that we can add the Barcode Reader HAT to our projects, using it as a novel means of input and control.
Barcodes come in all manner of formats. We are mostly used to barcodes on groceries, books and magazines. But over the past decade, QR codes (Quick Response) codes have been used to encode much more data into an easily digested format.
In a recent episode of Tom’s Hardware: The Pi Cast, a viewer asked if the Barcode Reader HAT could be used with QR codes, so we created our own QR code which contained the Tom’s Hardware URL and we can confirm that SB Components Barcode Reader HAT can be used with QR codes, either as a Raspberry Pi HAT, or via the keyboard emulation.
Barcode Reader HAT Project Ideas
Barcodes are not just for your groceries. There are many ways to generate your own barcodes, so the Barcode Reader HAT can be used to control projects with the wave of a card. At conferences or in the workplace, Barcode Reader HAT could be used to scan QR code business cards and save them to a database. In the classroom, learners could use QR codes to register attendance or control interactive displays.
This is a fun little HAT, if a little niche. For just over $50 this isn’t a HAT for everyone, but the form factor and ease of use will benefit those wishing to add barcode input to their Pi project. Barcode Reader HAT really comes into its own when used with other HATs.