Raspberry Pi LESSON 68: Using the MFRC522 RFID Module and Tag on Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McCarter with toptechboy.com and we're here today with episode number 68. in our incredible new tutorial Series where you're teaching your Raspberry Pi who's boss what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and if you're pouring your coffee as always I want to give a shout out to our friends over at Sun founder Sun founder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sun founder Raphael kit for Raspberry Pi now most of you guys probably already have your gear if you don't look down in the description there is a link over to Amazon you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and it's a really exciting lesson today because we're going to learn how to add an RFID module to our Raspberry Pi Projects Now what is an RFID module an RFID module is something that can read an RFID Card like this little card that has an embedded RFID tag in it or a little Bob like this little key fob like this that has an RFID tag in it and then you can read that tag on the Raspberry Pi and do some really neat and Cool Stuff one thing is it might just be to give you access like to unlock a door or to let you in you know you just flash it your card it sees that it's you it sees that it's your card and then it opens the door or perhaps you could use it for an attendance system so that as students come in they just tap the RFID module with their card it sees who they are it sees that they're there it does attendance or similar similarly using the little uh key fob now the way this works is each one of these little fobs has a unique RFID tag that has a unique ID associated with it and then the module reads that unique ID and I'm going to show you how to do that today but enough of this introductory discussion let's jump in and see if we can get going on this I'm going to come over here and kind of show you what we have set up I think I can show you right here and what you'll need to do is get this out of your sun founder kit it is the mfrc522 RFID module and then in the same Little Packet that contains that the same little mylar packet that contains that you get this RFID Card and you get this RFID Bob now this and this are basically the same but each one has its own unique ID associated with it now we are going to communicate with this through SPI okay we're going to use an SPI connection so I'm going to have to show you how to do that and there's five or six wires that have to be connected up and let's see I think that let's go ahead and get the thing wired up and the way that you can get things wired up is you can come over to the most excellent [Music] www.toptechboy.com and when you get over there use this happy little uh this happy little search icon right this happy little search icon right here behind my head and then search on something like using a mfrc 522 RFID module and tag with Raspberry Pi search on that you'll come to this page and then I have a schematic of how to hook it up we can zoom in on it and as you saw in the live shot I'm using this sun founder breakout board that makes it really quite easy to connect up it's like what one two three four five six seven pins you've got basically power and ground and then five pins and so it is pretty easy to hook up but I do suggest this a little bit more than trying to do on the screen I suggest you go to the website and find the schematic there to get it to get it hooked up and it just takes a couple of minutes to hook it up so it's really really pretty easy to get this thing wired up so let's come back over here we will look at the overhead shot and uh this just sort of shows you how I have it hooked up I couldn't get all these wires perfectly straight like I like to I had to kind of bend it there wasn't a way to get a really really super clean build on that and then again you can see I'm using the ribbon cable from my Raspberry Pi 4 over here connected to this gpio breakout board and that makes easy work of connecting these uh connecting these sensors up okay now let's see what we need to do the thing that we need to do now is we need to actually come in and we need to get SPI set up okay we need to come in and we need to get SPI set up and so the way we are going to do that is we're going to need to open up a terminal and I should say just for complete Clarity in case you haven't been following along tongue in case you haven't been following along and I actually need to switch my view over here to this one and give me just a second here to look at uh exactly what I have going on okay I think I think this is uh this is good I'm gonna make that font here a little bit bigger so it's a little bit easier for you to see what I am doing and I think we will come over here and let's go up to about 24. okay I think you can see that okay making sure everything is visible to you okay if we're going to use the SPI bus we need to set it up and so like I said I'm on the bullseye operating system and this should work for the old Buster but most of you guys hopefully are over to Bullseye you're over to the bullseye operating system by now so let me come in and the first thing that you're going to need to do is you're going to need to do a pseudo pip 3 install sp-i-d-e-v for like SPI developer okay now I will admit that on the bullseye version that I had this SPI DV was already there but you gotta have it for this to work so I'm going to have you install it and you might get a message saying that you're already up to date okay so you're already up to date like I am kind of likely and then also you need to do a pseudo pip through 3 install and you need to install the m f r c 522 Library MFR 522 library and then for me this one was already there as well so those might have been kind of two unneeded steps but because for this to work you've got to have it and I'm not sure exactly when these became standard in the operating system but at least now if you do those steps you know that you have them okay now we're going to need to come in and do a pseudo and then we're going to do a recipe config like that pseudo raspy config we need to come down to interface options we need to come down to SPI and then would you like the SPI interface to be enabled yes and then we need to say okay it's enabled and then we're going to go over and we're going to say finish and now we have the SPI libraries installed we have also we have the SPI enabled and so now we just want to see if if it looks like it's really there and if it looks like it's really going to be working so what I'm going to do is I'm going to say enable and I'm going to say enable SPI or no I'm sorry I'm going to LS we just enabled it LS we're going to look in Dev and then slash SP star for wild card and we should see a couple of things set up you yours might not show exactly like this but you can see I'm seeing SPI Dev 0.0 and SPI Dev 0.1 and so that is a good sign that we actually have this thing where we need it to be okay now I'm going to go to CD or what where I am I remember if you're following along with me you probably have a python folder so you want to go to that python folder so I'm going to go CDE Python and this is just wherever you keep your python files and so mine is python here and now I'm going to make a directory make dir and I'm going to say SPI and then I'm going to put SPI files and so this is where I'm going to download the SPI files that I need so I'm going to make directory SPI files I'm going to change directory SPI files files like that okay so that looks good now I need to go and I need to get those files that I need need to do my SPI this SPI work so I'm going to say git git clone https slash slash [Music] github.com okay and now slash l i t h i e r y l i t h i e r y slash now get this uppercase s p i Dash uppercase P lowercase y dot g i t I'm wondering yeah you should be able to see that so the the key thing is here you understand the GitHub business here but the key thing is l-i-t-h-i-e-r-y slash uppercase s p i Dash uppercase P little y dot g i t and now let's hit that and hopefully we'll see some magic happening yeah it seems like it's out looking for things whoa what is that that is not good so let's see what happened there uh I didn't do something right because it's wanting a login so let's see what I did wrong I say uh I say g i t c l o n e h t t p s colon colon g i t h u b Dot c-o-m slash there is no I I lied it's lth l t h i e r y l t h i e r y slash uppercase s p i Dash p y dot g i t okay let's try that now it's cloning that's good if you try to download something that's not there then it wants you to log in and that's kind of your clue that you typed something in wrong okay we're getting close men and women boys and girls we are getting close and so now I need to change directory down into that SPI files ah I'm already there I see when I when I did yeah I downloaded it into there so that's good so now I need to change directory or let's just LS and see what's in there yeah now I need to change directory into SPI Dash p y that you can see there okay now we are down in there now this is where the magic happens we're going to say pseudo Python 3 setup.py okay and then space install like that pseudo python3 setup dot Pi space install okay and that looks good that looks like we got everything installed and so we are moving right along so you know guys I think that if you have your device hooked up I think we're ready to try to start coding this thing okay and it's really pretty easy to come in and start talking to this thing so the first thing we are going to do is the first thing that we're going to do is we're going to import time okay and then from mfrc 522 we are going to import simple uppercase s simple and then m f r c 522 and then we're going to use the gpio pin so import RP I Dot gpio as gpio Okay so we've imported our library I'm going to run that just to see if I get an error because I want to make sure yeah I type this is kind of a kind of a little bit of a tedious thing to type in but I didn't get an error when I imported it so that means I am good to go now we need to create our card reader object we need to create our object our object for our card reader here okay let me move this over to a little better position where you can see things a little better [Music] okay and so you can see the action now as we're trying to connect up to this thing now I've got to create the object and I do that by I'll just call it Reader my object it's going to be called reader and that is going to be the library simple m f r c 522 that we just imported so now I have my reader object I should be able to interact with it now remember when we're using those gpio pins we don't want to get the error the second time we run the program where it says the gpio opens are in use and the way we don't have that problem is we do a try and then we do an accept okay try and accept like that okay and then what do we do on the accept well the accept we're going to do is we're going to do a accept uh we'll just do an accept like that I think and then what I will do in the acceptance gpio dot cleanup like that and so just to make sure that we are getting down here I'm going to just go ahead and print I'm going to print gpio good to go indicating that I've released the gpio pins when I control C to get out of the program okay and then here and try I wonder if I can just put a pass and just see if this will run here just to make sure I've got the kind of program flow that I want so it's running I don't have an error and of course it didn't uh let's see I'll do a that just went through once I'll do a file true so I create a loop here and then I will pass in that Loop and again we're just trying to see if our program flow is working and it says uh the channel is already in use because I'd run it and it stopped and I hadn't I hadn't released them so now let's see if I control C it says gpio good to go now if I run it I shouldn't get an error good so things are working now what I want to show you is if you hit this little red stop button it doesn't accept out it just terminates the program and then you'll get the error so you need to quit with the control C and so if I do that and come in and now I'm going to say control c i I quit the program that way make sure you can see okay I quit the program with the control C now when I run it I don't get the error okay that's what we want to see okay let's see if we can read these things I think we are about ready to try to read these things so it's in this while loop instead of passing what I'm going to do is I'm going to make a read so I'm going to say print and I'm going to print place card on reader like that prompt the user to put the card on the user on the reader and then I'm going to get the ID and that is the ID number of the little RF tag device so I'm going to get the ID and then I'm going to get any text that is associated with that ID and I'm going to get that with uh read or the object that we created dot read the method and then that should go out and that should get the card ID okay that should go out and that should get the card ID now what I can do is I can print and I will print ID like that space end it and then the ID that I just read and then print and I will get the text like that [Music] comma text okay and so now let's run this thing and see see what happens I'm going to come up I'm going to click run and it says place the card on the reader so I'm going to get this card okay I'm going to get this card and you can watch me I'm just going to come up and put it near there and you can see that it has the ID 121956026902 so that's unique to this card in the world no other ID card with no other RF tag will have that ID on it it is unique to this cart now let's see if we can read this type of card it's a little bit different type and so I'm going to come up and I'm going to put it there and okay it read it and you see it has a uh I hope it has a different number see did I read that more than once yeah you see the problem is I need to put a delay in here so I will put a Time dot sleep of about half a second like that okay uh so let's come up here and let's run this and see what happens okay place card on reader okay now that time you see I got an error I did read it successfully but I got an error on trying to read it and that time I got I got it without an error okay but you see this one about every fourth or fifth time that I read it I get an error so it seems like it is harder to read than this one see see this one I can just come in it reads it it reads it okay and you see it's a different number than that other one and every time it reads it so it seems like this has got a little better signal or a little bit easier to read and this one it just doesn't quite always do it about every fourth or fifth time I get an error and so basically for my projects I'm not going to use this type because I seem to have trouble reading that I'm going to use this type but luckily I have some number of these things so I'm going to get another one of those little readers a more professional YouTuber would have probably had a knife instead of having to bite into the package but let's see if I can get this one out okay so now I have two of these okay I have two of these and so this one is the number 902 and then this one is the number 732 so we've got 902 and we have and we have 732 that one actually got an error okay 732 and 902. that's the first time I've ever seen an error with one of these little blue ones I think I'm going to put a little bit longer delay in here of about a second okay and so let's try this one again so we come up reads it okay reads it reads it reads it I think sometimes bumping it is not good it's not good to maybe bump it okay so that seems to be working good by this one okay so that seems to be working pretty good now what you see is it's giving me the ID and so like you could imagine on the Raspberry Pi I could have a program that upon seeing a valid ID number it could do something like let's say open something or unlock something or turn something on with a proper ID it would maybe unlock your Raspberry Pi and let you go in and then start doing things okay but what you can see is you can also associate text you can associate text with the little RFID and you can see that since these are new out of the box they did not have any text associated with them and so what I can show you is I can show you how we could come in and we could actually write that thing okay so I think what I'll just say is my command CMD is going to be equal to input from the user and what am I going to input do you want to read or write okay question mark space and I think I'll just offer the r or the W you know you can read or write with an RW answer it that way you could do a lot more fancy and sophisticated uh prompt to the user but we're just going to do that because it's easy okay so I've got that now so I'm going to say if CMD is equal equal to right I will want to put some text on that okay and how would I do that I would do a uh I would do now an input so I will say txt the text that I want to associate with that is going to be input and then input your text like that okay and now that is input your text that looks good so now I've got the text now I can write the text and how would I do that the object is called reader.write and then I'm going to write what t x t like that okay and now we're done and now the other possibility would be if CMD equal equal to read then we would want to read it and then that should be all of this but I just need to indent this to keep it inside that if statement okay so now let's come up and let's run this okay so let's go ahead and just read to begin with with so I'm going to read and then which one am I going to read I'm going to read this one and I come up and when I read it it is 902 okay do I want to read or do I want to write well let's write this time okay we're going to write and then input your text I'm going to put my name Paul m c w h o r t e r okay and now I have I have input my text and then let's see why is it kind of hung there now after all right ah okay I know why because now what it wants is uh okay I needed to put another prompt in there because it's waiting to see the key to write to it and it didn't I didn't prompt it to do that so let's come back up here hopefully it makes sense what I just said okay so what we need to do before we write so we input the text and then we're going to say print place card by place card by the RFI by the reader we'll call it the reader like that okay now let's try it again so I'm going to run it okay do I want to read or write let's just read again okay so place the card by the reader we come in and we read it it's a 1 21 blah blah blah 902 okay this time we're going to write it okay input your text I will put Paul quarter like that okay place card by reader okay and then you know it got it and now it says do you want to read or write well this time let's read okay and then what are we going to read we're going to read this okay and it has now my ID and it has my name so that is pretty cool now the thing that's a little disappointing to me it's not actually putting my name on this token okay the ID is on the token but my name is on the module associated with the token so if I took this token somewhere else and read it it's not going to read the Paul mccorder part because the Paul mccorder part is stored on this uh on this RFID module if that makes sense I think it'd be pretty cool if you could actually put the data on here but all this is ever going to have is simply the RFID okay so that one's programmed so let's come up now let's see let's do a read again I've got the other one this time and I'm going to read it and you can see that this is 732 and this time I want to write it and what do I want to write I will write Anna mcquarter my wife okay so I will do that and now place the card by the reader so I come up and do that okay let's see it's not okay okay so it looks like it got it that time I had to just get it kind of a little closer it seems like this one it wanted to see a little bit closer or maybe I wasn't lined up as well so this time we are going to read it and then we are going to place the card by the reader okay and there's the number Anna mcquarter let's read okay and then there is the place the card by the reader okay there it is okay let's read it again and let's put it there I'm wondering if there's a little delay in there I'm not thinking about somewhere okay there it is okay so now let me try to read again but this time I'm going to go back to this one and read it okay and that's that and now I'm going to read okay and then that is that and again this one just doesn't seem to want to read this one as well okay you know what I'm suspecting is these are there it is okay just took a second to read it I'm suspecting if maybe you buy a dozen of these and kind of keep the ones that work well because it seems like everyone doesn't want to really work perfectly let's go back to this one and see if we can read it so I'm going to do a read and then I'm going to come in and uh okay it read it let's see if we can write it this time and who will I give this this will be Dutch my Fearless guard dog okay and now I put this there right now let me read and then touch boom look at that is that pretty cool or what so you guys uh if you have more than one kit or if you uh if you have uh some of the other kits uh a lot of the sun pounder kits have these in in there so I have four or five of these and four or five of those you can order them for like next to nothing on Amazon you can get a dozen of them for just a couple of bucks and so you guys can kind of stock up on these things if you want but I think this is just pretty darn cool and we have now added an RFID module to our Raspberry Pi to my our Raspberry Pi Projects okay so now this is what your homework assignment is your homework assignment is to create a system where you come up and you record the person's name and you record their favorite color okay so when you write your little Gizmo your little token you're gonna write a name and and a favorite color and you're going to put a name and a favorite color on this token okay and then when you come and you read the token you're going to read the name you're going to read the ID I'm you're going to read the ID you're going to read the name you're going to read the color and then you're going to have an RGB LED and you're going to turn that RGV led to the favorite color that has been stored associated with this ID how is that okay does that sound pretty cool I hope it does guys I hope you're having as much fun taking these classes as I am making them if you enjoyed this video give us a thumbs up it also helps us with the old YouTube juice if you'll leave a comment down below also and most importantly if you haven't done it already make sure that you make sure that you share this with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat video Paul McWhorter with toptechboy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 6,215
Rating: undefined out of 5
Keywords:
Id: 5ZJjx4dDUyw
Channel Id: undefined
Length: 32min 7sec (1927 seconds)
Published: Thu Jun 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.