RFID-Controlled Light Switch

RFID-Controlled Light Switch

This light switch can be turned on and off using designated RFID tags.

  • 1,242 views
  • 1 comment
  • 9 respects

Components and supplies

About this project

This is a light swtich (but can be used for many other things) which can be turned ON and OFF using dedicated 125 kHz RFID tags. In this case, 10 different tags (users) can be used.

I have a underground room in the commune space in my bloc (building) and to access this space I powered up a light system from my apartment. The switch for the light is on the entrance in the cellar and all my neighbors can access that space, and some of them put the light ON "in purpose" only to increase my electricity bill :)). After a while, I decided to do something to stop them doing this. Because the building has a Electra access system using 125 kHz tags with unique numbers I decided to use this tag because is attached to my keys all the time.

I used for this project a Arduino Nano, RFID reader RDM 6300, one 5V relay, one old Nokia charger and other few parts.

The program is not very hard to understand and have explanations for majority of the lines.

In OFF mode the white LED is ON (to be visible the box because is installed in a dark zone). When a authorized tag is putted in front of antenna the white LED goes OFF, green LED and relay goes ON and the light is starting. If a unauthorized tag is used the white LED is going OFF, red LED is coming ON for two seconds and after the white LED is coming ON again, the relay remain OFF all the time.

To put OFF the light you need to put again close to antenna one authorized tag, green LED become OFF, same the relay and the white LED become ON. If one unauthorized tag is present, when the light is ON, the green LED is going OFF and the red LED is going ON for two seconds after the green LED is ON again, nothing happen with the relay, the light remain on during this time.

After the light is ON you need to wait minimum two seconds before you can put again the tag near antenna and get the light OFF. This cycle can be repeated again and again and again...

Code

RFID_light_switch.inoArduino
// define constants for pins

#include <SoftwareSerial.h>

int green_LED = 3;
int RELAY = 5;
int red_LED = 4;
int white_LED = 12;

SoftwareSerial ssrfid = SoftwareSerial(6,8);

// variables to keep state
int readVal = 0;              // individual character read from serial
unsigned int readData[10];    // data read from serial
int counter = -1;             // counter to keep position in the buffer
char tagId[11];               // final tag ID converted to a string
int ON_OFF = 2;               // 
char* authorizedTags[10];     // array to hold the list of authorized tags

void initAuthorizedTags()     // fills the list of authorzied tags
{
  // add your own tag IDs here
  authorizedTags[0] = "1B00B2D95B"; //Tag 1
  authorizedTags[1] = "0000000000"; //Tag 2  
  authorizedTags[2] = "0000000000"; //Tag 3
  authorizedTags[3] = "0000000000"; //Tag 4
  authorizedTags[4] = "0000000000"; //Tag 5
  authorizedTags[5] = "0000000000"; //Tag 6
  authorizedTags[6] = "0000000000"; //Tag 7
  authorizedTags[7] = "0000000000"; //Tag 8
  authorizedTags[8] = "0000000000"; //Tag 9
  authorizedTags[9] = "0000000000"; //Tag 10
}

