Can't connect barcode scanner to Arduino UNO R4 WiFi

I have a GM65 barcode module (link here: https://www.dfrobot.com/product-1996.html) which I am trying to connect to my Arduino UNO R4 WiFi and print out the barcode to the Serial monitor. The main issue is I am unable to get Serial.available() to work. I have tried two different approaches:

  1. Using hardware UART pins 0, 1 for serial communication.
  2. Using SoftwareSerial for digital serial pins, 10, 11.
    I also tried using the GM65 library found here -> GitHub - xuegangxiao0117/GM65_scanner_for_Arduino: Arduino library for GM60 scanner
    Which i was unable to get to work either. I have seen videos online of it working but the problem boils down to Serial.available() is never triggered. There is not an issue regarding baud rate either.

Does anyone have any experience using a barcode scanner to connect to Arduino over UART RX/TX? If so, any help or tips would be appreciated. Thanks

On the R4, the hardware UART on pins 0 and 1 is Serial1, not Serial.

1 Like

How did you connect the barcode reader?

You need to have the barcode reader transmit pin connected to the Arduino receive pin, and the barcode reader receive pin to the Arduino transmit pin.

Show us the code that you can't get to work., and a photograph of your connections would be useful.

On Uno R4 you would use something like

#include <SoftwareSerial.h>
#include "GM65_scanner.h"
//SoftwareSerial mySerial(10, 11); // not needed, should not be used with UnoR4

GM65_scanner scanner(&Serial1);

Here is the setup. Black wire is TX on barcode, Yellow is RX.
This is what I tried for hardware UART, when trying SoftwareSerial i used pins 10, 11 and called SoftwareSerial mySerial(10, 11);

For just hardware UART pins, the code is pretty simple:

void setup() {
  Serial.begin(9600); 
  Serial1.begin(9600);
}

void loop() {
  if (Serial1.available()) { 
    char receivedChar = Serial1.read(); 
    Serial.print("Received: ");
    Serial.println(receivedChar); 
  }
}

I also tried with SoftwareSerial:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
void setup()
{
  Serial.begin(9600);
  while (!Serial) {};
  mySerial.begin(9600);
}
void loop()
{
  if (mySerial.available()) {
    i = mySerial.read();
    Serial.println(i);
  }
}

and trying with gm65_scanner.h:

#include "GM65_scanner.h"

GM65_scanner scanner(&mySerial);
int buttonPin = 4;

void setup() {

  pinMode(buttonPin,INPUT);
  Serial.begin(115200);

  mySerial.begin(9600);
  Serial.println("initialize the scanner...");
  scanner.init();
  Serial.println("enable the setting QR code");
  scanner.enable_setting_code();

  //scan once function, only works in command mode
  Serial.println("change to command trigger mode");
  scanner.set_working_mode(1);
}



void loop() {
  int button_state = digitalRead(buttonPin);
  if(button_state == HIGH){
    scanner.scan_once();
    
  }
}

I was unable to read any barcodes from the serial monitor in any of these cases. Also as a side note I'm having trouble using the GM65_scanner library, this code is from an example given in the lib. Regardless, I still have trouble getting it to output.

Also, the barcode reader scans and beeps when scanning, and it works with USB. Just not UART.

Do you mind elaborating on how to properly use the GM65_scanner library? I'm struggling to understand the functionality.

I'm not familiar with it.

Would I need to setup Serial and Serial1 then?
like Serial.begin(9600);
Serial1.begin(9600);

or is only Serial1.begin(9600); needed?

This sets up communications between the Arduino and the PC/laptop.

This sets up communications between the Arduino and the scanner.

Yes, you need both.

Unless ...

If all you ever want to do is have the Arduino take the output from the scanner and pass it through to the PC without alteration, then the Arduino would be wasted.

The scanner also has a USB interface, and with a suitable cable can be connected directly to the PC.

Thanks for posting the photo and code.

The connections look correct to me. You have the barcode scanner TX going to RX on the Arduino.

I don't have a barcode scanner, but I tested your code using a gps receiver.

The first code, using Serial1 worked straight away.
Once I had it working, I removed the line that prints "Received: ", and on the next line
changed Serial.println() to Serial.print() .

The second code that you posted using SoftwareSerial would not work for me - Until I changed the pins used from 10 & 11 to 2 & 3.
I tried using some other pins (but not all) . It seemed that low numbered pins worked but high numbers didn't.

Here's proof of both versions of the code working:

Hi @garbagepailasdoijlka.

As mentioned already by @JohnLincoln, only certain pins on the UNO R4 WiFi board can be used as the "SoftwareSerial" library's RX pin. These are the pins you can use:

  • 0-3
  • 6
  • 8
  • 11
  • 12
  • 15 (A1)-19 (A5)

(note that pin 10 is not supported)

I have submitted a proposal to add this missing information to the "SoftwareSerial" library documentation:

Hi @ptillisch,

in the example accompanying the software serial library in the Arduino UNO R4 core I see written:

// Note any pin can be used for TX, but only the following pins
// can be used for RX:
// D0, D1, D2, D3, D8, D14, D15, A1, A2, A3, A4, A5

... you do not specify D14 (A0), while you do specify D11 and D12, so ... which ones are really usable? :slight_smile:

Guglielmo

The ones documented in the table which will be added to the SoftwareSerial library documentation by the pull request I linked in my previous reply:

https://github.com/per1234/docs-content/blob/7b88e8630795f2c24bbae4d2531eb813cef502f3/content/learn/07.built-in-libraries/04.software-serial/software-serial.md#supported-pins

Supported Pins

Board RX TX
UNO R4 WiFi 0-3, 6, 8, 11, 12, 15 (A1)-19 (A5) Any

When investigating the subject of this forum topic, I also looked to that "SerialEcho" example sketch you mention for information about supported pins after not finding it in the SoftwareSerial library documentation. I found that the information in the comment was incorrect, incomplete, and confusing. I submitted a report about this to the developers:

Since experience tells me that it is not realistic to hope that we can maintain redundant copies of any given documentation, I have proposed to replace the information in the example sketch comment with a link to the SoftwareSerial library documentation. However, before that could be done the information must be added to the documentation. So I am waiting for my documentation pull request to be merged, after which I will submit a pull request to replace the erroneous comment in the SerialEcho example.

1 Like