Etiquetas

jueves, 17 de septiembre de 2015

RFID

RFID




RFID (Radio Frequency Identification acronym in Spanish Radio Frequency Identification) is a system for storing and remotely retrieving data using labels, cards, transponders or devices called RFID tags. The fundamental purpose of RFID technology is transmitting the identity of an object (like a unique serial number) using radio waves. RFID technologies are grouped into so-called Auto ID (automatic identification, or automatic identification).

RFID Tag or Tags are few, similar to a sticker which can be attached or incorporated into a product, animal or person small devices.

They contain antennas to enable them to receive and respond to requests by radio from an RFID transceiver. Passive tags require no internal power supply, while lending itself require. One advantage of using radio frequency (instead, for example, infrared) is not sight between transmitter and receiver is required.

The mode of operation of RFID systems is simple. The RFID tag containing identification data of the object to which it is attached, generating a radio frequency signal with that data. This signal can be detected by an RFID reader, which is responsible for reading the information in digital format and pass it to the specific application using RFID.


An RFID system consists of the following three components:

· RFID tag or transponder: composed of an antenna, a radio and an encapsulated transducer or chip material. The purpose of the antenna is to allow the chip, which contains the information, transmitting the identification information of the label. Several types of labels. The chip has an internal memory with a capacity depends on the model and ranges from a dozen to thousands of bytes. There are several types of memory:

· Read Only: the identification code it contains is unique and is customized for the manufacture of the label.

· Read and Write: identification information can be changed by the reader.

· Collision. These are special tags that allow a reader to identify several at once (usually labels must enter one by one in the coverage area of ​​the reader).

· RFID reader or transceiver: composed of an antenna, a transceiver and decoder. The reader sends signals periodically to see if any tags in its vicinity. When picking up a signal from a label (which contains the identification information of this), it extracts the information and passes the data processing subsystem.

· Data processing subsystem or RFID Middleware provides the means of data processing and storage.

