Arduino

Barcode Scanner Integrated to Arduino

Barcode Scanner Integrated to Arduino

This project is ideal to acquire information from barcodes to your Arduino. It could be use for store automation purposes.

A barcode is a machine-readable code in the form of parallel lines with varying widths.

Components

// Live from circuit.rocks shop

Hardware + Supplies

  • any USB Barcode Scanner

Connecting Everything

Stack the USB Host shield on top of the Arduino board

Then Connect The Barcode Scanner to USB slot of USB Host Shield.


Code:

Before coding anything, you must install the USB Host Shield Library on your Arduino IDE.

#include <usbhid.h>
#include <hiduniversal.h>
#include <Usb.h>
#include <usbhub.h>
#include <hidboot.h>
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb); String data = ""; class KbdRptParser: public KeyboardReportParser { void PrintKey(uint8_t mod, uint8_t key); protected: virtual void OnKeyDown (uint8_t mod, uint8_t key); virtual void OnKeyPressed(uint8_t key);
}; KbdRptParser Prs; void setup() { Serial.begin(115200); Usb.Init(); delay(200); Hid.SetReportParser(0, (HIDReportParser*)&Prs);
}
void loop(){ Usb.Task();
} void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) { uint8_t c = OemToAscii(mod, key); if (c) OnKeyPressed(c);
} void KbdRptParser::OnKeyPressed(uint8_t key) { char c = (char)key; if (key == 19) { String ret = data; data = ""; barcodeCallback(ret); } else { data += c; }
} void barcodeCallback(String data){ Serial.println(data);
}

Frequently Asked Questions

What does this Barcode Scanner Integrated to Arduino tutorial cover?

This project is ideal to acquire information from barcodes to your Arduino.

Can I use an Arduino Nano or Mega instead of UNO for the Barcode Scanner Integrated to Arduino build?

Yes. Nano shares the same ATmega328P and pinout. Mega has more I/O if you outgrow UNO. The code stays the same — just match the pin numbers used in the Sample Code section.

Why does my Barcode Scanner Integrated to Arduino sketch fail to upload?

Usually wrong COM port, missing CH340/CH341 driver for clone boards, or another program holding the serial port. Disconnect the board, install the driver, reselect the port, retry.

// written by circuitrocks

Official account for the Circuitrocks team. Store updates, product announcements, and shop-wide news from the Philippines maker electronics retailer behind the tutorials, kits, and components.