12. Input Devices¶
Group assignment¶
In this week’s group assignment, we tried to probe an input device’s analog and digital signals.
Individual assignment¶
In this week’s individual assignment, I connected a RFID reader to the board I designed for my final project. And tested it with a Mifare 1k RFID card. The RFID reader I choosed is PN532 module, a tiny size reader that is suitable to install under the Fab Kart, my Final Project.
Spec | Details |
---|---|
Supported Protocal | I2C, SPI, HSU (High Speed UART) |
Supported RFID Cards | Mifare 1k, 4k, Ultralight, and DesFire cards |
Max. Reading Distance | 5cm~7cm |
Size | 43*40mm |
Protocal Selection¶
The PN532 supports three type of protocals: High Speed UART(HSU), I2C and SPI. I can select the desired protocal by changing the combinations of the DIP switch on board:
Protocal | 1 | 2 |
---|---|---|
HSU | 0 | 0 |
I2C | 1 | 0 |
SPI | 0 | 1 |
I choosed I2C as commnuication protocal, it uses only two signal pins and I could add more input/output I2C sensors/devices to the same pins without connecting extra signal pin.
Programming¶
I wrote a program to test the PN532 module. To visualize the result, I also added a WS2812 LED strip, which can control each diode’s color and brightness on it.
//RFID Settings
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
uint8_t uid_SpeedBoost[] = {0x33, 0x9D, 0xB9, 0x12};
bool boostTriggered = false;
//WS2812 Strip Settings
#include "Freenove_WS2812_Lib_for_ESP32.h"
#define LEDS_PIN 12
#define CHANNEL 0
#define normal 0
#define start 1
#define rainbow 2
Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(8, LEDS_PIN, CHANNEL, TYPE_GRB);
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
PN532Setup();
stripSetup();
}
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0}; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++)
{
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println("");
if ((uid[0] == uid_SpeedBoost[0]) && (uid[1] == uid_SpeedBoost[1]) && (uid[2] == uid_SpeedBoost[2]) && (uid[3] == uid_SpeedBoost[3])) {
boostTriggered = true;
Serial.println("Speed Boost!");
lightStrip(rainbow);
lightStrip(normal);
}
// Wait 1 second before continuing
delay(1000);
}
}
Testing¶
References & Files Download¶
PN532 Test Program PN532 Library NDEF Library