· Electromagnetic induction is the phenomenon that causes the production of an electromotive force (emf or voltage) in a medium or body exposed to a variable magnetic field or a moving means relative to a static magnetic field. Thus, when said body is a conductor, an induced current is produced. This phenomenon was discovered in 1831 porMichael Faraday, who expressed indicating that the magnitude of the induced voltage is proportional to the change in magnetic flux (Faraday's Law).


Practice with RFID


The following practice we have done is to schedule an RFID card with a code that allows via a RFID reader validate the code and allow access.
The materials needed are an Arduino UNO card, a reader and RFID cards and some colored LEDs to indicate and the code is right or wrong.



For this practice will write two codes, one for writing to write the code on the cards that will use and other code reading in which we will read the code on the card introduced by the RFID reader.

Writing Code


/*
  Autor: Ángel Barquín                         Fecha: 8-5-2015
  Programa: rfid                               Versión: 1.0
  Dispositivo: ATMEL 328                       Compilador: AVR
  Entorno ID: 1.5.5-r2                         Simulador: VSM
  Tarjeta de aplicación: Arduino
 
  The program writes a particular block of a sector, and displays the values written.
  RFID used is RFID-RC522

   The pins are connected as follows 
  

  Signal     Pin              Pin
              Arduino Uno     MFRC522 board
  ------------------------------------------------
  Reset      9                RST
  SPI SS     10               SDA
  SPI MOSI   11               MOSI
  SPI MISO   12               MISO
  SPI SCK    13               SCK
 */
 
  #include <SPI.h>
  #include <MFRC522.h>
 
  #define SS_PIN 10   // definition of slave select pin.
  #define RST_PIN 9   / /reset pin definition.
  MFRC522 mfrc522(SS_PIN, RST_PIN);        //522. RSFF object creation must introduce SS pins and    RST defined above.
 
  byte datosEscribir[] = { 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4 };    //data we will write
  byte sector = 5;           //choose the sector in which we will write (0-16)
  byte numeroBloque= 22;      //block number that we will read
  byte bloqueContrasena= 23;  //block dedicated to the password
 
  void setup()
  {
    Serial.begin(9600);        // initialize the serial
    SPI.begin();               // initialize the spi
    mfrc522.PCD_Init();        // initialize the device MFR522
  }
 
  void loop()
  {
    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++) // we define the key, which defaults to FFFFFFFFFF, it is necessary that this written in the last block of each sector.
    {                            // if we change the value of the key, the card stops working
      key.keyByte[i] = 0xFF;
    }
    //The following functions are necessarily required because it makes a number of checks needed
    //learn to identify the card type and check if the connection is correct
    if ( ! mfrc522.PICC_IsNewCardPresent()) //checks for a new card
    {
      return;//if no new card, leaves and recheck if all
    }
    if ( !mfrc522.PICC_ReadCardSerial()) //with a card that read, we get avoid collioson between two or more cards present.
    {
      return;//if you can not read the card, if leaves and double check all
    }
   
   
    Serial.print("UID of the card: ");//reed the UID (Unique Identification Number) of the card and we show by serial.
    for (byte i = 0; i < mfrc522.uid.size; i++)
    {
      Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
      Serial.print(mfrc522.uid.uidByte[i], HEX);
    }
    Serial.println();
 
         
    byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);// reads and returns by serial card type
    Serial.print("PICC type: ");
    Serial.println(mfrc522.PICC_GetTypeName(piccType));
    if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI  &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K   &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K)
    {
      return;//if the card type is wrong, out of the if and double check all
    }

    byte status;
   
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueContrasena, &key, &(mfrc522.uid));//Comprobamos la key A
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
   
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, bloqueContrasena, &key, &(mfrc522.uid));//check the key B
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
   
    Serial.println("writing.. ");
   
    status = mfrc522.MIFARE_Write(numeroBloque, datosEscribir, 16);  //Write in the specified block, the specified data and 16 bytes
                 
    Serial.println("written values: ");
   
      // for,is used to repeat a block of statements enclosed in braces, there are three parts for (initialization; condition; increment)
      // It is useful for any repetitive operation. The initialization happens first and exactly once. Each time through the loop,
       // The condition is tested; if true, the statement block, and the increase is executed, then the condition is tested again.
       // When the condition becomes false, the loop exits. 
    for(int i=0;i<16;i++)
    { 
      Serial.print(datosEscribir[i]); //serial show by the values we have written
      Serial.print(" ");
    }
     // if,It used in conjunction with a comparison operator, if the statement in parentheses is true
      // Your statements bracketed run. If not, the program skips over the code.        
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("MIFARE_Write() failed: ");    //warns if the writing has failed
      Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.println();
    Serial.println("finalized");
    Serial.println();
    Serial.println("waiting card");
           
    mfrc522.PICC_HaltA();//It is terminated reading
    mfrc522.PCD_StopCrypto1();//The process ends
        
  }






Code read


