QR Code scanner for Arduino: Use 2d and QR barcode scanner with Arduino microcontroller (MCU)

If you are using Arduino MCU (microcontroller) to develop your system, and need a 1D/ 2D QR barcode scanner to work with it, then this article would be helpful for you. Here we introduce how we connect the Arduino Uno micro-controller with RTscan QR barcode scanners, and how to make them work with each other.

When we try to integrate a QR barcode scanner with the Arduino microcontroller board, we would meet these problems:

  1. The QR barcode scanner normally goes with a USB cable and RS232 cable, but Arduino board without this type connector, then how can we connect them together?
  2. Whatever QR barcode scanners we choose, how can we control the barcode scanner by Arduino and also upload decoded data to the Ardunio system?

Read below and find how RTscan provides solutions for above questions.

Contents

Part I: Introduction of Arduino UNO and RTscan QR barcode scanner

Part II: Connection solutions

  • Solution 1: Connect via Dupont interface
  • Solution 2: Connect via USB interface

Part III: Sample code/ Control the QR barcode scanner via Arduino system

  • TTL-232 Communication
  • USB CDC Communication
  • USB-HID Communication

Part VI: Video Demo


Part I:  Introduction of Arduino UNO and RTscan QR barcode scanner

Arduino UNO: The UNO is the most used and documented board of the whole Arduino microcontrollers family, many people choose this model for their projects. Arduino Uno has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller.

And for our application to connect with the QR barcode scanner, we need the 3.3V (or the 5.0v) power supply pin, and GND pin, RXD, TXD pins.

Arduino UNO:

blank
Arduino UNO Pinout

RTscan QR barcode scanners:

blank

In this article, we choose the RT870 as an example to do the connection and integration.

RT870 has RJ45 and micro USB output.

blank

Part II: Connection solutions:

Solution 1: Connect the QR barcode scanner via Dupont interface 

Connect RT870 to Arduino UNO serial port via Dupont cable.

In addition to the regular RJ45-USB and RJ45-RS232 cables, RTscan also provides the following DuPont cables to connect with Arduino:

blank
blank
blank

This is a simple solution, just connect the 5V power supply, GND, RX, and TX pins.

RT870 RJ45 Pinout

blank

PIN Connection:

RT870Arduino UNO
Pin4 VDD5V
Pin8 GNDGND
Pin6 RXDPin5
Pin7 TXDPin4

In this solution, the RT870 scanner needs to be set to TTL-232 Communication (follow Part III/TTL-232 communication).

Solution 2: Connect the 2D QR barcode scanner via USB interface

If your barcode scanner only has a USB interface instead of a Dupont male cable, and you want to connect it to the Arduino, it’s also fine, you can just use an Arduino USB host shield.

The Arduino USB Host Shield allows you to connect a USB device to your Arduino board, setup RTscan’s barcode scanner to HID mode, or USB CDC/Virtual Com mode (follow Part III/USB CDC Communication/USB-HIDCommunication).

blank
blank
blank

Part III: Sample code/ Control the QR barcode scanner via Arduino system

blank
blank

We developed sample code to work with our scanners so that you can copy our source codes and program your system very quickly and no need to write the whole code one by one; save your time and speed up your integration work!

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

 TTL-232 Communication

Step 1: Hardware connection

Connect RT870 to Arduino UNO serial port via Dupont cable.

RT870Arduino UNO
PIN4 VDD5V
PIN8 GNDGND
PIN6 RXDPIN5
PIN7 TXDPIN4
blank

Step 2: RT870 QR barcode scanner settings

RT870 needs to be set up with RS232 mode, scan the following barcode
Set to RS232 Mode, the default baud rate is 115200, 8-n-1

blank

Set the baud rate to 9600:

blank

For more details, please refer to “User-Manual-RT830A-RT870-RT860_V1.2.3”

Step 3: Run the sample code

Upload the below sample code rt870_test_demo.ino to Arduino UNO.


