/** * @title: StrongLink SL018/SL030 RFID reader demo * @author: marc@marcboon.com * @see: http://www.stronglink.cn/english/sl018.htm * @see: http://www.stronglink.cn/english/sl030.htm * * Arduino to SL018/SL030 wiring: * A3/TAG 1 5 * A4/SDA 2 3 * A5/SCL 3 4 * 5V 4 - * GND 5 6 * 3V3 - 1 */ #include #define TAG 17 // A3 char* mifare[] = { "1K", "Pro", "UltraLight", "4K", "ProX", "DesFire" }; void setup() { // Init serial port to host and I2C interface to SL018/SL030 Serial.begin(19200); Wire.begin(); // Flash red led (only on SL018) ledOn(true); delay(500); ledOn(false); // Prompt for tag Serial.println("Show me your tag"); } void loop() { // Wait for tag while(digitalRead(TAG)); // Read tag ID readID(); // Wait until tag is gone while(!digitalRead(TAG)); } void ledOn(boolean on) { // Send LED command Wire.beginTransmission(0x50); Wire.send(2); Wire.send(0x40); Wire.send(on); Wire.endTransmission(); } int readID() { // Send SELECT command Wire.beginTransmission(0x50); Wire.send(1); Wire.send(1); Wire.endTransmission(); // Wait for response while(!digitalRead(TAG)) { // Allow some time to respond delay(5); // Anticipate maximum packet size Wire.requestFrom(0x50, 11); if(Wire.available()) { // Get length of packet byte len = Wire.receive(); // Wait until whole packet is received while(Wire.available() < len) { // Quit if no response before tag left if(digitalRead(TAG)) return 0; } // Read command code, should be same as request (1) byte command = Wire.receive(); if(command != 1) return -1; // Read status byte status = Wire.receive(); switch(status) { case 0: // Succes, read ID and tag type { len -= 2; // Get tag ID while(--len) { byte data = Wire.receive(); if(data < 0x10) Serial.print(0); Serial.print(data, HEX); } // Get tag type byte type = Wire.receive(); if(type > 0 && type < sizeof(mifare)) { Serial.print(" Mifare "); Serial.print(mifare[type - 1]); } Serial.println(); } return 1; case 0x0A: // Collision Serial.println("Collision detected"); break; case 1: // No tag Serial.println("No tag found"); break; default: // Unexpected Serial.println("Unexpected result"); } return -1; } } // No tag found or no response return 0; }