Elechouse PN532 NFC Module V3 for Arduino etc.


  • Community Helper

    Howdy again, @MarkGabb
    It should look something like this:

    int triggerPin1 = 2;
    int triggerPin2 = 3;
    int triggerPin3 = 4;
    int indicatorPin1 = 5;
    int indicatorPin2 = 6;
    int indicatorPin3 = 7;
    int indicatorPin4 = 8;
    int handbrakePin1 = 9;
    int carRunState = 0;
    int handbrakeState = 0;
     
      #include <Wire.h>
      #include <PN532_I2C.h>
      #include <PN532.h>
    
      PN532_I2C pn532i2c(Wire);
      PN532 nfc(pn532i2c);
     
    void setup(void) {
      pinMode(triggerPin1, OUTPUT);
      pinMode(triggerPin2, OUTPUT);
      pinMode(triggerPin3, OUTPUT);
      pinMode(indicatorPin1, OUTPUT);
      pinMode(indicatorPin2, OUTPUT);
      pinMode(indicatorPin3, OUTPUT);
      pinMode(indicatorPin4, OUTPUT);
      pinMode(handbrakePin1, INPUT);
      Serial.begin(115200);
      Serial.println("Hello! I'm a Car!");
    
      nfc.begin();
    
      uint32_t versiondata = nfc.getFirmwareVersion();
      if (! versiondata) {
        Serial.print(versiondata);
        Serial.print("PN53x key scanner board not online");
        while (1); // halt
      }
      
      // Got ok data, report state
      Serial.print("Reader Online, ");
     
      
      // Set the max number of retry attempts to read from a card
      // This prevents us from waiting forever for a card, which is
      // the default behaviour of the PN532.
      nfc.setPassiveActivationRetries(0xFF);
      
      // configure board to read NFC tags
      nfc.SAMConfig();
        
      Serial.println("Waiting for a valid key");
    }
    
    void loop(void) {
      String ringUid;
      boolean success;
      uint8_t uid[] = { 0, 0, 0, 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 (NFC Ring, 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 (NFC Ring or Mifare Ultralight)
      success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
      
      if (success) {
        // Display some basic information about the 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("..");
          Serial.print(uid[i], HEX);
          ringUid += String(uid[i], HEX);
    
        }
        Serial.println(ringUid + "\n");
        if (ringUid == "42cbfd26f3f81" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx"){
          Serial.println("Key accepted!");
      }
        
        
        if ((ringUid == "42cbfd26f3f81" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx") && (carRunState == 0)){
          Serial.println("PERMISSION GRANTED, SYSTEMS ON");
          digitalWrite(triggerPin3, HIGH);   // sets latching relay trigger on for ignition mode in car to allow start
          digitalWrite(indicatorPin2, HIGH); //show state, second LED
          delay(200);                  // waits briefly
          digitalWrite(triggerPin3, LOW);    // removes latching relay trigger
          carRunState = carRunState++;
        }
        else if ((ringUid == "42cbfd26f3f81" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx") && (carRunState == 1)){
          handbrakeState = digitalRead(handbrakePin1); //check if handbrake is on or off
          if (handbrakeState = 0){//if handbrake is on then safe to crank car, if not then skip this mode entirely
            Serial.println("ENGINE CRANKING");
            digitalWrite(triggerPin1, HIGH);   // triggers output for engine starting
            digitalWrite(indicatorPin3, HIGH); //show state, third LED
            delay(2000);                  // waits for a second
            digitalWrite(triggerPin1, LOW);    // removes triggered output 
            Serial.println("ENGINE CRANKING COMPLETE");
          }
          carRunState = carRunState++;     
        }
        else if ((ringUid == "42cbfd26f3f81" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx" || ringUid == "xxxxxxxxxxxxxx") && (carRunState > 1)){
          Serial.println("SYSTEM OFF, SLEEP TIME");
          digitalWrite(triggerPin2, HIGH);   // Trigger latching relay to reset for vehicle OFF
          digitalWrite(indicatorPin1, HIGH); //show state, LED1 on
          digitalWrite(indicatorPin2, LOW); //LED2 off
          digitalWrite(indicatorPin3, LOW); //LED3 off
          delay(200);                  // waits briefly      
        digitalWrite(triggerPin2, LOW);    // Removes latching relay reset trigger
        delay(3000);
        carRunState = 0;
        digitalWrite(indicatorPin1, LOW); //LED1 off
      }
      }
    }
    

    If you're not using one of the ringUid bits then just delete it, the sketch can work with only one ring set up or it can work with 12. I had it set up with two rings, front and back, so that myself and the girlfriend could start the car and didn't have to worry about which way the ring was facing.



  • Hmmm this script seems very different to the one I've got loaded.....made some improvments??



  • Ok so ill upload tonight, ive forgotten physical pin mapping
    Arduino nano

    I know my reader is in a4 and a5 and is correct because the reader is showing in the terminal


  • Community Helper

    yeah, I think the one you grabbed a while back would have been seperate cards for on/ignition/off. It was a while back.
    Just set the pins however you want them, it's pretty arbitrary. I think from memory the handbrakepin1 basically does nothing, you could chop that out.



  • Sorry question wasn't terribly clear... I forget how to actually assign pins. so if I wanted trigger pin1 to be b5 do I just change the 2 to b5?



  • Lol strait back and full of questions

    So I've loaded the code and input my keys.... however I don't get any further messages from the board about state it just reads the uid back to be over and over... shouldnt get a message about state from the board?


  • Community Helper

    Yeah it should be returning a state like "key accepted" or whatever I've got written in there. Read the uid in however many tags/rings you've got and enter them in, re-load the sketch and see what happens - I may have pasted the uid in wrong.



  • Yeah I reloaded it and loaded in both rings it was reading, as well as the one I sent you...wjich I got the uid from the terminal windownnnwhich means I must be getting the right id

    but no go



  • Hello! I'm a Car!
    Reader Online, Waiting for a valid key
      UID Value: ..4..B..4C..22..99..3C..814b4c22993c81
    
      UID Value: ..4..B..4C..22..99..3C..814b4c22993c81
    
      UID Value: ..4..32..32..22..99..3C..814323222993c81
    
      UID Value: ..4..32..32..22..99..3C..814323222993c81
    

     Serial.println(ringUid + "\n");
     if (ringUid == "42cbfd26f3f81" || ringUid == "814b4c22993c81" || ringUid == "814323222993c81" || ringUid == "xxxxxxxxxxxxxx") {
        Serial.println("Key accepted!");
      }


  • ok poured over this again last night...im positive im missing something, but really no idea what it is...
    anyone with some programing experience got any tips on why this wouldnt be working?


  • Community Helper

    @MarkGabb said in Elechouse PN532 NFC Module V3 for Arduino etc.:

    Hello! I'm a Car!
    Reader Online, Waiting for a valid key
      UID Value: ..4..B..4C..22..99..3C..814b4c22993c81
    
      UID Value: ..4..B..4C..22..99..3C..814b4c22993c81
    
      UID Value: ..4..32..32..22..99..3C..814323222993c81
    
      UID Value: ..4..32..32..22..99..3C..814323222993c81
    

     Serial.println(ringUid + "\n");
     if (ringUid == "42cbfd26f3f81" || ringUid == "814b4c22993c81" || ringUid == "814323222993c81" || ringUid == "xxxxxxxxxxxxxx") {
        Serial.println("Key accepted!");
      }
    

    heh. I can see one thing wrong at least. 4b4c22993c81 not 814b4c22993c81 and 4323222993c81 not 814323222993c81


  • Community Helper

    If that doesn't work I'd recommend installing nxp taginfo on an android device, read your ring and take the UID from the "tech" tab. There are zeros that may be necessary. eg. 04 0B 4C 22 99 3C 81
    Don't forget to kill the process before locking your device if you're using the nfc ring unlock app.



  • @Lokki spot on. i read the tag reading the wrong way around
    im guessing it wont pass to next section until i have the circuits completed...
    i remove the part when it checks for handbrakes and have just set it to 0 permanantly

    it wont say engine cranking, but im pretty sure that because nothing other than nfc reader is connected to it

    do you know anyone who has done an eagle for this??
    i think this is worthy of sending the board out for proper creation


  • Community Helper

    MrStein has his own design of this, with different code. Mine is basically just hacked together in my head, as per the sketch output pins. It's possibly a bit too "organic" for me to go back and actually diagram. I'd have to start again, pretty much. In my other thread "quick and dirty vehicle start/entry sketch" I've written down what I've done, which parts were selected and why. That'd be a good start, but I'd go for larger relays if I were doing it again in an older car. Just remember, it's got to be a latching relay for the car power and a momentary relay for the starter motor.
    I'll have to set up the basic board again for testing, I expanded it, added a screen, did a few other of this and that and then removed it to sell the car. I'm pretty certain it cycled through properly without anything else being hooked up though, I've got a youtube video somewhere showing that happening with an LED.

    Just comment out anything that you're not using, like the handbrake part.



  • ill check that out and see where to go from there



  • yeah got everything working now using led in place of the relays for testing.....

    however i dont think i got the pins in right. the leds seem to be onn and then off on switch....

    int triggerPin1 = 2; //engine starter
    int triggerPin2 = 3; //accessory and power on
    int triggerPin3 = 4; //power off
    how do these pins relate in respect to a1 a2 and such??? i cant find any documentation on such


  • Community Helper

    With my setup they don't stay on, they're just changing the state of the latching relay or holding the engine starter relay on for a brief time. When I get back on Friday I'll have a close look at my board and get back to you.



  • Hello guys,

    I'm totally new in this and I bought few days ago the NFC Module V3 (same as you guys are using). On the other side i'm using an Arduino Micro from SParkfun Micro based on an ATmega32U4. I put in place libraries I uploaded sketches coming from elechouse and tried the first one so the read sketch.

    My arduino recognize the chip unfortunatelly it keeps waiting until I put a card or a key card over it. Of course I do it but nothing happen. No recognition no sign from the arduino nothing....

    Is there someone who had the same problem? or a beginning of an answer?

    Thank you :)



  • @lokki hey man did you ever get a chance to look into that one???


  • Community Helper

    @MarkGabb sorry dude, I may have gotten sidetracked. I've just pored over my monstrosity here, pin1=2=D2 etcetera
    So on the arduino I've got D2 linked to the cranking relay connected to the starter motor, via another relay so the arduino doesn't burn out.

    @Limpresible sounds like the same kind of arduino as a leonardo, they use the same IC. Now, when you say there's a chip being recognised that's a touch ambiguous - do you mean the reader loads up and is ready or the NFC tag is recognised but does not read?