/*
RT870 Test demo
by RTSCAN 20200903
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
 
void printHex(char *p, int len)
{
  char tmp[5];
  for (int i = 0; i < len; i++)
  {
    sprintf(tmp, “%02x “, p[i]);
    Serial.print(tmp);
  }
  Serial.println();
}
 
bool check_response(unsigned long timeout, String response)
{
  char c;
  String buffer;
  bool found = false;
  unsigned long t = millis();
 
  while (millis() – t < timeout)
  {
    if (mySerial.available() > 0)
    {
      c = mySerial.read();
      buffer += c;
      if (buffer.indexOf(response) != -1 || buffer.equals(response))
      {
        found = true;
        break;
      }
    }
  }
 
  while (mySerial.available() > 0)
  {
    c = mySerial.read();
    buffer += c;
  }
 
  Serial.println(buffer);
  printHex(buffer.c_str(), buffer.length());
 
  return found;
}
 
void send_cmd(char *p)
{
  mySerial.print(p);
  check_response(100, p + 3);
}
 
void factory_defaults()
{
  



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

And the RT870 automatically detects and reads bar code.

Output:

blank

Introduction of the serial port commands protocol for RT870

blank

Commad example: if we want to set the unit default, the command data as below

blank

USB CDC Communication

In this solution, the scanner is set to USB Com port emulation mode, and the Arduino recognizes the scanner via the USB host shield.

Step 1: Hardware connection

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

blank

Step 2: RT870 settings

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

Firstly, Set to USB cable

blank

Then, Set to USB-CDC mode

blank

*Recognized as  blankon 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/archive/master.zip.

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 go open all the examples codes by navigating to “File>Examples>USB_Host_Shield_20” and then select the example you will 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: acm_terminal. Ino



#include <cdcacm.h>
#include <usbhub.h>
 
#include “pgmstrings.h”
 
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
 
class ACMAsyncOper : public CDCAsyncOper
{
public:
    uint8_t OnInit(ACM *pacm);
};
 
uint8_t ACMAsyncOper::OnInit(ACM *pacm)
{
    uint8_t rcode;
    // Set DTR = 1 RTS=1
    rcode = pacm->SetControlLineState(3);
 
    if (rcode)
    {
        ErrorMessage<uint8_t>(PSTR(“SetControlLineState”), rcode);
        return rcode;
    }
 
    LINE_CODING lc;
    lc.dwDTERate = 115200;
    lc.bCharFormat = 0;
    lc.bParityType = 0;
    lc.bDataBits = 8;
 
    rcode = pacm->SetLineCoding(&lc);
 
    if (rcode)
        ErrorMessage<uint8_t>(PSTR(“SetLineCoding”), rcode);
 
    return rcode;
}
 
USB     Usb;
//USBHub     Hub(&Usb);
ACMAsyncOper  AsyncOper;
ACM           Acm(&Usb, &AsyncOper);
 
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 CDC mode”);
 
  if (Usb.Init() == -1)
      Serial.println(“OSCOKIRQ failed to assert”);
 
  delay( 200 );
}


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

Open the serial port for debugging

blank

Scan any barcode and it will be shown as below:

blank

 

USB-HIDCommunication

In this solution, the scanner is set to USB HID KBW mode, and the Arduino recognizes the scanner via the USB host shield.

Step 1: Hardware connection

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

blank

Step 2: RT870 settings

RT870 set to USB-HID mode, scan the following Setting bar code

Set to USB output

blank

Set to USB-HID mode

blank

*Recognized as  blankon 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/archive/master.zip.

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 go open all the examples codes by navigating to “File>Examples>USB_Host_Shield_20” and then select the example you will 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



#include <hidboot.h>
#include <usbhid.h>
#include <hiduniversal.h>
 
#include <SPI.h>
 
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” : ” “);
  …

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

Open the serial port for debugging

blank

Scan any barcode and it will be shown as below:

blank

Part VI: Video Demo

Please refer to the following video for specific operations:

If you need an OEM barcode scanner module for Arduino, please read: 

Arduino Barcode Scanner

If you need an OEM barcode scanner for Raspberry Pi, please read:

Raspberry Pi Barcode Scanner

1620 View
blank

Leave a Comment