Monthly Archives: February 2017

How to use the “MicroSD Card Adapter” with the Arduino Uno

Pin layout of MicroSD Card Adapter and Arduino Uno.

In this tutorial, it is shown how to read and write SD cards utilizing the “MicroSD Card Adapter” module and Arduino Uno. An major advantage of the module is that it can be used with the SD library that comes with the Arduino IDE. The SD library makes initialization, reading, and writing of the card very easy.

List of materials:
– Arduino Uno [Search on Aliexpress | Amazon |]
– Jumper wires [Search on Aliexpress | Amazon |]
– MicroSD Card Adapter [Search on Aliexpress | Amazon |]
– MicroSD card [Search on Aliexpress | Amazon |]

How to connect the “MicroSD Card Adapter” module to the Arduino Uno?

Schematic view of pin layout.

The module comes with a voltage regulator. Therefore, the Arduino’s 5V and 3.3V pin can be used for the voltage supply. The module communicates via SPI (Serial Peripheral Interface) to the Arduino Uno.  The following table shows the complete pin layout.

MicroSD Card Adapter Pin Arduino Uno Pin
CS 4
SCK 13

How to program the SD card reader?
As mentioned before, reading and writing an SD card is very simple when the standard SD library of the Arduino IDE is used. Make sure to use the latest version of the SD library (Sketch -> Include Library -> Manage Libraries -> Search for “SD”). For example, in my case, version 1.1.0 did not work with the module. Fortunately, version 1.1.1 did work without any problems. Moreover, the SD card must be formatted as FAT16 or FAT32. If something does not work as expected, a good start for debugging is always to upload CardInfo example of the library (File -> Examples -> SD -> CardInfo) to the Arduino and read the messages of the serial monitor.
In this tutorial’s code, a random number between 0 and 9 is written to an SD card. In particular, the number is written to a file named “file.txt”. Next, the content of “file.txt” is read. At the end of the loop function, a delay of 5 seconds is added. Please note that when the Arduino is started, it is checked whether a file named “file.txt” exists (see setup function). If so, the file is deleted.

// (c) Michael Schoeffler 2016,
#include <SD.h> //Load SD library

int chipSelect = 4; //chip select pin for the MicroSD Card Adapter
File file; // file object that is used to read and write data

