DE2120 Scanner

Hello, I have a barcode scanner setup with Arduino. It is used in a model train layout to individually ID Locomotives and Carriages, and will eventually have around 100 components to keep track of.
I downloaded/uploaded code and modified to suit my needs, here is code

const int d = 200;

const int ledPin = 13;
const int pin12 = 12;
const int pin11 = 11;
const int pin10 = 10;
const int pin9 = 9;

#include "SoftwareSerial.h"
SoftwareSerial softSerial(2, 3); //RX, TX: Connect Arduino pin 2 to scanner TX pin. Connect Arduino pin 3 to scanner RX pin.

#include "SparkFun_DE2120_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_DE2120
DE2120 scanner;

#define BUFFER_LEN 40
char scanBuffer[BUFFER_LEN];

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(pin12, OUTPUT);
  pinMode(pin11, OUTPUT);
  pinMode(pin10, OUTPUT);
  pinMode(pin9, OUTPUT);
  Serial.begin(115200);
  Serial.println("DE2120 Scanner Example 1 working");

  if (scanner.begin(softSerial) == false)
  {
    Serial.println("Scanner did not respond. Please check wiring. Did you scan the POR232 barcode? Freezing...");
    while (1);
  }
  Serial.println("Scanner online!");
}

void loop()
{


  if (scanner.readBarcode(scanBuffer, BUFFER_LEN))
  {

    Serial.print("Code found: ");
    for (int i = 0; i < strlen(scanBuffer); i++)
      Serial.print(scanBuffer[i]);
    Serial.print("\t Locomotive: ");

    if (scanBuffer[0] == ('0')  && scanBuffer[1] == ('0') && scanBuffer[2] == ('1'))
    {
      digitalWrite(pin12, HIGH);
      digitalWrite(ledPin, HIGH);
      Serial.print("V 80 007");
      delay(d);
      digitalWrite(pin12, LOW);
      digitalWrite(ledPin, LOW);
    }

    if (scanBuffer[0] == ('0')  && scanBuffer[1] == ('0') && scanBuffer[2] == ('2'))
    {
      digitalWrite(pin11, HIGH);
      digitalWrite(ledPin, HIGH);
      Serial.print("V 100 2263");
      delay(d);
      digitalWrite(pin11, LOW);
      digitalWrite(ledPin, LOW);
    }

    if (scanBuffer[0] == ('0')  && scanBuffer[1] == ('0') && scanBuffer[2] == ('3'))
    {
      digitalWrite(pin10, HIGH);
      digitalWrite(ledPin, HIGH);
      Serial.print("BR 94 713");
      delay(d);
      digitalWrite(pin10, LOW);
      digitalWrite(ledPin, LOW);
    }

    if (scanBuffer[0] == ('0')  && scanBuffer[1] == ('0') && scanBuffer[2] == ('4'))
    {
      digitalWrite(pin9, HIGH);
      digitalWrite(ledPin, HIGH);
      Serial.print("BR 196 178-0");
      delay(d);
      digitalWrite(pin9, LOW);
      digitalWrite(ledPin, LOW);
    }
    
    Serial.println();
  }
  
delay(75);
}

Can anyone help with improving the if(scanBuffer[0] == …) portion, I have been trying many things. Thought I could use a for loop to run through the array and give me a single number such as 001 to the use in the if statement but I can’t make it work (I am only self taught). The example shown seems to be the only way I can do this reliably, but I’m sure it could be improved/ condensed and with 100 components or more this method takes lots of time/space.

Any help or advice would be appreciated

1 Like

Hi Jason,

Hmmm, this method would be rather cumbersome for large amounts of data, you will eventually run out of space on your Arduino to store these entries. Personally, if I was doing this project I would actually create a Microsoft Excel spreadsheet and have python on a computer access this file. You could then have the scanner linked via a serial connection to your computer so when you scan a barcode on one of your locomotives/carriages is scanned it is passed back to the computer where python pulls up the line with all of the information about it from the excel spreadsheet. The useful part about this is that you can manually modify/add/remove different items from this list in excel without having to worry about formatting code.

Here is a handy link on how to program python to work with excel spreadsheets:

To get the scanner to communicate with your computer and python you will need to read up on the config settings in the manual for the scanner: https://cdn.sparkfun.com/assets/b/5/0/e/e/DY_Scan_Setting_Manual-DE2120___19.4.6___.pdf

and also read up on how to interface python with serial:


Hi Mitchell,

I love this idea, have been thinking I may be starting to ask to much from Arduino! My experience in programming is very small, would like to know more and learning python already sounded interesting.
Before I dive in can I do this with raspberry pi instead of a computer? If not I’m using a MacBook Pro does that change anything ?

Thank You,
J

1 Like

Hi Jason,