void setup()
{                
  Serial.begin(9600);
  ssrfid.begin(9600);
  ssrfid.listen(); 
  
  pinMode(green_LED, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(red_LED, OUTPUT);
  pinMode(white_LED, OUTPUT);
  
  digitalWrite(green_LED, LOW);
  digitalWrite(RELAY, LOW);
  digitalWrite(red_LED, LOW);
  digitalWrite(white_LED, HIGH);
  
  initAuthorizedTags();
}

int checkTag()             // check if the tag ID we just read is any of the authorized tags
{
  int i;
  for (i = 0; i < 10; ++i)
  {
    if (strcmp(authorizedTags[i], tagId) == 0)
    {
      return 1;
    }
  }
  return 0;
}

void parseTag()            // convert the int values read from serial to ASCII chars
{
  int i;
  for (i = 0; i < 10; ++i)
  {
    tagId[i] = readData[i];
  }
  tagId[10] = 0;
}

void processTag()         // once a whole tag is read, process it
{
  parseTag();             // convert id to a string
  printTag();             // print it
  if (checkTag() == 1)    // check if the tag is authorized
  {
    tagSuccess();         // if so, relay and green LED will be turned from OFF to ON or from ON to OFF, depend the value of ON_OFF variable (odd or even)
  } 
  else
  {
    tagFailed();          // otherwise, inform user of failure
  }
}

void printTag()
{
  Serial.print("Tag value: ");
  Serial.println(tagId);
}

void tagSuccess()                     // perform an action when an authorized tag was read
{
  Serial.println("Tag authorized.");
  if ((ON_OFF % 2) == 0)              // if ON_OFF is even number means the relay (light) and the green LED is "off" and will go "on"
  {
  digitalWrite(green_LED, HIGH);
  digitalWrite(RELAY, HIGH);
  digitalWrite(white_LED, LOW);
  digitalWrite(red_LED, LOW);
  Serial.println("Light ON");
  delay(2000);
  }
    else                              // if ON_OFF is odd number means the relay (light) and the green LED is "on" and will go "off"
  {
  digitalWrite(green_LED, LOW);
  digitalWrite(RELAY, LOW);
  digitalWrite(white_LED, HIGH);
  digitalWrite(red_LED, LOW);
  Serial.println("Light OFF");
  delay(2000);
  }
  ON_OFF ++;                          // increment ON_OFF for next cicle
}

void tagFailed()                          // inform the user that the tag is not authorized
{
  Serial.println("Unauthorized access!");
  digitalWrite(green_LED, LOW);           // put the green LED low
  digitalWrite(red_LED, HIGH);            // put the red LED high, signal for unauthorized tag
  delay(2000);
  if ((ON_OFF % 2) != 0)
  {
    digitalWrite(green_LED, HIGH);        // if tag fail and the light is on, green LED will be put back high
  }
}


void clearSerial()                  // this function clears the rest of data on the serial, to prevent multiple scans
{
  while (ssrfid.read() >= 0) 
  {
    ;                               // do nothing
  }
}

void loop() 
{
  // turn red LED off
  digitalWrite(red_LED, LOW);

  if (ssrfid.available() > 0) 
  {
    readVal = ssrfid.read();        // read the incoming byte:
    if (readVal == 2)               // a "2" signals the beginning of a tag
    {
      counter = 0;                  // start reading
    } 
    else if (readVal == 3)          // a "3" signals the end of a tag
    {
     processTag();                  // process the tag we just read
     clearSerial();                 // clear serial to prevent multiple reads
     counter = -1;                  // reset reading state
    }
    else if (counter >= 0)          // if we are in the middle of reading a tag
    {
     readData[counter] = readVal;   // save valuee
     ++counter;                     // increment counter
    } 
  }
}

Custom parts and enclosures

Schematic
Target 3001 format
rfid_light_switch_ElXJusjKEb.T3001

Schematics

Schematic
Schematic xhtffyychc

Comments

Author

Stoica c florin
stoica_c_florin
  • 1 project
  • 3 followers

Additional contributors

Published on

April 30, 2019

Members who respect this project

PhotoProfilepicDefaultDefaultDefaultDefaultDefault

and 2 others

See similar projects
you might like

Similar projects you might like

Bluetooth Controlled Light Switch

Project showcase by Team The Instrumentation Crustaceans

  • 23,402 views
  • 4 comments
  • 26 respects

IR Remote Controlled Power Switch for Rapsberry Pi 3

Project tutorial by Vilém Kužel

  • 1,084 views
  • 1 comment
  • 5 respects

BT Controlled Light (using BLE Shield 2.1)

Project tutorial by Arduino_Scuola

  • 1,135 views
  • 0 comments
  • 1 respect

How to control your light switch from your smartphone?

Project in progress by Mahmoud Ahmed

  • 23,468 views
  • 3 comments
  • 15 respects

Control Your Light Switch with Your Smartphone

Project tutorial by Alasdair Allan

  • 14,932 views
  • 1 comment
  • 33 respects

Arduino Controlled Lock Box with Solenoid and RFID

Project tutorial by Team RobotGeek Projects Team

  • 6,203 views
  • 3 comments
  • 20 respects
Add projectSign up / Login