Monday, December 21, 2015

RFID Application for Allen-Bradley PLC using RFID USB Reader And Arduino




This topic about how to use RFID application for Allen-Bradley PLC.
RFID device use RFID USB reader and connect to Arduino plus USB Host Shield.
And then Arduino connect to Allen-Bradley PLC with DF1 communication/protocol.
Must use RFID tags to be read by RFID reader.
Each RFID tag has a unique id as many as 10 digits.
RFID tags can replace the function of the switch, sensor, or password.
In this RFID application, each RFID tag is used to activate the PLC Output.

See the animated image below:


PLC RFID Application


Video demonstration:

RFID Application for Allen-Bradley PLC using RFID USB Reader, Arduino and USB Host




Hardware required in the RFID application

  1. RFID USB Reader, I use RFID 125khz
  2. RFID tags , I use RFID Keychain Tag 125 KHz, RFID Card Tag 125 KHz and RFID Wrist Tag 125 KHz
  3. Arduino UNO R3
  4. Arduino USB Host Shield
  5. Allen-Bradley PLC, I use MicroLogix 1000
  6. RS 232 PLC Cable for MicroLogix 1000 : http://program-plc.blogspot.com/2015/11/how-to-make-rs-232-plc-cable-for-allen.html
  7. TTL to RS 232 Module
  8. if female RS 232 then use DB9 Male crossover cable
  9. Adapter tor Arduino UNO

RFID Arduino PLC Hardware


Hardware connections for RFID application


Hardware connections for RFID application


Software required in the RFID application

  1. Arduino Software (IDE): https://www.arduino.cc/en/Main/Software
  2. I use Arduino IDE version 1.6.6
  3. Arduino library : click here
  4. Unzip and copy paste DF1 and USBHostShield2 folder to ..\arduino-nightly\libraries


Project file for RFID application

  1. Arduino project file : click here
  2. Allen Bradley MicroLogix 1000 PLC : click here


Setting for RFID application

  1. DF1 setting  : http://program-plc.blogspot.com/2015/11/allen-bradley-plc-arduino-allen-bradley.html
  2. RFID Tag number setting in the PLC Ladder:

  3. Example:
    RFID Tag number (10 digits) = 0011487710
    Split per 2 digits - 4 digits - 4 digits = 00 - 1148 - 7710
    Set to N7:00 = 0, N7:01 = 1148 and N7:02 = 7710

PLC Ladder Setting for RFID application


Arduino code for RFID application

#include <usbhub.h>
#include <DF1.h>

#define DF1destination 1
#define DF1baud 9600
#define DF1format SERIAL_8N1
#define DF1timeout 1000


enum
{
  DF1PACKET1,
  DF1TOTAL_NO_OF_PACKETS
};
DF1Packet DF1packets[DF1TOTAL_NO_OF_PACKETS];

DF1packetPointer DF1packet1 = &DF1packets[DF1PACKET1];


unsigned int RFID_DF1writeRegs[3];
unsigned int RFID_USB_Reader_Number[10];
unsigned int DF1successful;
unsigned long DF1successful_timeout;

USB Usb;
USB_DEVICE_DESCRIPTOR buf;
uint8_t addr;
uint8_t rcode;
uint8_t PLC_State;
bool RFID_USB_Reader_RUN = false;
uint8_t rfid_inc;
  
void setup() {
    if (Usb.Init() == -1){
      //RFID USB Reader did not start
      while(1);   
    }
 
  DF1_construct(DF1packet1, DF1destination, DF1_WRITE_N7, 0, 3, RFID_DF1writeRegs);

  DF1_configure(&Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);

  PLC_State=0;
  rfid_inc=0;
}