The Pi is essentially a computer, it usually runs a distro of Linux and is a tiny ARM device with all of the onboard parts required to operate independently. As for programming an Arduino from a OSx device such as a MacBook, yes, there shouldn’t be any problems with installing the IDE for Mac and flashing the board that way. Otherwise, I’d take a look at this tutorial on Arduino, should answer all of your questions about using the Pi to control an Arduino and program it. Have a Merry Christmas!

1 Like

In Addition to Bryce’s comments programming with python on a raspberry pi will also be pretty much the same to programming on a MacBook, if not better as you have access to the GPIO pins on the pi.

1 Like

Ok, sorry to be a simpleton I have been programming Arduino from MacBook and my thoughts were to replace it with Pi so to leave it permanently installed in the system. I know nothing about Pi and will start reading.

Thanks Guys,
J

1 Like

Hold up, I mean, I want to run ID program from PI to Arduino not program Arduino code I will do that from Mac. Sorry for any confusion.
Also I assume I write python for Pi on Mac is this correct?

No worries Jason,

The Raspberry Pi is its own computer with the capability to program and run python onboard. You will need to plug it into a monitor and keyboard, or use a program such as VNC or Putty (or similar for OSx) to interact with it. If you’re new to using Raspberry Pi I’d take a look at our workshop, it’s completely free and should answer lots of questions regarding the applications of the Pi, and the link that @Bryce put up should cover how to use it to program your Arduino if you’re running and scripts on it. Please let us know if there’s anything else that you need, have a great day!

1 Like

Thanks again, will start on another new project!

Cheers,
J

3 Likes

Hi Jason,

No worries, make sure to let us know if you have any further questions, and feel free to send through your project to the site if you’d like (There’s a store credit reward if it goes online) Have a Merry Christmas!

Hi Jason

Im extremely interested in your project, Can I copy what you are doing I was thinking about a similar project and your experience and know how would help me a lot.
Could you tell me what parts I would need to copy your barcode scanner setup with Arduino?
Thanks

2 Likes

Hi Matthew,

I hope I can help. Are you interested in the Arduino DE2120 barcode setup, or as in the forum using pyxl to access data?

1 Like

Also, are you trying to use the scanner with objects in motion?

1 Like

Hi Jason,
Thanks for your time I am interested in how you got to where you are so far. so to speak, What would I need to catch up? I thinking because I haven’t seen a image of your hardware you have set up. I think you have a 2D Barcode Scanner Module - DE2120 and you have programed it with
SparkFun_DE2120_Arduino_Library?
You have a laptop and programed the DE2120 from it?
Where do you get the barcodes from and how do you program them?
Can you tell me what to buy to catch up to your project please?
I see this https://www.sparkfun.com/products/16441
Maybe this is what I need to start with?

I will be using openpyxl because as Mitchell said how easy it is to ad additional products in your case could be unlimited train carriages with different products.
I will be reading up on the above suggestions people have submitted.
I would be trying to use the scanner with objects in motion YES
Thanks

1 Like

Hi @Matthew126715,

Jason’s original setup was a DE2120 Scanner connected to an Arduino via serial.

Now I believe Jason is connecting the Arduino to a computer to pass on the barcode number to an excel spreadsheet which will have a data entry containing all the information about that part corresponding to the barcode number. This will be done using a python script.

1 Like

Thanks
Can I do the same project with RFID tags?
What is the equivalent products I would need and when can I get them. Do you have the items in stock? So I can buy them
Thanks

1 Like

Does it have to be an excel spreadsheet or could it be a google sheet?
Thanks

1 Like

Hi Matt,

Potentially it could be a google sheet, but I would reccomend starting out with excel as that way it is on a local machine and you don’t have to access the internet. as for an RFID reader here is a super neat one that is usb: https://core-electronics.com.au/rfid-usb-reader.html

1 Like

Thanks Mitchell
That video was a good example explanation of the RFID USB Reader.

I need a programmable RFID tag and the waterproof could be an advantage.
I really want and need a long range scan that could scan from a metre or so if possible.

I really want to copy Jason’s project and incorporate RFID as well as the barcode scan.
Like buying an item from a store. You scan the barcode and it shows the product. You buy the product and the customer walks past the RFID scanner and the RFID tag gets scanned and shows the customer left the store from ( RFID scanner ID NUMBER door 3)
Do you have the parts I need for this project?
Thanks

1 Like

Hi Matthew,

I will send through some info after Christmas with pics ( no time now) and I’m also incorporating a rfid scanner in the system.
As for the barcodes I print my own with a label printer that has barcode capabilities, you can choose different types of codes, size, laminated(waterproof), and of course write whatever you need.
I’m waiting for the rfid scanner arrive, I chose the ID 3-LA for the range I’m looking for and also so I can make a custom antenna.

1 Like