Arduino Barcode Scanner: How to Connect an OEM Barcode Scanner Module with Arduino MCU

If you are developing an embedded system using an Arduino microcontroller and need to integrate a high-performance 1D/2D OEM barcode scanner module, this comprehensive guide is for you.

Integrating an OEM embedded barcode scanner engine with an Arduino board typically comes with two primary challenges:

  1. Physical Interface Mismatch: Most OEM barcode scanner modules use a fine-pitch 12-pin TTL FFC (Flexible Flat Cable) interface, whereas Arduino boards feature standard 2.54mm pin headers. How do you securely connect them?

  2. Software Control & Data Parsing: Once connected, how can Arduino trigger the scanner, control its behavior via serial commands, and successfully parse the decoded barcode data?

As a professional manufacturer of barcode scanning technology, RTscan provides complete hardware and software solutions to solve these challenges, helping you accelerate your prototyping and product development.

Part I: Introduction of Arduino UNO and RTscan Arduino USB & TTL Host Shield:

Arduino UNO:

The Arduino UNO is the most widely used and well-documented board in the microcontroller family, making it the perfect choice for project development. Key pins required for barcode scanner integration include:

5.0V Power Supply & GND: To power the scanner engine.

RXD & TXD Pins (Digital 0/1 or SoftwareSerial): For serial data communication and control commands.

 

RTscan Arduino USB & TTL Host Shield:

TTL-232 interface:

Thanks to the built-in TTL-232 interface, a beeper, and a trigger button, this Arduino host shield makes it achievable for our customers to use a small barcode scan engine directly with Arduino: 
Connect the OEM barcode scan engine directly to this host shield via the TTL-232 interface; there is no need to use an extra USB Kit board anymore. This not only saves cost but also saves space for our customers.
You can use this solution with any of RTscan’s OEM barcode scanner engines.

USB Port:

This Arduino host shield also has a built-in standard USB-A connector for direct connections of any RTscan scanners with a native USB interface. So you can also use any RTscan’s USB barcode scanners with this host shield.

 

Part II: Hardware solutions introduction:

RTscan Arduino host shield provides two types of hardware connection options:

Option 1: TTL-232 Interface

If you prefer to use a small OEM barcode scanner module with an Arduino board, then you can combine them simply as image below: 

♠ All RTscan’s OEM Scanner Modules that are compliant with this solution:

Option 2: Connect via USB

The above-mentioned TTL‑232 interface uses an FPC flat cable, but a usual flat cable can’t be longer than 30 cm to ensure proper communication. So if a 30‑cm cable is not long enough for your application, you can use RTscan’s USB kit board and connect the scanner to the Arduino host via the USB interface. 

♠  All RTscan’s “OEM scanner modules + USB kit board” are compliant with this solution. 

Part III: Software Integration & Arduino C++ Sample Codes

To save you time and speed up your time-to-market, RTscan has developed fully tested Arduino sample codes. You can easily copy and paste these codes into your Arduino IDE without writing driver logic from scratch.

In this article, we choose RT214 as an example to do the programming.

1. TTL-232 Communication Setup & Code

  • Step 1: Connect the RT214 + EVK board to the Arduino Uno as shown in Solution 1.

  • Step 2: Configure the RT214 to TTL-232 mode (9600 baud, 8-N-1) by scanning the setup barcodes below:

Enter setup

Set to TTL-232 mode

For more details, please refer to “RT214_User_Guide”

Step 3: Upload the following optimized sample code (rt214_uart_demo.ino) to your Arduino:

/* * RTscan RT214 Arduino UART TTL Demo Code
* Comm Protocol: ~0000[StorageType][Tag][SubTag][Data];
* Default Baud Rate: 9600, 8-N-1
*/

#include <SoftwareSerial.h>

// Define Arduino pins for SoftwareSerial (Rx, Tx)
SoftwareSerial scannerSerial(10, 11);