/*
  Autor: Ángel Barquín                         Fecha: 8-5-2015
  Programa: ethernet mas temperatura           Version: 1.0
  Dispositivo: ATMEL 328                       Compilador: AVR
  Entorno ID: 1.5.5-r2                         Simulador: VSM
  Tarjeta de aplicacion: Arduino
 
 
The program reads a particular block of a sector serial sample values check the read are the same as the password
   RFID used is RFID-RC522

   The pins are connected as follows

  Signal     Pin              Pin
             Arduino Uno      MFRC522 board
  --------------------------------------------
  Reset      9                RST
  SPI SS     10               SDA
  SPI MOSI   11               MOSI
  SPI MISO   12               MISO
  SPI SCK    13               SCK
 */
 

  #include <SPI.h>   //SPI include the library
  #include <MFRC522.h>  //MFRC522 include the library
  #define SS_PIN 10  //We define the slave select pin.
  #define RST_PIN 9  //We define the reset pin.
  #define led_rojo 6  //We define the red LED
  #define led_verde 7  //We define the green LED
  MFRC522 mfrc522(SS_PIN, RST_PIN);   //Mfrc522 creation of the object. We must introduce the SS pin and RST defined above.
 
  byte clave[]={1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4}; //Password user-defined

 
  byte buffer[18];             //We define the buffer size has to be 18
  byte sector= 5;              //You choose the area you are going to read (0-16)
  byte numeroBloque= 22;        //block number that we will read
  byte bloqueContrasena= 23;    //dedicated to block password (multiple of 4)
 
  void setup()
  {
    pinMode(led_rojo, OUTPUT);
    pinMode(led_verde, OUTPUT);
    Serial.begin(9600);
    SPI.begin();                  // Start the SPI protocol
    mfrc522.PCD_Init();                             // Start the device MFR522
    Serial.println("Waiting card reading"); // alerts the customer is waiting for a card approach
  }
 
  void loop()
  {
    boolean b=true;
    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++)          //We define the key, which defaults to FFFFFFFFFF, it is necessary that this written in the last block of each sector.
    {                                     //if we change the value of the key, the card stops working
      key.keyByte[i] = 0xFF;
    }
         
    if ( !mfrc522.PICC_IsNewCardPresent()) //checks for a new card
    {
      return;//if there is no card back up the loop until it finds card
    }     
    if ( !mfrc522.PICC_ReadCardSerial()) //With this card reads only we manage to avoid the collision between two or more cards present.
    {     
      return;//if the card does not read back to the beginning
    }
         
    byte status;
         
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueContrasena, &key, &(mfrc522.uid));//Comprobamos la key A
   
    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
   
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, bloqueContrasena, &key, &(mfrc522.uid));//check the key B

    if (status != MFRC522::STATUS_OK)
    {
      Serial.print("PCD_Authenticate() failed: ");
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
                 
    byte tamanoBuffer = sizeof(buffer);     
    status = mfrc522.MIFARE_Read(numeroBloque,buffer,&tamanoBuffer);//We read the data and store in buffer
    Serial.println("Leyendo...");//We indicated that we are reading
    
    for (int i = 0; i< tamanoBuffer-2 ; i++)
    {
      Serial.print(buffer[i], HEX);  //We write in the serial data read
      Serial.print(" ");
    }
    Serial.println();     
          
    for (int i=0; i<tamanoBuffer-2 ;i++) //We check if the read value is equal to the password
    {
      if (clave[i]!=buffer[i])// si la clave no es igual al valor
      {
        //Serial.println("1 good value");           
        b=false;
        Serial.println("Access denied");
        digitalWrite(led_rojo, HIGH);  // turn on the red light
        digitalWrite(led_verde, LOW);  // turn off the green LED
        delay(2000);
        digitalWrite(led_rojo, LOW);  // We turn off the red light after 2 seconds
        break;
      }
    }
    if(b == true) //si b=true, if the values read are equal to those of the serial password show that applies comprobración
    {
      Serial.println("Access granted");
      digitalWrite(led_rojo, LOW);  // turn off the green LED
      digitalWrite(led_verde, HIGH);  // turn on the green LED
      delay(2000);
      digitalWrite(led_verde, LOW); //the green LED turn off after 2 seconds
    }
   
    Serial.println();
    Serial.println("Waiting for Next Card");

    mfrc522.PICC_HaltA();// It is terminated reading
    mfrc522.PCD_StopCrypto1();//The process ends
  }







I2C communication protocol

I2C communication protocol


I2C is a serial communications bus. Its name comes from Inter-Integrated Circuit (Inter-Integrated Circuit)

The Philips Design in 1992. It is a bus widely used in industry, mainly to communicate microcontroller and its peripherals in embedded systems and integrated generalizing more to communicate with each other that normally reside in the same PCB circuits. Some of the electronic equipment including integrated circuits with I2C bus are 24Cxx memories, signal processors TVs (LA7610, TA1223, DTC810, ...), video encoders DVD players (SAA 7128, TC 90A32F, ...), preamplifiers video monitors (KB 2502), etc.