void setup() {
  Serial.begin(9600); // start serial connection to print out debug messages and data
  pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
  if (!SD.begin(chipSelect)) { // Initialize SD card
    Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
  if (SD.exists("file.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("file.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");

void loop() {
  file ="file.txt", FILE_WRITE); // open "file.txt" to write data
  if (file) {
    int number = random(10); // generate random number between 0 and 9
    file.println(number); // write number to file
    file.close(); // close file
    Serial.print("Wrote number: "); // debug output: show written number in serial monitor
  } else {
    Serial.println("Could not open file (writing).");
  file ="file.txt", FILE_READ); // open "file.txt" to read data
  if (file) {
    Serial.println("--- Reading start ---");
    char character;
    while ((character = != -1) { // this while loop reads data stored in "file.txt" and prints it to serial monitor
    Serial.println("--- Reading end ---");
  } else {
    Serial.println("Could not open file (reading).");
  delay(5000); // wait for 5000ms

If everything works correctly, the serial monitor should show a similar output as shown in the following screenshot:

Serial monitor output while using MicroSD Card Adapter.

Video Tutorial

How to use the ultrasonic sensor/actuator “HC-SR04” with the Arduino Uno

In this tutorial, it is shown how to use the ultrasonic sensor (and actuator) “HC-SR04” with the Arduino Uno. The HC-SR04 has an ultrasonic transmitter and receiver. The transmitter sends out a 40 kHz burst signal (which is not audible by humans). The receiver captures the signal if it is reflected by an obstacle. Therefore, the HC-SR04 can be used for distance measurements and alarm detection systems. The HC-SR04 supports 50 measurements per second and distances between 2cm and about 300cm.

List of materials:
– Arduino Uno [Search on Aliexpress | Amazon |]
– Jumper wires [Search on Aliexpress | Amazon |]
– ultrasonic sensor HC-SR04 [Search on Aliexpress | Amazon |]

How to connect the HC-SR04 to the Arduino?

Schematics of how to connect the HC-SR04 ultrasonic sensor module to the Arduino.

The HC-SR04 module has four pins. The module’s Vcc pin has to be connected with the 5V pin of the Arduino. In this tutorial, the module’s “Trig” pin (trigger) is connected to the Arduino digital pin 2 and the “Echo” pin is connected to Arduino’s digital pin 3. Lastly, the module’s “Gnd” pin is connected to one of the Arduino’s GND pins. Here, a mini breadboard is also utilized so that the HC-SR04 can be connected to the Arduino in an upright position.

How to program distance measurements with the HC-SR04?
The ultrasonic module is used to measure the distance between the module and an obstacle. In order to measure the distance, the module must be prepared to send out the 40 kHz burst signal. The burst signal is sent when the signal of the “Trig” pin goes from HIGH to LOW. In addition, the HIGH signal must have been present for at least 10 microseconds. The module sends a pulse on the echo pin with the duration of the round-trip-time of the burst signal. The round-trip-time is the time from the transmitter to an obstacle and from the obstacle to the receiver. Fortunately, the pulseIn-function covers this use-case perfectly. If the second parameter is HIGH, the pulseIn-function waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW.
In order to obtain the distance, the duration has to be divided by two, since only the one-way distance is of interested (from module to obstacle). Next, the acoustic velocity of the air has to be taken into account which is about 343.5 meters per second (only at a temperature of 20°C). Since the duration measured by the pulseIn-function is in microseconds and we are interested in cm, the  divided duration has to be multiplied by 0.03435.


// (c) Michael Schoeffler 2016,

const int pinTrigger = 2; // pin for sending out the signal
const int pinEcho = 3; // pin for receiving the echo of the signal
long duration = 0; // the roundtrip duration of the signal in us

void setup() {
  pinMode(pinTrigger, OUTPUT);
  pinMode(pinEcho, INPUT);
  Serial.begin(9600); // open serial connection to print out distance values

void loop() {
  digitalWrite(pinTrigger, LOW); // turn off the trigger
  noInterrupts(); // disable interrupts as they might interfere with the measurement
  digitalWrite(pinTrigger, HIGH);// prepare to send "trigger" command to module
  delayMicroseconds(10); // wait for 10us (module sends signal only, if trigger had a HIGH signal for at least 10 us)
  digitalWrite(pinTrigger, LOW); // module sends signal now
  duration = pulseIn(pinEcho, HIGH); // waiting for a HIGH signal on the echo pin.
  interrupts(); // enable interrupts, we are done with the measurement
  // from roundtrip duration to distance:
  long durationOneWay = duration / 2; // divided by two, since duration is a roundtrip signal
  // acoustic velocity of air at a temperature of 20°C => ~343.5 m/s
  // => 0.03435 cm/us
  long distance = durationOneWay * 0.03435; // distance in cm
  Serial.print("Distance[cm]: ");
  delay(500); // wait for 500ms (added only for debugging reasons)


Distance measurement with the Arduino Uno and HC-SR04 module.

If the code is uploaded to an Arduino, the output of the serial monitor should show the distance between the module and an obstacle. Since many factors, such as temperature, the obstacle’s material and surface characteristics, have an influence on the accuracy of the measurements, it is very challenging to achieve highly accurate measurements with such a low-cost ultrasonic sensor. Nevertheless, the HC-SR04 is a cheap and easy-to-use ultrasonic module with a wide range of possible applications.

Video Tutorial

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 Aliexpress | Amazon |]
– Jumper wires (female/male) [Search on Aliexpress | Amazon |]
– RFID-RC522 module [Search on Aliexpress | Amazon |]
– RFID tags [Search on Aliexpress | Amazon |]

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
3.3V 3.3V

How to program the RFID reader?
The code makes use of an existing RFID library which can be found here: 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,

#include "SPI.h" // SPI library
#include "MFRC522.h" // RFID library (

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: