Arduino Card Swipe Switch Project

Card Swipe Switch Project

This Arduino card swipe switch project is based on the standard card swipe system that is typically used for home security systems, secured access doorways or visitor ID registration systems. When the sensor detects the presence of a card, it will trigger the LED to change colour from red (default state) to blue temporarily and then to green, indicating a successful card swipe. Of course, in commercial applications, an LED as the output of such a system can be replaced by magnetically-locked doors, registration software/procedures, etc. To achieve this, the HW-487 photo interruption sensor module is used in order to detect the presence of a card.

This simple sensor module is built with an optical emitter that sends out light and an optical detector that will receive the light. When an opaque object/item (e.g. card) is in the way of that light path, the detector will send a HIGH output signal, basically acting as a switch. As the output module for this project, an HW-479 RGB LED module is used, which offers the capability of displaying all the colours of the visible spectrum due to the module essentially integrating three coloured LEDs that can be analogue-controlled. For this project, the RGB LED module will be illuminated in blue, red and green colours by lighting up each of the three LEDs individually.

However, one of the advantages of an RGB LED is that you can individually program each of the three LEDs to a certain hue value through code, displaying whatever colour you would like. An additional advantage of an RGB LED module is that its wiring is simple as each of the three coloured-LED pins can be directly connected to an Arduino digital or analogue output pin, allowing for full control of the separate LEDs. If you want to DIY this project, you will need to source components:

  • Arduino Nano (other Arduino-compatible boards will work)
  • USB cable (compatible with the Arduino board)
  • Breadboard
  • Male-Male Jumper Wires (7)
  • HW-487 Photo Interruption Sensor Module
  • HW-479 RGB LED Module (common cathode)
  • 220Ω resistors (3)

How to wire the card swiping switch

Depending on your Arduino board, you may or may not require a breadboard to plug your board in. In this example, an Arduino Nano is used, thus requiring a breadboard but if you are using an Arduino Uno, for example, the jumper wires can be plugged in from the components on the breadboard directly to the board pins.

The wiring for card swiping switch is fairly simple as aside from the main modules, not a lot of extra components are needed. In terms of the photo interruption sensor touch sensor module, the HW-487 sensor module that is featured in this project has a very simple 3-pin configuration where the signal pin sends a digital output signal to the Arduino whenever the light path is blocked (e.g. by a card). In terms of the HW-479 RGB LED module, the reason that 220Ω resistors are connected in series with each of the three LED outputs from this module is to prevent the LEDs from burning out when applying a supply voltage of +5 volts. The following is the circuit wiring diagram of the Card Swipe Switch provided by FS Technology.

  • 1.HW-487 Photo Interruption Sensor Module: Connect the signal (S) pin to D2 on your Arduino board, the positive (+) pin to +5v and the GND (G) pin to GND.
  • 2.HW-479 RGB LED Module: Insert one 220Ω resistor in the breadboard in series with the output pin for the red colour LED signal (R), insert another 220Ω resistor in series with the output pin for the green colour LED signal (G) and insert the last 220Ω resistor in series with the output pin for the blue colour LED signal (B). Connect the red colour output pin (R) to D3, the green colour output pin (G) to D4 and the blue colour output pin (B) to D5. Connect the negative (-) pin to GND on your Arduino.
  • 3.Now, you can plug in your Arduino board via the USB cable to the computer.
Card Swipe Switch Wiring

Project Code

int sensorPin = 2;

int redPin = 3, greenPin = 4, bluePin = 5;

boolean val = 0;

 

void setup(){

 pinMode(sensorPin, INPUT);

 pinMode(redPin, OUTPUT);

 pinMode(greenPin, OUTPUT);

 pinMode(bluePin, OUTPUT);

}

 

void loop (){

 val = digitalRead(sensorPin);

 

 if (val == HIGH) {

 digitalWrite(redPin, LOW);

 digitalWrite(greenPin, LOW);

 digitalWrite(bluePin, HIGH);

 delay(1000);

 digitalWrite(bluePin, LOW);

 digitalWrite(greenPin, HIGH);

 delay(5000);

}

else {

 digitalWrite(redPin, HIGH);

 digitalWrite(greenPin, LOW);

 digitalWrite(bluePin, LOW);

}

}

About the code

  • The first section of the code starts by defining several integer variables which correspond with the pins connected to the HW-487 Photo Interruption Sensor Module and the HW-479 RGB LED module. The signal/output pin of the HW-487 sensor is assigned to D2 and named ‘sensorPin’ whereas, for the HW-479 module, we assign three separate variables to the three pins connected to each of the LEDs within the RGB LED package. For simplicity, each of the assigned pin names matches the LED colours on the RGB LED module accordingly. The last line of this section is where we declare a boolean variable named ‘val’ and set it to 0 (LOW). This boolean variable will be later used to store the output signal that comes from the HW-487 sensor.
  • In the void setup section, the pin modes for each of the previously-declared pins are declared as either an input or output pin. The HW-487 sensor, named ‘sensorPin’, is declared as an input device since information from the sensor is being sent to the Arduino board to be processed. When the card is swiped, the sensor sends a HIGH/1 signal back to the Arduino but otherwise, the sensor sends back a LOW/0 signal. On the other hand, the HW-479 RGB LED module is set as an output device since the Arduino controls its digital output state (whether the LED is on/off).
  • Moving onto the void loop section, the boolean variable that was declared earlier is now assigned to whatever the digital output value is of the HW-487 sensor. This is done through the use of a digitalRead() function to capture the output value from the sensor. Next, an if-else statement is introduced which states that whenever the HW-487 sensor sends back a HIGH value (i.e. a card is swiped), the blue LED will switch on for one second and then the green LED will switch on for five seconds. However, if the sensor sends back a LOW value (default state), the red LED will only be switched on. 
  • During the writing of the code, it was important that when one coloured LED is switched on, all the other LEDs are switched off (LOW). Otherwise, other colour combinations can be made by illuminating multiple LEDs at once due to the RGB LED module integrating three separate LEDs into one package.

Next steps

After completing the project, you could start brainstorming some ideas on how you can utilize the concepts and/or components of this project and apply them to other useful applications. Although the output for the photo interruption sensor module is the RGB LED module, another standard output device that could be used is a relay, which can be connected in series with consumer devices PCBA (e.g. lightbulb, speaker, wireless device etc). A lot of commercial-grade card readers now are quite compact in size and this is mainly due to the use of PCBA. In terms of transforming your project from being on a breadboard to a smaller, more professional product, the components (both through-hole and surface mount) used can be soldered onto a PCB board and assembled. This not only enhances the visual aspects of the circuitry but also the mechanical & electrical stability, connectivity and reliability of the entire project. Therefore, as a step further, fabricating a custom PCB and then circuit assembly it is a factor that an electronic hobbyist should consider.

DIY Electronics Project Blog

Arduino UNO Interrupt Timer

Arduino UNO Interrupt Timer The Timer is a feature embedded in every microcontroller and has certain functions regarding the passage of time. The Timer function

Read More »