Lines using the I2C bus to transmit information are:

• A data to serial data (SDA)

• Another to watch the serial clock signal (SCL)

• Third line is also needed, but this is only the reference (GND). As often communicate circuits in a same plate that share a same mass this third line usually not necessary.

• The first two lines are open drain, so they need pull-up resistors.

• The devices connected to the I2C bus have a unique address for each. They can also be masters or slaves. The master device initiates the transfer of data and also generates the clock signal, but it is not necessary that the teacher is always the same device, this feature can be switched the devices with that capability. The bus allows connection of multiple masters, since it includes a collision detector.


• Transactions on the I2C bus have this format:
| Home | 8 Bits Address | Reading o | Bit annealing | Condition repeated call
| Writing |
| Start | A7 A6 A5 A4 A3 A2 A1 R / W | ACK | ... DATA ... | ACK | stop | idle |




• The time displayed in milliseconds (ms 97 204) is the time it took to get the rating. The bus is free when SDA and SCL are logic high. Also keep in mind that the firmware takes about 3 seconds to boot the first time. Before a data exchange between the master circuit and the Slaves, the Master is established should inform the start of communication (Start condition) SDA line falls to zero while SCL remains high. From this point begins the data transfer

• The following is the S which is the bit of Star. The master starts the communication by sending a pattern called "start condition". This alerts the slave devices, putting them waiting for a transaction.

• Then it appears 90. This is the 7-bit address plus 1 bit dedicated to the function read or write (WRITE / READ). In this last bit will use 0 for writing and one for reading. The address sent is compared by each slave on the bus with its own address, if the two match, the slave addressed is considered a slave-slave-transmitter or receiver depending on the bit R / W.

Consider the 90.

9 out of binary hexadecimal number 1001 consists of the address bits (A7 (1), A6 (0), A5 (0) and A4 (1)) which are the standard DS 1621 is assigned the chip.

0 hexadecimal leaves the binary number 0000 comprises 3 bits of address and a fourth bit indicates whether you are writing or reading (A3 (0), A2 (0), A1 (0) and WRITE (0)) in this case the teacher is writing to the slave. If reading, as is later the teacher read and change the number to 0001 and the resulting number will be 91.


The A3, A2 and A1 bits are both 0 because they are connected to GND as shown in the image, if one were to be Vcc.



• The address sent is compared by each slave on the bus with its own address, if the two match, the slave addressed is considered a slave-slave-transmitter or receiver depending on the bit R / W.

• In the state free bus, any device can take the I2C bus as a teacher.

•.After 90 appears A. The slave responds by sending an ACK bit that tells the master device that the slave acknowledges the request and is able to communicate.

• Following the exchange of information between devices begins.

• The master sends the address of the internal register of the device you want to read or write. In the case of the previous image is AA, which is binary 10101010. Data byte 1: This is the first data byte itself as above we can not choose us and is imposed by the protocol. Here we can put the data we want in case of communication with remote sensing is to use a common registration number we want to write or read.

This step as many times as necessary repeats.

• The slave responds with another ACK bit.

• Now the teacher can begin to read or write data bytes. All data bytes must consist of 8 bits, the maximum number of bytes that can be sent in a transmission is not restricted, being the slave who determines this amount according to their characteristics.

• Each byte read / written by the teacher must necessarily be recognized by an ACK bit by the master / slave device.

• Repeat the previous 2 steps to end the communication between master and slave.

• Even if the teacher always monitors the status of the clock line, a slave of low speed or be stopped transferring data while performing another function, you can force the SCL line low. This makes the master into a wait state, during which transmits information not waiting for the slave is ready to continue the transfer at the point where it had been stopped.

• When communication is complete, the master will transmit a "stop condition" to free the bus (p letter at the end)

• After the "stop condition", it is mandatory for the bus to be idle (unemployed) for a few microseconds.




Image of the practice in Proteus



Image of the bus I2C



Image of the Real Time Clock



Image of the LCD




Image of the source code In Proteus