Kutluhan AktarJLCPCB
Published © CC BY

RFID Desk Lamp with RGB Color Scheme Lock

Enhance a dilapidated desk lamp w/ insipid features into an RFID enabled one w/ personalized RGB color scheme lock via Arduino.

IntermediateFull instructions provided1 hour3,483
RFID Desk Lamp with RGB Color Scheme Lock

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
SparkFun MFRC522 RFID Module
×1
Adafruit 2-Way Relay
×1
RFID Key or Card
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×3
Conductive Potentiometer, Knob
Conductive Potentiometer, Knob
×3
RGB Diffused Common Anode
RGB Diffused Common Anode
×1
5 mm LED: Green
5 mm LED: Green
×1
Resistor 220 ohm
Resistor 220 ohm
×4
Jumper wires (generic)
Jumper wires (generic)
×1
JLCPCB Customized PCB
JLCPCB Customized PCB
×1

Software apps and online services

Arduino IDE
Arduino IDE
KiCad
KiCad

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Custom parts and enclosures

Gerber Files

Fabrication Files

Schematics

PCB_1

PCB_2

PCB_3

Code

RFID_Desk_Lamp_with_RGB_Lock.ino

Arduino
         /////////////////////////////////////////////  
        //           RFID Desk Lamp                //
       //       with RGB Color Scheme Lock        //
      //          ---------------------          //
     //             (Arduino Nano)              //           
    //            by Kutluhan Aktar            // 
   //                                         //
  /////////////////////////////////////////////

// Enhance a dilapidated desk lamp w/ insipid features into an RFID enabled one w/ personalized RGB scheme lock via Arduino.
//
// You can register a new UID with the code below (registerUID) by turning it into uncommented.
//
// By using three potentiometers, you can adjust the RGB color scheme.
//
// In this version, for activating the LOCK, you have to reduce all potentiometer values to zero (black). But, if you want to use a different pattern, you can change it in the UID() function.  
//
// UNLOCK :
// When the current UID read by the RFID module is accurate and thus the controlLed is HIGH.
//
// LOCK :
// Red Potentiometer Value = 0
// Green Potentiometer Value = 0
// Blue Potentiometer Value = 0
//
//
// Connections
// Arduino Nano :           
//                            MFRC522
// D9  ----------------------- RST
// D10 ----------------------- SDA
// D11 ----------------------- MOSI
// D12 ----------------------- MISO
// D13 ----------------------- SCK
//                            5mm Green LED (controlLed)
// D2  -----------------------
//                            RGB
// D3  -----------------------
// D6  -----------------------
// D5  -----------------------
//                            2-Way Relay
// D7  ----------------------- IN_2
// D8  ----------------------- IN_1
//                            Potentiometer (Red)
// A0 -----------------------
//                            Potentiometer (Green)
// A1 -----------------------
//                            Potentiometer (Blue)
// A2 -----------------------

/*

   Typical pin layout used:
   -----------------------------------------------------------------------------------------
               MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
               Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
   Signal      Pin          Pin           Pin       Pin        Pin              Pin
   -----------------------------------------------------------------------------------------
   RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
   SPI SS      SDA(SS)      10            53        D10        10               10
   SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
   SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
   SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15

*/

#include <EEPROM.h>   // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h>      // RC522 Module uses SPI protocol
#include <MFRC522.h>  // Library for Mifare RC522 Devices

// Create the MFRC522 instance.
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

// Define the MFRC522 module key input.
MFRC522::MIFARE_Key key;

// Define the readCard byte array which stores the scanned ID read by RFID Module.
byte readCard[4]; 

// Define the data holders:
String savedUID;
String lastRead;
String successUID;
int red;
int green;
int blue;

// Define the LOCK boolean to either set or remove the LOCK.
boolean LOCK = true;

// Define RGB pins.
#define redPin 3
#define greenPin 6
#define bluePin 5
// Define the controlLed pin.
#define controlLed 2
// Define the relay pins (if needed, you can use both pins on the relay).
#define IN_1 8
#define IN_2 7
// Define potentiometer pins.
#define pot_r A0
#define pot_g A1
#define pot_b A2

