Hi
I tried modifying the code for the parallax RFID-reader test program to work with the Seeed Studio RFID reader This one
this is how the modified code looks like:
int val = 0;
char code[10];
int bytesread = 0;
void setup() {
Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 9600bps
}
void loop()
{
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
}
bytesread = 0;
delay(500); // wait for a second
}
}
}
This does nothing for me, it should print the code in the serial monitor, but it does not, not even the tx led on the arduino-board lights up
having no luck with that one, I tried so simplify it even more,
to just give a beep in a buzzer connected to pin 3.
this turned out to work, so I know my wiring of reader tx, arduino rx is correct, and that the reader is working.
the test code is:
int val = 0;
char code[10];
int bytesread = 0;
int buzPin = 3;
void setup() {
pinMode(buzPin, OUTPUT);
Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 9600bps
}
void loop()
{
if(Serial.available() > 0) { // if data available from reader
digitalWrite(buzPin, HIGH);
}
}
I googled around and can't find anyone that actually has used this RFID reader on a arduino before... anyone with any good ideas?
Since your simple test seems to work, add a little bit to it:
void loop()
{
if(Serial.available() > 0) { // if data available from reader
val = Serial.read();
Serial.println(val,DEC);
digitalWrite(buzPin, HIGH);
delay(100);
digitalWrite(buzPin, LOW);
delay(200);
}
}
and see what exactly is coming over the serial port. I think the rfid module is supposed to output some characters every time you bring the tag into range so you should be able to get it to do that at least and then worry about the protocol later.
my code is mainly based of THIS with some modifications, his tags were 10-bit, mine are 12-bit, he opened a lock, I have to know the difference between the 6 tags in my system.
I think I need some help with some new issues that have emerged:
if I read a tag, it shows up in the serial monitor as a bad tag, and has a square added after the tag-ID (empty bit?)
if i read the next tag, it still says bad tag, and keeps the same tagID as the first tag read. I have to reset the arduino for each tag it reads... :-/
the code:
//RFID Clothes Dryer
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial= SoftwareSerial(rxPin, txPin);
int buzzPin = 5; //buzzer
int led1Pin = 6; //user 1
int led2Pin = 7; //user 2
int led3Pin = 8; //user 3
int led4Pin = 9; //user 4
int doorPin = 12; //doorswitch
int startPin = 13; //start connected to pin 13
boolean open = true; // default start up is to assume the lock is open
int val = 0;
char code[12];
int bytesread = 0;
int pulse, switchVal;
//RFID tags
char tag1[13] = "390051A9ED2C"; //user1
char tag2[13] = "39003A80D457"; //user2
char tag3[13] = "3900315FECBB"; //user3
char tag4[13] = "390031587D2D"; //user4
char tag5[13] = "24004D4AF5D6"; //delete
char tag6[13] = "24004D31174F"; //read
void beep(int state){
switch (state){
case 1: //long beep, bad tag
for (long i = 0; i < 2048 * 1; i++ )
{
digitalWrite(buzzPin, HIGH);
delayMicroseconds(244);
digitalWrite(buzzPin, LOW);
delayMicroseconds(244);
}
break;
case 2: //short beep, good tag
for (long i = 0; i < 1024 * 1; i++ )
{
digitalWrite(buzzPin, HIGH);
delayMicroseconds(122);
digitalWrite(buzzPin, LOW);
delayMicroseconds(122);
}
}
}
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode (buzzPin, OUTPUT);
pinMode (led1Pin, OUTPUT);
pinMode (led2Pin, OUTPUT);
pinMode (led3Pin, OUTPUT);
pinMode (led4Pin, OUTPUT);
pinMode (doorPin, INPUT);
pinMode (startPin, OUTPUT);
}
boolean checkTag(char *tag){
for (int x=0;x<12;x++){
if( tag[x] != code[x]){
return false;
}
}
return true;
}
boolean findGoodTag(){
if (checkTag(tag1)){ return true;}
else if (checkTag(tag2)){ return true;}
else if (checkTag(tag3)){ return true;}
else if (checkTag(tag4)){ return true;}
else if (checkTag(tag5)){ return true;}
else if (checkTag(tag6)){ return true;}
else{
Serial.print("Bad tag: ");
Serial.println(code);
beep(1);
return false;
}
}
void loop()
{
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
RFID.begin(9600);
if((val = RFID.read()) == 12)
bytesread = 0;
while(bytesread<12)
{ // read 12 digit code
val = RFID.read();
if((val == 12)||(val == 14))
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
if((bytesread == 12) && (findGoodTag()))
{ // if 12 digit read is complete
delay(500);
}
}
Hi,
I am working on a similar prjct and I would like to thank you for sharing this with us.
I think that you had problems with the first few codes because you were using Serial.available() > 0
I have a problem. I have a loop() similar to yours. When the code goes into the RFID stuff it hangs until it gets a proper tag read.
The aim is to have a switch that turns on the circuit. and the tag that switches it off. When it is on I would like to have a second switch that, (when closed) sends an alarm signal.
////////////////////////////
//define all the pins used
//////////////////////////// #define mainSwitch 7 #define LED 4 #define button 7
boolean active = false; //circuit on/active
SoftwareSerial RFID= SoftwareSerial(rxPin, txPin); //create a Serial object RFID
char key[13] = "3900A526F54F";
char code[12];
int val = 0;
int bytesread = 0;
void setup()
{
Serial.begin(9600); //open serial hardware
RFID.begin(9600); //open serial software
pinMode(rxPin, INPUT); //set pin 2 on arduino for receiving RFID data
pinMode(txPin, OUTPUT); //this is not important
pinMode(button, INPUT); //this is the main switch
pinMode(LED, OUTPUT); //this is the LED that shows the circuit is on
}
void loop()
{
int buttonState = digitalRead(button);
if (active==false) {
// if the state has changed turn on the circuit
if (buttonState == HIGH) {
digitalWrite(LED, HIGH);
active=true;
}
}
if (active) {
val=0;
bytesread=0;
//if(Serial.available() > 0) //If I comment out this the tag is never read
//{
if((val = RFID.read()) == 12) //I am not sure why to keep this
//{
bytesread = 0;
while(bytesread<12) // I think this is where it starts hanging until it finishes reading
{
// read 12 digit code
val = RFID.read();
if((val == 12)||(val == 14))
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
if((bytesread == 12) && (findGoodTag()))
{ // if 12 digit read is complete
Serial.print(code);
Serial.println(" <<Card ID"); //print the whole 13 bytes
active=false; //if the tag is correct it switches all to non active
digitalWrite(LED, LOW);
delay(50);
}
}
//}
//}
// Here after the Serial.available I would like to check my second switch (ALARM)
// but it never get here
}
boolean findGoodTag(){
for (int x=0;x<12;x++){
if( key[x] != code[x]){
return false;
}
}
return true;
}
Hi, Reading Rfid tags is fun, Just wait until you test the checksum.
his tags were 10-bit, mine are 12-bit
From using the Innovations ID20 Rfid reader, I think you may be confused with the 10 and 12 byte of tag data.
My tags are 10 byte of tag data plus 2 bytes of checksum !!!!
So I read 12 bytes , and compare the first 10 bytes to the checksum.
I got mine working. I hate connectors that can be installed either way, when the way it's installed matters.
I had the antenna loop plugged in backwards, and got nothing but garbage when it scanned a tag.
The hardware setup is pretty simple if you have a protoshield from sparkfun. If not, and proto board can be used, but the antenna connector on the bottom side of the PCB is too long for the proto board to sit flat.
The code is straight-forward. I'm lighting up an LED when the card or fob is recognized.
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
//create a Serial object RFID
SoftwareSerial RFID = SoftwareSerial(rxPin, txPin);
char code[20];
int val = 0;
int bytesread = 0;
char Red[] = "3900A52D9223";
char Blue[] = "39009F46D131";
char Yellow[] = "390050448FA2";
char Card1[] = "210014E221F6";
char Card2[] = "210014C9906C";
int ledCnt = 5;
int LED[] = {13, 11, 9, 7, 5};
void setup()
{
Serial.begin(9600); //open serial hardware
RFID.begin(9600); //open serial software
pinMode(rxPin, INPUT); //set pin on arduino for receiving RFID data
pinMode(txPin, OUTPUT); //this is not important
for(int i=0; i<ledCnt; i++)
{
pinMode(LED[i], OUTPUT);
}
}
void loop()
{
val = 0;
bytesread = 0;
while(bytesread < 12)
{
// read 12 digit code
val = RFID.read();
if(val == 3)
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
if(val != 2)
{
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
code[bytesread] = '\0'; // add the NULL
}
}
if(bytesread >= 12)
{ // if 12 digit read is complete
Serial.print("Tag: [");
for(int i=0; i<bytesread; i++)
{
Serial.print(code[i]);
}
Serial.println("]"); //print the whole 13 bytes
int tag = FindValidTag(code);
Serial.print("Tag number: ");
Serial.println(tag);
for(int i=0; i<ledCnt; i++)
digitalWrite(LED[i], LOW);
if(tag > 0 && tag <= ledCnt)
digitalWrite(LED[tag-1], HIGH);
}
}
int FindValidTag(char *code)
{
if(strcmp(code, Red) == 0)
return 1;
else if(strcmp(code, Blue) == 0)
return 2;
else if(strcmp(code, Yellow) == 0)
return 3;
else if(strcmp(code, Card1) == 0)
return 4;
else if(strcmp(code, Card2) == 0)
return 5;
else
return 0;
}
You'll need to change the char array values and pin values to match your setup.
There was some sample code on another thread that I used as a starting point. It used SoftwareSerial, and it worked. At least it talked to the RFID reader. I saw no reason to change what worked.
Wow comparing your code to some other examples, you differ greatly starting wit this part.
Your code
//create a Serial object RFID
SoftwareSerial RFID = SoftwareSerial(rxPin, txPin);
char code[20];
int val = 0;
int bytesread = 0;
char Red[] = "3900A52D9223";
char Blue[] = "39009F46D131";
char Yellow[] = "390050448FA2";
char Card1[] = "210014E221F6";
char Card2[] = "210014C9906C";
int ledCnt = 5;
int LED[] = {13, 11, 9, 7, 5};
Can you explain this code? remember I'm a newbie.
What I get from it is you create a serial object name it RFID and use the UART Rx Tx pins for it. Then I start losing you. What is "char code 20" and the rest of the code before void setup? I understand what your doing just not how if that makes sense.
The "char code[20]" statement reserves space for 20 characters, in an array named code. The RFID items have 12 character codes. After putting each character into the array, I put a NULL in the next position, so that at all times I have a properly terminated C string.
I have 3 fobs and two cards. When I scanned them, I got the strings shown as the values in the Red, Green, etc. arrays.
Since I wanted to be able to identify which item was scanned, I created the arrays with the known scan results.
There are 5 items, so I have LEDs. I light up one based on which item was scanned. The LED array defines the pins that the LEDs are connected to.
The rest of the code deals with collecting the data from the RFID reader, determining which item was scanned, and lighting only the appropriate LED.
All the string handling functions start with str. The strcmp function compares two strings. The comparison is based on the order of the words in a dictionary. If the first string comes before the second string, the return value will be -1. If the strings are the same, the return code will be 0. If the first string comes after the second string, the return code will be +1.
Ok so your comparing the read tag code vs the stored tag codes red, blue, etc.. if they are the same then the return is 0, correct?
So in the code
if(strcmp(code, Red) == 0)
return 1;
where in the code is the referenced return 1???
What I'm trying to figure out is if I want to have a specific action to happen according to whether the code is recognized or not. such as trigger relay or print msg on LCD etc..