Thursday, March 29, 2018

RFID and Wemos D1 mini (1)

I wanted to connect an RFID reader to my home network.
Therefore i used a ESP8266, more specific the Wemos D1 mini Wifi module.

Test 1 (fail)

I found a project on http://www.instructables.com/id/WiFi-RFID-Reader/ using a Wemos D1 mini. The exact info about the pins was missing.
As a test i created the circuit as a test on a breadboard using the Wemos D1 mini pin info on https://escapequotes.net/esp8266-wemos-d1-mini-pins-and-diagram/. (See also this picture with Wemos D1 mini connections)




RFIDESP8266Wemos D1 mini
RSTGPIO05 (free GPIO)D1
SSGPIO4 (free GPIO)D2
MOSIGPIO13 (HW SPI)D7
MISOGPIO12 (HW SPI)D6
SCKGPIO14 (HW SPI)D5
GNDGNDG
3.3V3.3V3V3
The instructional also contains software to read basic RFID info and send it over the serial (usb) line.

define RFID module

#include "MFRC522.h"
#define RST_PIN 15 // RST-PIN for RC522 - RFID - SPI - Modul GPIO15 
#define SS_PIN  2  // SDA-PIN for RC522 - RFID - SPI - Modul GPIO2
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

Initialize RFID module

void setup() {
  Serial.begin(9600);    // Initialize serial communications
  SPI.begin();           // Init SPI bus
  mfrc522.PCD_Init();    // Init MFRC522 
}

Read RFID tag

void loop() { 
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    delay(50);
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }
  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
}

// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}
Unfortunate the example above did not working for me. I got output on the terminal, however there was a message about perhaps bad connections.

Test 2 (succes)

More succesfull was the next example that  i found this on github:
https://github.com/Jorgen-VikingGod/ESP8266-MFRC522

Wiring RFID RC522 module

The following table shows the typical pin layout used:
SignalMFRC522WeMos D1 miniNodeMcuGeneric
RST/ResetRSTD3 [1]D3 [1]GPIO-0 [1]
SPI SSSDA [3]D8 [2]D8 [2]GPIO-15 [2]
SPI MOSIMOSID7D7GPIO-13
SPI MISOMISOD6D6GPIO-12
SPI SCKSCKD5D5GPIO-14
  • [1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
  • [2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
  • [3] The SDA pin might be labeled SS on some/older MFRC522 boards (including the one i used)

Define RFID module

#include "MFRC522.h"
#define RST_PIN 5 // RST-PIN for RC522 - RFID - SPI - Modul GPIO5 
#define SS_PIN 4 // SDA-PIN for RC522 - RFID - SPI - Modul GPIO4 
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

Initialize RFID module

void setup() {
  Serial.begin(115200);    // Initialize serial communications
  SPI.begin();          // Init SPI bus
  mfrc522.PCD_Init();    // Init MFRC522
}

Read RFID tags

void loop() { 
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    delay(50);
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }
  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
}

// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}
THis did work and i expected that was a good starting point for the rest of my project.

2 comments:

mashuptwice said...

Thanks for your documentation, the second example worked perfect for me!

KEVIN RICHMOND said...

Thanks for the post.
In my case I had to do some small changes in the pinout and in the code lines related to RST and SDA (SS) pins.

Pinout:

MRFC522 Wemos D1 mini

RST D1
SDA D2
MOSI D7
MISO D6
SCK D5

Code:

#include "MFRC522.h"
#define RST_PIN 5 // GPIO5 // D1
#define SS_PIN 4 // GPIO4 // D2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance