Hi all,
I have an ID-12 RFID reader, the output of which I am trying to display on an LCD screen. Both LCD screen and RFID reader are currently in situ on my model railroad.
When I tested the RFID reader with the Arduino, it was able to read tags using the code below, although the range was shorter than anticipated (as I was using the USB for power):
#include <SoftwareSerial.h>
SoftwareSerial rfidSerial(6, 7); // RX, TX
char val = 0;
void setup() {
// put your setup code here, to run once:
rfidSerial.begin(9600);
Serial.begin(9600);
}
void loop () {
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if(Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // if 12 digit read is complete
Serial.print("5-byte code: ");
for (i=0; i<5; i++) {
if (code[i] < 16) Serial.print("0");
Serial.print(code[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Checksum: ");
Serial.print(code[5], HEX);
Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
Serial.println();
}
bytesread = 0;
}
}
}
Now, however, all that’s changed is that the output is to an LCD rather than a serial output, as per below:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
SoftwareSerial rfidSerial(9, 7); // RX, TX
char val = 0;
LiquidCrystal_I2C lcdScreen(0x27,2,1,0,4,5,6,7); //LCD Display
void setup() {
// put your setup code here, to run once:
rfidSerial.begin(9600);
Serial.begin(9600);
}
void loop () {
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) { // check for header
bytesread = 0;
while (bytesread < 12) { // read 10 digit code + 2 digit checksum
if(Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // ready to read next digit
}
}
// Output to LCD:
if (bytesread == 12) { // if 12 digit read is complete
lcdScreen.clear();
lcdScreen.home();
lcdScreen.print("5-byte code: ");
for (i=0; i<5; i++) {
if (code[i] < 16) lcdScreen.print("0");
lcdScreen.print(code[i], HEX);
lcdScreen.print(" ");
}
lcdScreen.println();
lcdScreen.print("Checksum: ");
lcdScreen.print(code[5], HEX);
lcdScreen.println(code[5] == checksum ? " -- passed." : " -- error.");
lcdScreen.println();
}
bytesread = 0;
}
}
}
The ID-12 is running from the Arduino’s 5V output, and the serial output of it is connected to pin 9. Pin 7 is not connected at all. However, when I pass an RFID tag under the reader, I get no output on the LCD.
I can confirm the LCD display is working, as I’ve tested that separately. I’m not sure if the problem is with the way I’ve written the code, or the way the ID-12 is set up. The three connections are exactly as they were for the test (aside from the RX pin being 9 instead of 6), and the power connections are also the same. So too is the code.
Any help would be greatly appreciated, as this whole system relies on having the information from the RFID reader to function.
Cheers,
Tbdanny