Tag Archives: Arduino

How to use the RFID-RC522 module (RFID reader) with the Arduino Uno

Setup of this tutorial: RFID reader and two RFID tags.

RFID stands for Radio-frequency identification and can be used for many application that require an identification mechanism. In this tutorial, it is shown how to use the RFID-RC522 module with the Arduino Uno. The RFID-RC522 module is an RFID reader, which can read RFID tags in close-range. In order to read an RFID tag, the reader and the tag must have the same frequency. The RFID-RC522 module reads only High frequency tags at 13.56 MHz.
In order to demonstrate the RFID-RC522 module, a simple application is programmed which identifies a user based on an RFID tag.

List of materials:
– Arduino Uno [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– Jumper wires (female/male) [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– RFID-RC522 module [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– RFID tags [Search on Amazon.com | Amazon.de | Amazon.co.uk]

How to connect the RFID-RC522 to the Arduino?

Schematic of how to connect the RFID-RC522 to the Arduino.

The RFID-RC522 module comes with eight pins (of which seven pins are used in this tutorial). In the following, the pin layout is shown as recommended by the documentation of the MFRC522 library (used later in the programming section). The RFID-RC522 module runs with 3.3V. Therefore, the module’s 3.3V pin must be connected to the Arduino’s 3.3V. The module might get damaged, if it is accidentally connected to the Arduino’s 5V pin. The complete pin layout is shown by the following table (works only for the Arduino Uno! Mega, Nano etc. have different recommended pin layouts!):

RFID-RC522 Pin Arduino Uno Pin
SDA 10
SCK 13
MOSI 11
MISO 12
IRQ UNUSED
GND GND
RST 9
3.3V 3.3V

How to program the RFID reader?
The code makes use of an existing RFID library which can be found here: https://github.com/miguelbalboa/rfid. If you are using the Arduino IDE, the library can be easily installed by clicking “Sketch->Include Library->Manage Libraries”. Then, search for “RFID” and you will find an entry with the title “MFRC522 by GithubCommunity”. If you cannot find such an entry, I recommend to visit the official website of the library for further assistance. In order to get the RFID reader running, you have to setup the library itself, a SPI connection, as well as the reader. Luckily, the library offers two very handy convenience functions for reading tags: The first functions allows to detect whether an RFID tag/card is present. The second function reads the RFID tag id. In the following code, a for-loop is used to iterate through the tag. In each iteration, a single byte is read and printed out as hexadecimal value (00..FF). Such an mechanism can be used to identify users, when each user owns a different RFID tag.

// (c) Michael Schoeffler 2014, http://www.mschoeffler.de

#include <SPI.h> // SPI library 
#include <MFRC522.h> // RFID library (https://github.com/miguelbalboa/rfid)

const int pinRST = 9;
const int pinSDA = 10;

MFRC522 mfrc522(pinSDA, pinRST); // Set up mfrc522 on the Arduino

void setup() {
  SPI.begin(); // open SPI connection
  mfrc522.PCD_Init(); // Initialize Proximity Coupling Device (PCD)
  Serial.begin(9600); // open serial connection 
}

void loop() {
  if (mfrc522.PICC_IsNewCardPresent()) { // (true, if RFID tag/card is present ) PICC = Proximity Integrated Circuit Card
    if(mfrc522.PICC_ReadCardSerial()) { // true, if RFID tag/card was read
      Serial.print("RFID TAG ID:"); 
      for (byte i = 0; i < mfrc522.uid.size; ++i) { // read id (in parts)
        Serial.print(mfrc522.uid.uidByte[i], HEX); // print id as hex values
        Serial.print(" "); // add space between hex blocks to increase readability
      }
      Serial.println(); // Print out of id is complete. 
    }
  }
}

If the code has been successfully uploaded to the Arduino and an RFID card or tag is in close-range to the RFID reader, the output on the Serial Monitor (Tools->Serial Monitor, Ctrl+Shift+M) should look like this (picture shows position of the RFID card as well as the corresponding serial monitor output):

RFID reader is reading the ID of an RFID card. In addition, the serial monitor output of the program is shown.

Video Tutorial:

____________________

How to build an optical/light barrier with the Arduino, the Keyes laser module (KY-008) and a laser receiver/detector module

Light barrier built with the Arduino, a laser module, and a laser detector module.

In this tutorial, it shown how to build an optical barrier. The barrier detects any movement between the sender and the receiver. The sender is a laser module (Keyes KY-008) that emits red light (wavelength: 650nm). The laser detector/receiver is an unnamed module that returns either a LOW signal or a HIGH signal (no “analog values” in between).

List of materials:

– Arduino Uno [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– Jumper wires (female/male) [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– KY-008 red laser module [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– Unnamed laser detector/receiver [Search on Amazon.com | Amazon.de | Amazon.co.uk]

Schematic of a laser barrier.

How to connect the laser module and laser detector to the Arduino?


The laser module has three pins. Only two pins are used: The “-” pin is connected to one of the Arduino’s GND pins. The “S” pin of the laser module is connected to the Arduino’s pin “2”. Therby, the laser can be switched off or on by sending either a LOW signal or a HIGH signal. The detector module has also three pins. The module’s “VCC” pin is connected to the Arduino’s “5V” pin and the GND of the module is connected to the Arduino’s GND. Lastly, the signal pin”OUT” of the module is connected to pin “3”.

How to program the light/optical barrier?
First, the pin values for the laser and the receiver are initialized. Next, the laser pin is set to OUTPUT mode and the receiver pin is set to INPUT mode. Then, the laser is switched on by setting “pinLaser” to HIGH. In order to monitor the light barrier’s state, a serial connection is started.
In the loop function, the value of the receiver is read. The retrieved value can either be LOW or HIGH. LOW means that the laser does not reach the detector, e.g., something went through the light barrier and is located between the laser and the detector. Then, the value of the detector is written to the console. Finally, a delay of 1000ms is added for convenience purposes (serial monitor is not “flashing” the values).

// (c) Michael Schoeffler 2014, http://www.mschoeffler.de

const int pinLaser = 2; // output signal pin of laser module/laser pointer
const int pinReceiver = 3; // input signal pin of receiver/detector (the used module does only return a digital state)


void setup() {
  pinMode(pinLaser, OUTPUT); // set the laser pin to output mode
  pinMode(pinReceiver, INPUT); // set the laser pin to output mode
  digitalWrite(pinLaser, HIGH); // emit red laser
  Serial.begin(9600); // Setup serial connection for print out to console
}

void loop() {
  int value = digitalRead(pinReceiver); // receiver/detector send either LOW or HIGH (no analog values!)
  Serial.println(value);  // send value to console
  delay(1000); // wait for 1000ms
}

If the code has been successfully uploaded to the Arduino, the output on the Serial Monitor (Tools->Serial Monitor, Ctrl+Shift+M) should look like this:

Serial monitor output of the light barrier example. If something is inside the light barrier, “0” is printed out to the monitor. Otherwise, if the red laser light points to the detecctor, “1” is printed out.

Remarks:
The detector’s LED, which is located next to the detector, indicates whether the detector is connected to a power supply.

Video Tutorial:

____________________

How to blink an LED using an Arduino Uno

An Arduino (Uno) can be used to blink an LED. In this tutorial, an LED is controlled which changes its light status every 1000ms.

List of materials:
– Arduino Uno [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– Jumper wires (female/male) [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– Breadboard [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– LED [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– resistor [Search on Amazon.com | Amazon.de | Amazon.co.uk]

Picture of how to connect an Arduino Uno to an LED.

LEDs have a forward voltage [V] and a forward current [mA]. Typically, the numbers of these two values can be found in the spec of the LED type. The LED used in this tutorial has a forward voltage of 2.2V and a forward current of 30mA. As the Arduino delivers 5V, 2.8V (5V-2.2V) have to be absorbed by a resistor. According to Ohm’s Law R=V/I, a resistor with 93.3 ohms (2.8V/0.03A)  is needed. Since one typically does not have an resistor with exactly 93.3 ohms, the next “higher” one is used. Therefore, a 150 ohms resistor is used in this tutorial. If the forward voltage/current is not known, the use of a 1k ohms resistor is safe in most cases.

How to connect the LED to the Arduino?

Scheme of how to connect an Arduino Uno to an LED.

The Arduino’s GND pin is connected to the LED’s cathode (usually the short leg). One of the resistor’s pins is connected to the LED’s anode (long leg). The other pin of the resistor is connected to the Arduino’s pin “2”. As current flows only in one direction through the LED, the LED can not be connected the other way around. Resistors can be connected the other way around. Moreover, it does not matter whether the resistor is wired between GND and LED’s cathode or between pin “2” and LED’s anode.

How to program a blinking LED?
First, a variable is defined which stores the information that the LED is controlled with pin “2” (outLED). In addition, another variable is needed, which represents the LED’s state. As the state will be toggled (on, off, on, off,…), the variable is named “toggle”. In the setup function, pin “2” is set to output mode. In the loop function, toggle is inverted by use of the “!”-operator. The “!” is very helpful to describe a blinking state in one line of code. Next, the state stored in “toggle” is written to pin “2”. Finally, a delay of 1000ms is added in order to be able to perceive the blinking effect.

// (c) Michael Schoeffler 2014, http://www.mschoeffler.de

const int outLED = 2; // output pin for LED
boolean toggle = LOW; // variable that is used to store the pin's state (switched on/switched off)

void setup() {
  pinMode(outLED, OUTPUT); // set the LED pin to output mode
}

void loop() {
  toggle = !toggle; // change the state of the LED. If the state represents LOW, then change state to a HIGH value (done by "!" operator) and the outher way around.
  digitalWrite(outLED, toggle); // write state variable to LED pin
  delay(1000); // wait for 1000ms in order to achieve a blinking LED effect
}


Video Tutorial

_________________________

How to use an analog joystick (KY-023) with an Arduino Uno

KY-023 Analog Joystick.

The KY-023 (or KY-23) is an easy-to-use analog joysticks for the Arduino microcontroller. The joysticks features two dimensions (x and y axis) as well as a state that is set when the joystick is pressed. In this tutorial, it is shown how to connect the KY-023 joystick with the Arduino and how to use it.

List of materials:
– Arduino Uno [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– Jumper wires (female/male) [Search on Amazon.com | Amazon.de | Amazon.co.uk]
– KY-023 analog joystick [Search on Amazon.com | Amazon.de | Amazon.co.uk]

Typically, the KY-023 comes as a module with 5 male pins:

  • GND (Ground Pin)
  • +5V (5V Pin)
  • VRX (voltage proportional to x-axis)
  • VRY (voltage proportional to y-axis)
  • SW (joystick pressed PIN)

How to connect the KY-023 to the Arduino?
Connecting the KY-023 to the Arduino is very straight forward. The module’s “GND” has to be connected to the Arduino’s “GND”. As the KY-023 work also 5 Volt, the module’s “+5V” has to be connected to one of the Arduino’s “5V” pins.
The joystick is basically a combination of two potentiometers. This means, when the joystick is moved along the x-axis the resistance changes resulting also in a change of voltage. The voltage can be used to detect the x-position of the joystick by connecting the VRX Pin to an analog input of the Arduino. The same applies for the y-axis. The y-axis’ position can be read by connecting VRY to an analog input. In this tutorial, VRX is connected to “A0” and VRY is connected to “A1”. Lastly, SW is connected to the Arduino’s digital pin “2”.

Picture of how to connect KY-023 to Arduino Uno.

Scheme of how to connect KY-023 to Arduino Uno.

How to program the KY-023 with the Arduino IDE?
The source code is fairly simple. First, we will setup the two analog pins A0 and A1 and the digital pin “2” to detect whether the joystick is pressed. Next, we setup the serial connection which is utilize to print out the current state of the analog joystick on the console.

Then, we read the analog data of the x- and y-axis. Thereby, the analog voltage of the potentiometers is mapped to a digital value between 0 and 1023. If the button is set up as shown in the upper picture/scheme, then moving the joystick to the left will result in an x value of 0. If the button is moved to the right, the x value will be 1023. If the joystick is moved to the top/north, then the y value will be 0. If the joystick is moved down, the y value will be 1023.

The pin mode of the button (for detecting whether the joystick is pressed) uses a pull-up resistor. The result is that the value of the pin will be 0 if the button is pressed. Therefore, the variable name “notPressed” is used in the source code.

// (c) Michael Schoeffler 2014, http://www.mschoeffler.de

const int inX = A0; // analog input for x-axis
const int inY = A1; // analog input for y-axis
const int inPressed = 2; // input for detecting whether the joystick/button is pressed

int xValue = 0; // variable to store x value
int yValue = 0; // variable to store y value
int notPressed = 0; // variable to store the button's state => 1 if not pressed

void setup() {

  pinMode(inX, INPUT); // setup x input
  pinMode(inY, INPUT); // setup y input
  pinMode(inPressed, INPUT_PULLUP); // we use a pullup-resistor for the button functionality
  
  Serial.begin(9600); // Setup serial connection for print out to console
}

void loop() {
  xValue = analogRead(inX); // reading x value [range 0 -> 1023]
  yValue = analogRead(inY); // reading y value [range 0 -> 1023]
  notPressed = digitalRead(inPressed); // reading button state: 1 = not pressed, 0 = pressed

  // print out values
  Serial.print("X: ");
  Serial.println(xValue);
  Serial.print("Y: ");
  Serial.println(yValue);
  Serial.print("Not pressed: ");
  Serial.println(notPressed);

  // The following delay of 1000ms is only for debugging reasons (it's easier to follow the values on the serial monitor)
  delay(1000); // Probably not needed for most applications
}

If the code has been successfully uploaded to the Arduino, the output on the Serial Monitor (Tools->Serial Monitor, Ctrl+Shift+M) should look like this:

Video Tutorial:

_______________