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 of 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 the above questions.

Contents

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

Part II: Connection solutions

  • Solution 1: Connect via UART/ 12PIN TTL-232
  • 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:

Arduino UNO Pinout

RTscan QR barcode scanners:

 

In this article, we choose RT870 and RT830C as examples to introduce our solutions.

 

 

Part II: Connection solutions:

Solution 1: Connect the QR barcode scanner via a 12pins flat cable (TTL-232 signal) interface ( for RT830C): 

RTscan developed a multi-interface Shield, connect the shield with Arduino UNO, then connect the RT830C via a 12pins flat cable:

 
 

Solution 2: Connect the QR barcode scanner via a 4 pins UART (RS232 signal) interface ( for RT830C): 

Use the multi-interface Shield, connect the shield with Arduino UNO, then connect the RT830C via a 4pins cable. The RT830C comes out RS232 signal to the shield, and the shield converts it to TTL-232 signal and transmits to Arduino board.

 

Note: to learn more about RT830C, please view:   https://www.rtscan.net/Code-Readers/fixed-mount-embedded-kiosk-barcode-scanner-rt830c/

 

Solution 3: Connect the 2D QR barcode scanner via USB interface ( for RT870)

The RT830C without its own glass window, if you prefer an embedded barcode scanner with its own glass window, then you can choose our RT870 and use this solution: use an Arduino USB host shield as shown below.

 

 

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

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!

1. TTL-232 Communication (compatible with above solution 1 and solution 2)

Step 1: Hardware connection

Follow above solution 1 or solution 2 to connect the RT830C with Arduino Uno.

 

Step 2: RT830C barcode scanner settings

RT830C needs to be set up with 232 output, scan the following barcode
Set to 232 Mode, the default baud rate is 115200, 8-n-1

Scan the below code to set the baud rate to 9600:

For more details, please refer to the full user manual.

 

