RC522 with Micro:bit SPI help please

Issue #317 resolved
Tim Harding created an issue

Hi

I have been trying to get a Micro:bit to talk to a RC522 RFID Reader. I would like to get it to work with a Walt Disney World Magicband.

I can only program in Blocks, and so have been using MakeCode. This I could get to read the default Tags and Cards using an RFID-RC522 Board, but it would not read the WDW Magicband.

I tried a MINI RFID-RC522 using an Arduino, and this successfully read the WDW Magicband. However I do not understand Arduino code. So I tried this on the Micro:bit using MakeCode, but it did not work.

Has anyone any suggestions how I may get the Micro:bit to read the RFID using MicroBlocks?

Kind regards

Tim

Comments (38)

  1. John Maloney repo owner

    Have you tried using the MicroBlocks RFID (RC522) = library (in the Sensing category)?

  2. Tim Harding reporter

    Hi John

    Yes the MicroBlocks RFID (RC522) library is what I am using, but to me it does not appear to work with the MINI RFID-RC522. Which is the one that worked with the Magicband in MakeCode. I have not tried it yet with the other RFID-RC522 Board yet, but it did not work in MakeCode.

    I am using the following Pins; 13 - SCK, 14 - MISO, 15 - MOSI, 16 - SDA

    Or is it the way I have built my Code? Is this image below correct?

    Kind regards

    Tim

  3. John Maloney repo owner

    I think you meant 9600 baud in the serial open block.

    However, the MicroBLocks RFID (RC522) library support communication with the RC522 board via I2C or SPI, not serial. Hopefully your board is configured to use I2C or SPI, in which case you don't need to use the serial blocks at all.

    I would start by initializing the library, then testing the RC522 is connected block. If that doesn't return true then something is wrong with either the initialization or the wiring. Once that works, then RC522 card UID block should work, too.

    Good luck!

  4. Tim Harding reporter

    Hi

    Can you clarify the pins it shuod be on for RC522 SPI library plesae as its coming back false.

    Kind Regards

    Tim

  5. John Maloney repo owner

    The SPI pins are different for different boards. What board are you using?

    I tried a MINI RFID-RC522 using an Arduino, and this successfully read the WDW Magicband. However I do not understand Arduino code.

    Could you send that code (or a link to it)? Thanis!

  6. Tim Harding reporter

    Thank you for your help with this.

    The two Boards that I have are

    Mini RFID Board

    https://www.amazon.co.uk/gp/product/B07S9V6QRX/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

    RFID Board

    https://www.amazon.co.uk/gp/product/B09XTCJRF3/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

    The Mini RFID Board does not appear to work with a Micro:bit using either MakeCode or MicroBlocks. But it does work on an Arduino and can read WDW Magicbands and various other RFID Tags and RFID Cards.

    The Arduino Code I used is rfid_read_personal_data.ino and the Arduino Library is MRC522 V1.3.5

    The RFID Board seems to work with the Micro:bit reading various RFID Tags and RFID Cards. But it does not work with the WDW Magicband. Using MakeCode I can read the ID and Data on an RFID Tag or RFID Card. Using MicroBlocks it gives a TRUE for Board Detected, but it does not appear to read any Data or ID from RFID Tags or RFID Cards.

    I have just discovered MicroBlocks, after having used MakeCode for sometime now, and I am getting to learn about what it can offer. So I am having fun using a method of programming that I can understand. Are there any plans be able to program Arduino Boards using MicroBlocks?

    Kind regards

    Tim

  7. John Maloney repo owner

    Glad you are having fun with MicroBlocks!

    Digging into the Arduino code a bit, I see that it is using some features that the MicroBlocks library does not currently support, including authentication. The [Arduino example(https://github.com/miguelbalboa/rfid/blob/master/examples/rfid_read_personal_data/rfid_read_personal_data.ino) is using the default key -- did you need to change that to access the WDW Magicband?

    One nice thing about MicroBlocks is the that the libraries are written in MicroBlocks itself. Thus, with a bit of work, one could implement the extra RFIC card features that the Arduino library is using just by writing MicroBlocks code. However, in this case, doing that would require a working knowledge of Arduino C++ and might also require digging into the RFID world, so it's a fairly advanced project.

    The RFID library was contributed by José, a member of the MicroBlocks community. I will ask him if he'd be willing to extend the library with the features needed to read the WDW Magicband.

  8. John Maloney repo owner

    I couldn't find a lot of documentation for the Mini RFID Board, but it sounds as though it is configured to use SPI. Since it works with the Arduino library, you should be able to make it work with the micro:bit by connecting to the micro:bit SPI pins:

      Pin 13 - SPI Clock
      Pin 14 - SPI MISO
      Pin 15 - SPI MOSI
    

    You will also need to connect power, ground, and possibly a pin to use as the SPI chip select. Check the Arduino wiring to see what connections are needed. If you get the connections right, that board should work with the micro:bit using both MakeCode and MicroBlocks.

  9. John Maloney repo owner

    I haven't heard back from him but this is a big holiday week in Spain. I'll let you know.

  10. José García

    Hi, Tim and John.

    I took a look at the code from https://github.com/miguelbalboa/rfid/blob/master/examples/rfid_read_personal_data/rfid_read_personal_data.ino and seems to be possible to adapt to microBlocks. There are some things to consider:

    1. I only have the second model and don't have a WDW Magicband
    2. Assuming that the default key for authentication is used, to obtain the personal data de RFID UID is needed and. Can you confirm that it can be read with microBlocks?
    3. Using the second model, can you send the info that the Arduino program shows.

    Regards

  11. Tim Harding reporter

    Hi José and John

    Thank you for your help. I am not at home currently, but will send the Arduino Code as soon as I am able.

    1. I assume you are referring to the Mini RFID Board? I could try any code that you might have with a WDW Magicband, or I could post you one of mine 🙂
    2. MicroBlocks worked with the (big) RFID Board, as it showed connected as TRUE, but no Data.
    3. I will send you a copy when I get home.

    Kind regards

    Tim

  12. Tim Harding reporter

    I have two boards,and both are SPI which look like this

    RFID Board

    Mini RFID

    Kind Regards

    Tim

  13. Tim Harding reporter

    Hi José

    Thank you again for your help, I really do appreciate you helping me with this project of mine, and I am sorry for the delay in replying to you.

    I have tried various attempts using your new code (and the earlier version from MicroBlocks to double check) but I still cannot get it to work properly?

    I tried the different Variables to see what response I would get. I could confirm that the RFID Board was Connected, in SPI Mode, and SDA (ssPin) was Pin 16, but I could not read back any Data at all.

    I have attached some Sceen Captures of the Data that I read back using the Arduino and various RFID devices.

    If it would help you, to help me with this, I could get a Magicband posted to you, if there is an option on this site to send personal messages?

    Kind regards

    Tim

    Blue RFID Tag

    Grey RFID Card

    RFID ID Card K

    RFID ID Card T

    White RFID Card

    Walt Disney World Magicband

  14. John Maloney repo owner

    It looks like the Arduino program is not able to authenticate when communicating with the White RFID card and the Walt Disney World Magicband, so it can't read the personal data. It would make sense that Disney would protect the personal data with authentication so you may not be able to read that data with programs written with either Arduino or MicroBlocks unless you have the Disney authentication credentials. In that case, it won't help to send a Magicband to José.

  15. Tim Harding reporter

    Yes I was aware of the Disney Security, but I was hoping to be able to read the RFID Card UID only, as this is unique to each RFID device. This will allow me to “interact" with certain items at home. 🙂

  16. John Maloney repo owner

    You should be able to read the card UID from the Disney Magicband, since the Arduino program can. I think that part of the data is not encrypted.

    If "RC522 is connected" returns true then you've got communication with the RFID card reader, so I think this is close to working.

    The next step is to see if "RC522 is card present" returns true. You may need to experiment with various positions and distances between the reader and the RFID card.

    Once "RC522 is card present" returns true, the "RC522 card UID" block should return the card ID.

  17. Dariusz Dorożalski

    The "blue" module seems to be auto-enabled by the pull-up/RC on RST.

    rc522.jpg

    The "black" one, has RST pin directly connected to [MFRC522.pdf@NXP(https://www.nxp.com/docs/en/data-sheet/MFRC522.pdf)]

    "6 NRSTPD , reset and power-down input:

    power-down: enabled when LOW; internal current sinks are switched off, the oscillator is inhibited and the input pins are disconnected from the outside world

    reset: enabled by a positive edge"

    So RST should be connected to HIGH for normal operation.

  18. John Maloney repo owner

    Yes, I believe doing that might make your black (Mini RFID) board hardware work. The blue board had a built-in pull up resistor so you can leave the RST pin unconnected.

    If that does not work, you could connect the RST pin to a free pin on the micro:bit and add a few blocks to first output "false", wait a millisecond, and then output "true". That sequence should reset the board. You should add a final 50 msec wait to allow the chip to start up.

  19. John Maloney repo owner

    Here is the relevant code from the Arduino library:

        // If a valid pin number has been set, pull device out of power down / reset state.
        if (_resetPowerDownPin != UNUSED_PIN) {
            // First set the resetPowerDownPin as digital input, to check the MFRC522 power down mode.
            pinMode(_resetPowerDownPin, INPUT);
    
            if (digitalRead(_resetPowerDownPin) == LOW) {   // The MFRC522 chip is in power down mode.
                pinMode(_resetPowerDownPin, OUTPUT);        // Now set the resetPowerDownPin as digital output.
                digitalWrite(_resetPowerDownPin, LOW);      // Make sure we have a clean LOW state.
                delayMicroseconds(2);               // 8.8.1 Reset timing requirements says about 100ns. Let us be generous: 2μsl
                digitalWrite(_resetPowerDownPin, HIGH);     // Exit power down mode. This triggers a hard reset.
                // Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74μs. Let us be generous: 50ms.
                delay(50);
                hardReset = true;
            }
        }
    
  20. Tim Harding reporter

    I really do apprecite all your help everyone.

    I am not a programmer, but this is fun and I am learning…

    The Good News is that after adding the Reset Pin and Blocks I managed to get it sort of working with the bigger Blue RFID Board.

    It now says that the Board is connected, and it can see the RFID Cards and blue RFID Tag when presented anywhere within about 10mm of the Target on the RFID Board. But I cannot seem to Read anything information from any of these RFID Devices.

    Unfortunatelty it does not detect the WDW Magicband at all?

    As for connecting RST to 3.3V this did not work with either Board.

    Also the smaller Black MINI RFID Board does not seem to show as Connected using any method?

  21. John Maloney repo owner

    I am not a programmer, but this is fun and I am learning…

    That's great! This project involves both hardware and software, plus knowledge of RFID systems, which are new to me, so I am learning, too.

    Does the Black MINI RFID Board work with the Arduino program? If not, then there may be a wiring issue. For example, the board might be configured to I2C by default, instead of SPI. (The chip supports both. Some boards have a jumper to switch between SPI and I2C.)

    If the Black MINI RFID Board works with the Arduino program but not MicroBlocks, it may be something to do with the RST pin. Do you have pin 7 of your microcontroller connected to the RST pin?

    I'm not sure why it is not detecting the WDW Magicband since the Arduino program was able to detect that and give you the card UID.

    If you want to send a WDW Magicband to José, you can email interest@microblocks.fun and I will e-introduce you to José so he can give you his mailing address.

  22. José García

    Hi.

    I just tested to read and write data with the modified library using micro:bit and it works. For me the RST pin it’s not needed, as Dariusz says it’s auto-enabled.

    I tested with micro:bit, nodeMCU and ED1 with the “blue” module using SPI , and the M5Stack RFID using I2C.

    Tim, the tag will be very close to the reader, less than 2 cm.

    Here you have the last library, with reading and writing capabilities (using the default key) and the test tprogram:

    https://cloud.citilab.eu/s/Kf7G8DZc85swRgH

    Please let me know it works.

  23. Tim Harding reporter

    Nearly working… but as expected I am unable to write to the WDW Magicband, but I was able to write to all bar one of the other RFID devices that I have.

    However, I was getting some intermittant responses using the SAY Block, it would just show 0: then 2:TIM then 4: then 6: then 8:

    So I decided it must be an issue with my wiring, so I soldered the Pins together.

    But it still seemed to miss every other read, so I went back to using the Scroll Test Block to show me all 8 Personal Data words. But they all showed “nothing” except No. 2 which is the one that I had written to. I was unable to get any Data from the UID and so I looked at Personal Data 0 and on the Blue RFID Tag this just gave me an “M”, but on the SAY Block when I got a reading from this, it showed some “squares”.

    On the Arduino it showed a unique UID for each of the RFID Devices that I have, how can I read back the UID from them? As for my Project that would be enough to do what I want to be able to do with it for now. By presenting an RFID Device to the Reader on the Arduino it showed something like

    Which Data is the UID contained in?

    Also I found that the RFID Devices worked best at about 10 - 15 mm from the Mini RFID Reader, which will be perfect when it is mounted inside the Box that I hope to 3D Print.

    https://www.youtube.com/redirect?event=video_description&redir_token=QUFFLUhqbUwtWmhZWjloRlp3WUtsWURnTXhkNHdUX0ZUQXxBQ3Jtc0tsckFmbzZnN3UzYm5WLXIxeU1TWFE0ckRrMnNVcE5PR2dHOFlGcEZSRmdoblRkODVSQzU0dEJHdE1zenVrZ0xoZmZTMzJJMGZpTVYtNmlnMjExd1FwU0EzaGQ4UDBOSmtDSi1aam9paG11ekIzVFlrTQ&q=https%3A%2F%2Fwww.thingiverse.com%2Fthing%3A4271417&v=HJ8CTLgmcSk

    This is what I hope to use as a base for my project but using a Micro:bit. :)

  24. José García

    Hi,

    Great to hear that you can write and read personal data to RFID tags. Probably the WDW Magicband has a different key than the default, and this why doesn’t allow the code either read or write. To read only the UID use this simple code:

    Can you confirm that the WDW Magicband doesn’t read the code? If this it's the cause, can read-it from Arduino?

    I suppose that the problems you have are due to the proximity with the tag. Try to leave the tag always in the same position while reading or witting the data, I left if contact with the board.

    The UID doesn’t have any info, it’s a six bytes unique identifier for every RFID tag.

    Regards.

  25. Tim Harding reporter

    Hi José,

    The RFID Tag, Cards, and WDW Magicband all show the same response when placed near to the RFID Reader.

    But how do I identify the items in the “item list” to allow me to recognise which RFID Tag or Card is presented to the RFID Reader?

    The WDW Magicband gave this response on the Arduino showing the UID as a series of HEX Numbers 04 50 83 6A A2 53 80

    Kind regards

    Tim

  26. Tim Harding reporter

    Hi José,

    I really do appreciate you helping me with this. If only we were in the same room (or country?)

    I put in the HEX Numbers for the Magicband, but it does something wierd?

    Or is it just my lack of programming skills?

    Kind regards

    Tim

  27. Tim Harding reporter

    Eureka!

    This worked.

    I replaced the HEX Number with it’s corresponding Decimal Number and it worked :)

    Although it does seem to add an extra Number at the front and then only uses the first 3 Numbers from the WDW Magicband.

    But it works !!

    ie WDW Magic Band

    HEX Code 04 50 50 83 6A A2 53 80

    Decimal Code 4 80 131 106 162 83 128

    Additional 1st Number 136 (Decimal)

    So I used 136 4 80 131 to identify the Magic Ban presented to the RFID Reader

    Then when I tried it with the Blue Tag I spotted that it was in Decimal 156 48 215 109 (HEX 9C 30 D7 6D)

    So the WDW Magicband has a supplementary number at the front.

    So I can use your Block Code above to read the 4 Digit Decimal Number for any RFID Device and create my own Table of which Device is Which :)

    Next step is to control some Neopixels and a DF MP3 Player.

    I’ll keep you posted how I get along.

    Many many thanks for all your help, and everyone else who joined in.

    Kind regards

    Tim

  28. José García

    Hi Tim,

    Great to see that finally works!

    Sorry, I put to much number in the code, you are in the right way.

    Kind regards

  29. John Maloney repo owner

    Thanks for all the help. José!

    I'm marking this resolved, but you can continue to add comments if you need to continue the discussion.

  30. Log in to comment