RFID-Controlled Light Switch
This light switch can be turned on and off using designated RFID tags.
Components and supplies
1
Arduino Nano R3
Project description
Code
RFID_light_switch.ino
arduino
1// define constants for pins 2 3#include <SoftwareSerial.h> 4 5int green_LED = 3; 6int RELAY = 5; 7int red_LED = 4; 8int white_LED = 12; 9 10SoftwareSerial ssrfid = SoftwareSerial(6,8); 11 12// variables to keep state 13int readVal = 0; // individual character read from serial 14unsigned int readData[10]; // data read from serial 15int counter = -1; // counter to keep position in the buffer 16char tagId[11]; // final tag ID converted to a string 17int ON_OFF = 2; // 18char* authorizedTags[10]; // array to hold the list of authorized tags 19 20void initAuthorizedTags() // fills the list of authorzied tags 21{ 22 // add your own tag IDs here 23 authorizedTags[0] = "1B00B2D95B"; //Tag 1 24 authorizedTags[1] = "0000000000"; //Tag 2 25 authorizedTags[2] = "0000000000"; //Tag 3 26 authorizedTags[3] = "0000000000"; //Tag 4 27 authorizedTags[4] = "0000000000"; //Tag 5 28 authorizedTags[5] = "0000000000"; //Tag 6 29 authorizedTags[6] = "0000000000"; //Tag 7 30 authorizedTags[7] = "0000000000"; //Tag 8 31 authorizedTags[8] = "0000000000"; //Tag 9 32 authorizedTags[9] = "0000000000"; //Tag 10 33} 34 35void setup() 36{ 37 Serial.begin(9600); 38 ssrfid.begin(9600); 39 ssrfid.listen(); 40 41 pinMode(green_LED, OUTPUT); 42 pinMode(RELAY, OUTPUT); 43 pinMode(red_LED, OUTPUT); 44 pinMode(white_LED, OUTPUT); 45 46 digitalWrite(green_LED, LOW); 47 digitalWrite(RELAY, LOW); 48 digitalWrite(red_LED, LOW); 49 digitalWrite(white_LED, HIGH); 50 51 initAuthorizedTags(); 52} 53 54int checkTag() // check if the tag ID we just read is any of the authorized tags 55{ 56 int i; 57 for (i = 0; i < 10; ++i) 58 { 59 if (strcmp(authorizedTags[i], tagId) == 0) 60 { 61 return 1; 62 } 63 } 64 return 0; 65} 66 67void parseTag() // convert the int values read from serial to ASCII chars 68{ 69 int i; 70 for (i = 0; i < 10; ++i) 71 { 72 tagId[i] = readData[i]; 73 } 74 tagId[10] = 0; 75} 76 77void processTag() // once a whole tag is read, process it 78{ 79 parseTag(); // convert id to a string 80 printTag(); // print it 81 if (checkTag() == 1) // check if the tag is authorized 82 { 83 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) 84 } 85 else 86 { 87 tagFailed(); // otherwise, inform user of failure 88 } 89} 90 91void printTag() 92{ 93 Serial.print("Tag value: "); 94 Serial.println(tagId); 95} 96 97void tagSuccess() // perform an action when an authorized tag was read 98{ 99 Serial.println("Tag authorized."); 100 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" 101 { 102 digitalWrite(green_LED, HIGH); 103 digitalWrite(RELAY, HIGH); 104 digitalWrite(white_LED, LOW); 105 digitalWrite(red_LED, LOW); 106 Serial.println("Light ON"); 107 delay(2000); 108 } 109 else // if ON_OFF is odd number means the relay (light) and the green LED is "on" and will go "off" 110 { 111 digitalWrite(green_LED, LOW); 112 digitalWrite(RELAY, LOW); 113 digitalWrite(white_LED, HIGH); 114 digitalWrite(red_LED, LOW); 115 Serial.println("Light OFF"); 116 delay(2000); 117 } 118 ON_OFF ++; // increment ON_OFF for next cicle 119} 120 121void tagFailed() // inform the user that the tag is not authorized 122{ 123 Serial.println("Unauthorized access!"); 124 digitalWrite(green_LED, LOW); // put the green LED low 125 digitalWrite(red_LED, HIGH); // put the red LED high, signal for unauthorized tag 126 delay(2000); 127 if ((ON_OFF % 2) != 0) 128 { 129 digitalWrite(green_LED, HIGH); // if tag fail and the light is on, green LED will be put back high 130 } 131} 132 133 134void clearSerial() // this function clears the rest of data on the serial, to prevent multiple scans 135{ 136 while (ssrfid.read() >= 0) 137 { 138 ; // do nothing 139 } 140} 141 142void loop() 143{ 144 // turn red LED off 145 digitalWrite(red_LED, LOW); 146 147 if (ssrfid.available() > 0) 148 { 149 readVal = ssrfid.read(); // read the incoming byte: 150 if (readVal == 2) // a "2" signals the beginning of a tag 151 { 152 counter = 0; // start reading 153 } 154 else if (readVal == 3) // a "3" signals the end of a tag 155 { 156 processTag(); // process the tag we just read 157 clearSerial(); // clear serial to prevent multiple reads 158 counter = -1; // reset reading state 159 } 160 else if (counter >= 0) // if we are in the middle of reading a tag 161 { 162 readData[counter] = readVal; // save valuee 163 ++counter; // increment counter 164 } 165 } 166} 167
RFID_light_switch.ino
arduino
1// define constants for pins 2 3#include <SoftwareSerial.h> 4 5int green_LED = 3; 6int RELAY = 5; 7int red_LED = 4; 8int white_LED = 12; 9 10SoftwareSerial ssrfid = SoftwareSerial(6,8); 11 12// variables to keep state 13int readVal = 0; // individual character read from serial 14unsigned int readData[10]; // data read from serial 15int counter = -1; // counter to keep position in the buffer 16char tagId[11]; // final tag ID converted to a string 17int ON_OFF = 2; // 18char* authorizedTags[10]; // array to hold the list of authorized tags 19 20void initAuthorizedTags() // fills the list of authorzied tags 21{ 22 // add your own tag IDs here 23 authorizedTags[0] = "1B00B2D95B"; //Tag 1 24 authorizedTags[1] = "0000000000"; //Tag 2 25 authorizedTags[2] = "0000000000"; //Tag 3 26 authorizedTags[3] = "0000000000"; //Tag 4 27 authorizedTags[4] = "0000000000"; //Tag 5 28 authorizedTags[5] = "0000000000"; //Tag 6 29 authorizedTags[6] = "0000000000"; //Tag 7 30 authorizedTags[7] = "0000000000"; //Tag 8 31 authorizedTags[8] = "0000000000"; //Tag 9 32 authorizedTags[9] = "0000000000"; //Tag 10 33} 34 35void setup() 36{ 37 Serial.begin(9600); 38 ssrfid.begin(9600); 39 ssrfid.listen(); 40 41 pinMode(green_LED, OUTPUT); 42 pinMode(RELAY, OUTPUT); 43 pinMode(red_LED, OUTPUT); 44 pinMode(white_LED, OUTPUT); 45 46 digitalWrite(green_LED, LOW); 47 digitalWrite(RELAY, LOW); 48 digitalWrite(red_LED, LOW); 49 digitalWrite(white_LED, HIGH); 50 51 initAuthorizedTags(); 52} 53 54int checkTag() // check if the tag ID we just read is any of the authorized tags 55{ 56 int i; 57 for (i = 0; i < 10; ++i) 58 { 59 if (strcmp(authorizedTags[i], tagId) == 0) 60 { 61 return 1; 62 } 63 } 64 return 0; 65} 66 67void parseTag() // convert the int values read from serial to ASCII chars 68{ 69 int i; 70 for (i = 0; i < 10; ++i) 71 { 72 tagId[i] = readData[i]; 73 } 74 tagId[10] = 0; 75} 76 77void processTag() // once a whole tag is read, process it 78{ 79 parseTag(); // convert id to a string 80 printTag(); // print it 81 if (checkTag() == 1) // check if the tag is authorized 82 { 83 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) 84 } 85 else 86 { 87 tagFailed(); // otherwise, inform user of failure 88 } 89} 90 91void printTag() 92{ 93 Serial.print("Tag value: "); 94 Serial.println(tagId); 95} 96 97void tagSuccess() // perform an action when an authorized tag was read 98{ 99 Serial.println("Tag authorized."); 100 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" 101 { 102 digitalWrite(green_LED, HIGH); 103 digitalWrite(RELAY, HIGH); 104 digitalWrite(white_LED, LOW); 105 digitalWrite(red_LED, LOW); 106 Serial.println("Light ON"); 107 delay(2000); 108 } 109 else // if ON_OFF is odd number means the relay (light) and the green LED is "on" and will go "off" 110 { 111 digitalWrite(green_LED, LOW); 112 digitalWrite(RELAY, LOW); 113 digitalWrite(white_LED, HIGH); 114 digitalWrite(red_LED, LOW); 115 Serial.println("Light OFF"); 116 delay(2000); 117 } 118 ON_OFF ++; // increment ON_OFF for next cicle 119} 120 121void tagFailed() // inform the user that the tag is not authorized 122{ 123 Serial.println("Unauthorized access!"); 124 digitalWrite(green_LED, LOW); // put the green LED low 125 digitalWrite(red_LED, HIGH); // put the red LED high, signal for unauthorized tag 126 delay(2000); 127 if ((ON_OFF % 2) != 0) 128 { 129 digitalWrite(green_LED, HIGH); // if tag fail and the light is on, green LED will be put back high 130 } 131} 132 133 134void clearSerial() // this function clears the rest of data on the serial, to prevent multiple scans 135{ 136 while (ssrfid.read() >= 0) 137 { 138 ; // do nothing 139 } 140} 141 142void loop() 143{ 144 // turn red LED off 145 digitalWrite(red_LED, LOW); 146 147 if (ssrfid.available() > 0) 148 { 149 readVal = ssrfid.read(); // read the incoming byte: 150 if (readVal == 2) // a "2" signals the beginning of a tag 151 { 152 counter = 0; // start reading 153 } 154 else if (readVal == 3) // a "3" signals the end of a tag 155 { 156 processTag(); // process the tag we just read 157 clearSerial(); // clear serial to prevent multiple reads 158 counter = -1; // reset reading state 159 } 160 else if (counter >= 0) // if we are in the middle of reading a tag 161 { 162 readData[counter] = readVal; // save valuee 163 ++counter; // increment counter 164 } 165 } 166} 167
Downloadable files
Schematic
Schematic

Schematic
Schematic

Documentation
Schematic
Target 3001 format
Schematic
Comments
Only logged in users can leave comments