Step 3: Run the sample code

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

 
/*
RT830c 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  lt; 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  lt; timeout)
  {
    if (mySerial.available()  gt; 0)
    {
      c = mySerial.read();
      buffer += c;
      if (buffer.indexOf(response) != -1 || buffer.equals(response))
      {
        found = true;
        break;
      }
    }
  }
 
  while (mySerial.available()  gt; 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: sales@rtscan.net
 

 

 

And the RT830c automatically detects and reads bar codes.

 


Introduction of the serial port commands protocol for RT830c

 

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

 

2.USB CDC Communication (compatible with above solution 3 )

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.

 

Step 2: RT870 settings

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

Firstly, Set to USB cable

Then, Set to USB-CDC mode

*Recognized as USB com port 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 select the example.

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"<br />#endif
#include  "SPI.h"<br /> 
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- gt;SetControlLineState(3);
 
    if (rcode)
    {
        ErrorMessage lt;uint8_t gt;(PSTR("SetControlLineState"), rcode);
        return rcode;
    }
 
    LINE_CODING lc;
    lc.dwDTERate = 115200;
    lc.bCharFormat = 0;
    lc.bParityType = 0;
    lc.bDataBits = 8;
 
    rcode = pacm- gt;SetLineCoding( lc);
 
    if (rcode)
        ErrorMessage lt;uint8_t gt;(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: sales@rtscan.net

 

Open the serial port for debugging

Scan any barcode and it will be shown as below:

 

3.USB-HIDCommunication

In this solution, the scanner is set to USB HID KBW mode. 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.

Step 2: RT870 settings

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

Set to the USB output

Set to USB-HID mode

*Recognized as HID Keyboard 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 select the example.

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  lt;hidboot.h gt;
#include  lt;usbhid.h gt;
#include  lt;hiduniversal.h gt;
 
#include  lt;SPI.h gt;
 
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: sales@rtscan.net

 

Open the serial port for debugging

Scan any barcode and it will be shown as below:

 

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

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

Embedded barcode scanner for Raspberry Pi

Read More


If you are using Raspberry Pi microcontroller / single-board computer to develop your system, and at the same time, you need a 1d/ 2d embedded barcode scanner to work with it, then this article would be helpful for you:

Here we introduce how we connect the Raspberry Pi 3 Model B+ micro-controller with RTscan embedded barcode scanner and how to make them work with each other.

When we try to integrate an embedded barcode scanner with the Raspberry Pi microcontroller board, but we would meet these problems:

  1. How can we connect the embedded barcode scanner with the Raspberry Pi board together?
  2. When we choose an embedded barcode scanner, how can we control the barcode scanner by Raspberry Pi, meanwhile, how to upload decoded data to the Raspberry Pi system?

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

Contents

 

Part I: Introduction of Raspberry Pi 3 model B+ and RTscan embedded barcode scanner

Part II: Connection solutions

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

Part III: Python Sample code/ Make the embedded barcode scanner work with Raspberry Pi system:

  • TTL-232 Communication& Python Sample code
  • USB CDC Communication
  • USB-HID

Part VI: Video Demo

 

Part I: Introduction of Raspberry Pi 3 model B+ and RTscan embedded barcode scanners

Raspberry Pi 3B+:

Raspberry Pi 3 Model B is the latest iteration of the most popular single-board computer. It provides a quad-core 64-bit ARM Cortex-A53 CPU running at 1.2GHz, four USB 2.0 ports, wired and wireless networking, HDMI and composite video output, and a 40-pin GPIO connector for physical interfacing projects.

 

Raspberry Pi 3 Pinout

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

And also, we will demo how to connect our embedded barcode scanners: with raspberry Pi via USB interface.

RTscan embedded barcode scanners:

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

RT870 has RJ45 and micro USB output.

Part II: Connection solutions:

RTscan provides two types of solutions:

  • Solution1: Dupont interface 

RTscan provides the following DuPont interface to connect the embedded barcode scanner with Raspberry Pi:

This is a simple solution, just connect the 5.0V power supply (the EVK needs 5.0v power supply), GND, RX, and TX pins.

 

RT870 RJ45 Pinout

 

PIN Connection:

RT870 Raspberry Pi 3B+
Pin4 VDD 5V(Pin2 / Pin4)
Pin8 GND GND(Pin6)
Pin6 RX Tx(Pin8)
Pin7 TX Rx(Pin10)

Solution 2: Connect via USB interface

For Raspberry Pi, if you prefer to connect the embedded barcode scanner via USB interface, it also is fine, setup RTscan’s barcode scanner to HID mode (work directly) or USB CDC/Virtual Com mode (follow Part III/USB CDC communication).

 

 

Part III: Python Sample code/ Make the embedded barcode scanner work with Raspberry Pi system:

 

We developed Python 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. Below we show how they work together when they connect via TTL-232, USB-CDC, and USB-HID.

 

 

TTL-232 Communication& Python Sample Code

 

Step 1: Hardware connection

Connect the 5.0v power supply pin, and GND pin, RXD, TXD pins via Dupont cable.

Raspberry RT870
TX(GPIO14)(PIN8) RX
RX(GPIO15)(PIN10) TX
5V POWER
GND GROUND

 

Step 2: RT870 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

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

 

 

Step 3: Raspberry Pi Settings

Use serial port 0 in Raspberry Pi,

/dev/ttyS0

More settings would be required to enable Raspberry Pi serial0, please see below (If you already opened it, then ignore this):

1.Enable serial port ttyS0

Open the serial port/dev/ttyS0 
Via command window, input:
sudo rasp-config

2.Open the system configuration interface as shown below, select Interfacing Options

Then select serial:

Select No:

Choose Yes:

Finally, save and exit.

Now, please enter the following command, check the serial port ttyS0:
ls -la /dev/
But, if it still cannot be used, please double-check and re-configure it.

3. Close the Console

In the terminal, input:

sudo systemctl stop serial-getty@ttyS0.service
sudo systemctl disable serial-getty@ttyS0.service

Then, restart

And input:

sudo systemctl mask serial-getty@ttyS0.service

Now it works!

4. Modify Raspberry Pi CPU frequency (optional, you would no need to do this)

After the above steps 1 and 2, the settings are theoretically completed. But some times:

(1)After sending a string via serial port, in most cases the receiver shows up garbled data.

(2) When the serial port receives data, if it appear data like: **\xe8 \xe9**

This would be caused by the wrong working frequency of the Raspberry Pi, information link:
Raspberry Pi 3 login via UART on GPIO Baud Rates broken

The modification method is described as below:

sudo vim /boot/config.txt

Find out whether the sentence core_freq=250 is enabled. If not, remove the # number in front of this sentence, or add core_freq=250 at the boot/config.txt, done.

 

 

Step 4: Run the Python sample code

Run: RT870_TTL_RS232.py

#RT870 python test demo
#ttl-232 readme
#by RTSCAN 20200901
 
 
import sys
import serial
import time
 
#default usb device /dev/ttyS0 115200 8n1
ser = serial.Serial("/dev/ttyS0",115200,timeout=0.5)
 
print('serial test start ...')
if ser != None:
    print('serial ready...')
else:
    print('serial not ready')
    sys.exit()
 
...

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

 

And the RT870 automatically detects and reads bar code.

Output:

 

Introduction of commands protocol for RT870

Example:

Response: 30 44 30 31 30 30 06 2e

For more details, please refer to 《Programming_Commands_Guide(RT203-RT830).pdf》

 

 

USB CDC Communication

If we want to use the USB port and make it work as a virtual serial port, set up the RT870 to USB Com port emulation mode. And the RT870 scanner will be detected as a COM device in Raspberry Pi.

 

Step 1: Hardware connection

Connect RT870 to the USB port of Raspberry Pi through a data cable

Step 2: RT870 Settings

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

Set to USB output

Set to USB CDC mode

 

Step 3: Raspberry Pi Settings

The RT870 USB CDC mode is the USB Com port emulation mode, the device name usually is:

/dev/ttyACM0

 

 

Step 4: Run the Python sample code

Run:  python RT870_USB_CDC.py

#RT870 python test demo
#USB-CDC readme
#by RTSCAN 20200901
 
 
import sys
import serial
import time
 
#default usb device /dev/ttyS0 115200 8n1
ser = serial.Serial("/dev/ttyACM0",115200,timeout=0.5)
 
print('serial test start ...')
if ser != None:
    print('serial ready...')
else:
    print('serial not ready')
    sys.exit()
 
ser.timerout=1 #read time out
ser.writeTimeout = 0.5 #write time out.
 
def printHex(str):
    for i in str:
        print('0x%02x'%ord(i)),
    print("")
 
def send_cmd(str):
    ser.write(str)
    time.sleep(0.2)
    t = ser.read(ser.in_waiting)
   ...

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

 

Output:

 

 

USB-HID

If you prefer to connect the embedded barcode scanner via USB Keyboard simulation, it also is fine, setup RTscan’s barcode scanner to HID mode:

Then open “Text editor” from Raspberry Pi software, now run the bar code scanning, the decoded data will show up directly.

The above python sample code can be used for other embedded barcode scanners, e.g: RT830B.  But we can provide sample code for most of our embedded scanners, if you want to learn more about them, please view the links below: 

https://www.rtscan.net/barcode-readers/fixed-mount-barcode-readers/

Part VI: Video Demo

You can also refer to the following video demo, to see how the embedded barcode scanner work with Raspberry Pi:

https://www.youtube.com/watch?v=V32ZUt18ljM&feature=youtu.be

 

 

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

Arduino Barcode Scanner

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

Raspberry Pi Barcode Scanner

 

Read More