Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC barcode

Not open for further replies.


New Member
I need to connect a Barcode scanner to a PIC. Can u tell me how i need to go about doing this? Also, i need to create a database and link it to all. Can i get some ideas/feedback?
Hi. Do you have the scanner yet? Have you considered using Microsoft Excel for the database?

I worked at a company that tracked their product by pre-printing on clear address labels, which then got attached to the product. When scanned, the output was treated as a keyboard input (this scanner was paralleled with the keyboard) and the carriage return inserted at the input's end made Excel jump to the next cell down automatically. This is the simplest way to do the job known to me.

Luck on your project.

I just did a project for scanning key's in, **broken link removed** and a Symbol Tech LS1900 scanner.
The scanner has RS232 output, for the database ~ 32k I2C EE then upload it to a PC to process it.
Pfft. More thought, less haste.
You want to connect to a PIC. There are serial port versions of these scanners, if your PIC is limited to using a software serial port, or has a USART available. If you have a USB-ready PIC, there are USB scanners too. I have studied some PIC BASICs on and off over the years. Great Cow (commonly refered to as GCBASIC) is free, and will probably let you create code for your project quickly.

If you are making a remote unit to carry around for inventory work, get a PIC with I2C or SPI capability. This allows you to connect SEEPROM with considerable storage capacity. At days' end, you just use the serial port to dump the stored data to a PC for porting to a database.

A word about line sizes: the final label we chose to use was largely based on the printed lines in the bar code. The 300 dpi laser printer we had to work with couldn't produce bar lines in a resolution fine enough for our cheapo LED scanner using smaller address labels. Larger labels, like one-something by two-something, were needed to produce scannable bar codes. A laser scanner would have made smalled lines usable, but at the time they were still pretty expensive, over $200 at best. Our used LED scanner came off Ebay for under $30.

Csaba911, can you share the code and/or diagrams with us?
i chose the Unitech MS180 scanner. connected serially thru a max232 chip. however i am getting a problem receiving data to the pic. can you spot the error? code used:

#include "p18f452.h"
#include <usart.h>
#include <stdio.h>

void read_scanner (void)
/* Serial Port &
Continuous Receive enabled */

TRISCbits.TRISC6 = 0; // set TX (RC6) as output
TRISCbits.TRISC7 = 1; // and RX (RC7) as input

/* USART Tx off &
USART Rx on */

// configure USART
25 );
baud rate for asynchronous mode(BRGH=1)@9600bps,
yields lower %error=+0.16
SPBRG value = 25

while( ! PORTAbits.RA0 ); //wait for RA0 high
WriteUSART( PORTD ); //write value of PORTD
if(PORTD == 0x80) // check for termination
break; // value


void main (void){
I have just looked at your code, and see a few things.

1) There are no fuses set, so the WDT will always reset the chip unless you set the config bits manually?

2)You are attempting to WriteUSART but turned transmit off.

3) You are not attempting to read data from the UART

4) You are waiting for RA0 to go high for some reason?

okay i re-did that section with the writing part to:

		while(PIR1bits.RCIF);	//wait for char to be received
		    //return(RCREG);		//and return it
			for (i=0;i<10;i++)

before was what they had in the c-18 manual. sorry about that. the transmit is off because i do not want to write to the usart. a barcode scanner jus required it to read from it.
however this is still not working.
Did you add the fuses?

Your code looks like it waits for the recieve interrupt flag to be set, then loops from 0 to 10, receives 1 byte then closes the USART, this doesn't look correct.

Please re-post your code with the changes, you should be using ReadUSART or something to read the data.

i tried the readusart, but that didnt work, which is why i made the counter to receive the 10 bytes. i dont know what u mean by the fuses. can you explain please?
The fuses are what set the configuration of the PIC, for example the oscillator setting, and the watch dog timer.

The watchdog timer will reset your chip if the watchdog flag is not cleared before the timeout, kind of like a failsafe keepalive, if the PIC doesnt respond, something has gone wrong, so reset the controller.

Your code natively flashed will put the PIC into a constant reset loop, much like leaving MCLR floating. (This should be pulled high with a resistor, 10k should do it, btw), it has the same effect.
So, what we do is turn this off, or put code to make the doggy happy. Easiest to turn it off.

Likewise with the OSC setting, we need to tell it what kind of clock we are using, in your case i'm guessing external so XT would be selected.

The settings for this can be done within MPLAB IDE, or in code, I find it easier to select it in code, to do this you need to define some configuration options within the code itself.
Fuses are also known as Configuration Bits.

I have also in the code below defined the crystal clock frequency. Set to 4MHz to suit your application.
#include "p18f452.h"
#include <usart.h>
#include <stdio.h>
#pragma config WDT      = OFF
#pragma config OSC     = XT
#define GetSystemClock()        4000000
#define GetPeripheralClock()    GetSystemClock()
#define GetInstructionClock()   (GetSystemClock() / 2)

void read_scanner (void)

RCSTA I have converted from Binary just as I find it easier to read as Hex, there is no impact on changing this.

The FOR loop you are using has CloseUSART within the loop, so wouldn't this after receiving 1 character shut off the port?

can someone help me,

i am doing similar project too, which is to connect a barcode scanner to 18f452 portablely. which mean connect the ps2 port manually to its data, clk, vcc and gnd. but my problem is i cant turn on the scanner by juz 5v Vcc regulated from a 9v battery. and the scanner can only be turned on by connecting it to cpu oni..... i am using unitech ms180 too.
i have found the mistakes, now i can use battery for my scanner... and now i am working on the code to process the data from scanner...= )
Not open for further replies.

New Articles From Microcontroller Tips