void loop() {
  uint8_t  buf[64];
  uint16_t rcvd=0;
  Usb.Task();
  if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
  {
  RFID_USB_Reader_RUN = true;

            switch(PLC_State) {
                    case 0:  // RFID USB Reader Configuration                  
                            rcode = RFID_USB_Reader_Configuration();
                            if(rcode){
                              asm volatile ("  jmp 0");
                            }else{
                              PLC_State=1;
                              rfid_inc=0;
                            }
                            break;

                            
                      case 1: // RFID Key Tags Detect                                      
                      rcode = RFID_USB_Reader_BULK_IN(0x01,&rcvd, buf, 1);
                      if(rcode ==0){//1
                        if(rcvd==8){//2
                          if(buf[2]!=0){//3
                            if(30<=buf[2] && buf[2]<=39){//4
                              if(rfid_inc<10){//5
                                RFID_USB_Reader_Number[rfid_inc] =  RFID_USB_Reader_Keycode_to_Number (buf[2]);
                                rfid_inc++;  
                              }else{//5
                                PLC_State=2;
                                DF1successful = DF1packet1->DF1successful_requests;                                
                                DF1successful_timeout = millis() + 2000;
                              }//5
                            }//4
                            if(buf[2]==40){//6
                              PLC_State=2;
                              DF1successful = DF1packet1->DF1successful_requests;                              
                              DF1successful_timeout = millis() + 2000;
                            }//6
                          }//3
                        }//2
                      }//1                  
                      break;

                    case 2: //RFID USB Reader Number and Send to Allen-Bradley PLC
                      //Reduce from 10 memory to 3 memory
                      //0011487710 RFID Key Tag 1
                      // 00-1148-7710 = N7:0-N7:1-N7:2
                      //0008997168 RFID Key Tag 2
                      // 00-0899-7168 = N7:0-N7:1-N7:2
                      //0006216553 RFID Key Tag 3
                      // 00-0621-6553 = N7:0-N7:1-N7:2
                      RFID_DF1writeRegs[2] = (RFID_USB_Reader_Number[6]*1000 )+ (RFID_USB_Reader_Number[7]*100 ) + (RFID_USB_Reader_Number[8]*10 ) + RFID_USB_Reader_Number[9];
                      RFID_DF1writeRegs[1] = (RFID_USB_Reader_Number[2]*1000 )+ (RFID_USB_Reader_Number[3]*100 ) + (RFID_USB_Reader_Number[4]*10 ) + RFID_USB_Reader_Number[5];
                      RFID_DF1writeRegs[0] = (RFID_USB_Reader_Number[0]*10 ) + RFID_USB_Reader_Number[1];                    
                      DF1_update();
                      if(DF1packet1->DF1successful_requests!=DF1successful){
                        PLC_State=1;
                        rfid_inc=0; 
                      }
                      if(millis() > DF1successful_timeout){
                        PLC_State=1;
                        rfid_inc=0;   
                      }
                      break;
                                       
            }
            
  }else{
    if(RFID_USB_Reader_RUN){
      //RFID USB Reader Error
      asm volatile ("  jmp 0"); 
    }
  }


}


void RFID_USB_Reader_GetAddresses(UsbDevice *pdev)
{
    UsbDeviceAddress adr;
    adr.devAddress = pdev->address.devAddress;
    addr = adr.devAddress;
}

uint8_t RFID_USB_Reader_Configuration() {
  uint8_t rcode;
  Usb.ForEachUsbDevice(&RFID_USB_Reader_GetAddresses);
  rcode = Usb.getDevDescr(addr, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*) & buf);
  if (rcode) {
    return (rcode);
  }else{
    rcode = Usb.setConf(addr, 0, buf.bNumConfigurations);
    return (rcode);            
  }        
  return (USB_STATE_ERROR);
}



uint8_t RFID_USB_Reader_BULK_IN(uint8_t vHXFR,uint16_t *pktsize, uint8_t* data, unsigned long timeout) {
  unsigned long timeout_start = millis() + timeout;
    
  while((long)(millis() - timeout_start) < 0L) {
    Usb.regWr(rHXFR, vHXFR);
      if((Usb.regRd(rHIRQ) & bmRCVDAVIRQ)==bmRCVDAVIRQ){
      uint16_t buff_pktsize = Usb.regRd(rRCVBC);
      *pktsize = buff_pktsize;
      data = Usb.bytesRd(rRCVFIFO, buff_pktsize, data);
      Usb.regWr(rHIRQ, bmRCVDAVIRQ);
      return (0);   
      }   
   }      
             
  return (1);
}


byte RFID_USB_Reader_Keycode_to_Number (byte RFID_USB_Reader_Keycode){
  switch(RFID_USB_Reader_Keycode) {
     case 30:
        return 1;
     case 31:
        return 2;
     case 32:
        return 3;
     case 33:
        return 4;
     case 34:
        return 5;
     case 35:
        return 6;
     case 36:
        return 7;
     case 37:
        return 8;
     case 38:
        return 9;
     case 39:
        return 0;
     default:
        return RFID_USB_Reader_Keycode;
  }
}


Labels:










Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !

Your email is stored safely on Google FeedBurner