void setup() {
Serial.begin(115200); // Hardware Serial to PC for Debugging
scannerSerial.begin(9600); // Software Serial to RTscan Module
Serial.println(“RTscan Barcode Scanner Initialized. Ready to test.”);
}

void loop() {
// If barcode data is received from the scanner, forward it to the PC Serial Monitor
if (scannerSerial.available() > 0) {
String barcodeData = scannerSerial.readStringUntil(‘\r’);
Serial.print(“Scanned Code: “);
Serial.println(barcodeData);
}
}

// Function to send serial control commands to the scanner module
void sendCommand(const char* storageAndTag) {
const char* prefix = “\x7E\x01\x30\x30\x30\x30”; // “~0000”
const char* suffix = “\x3B\x03”; // “;”

scannerSerial.print(prefix);
scannerSerial.print(storageAndTag);
scannerSerial.print(suffix);

Serial.print(“Command Sent: ~0000”);
Serial.print(storageAndTag);
Serial.println(“;”);
}

// Quick command utilities
void startScanning() {
sendCommand(“#SCNTRG1”); // Trigger Software Scan
}

void stopScanning() {
sendCommand(“#SCNTRG0”); // Stop Scanning
}

Please contact us to get full sample codes: sales@rtscan.net

Common Serial Commands for RT214 (HEX):

  • Trigger Mode: 1B 31

  • Stop Scanning: 1B 30

  • Auto-Sensing Mode: 1B 32

  • Continuous Mode: 1B 33

For more details, please refer to 《RTscan Serial Programming Command Manual.PDF》

2. USB CDC (Virtual COM) Communication

Here, we still take RT214 as an example to show how to  make a USB barcode scanner to work with Arduino via USB-CDC interface:

Step 1: Connect the scanner to the Arduino USB Host Shield.

 

Step 2: Scan the configuration barcode to set the scanner to USB-CDC mode.

Enter setup

Set to USB-CDC mode:

Recognized as a COM port device on the computer:

Step 3: Install the standard USB_Host_Shield_2.0 library via the Arduino Library Manager.

Step 4: Run the acm_terminal.ino example snippet provided by RTscan to capture barcodes instantly at 115200 baud rate.

Please contact us to get full sample codes: sales@rtscan.net

3. USB-HID (Keyboard Simulation) Communication

In USB-HID mode, the scanner mimics a hardware keyboard, converting decoded barcode data directly into ASCII keyboard inputs.

Step 1: Hardware connection

Connect the RT214 scanner to the USB port of the Arduino USB host shield through a USB data cable.

 

Step 2: RT214 settings

RT214 set to USB CDC mode, scan the following Setting bar code

Enter setup:

Set to USB-HID mode:

Recognized as HID keyboad Device on the computer:

 

Step 3: Include the library

Arduino Library Manager

First, install Arduino IDE version 1.6.2 or newer, then simply use the Arduino Library Manager to install the library.

Please see the following page for instructions:

http://www.arduino.cc/en/Guide/Libraries#toc3

Manual installation

First download the library by clicking on the following link:

https://github.com/felis/USB_Host_Shield_2.0

Then uncompress the zip folder and rename the directory to “USB_Host_Shield_20”, as any special characters are not supported by the Arduino IDE.

Now open up the Arduino IDE and open “File>Preferences”. There you will see the location of your sketchbook. Open that directory and create a directory called “libraries” inside that directory. Now move the “USB_Host_Shield_20” directory to the “libraries” directory.

The final structure should look like this:

  • Arduino/
    • libraries/
      • USB_Host_Shield_20/

Now quit the Arduino IDE and reopen it.

Now you should be able to open all the examples codes by navigating to “File>Examples>USB_Host_Shield_20” and then selecting the example you would like to open.

For more information please visit the following sites:

http://arduino.cc/en/Guide/Libraries

https://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use.

 

Step 4: Run the sample code

Run: Scanner_USBHIDUniversalKbd. ino

[cpp]
#include
#include
#include

#include

class KbdRptParser : public KeyboardReportParser
{
public:
void PrintKey(uint8_t mod, uint8_t key);

bool isScanEnd();
void PrintScanCode();

protected:
void OnControlKeysChanged(uint8_t before, uint8_t after);

void OnKeyDown (uint8_t mod, uint8_t key);
void OnKeyUp (uint8_t mod, uint8_t key);
void OnKeyPressed(uint8_t key);

String ScanCode = “”;
uint32_t FreeTime = 0;
bool StartScan = false;
};

void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
MODIFIERKEYS mod;
*((uint8_t*)&mod) = m;
Serial.print((mod.bmLeftCtrl == 1) ? “C” : ” “);
Serial.print((mod.bmLeftShift == 1) ? “S” : ” “);
Serial.print((mod.bmLeftAlt == 1) ? “A” : ” “);
Serial.print((mod.bmLeftGUI == 1) ? “G” : ” “);

Serial.print(” >”);
PrintHex(key, 0x80);
Serial.print(“< “); Serial.print((mod.bmRightCtrl == 1) ? “C” : ” “); Serial.print((mod.bmRightShift == 1) ? “S” : ” “); Serial.print((mod.bmRightAlt == 1) ? “A” : ” “); Serial.println((mod.bmRightGUI == 1) ? “G” : ” “); }; void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) { MODIFIERKEYS beforeMod; *((uint8_t*)&beforeMod) = before; MODIFIERKEYS afterMod; *((uint8_t*)&afterMod) = after; if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) { Serial.println(“LeftCtrl changed”); } if (beforeMod.bmLeftShift != afterMod.bmLeftShift) { Serial.println(“LeftShift changed”); } if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) { Serial.println(“LeftAlt changed”); } if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) { Serial.println(“LeftGUI changed”); } if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) { Serial.println(“RightCtrl changed”); } if (beforeMod.bmRightShift != afterMod.bmRightShift) { Serial.println(“RightShift changed”); } if (beforeMod.bmRightAlt != afterMod.bmRightAlt) { Serial.println(“RightAlt changed”); } if (beforeMod.bmRightGUI != afterMod.bmRightGUI) { Serial.println(“RightGUI changed”); } } void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) { // Serial.print(“DN “); // PrintKey(mod, key); uint8_t c = OemToAscii(mod, key); if (c) OnKeyPressed(c); } void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) { // Serial.print(“UP “); // PrintKey(mod, key); FreeTime = millis(); StartScan = true; } void KbdRptParser::OnKeyPressed(uint8_t key) { // Serial.print(“ASCII: “); // Serial.println((char)key); ScanCode = ScanCode + String((char)key); }; bool KbdRptParser::isScanEnd() { if(millis() – FreeTime > 200 && StartScan){
StartScan = false;
return true;
}

return false;
};

void KbdRptParser::PrintScanCode()
{
Serial.print(“ScanCode: “);
Serial.println(ScanCode);
ScanCode = “”;
};

USB Usb;
HIDUniversal Hid(&Usb);
KbdRptParser Prs;

void setup()
{
Serial.begin( 115200 );
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect – used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
Serial.println(“Start”);
Serial.println(“Please set the scanner to USB HID mode”);

if (Usb.Init() == -1)
Serial.println(“OSC did not start.”);

delay( 200 );

Hid.SetReportParser(0, &Prs);
}

void loop()
{
[/cpp]

Please contact us to get full sample codes: sales@rtscan.net

Need a Professional Barcode Solution for Your Embedded Project?

RTscan provides a comprehensive range of OEM and embedded barcode scanners compatible with both Arduino and Raspberry Pi systems.

Related Guides & Deep Dives:

Request an Evaluation Sample Today!

Accelerate your development cycle with our plug-and-play evaluation kits. Contact our application engineers today for hardware selection assistance, full source code repositories, and direct technical support.

Ask A Sample To Test Now!

    You can also contact us directly. For sales: ; for technical support: . Thank you!