void setup() {
  
  Serial.begin(9600); 
  
  // Initialize MFRC522 Hardware
  SPI.begin();          
  mfrc522.PCD_Init();    

  // If you have not registered a UID to the EEPROM yet, upload the code after turning these lines uncommented:
  
  /*
  // Save the new card or key tag UID to the EEPROM. But, do not forget it only has 1KB memory.
  Serial.print("Approximate the new card or key tag to scan and register the new UID.\n");
  do{
  // Wait for the new card reading process.
  successUID = registerCardUID();
  }while(!successUID);
  */
  
  // Get the saved UID in the EEPROM.
  get_saved_UID();
  Serial.print("UID is received from EEPROM :\n----------------------------------\n");
  Serial.print(savedUID);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(controlLed, OUTPUT);
  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT);
}

void loop(){
  
  // Turn relay and controlLed off.
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, HIGH);
  digitalWrite(controlLed, LOW);
  
  // Remove the LOCK if the current UID read by the RFID module is accurate. Or, set it if the RGB Color Scheme Lock is given.
  UID();

}

int UID(){
  // Get the last UID from MFRC522.
  if ( ! mfrc522.PICC_IsNewCardPresent()){
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()){
    return;
  }
  for(int i=0;i<mfrc522.uid.size;i++){
    lastRead += mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ";
    lastRead += String(mfrc522.uid.uidByte[i], HEX);
  }
  
  // Arrange the lastRead for comparison.
  lastRead.trim();
  lastRead.toUpperCase();

  
  // Remove the LOCK if the UID is accurate.
  if(lastRead == savedUID){
    LOCK = false;
    while(LOCK == false){
      // Get potentiometer data from 0 to 255.
      readPotentiometer();
      // Adjust RGB led colors in regard to potentiometer values.
      adjustColor(red, green, blue);
      // Turn relay and controlLed on.
      digitalWrite(controlLed, HIGH);
      digitalWrite(IN_1, LOW);
      // Set the LOCK.
      if(red == 0 && green == 0 && blue == 0){
        LOCK = true;
        // Blank the lastRead.
        lastRead = "";
        }
      }
    }
}

int readPotentiometer(){
  red = map(analogRead(pot_r), 0, 1023, 0, 255);
  green = map(analogRead(pot_g), 0, 1023, 0, 255);
  blue = map(analogRead(pot_b), 0, 1023, 0, 255);
}

void adjustColor(int r, int g, int b){
  r = 255 - r;
  g = 255 - g;
  b = 255 - b;
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
}

int registerCardUID() {
  // Detect the new card UID. 
  if ( ! mfrc522.PICC_IsNewCardPresent()) { 
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }

  // Display the new UID.
  Serial.print("\n----------------------------------\nNew Card or Key Tag UID : ");
  for (int i = 0; i < mfrc522.uid.size; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.print("\n----------------------------------\n");
  
  // Save the new UID to EEPROM. 
  for ( int i = 0; i < mfrc522.uid.size; i++ ){
   EEPROM.write(i, readCard[i] );
  }
  Serial.print("UID is saved successfully to the EEPROM.\n");
  
  // If the card registering process is successful, return 1 and end the reading process.
  mfrc522.PICC_HaltA();
  return 1;
}

int get_saved_UID(){
  // Get the saved UID in the EEPROM.
  for(int i=0;i<4;i++){
    savedUID += EEPROM.read(i) < 0x10 ? " 0" : " ";
    savedUID += String(EEPROM.read(i), HEX);
    }
  // Arrange the savedUID for comparison.
  savedUID.trim();
  savedUID.toUpperCase();
}

Credits

Kutluhan Aktar

Kutluhan Aktar

79 projects • 291 followers
Self-Taught Full-Stack Developer | @EdgeImpulse Ambassador | Maker | Independent Researcher
JLCPCB

JLCPCB

68 projects • 38 followers
JLCPCB, is the largest PCB and PCB Assembly prototype enterprise in Asia. Coupon code "JLCPCBcom" for all and permanantly available.

Comments