USB Barcode Scanner (Raspberry Pi)

Introduction: USB Barcode Scanner (Raspberry Pi)

About: the Raspberry Pi is Awesome!

Basic tutorial of how to setup a USB barcode scanner with the Raspberry Pi.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Parts

Step 2: Setup

1. Plug in USB Barcode Scanner to Raspberry Pi

2. Boot Pi and open terminal

Type "sudo raspi-config"

3. Go to interfacing options and enable "Serial"

4. Install dependencies

Type "pip install requests"

5. Navigate to https://upcdatabase.org/ and create a free account and take note of api key

6. Edit line 6 of barcode.py and enter your api key

7. Type "sudo python barcode.py" to run script (press ctrl+c to exit script)

Step 3: Code

Dependencies:

"pip install requests"

run:

sudo python barcode.py

Step 4: Additional Info

Be the First to Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Silly Hats Speed Challenge

      Silly Hats Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020

    14 Discussions

    0
    AkiraR2
    AkiraR2

    Question 2 years ago on Step 4

    It shows an error called
    No such file/directory dev/hidraw0 exists
    how should I fix it?

    0
    piddlerintheroot
    piddlerintheroot

    Answer 2 years ago

    What version RPI are you running it on? Are you running it in sudo? Try running the updated the code.

    0
    yusufgmi97
    yusufgmi97

    Reply 2 months ago

    i think because you did not plug in your barcode scanner,if you plug in the barcode scanner,the hidraw0 file will appear in dev folder.

    0
    yusufgmi97
    yusufgmi97

    2 months ago

    1)i did not find directory of /dev/hidraw0,'rb',any file that i can used to connect with my raspberry?
    (pi3 b+ and raspbian buster)

    2)i try used /dev/ttyAMA0,the barcode.py is didnt have any error,but the terminal didnt show any result

    0
    thone
    thone

    Reply 2 months ago

    I just posted a comment that might help you with this.

    0
    yusufgmi97
    yusufgmi97

    Reply 2 months ago

    ok tq thone

    0
    thone
    thone

    2 months ago

    I just wanted to add a tip. I found that if I had anything else plugged in, like a mouse or keyboard, the scanner didn't always default to hidraw0 which broke the program. So to avoid having to manually change this every time, I did the following.

    1. Install pyudev (https://pyudev.readthedocs.io/en/latest/guide.html):
    sudo pip install pyudev
    Note: This needs to be installed for the root since the script has to be run with root permissions.
    2. Determine which hidraw is your scanner.
    - start with your scanner unplugged
    - open a terminal on your raspberry pi and type: udevadm monitor
    - plug in your scanner and it will print a lot of lines
    - go through the text that printed and determine which hidraw# the scanner was assigned to
    Note: for this example my scanner is hidraw0
    3. Find a unique identifier for your scanner
    type python3
    >>>import pyudev as pd # This imports the function
    >>>context = pd.Context() # pyudev requires a context to be used
    >>> for device in context.list_devices(subsystem='hidraw'):

    ... print(device)

    (make sure you have 4 spaces before print(device) and hit enter twice)
    Copy the path right before /hidraw/, mine printed like this:
    Device('/sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0/0003:046A:B091.0014/hidraw/hidraw1')
    Device('/sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.4/1-1.4:1.0/0003:0416:C141.0013/hidraw/hidraw0')

    The information after the period will change depending on which usb port it's plugged into. So you will want to copy the part up to the period. For me this is 0003:0416:C141
    4. Add the pyudev code to provide dynamic support of your scanner.

    To do this open the python script in your preferred code editor or text editor and add the following code after the last import line

    import pyudev as pd
    context = pd.Context()
    for device in context.list_devices(subsystem='hidraw'):
    if ('{0}'.format(device.find_parent('hid'))).find('0003:0416:C141') != -1:
    location = device.device_node
    Make sure you replace the 0003:0416:C141 with your device information

    change def barcode_reader(): to def barcode_reader(device_local):
    change fp = open('/dev/hidraw0', 'rb') to fp = open(device_local, 'rb')

    now when you call the function you need to add the location i.e. UPC_lookup(api_key,barcode_reader(location)) instead of UPC_lookup(api_key,barcode_reader())

    I hope this helps!
    0
    skishn
    skishn

    1 year ago

    I get this error message when running on my pi B+ using sudo:

    {
    "status": 400,
    "message": "Bad Request. The code you are trying to enter does not contain all digits.",
    "error": true
    }

    The barcode that gets reported on the terminal does include all digits. When I try to check the code on the website using my api key, I get an item not found, but that's different from this error. Any ideas?

    Sample UPC (bottle of tylenol):
    050428377512

    0
    piddlerintheroot
    piddlerintheroot

    Reply 1 year ago

    Looks like the request is malformed. I wrote the script a while ago, so most likely the UPC_lookup function is deprecated as it was written for the old API. https://www.upcdatabase.com/xmlrpc.asp provides documentation for the new API.

    0
    JohnphilC
    JohnphilC

    Question 1 year ago on Step 3

    where do i find the api key? what does it look like?

    0
    tssparky
    tssparky

    Question 2 years ago

    Is a pi3 mandatory? I have a pi2, and I'm running into difficulties getting python script to respond with python2, and python3 can't install request.

    0
    Guus_e
    Guus_e

    Question 2 years ago

    Hello, i'm completely new with the raspberrypi. I've used arduino alot but was wondering what u use the micro SD and jumper cables for. I can't find any use for them in your project, do you have a cicuit I can follow?

    0
    bivav
    bivav

    2 years ago

    Hi there... I just wanted to know if it is possible to buy any bar code scanner to work with this code.. can we?

    Thank you in advance!!