#73 nRF24L01 Send (and receive) data with your Arduino!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now today we're going to be talking about two-way radio communication using this module here and this module here now these are NRS 24 l01 possibly suffix with a plus but they're all much of the same really now these units are particularly cheap ubiquitous and work like a dream however getting the two talking together for a beginner the absolute basics is something of a jungle so what I've done is put together this video with a cut-down version of the example sketch that comes with it in fact there's several sample sketches ones called getting started so what I've actually done is paired it to the bone put in some comments just reformatted a bit and made it do things as simply as I can as Albert Einstein said make it as simple as possible but no simpler so that's what we're going to end up with now these little units as I say are cheap they're well easy to use because the clever people that make the libraries to use these have sort of abstracted all the intricacies of ways you don't have to talk to registers or anything you just say transmit receive whatever and it's all very simple now this board as you can send on to me has got a sort of a built-in aerial already so this little bit down the bottom here is in fact it's aerial they can get different sorts to this you can get some that actually have a proper plugin aerial like you might get say from a walkie-talkie they are more expensive but then again you do get a bigger range as well so these units probably give you a range of well they say about a hundred meters in clear space I reckon you'd get 200 meters frankly if you were say in an open field right and these one on the ground that was sort of a meter up from the ground I reckon you're putting it 200 meters because not least you can set the power on these quite nicely so now at the moment I've got them set to absolute minimum power because it's the power consumption of these units the give begin in so much trouble or at least partly anyway so I've got it set to the absolute minimum power so I can run it from my Arduino board now only needs 3.3 volts let me repeat that it needs 3.3 volts not 5 you can make these up to 5 you'll have no transmitter left so 3.3 it is now whilst I am running it from the Arduino or at least well it's a nano this is a nano both of news outlet designs it's and nano put onto a development board but it's the 3.3 volts on here I'm actually sucking the power out now the datasheet says you only need about 13 milliamps when it come in receive mode which takes the most power ironically enough and understand that but the power to transmit at the lowest level takes about six or seven Millions not a lot and the range there might be something like 10 feet as it happens that sees me roll along so well projects in mind I'm just toying with it a moment so I do want a limited range now there are alternatives to this I did think as you can see in the back here I've got some of these power things for bed boards so here's one and there's another one already set up so what this is this is a power unit you put your your 12 volts in here and then it's switchable 3.3 or 5 volts in up and this side here is always 5 volts but this one then switches between 3.3 and 5 no idea is you get this breadboard which is well once again ubiquitous and you can find it on eBay all over the place and they just plug into the holes I three holes and it's all quite firm and stable as it happens I'm not friends with this particular one at the moment because the 5 volt regulator wasn't working either I'd blown it up inadvertently whatever I had to replace that one that's the AMS one more month 7 5 volt listen the 3 volt one is ok so I'm thinking how did that blow up mmm dodgy that anyway so that's working ok now and it is switchable as I say I actually prefer the one owner here not least because it's got a nice on/off switch there it's still got the main 12 volts in doesn't actually say what you can put into it you can put 12 as I've been using that and here of course you can change either side to 3.3 or 5 volts or indeed switch it off just by moving these little jumpers to the right place if you put it in the middle at the offsets 5 volts and the left-hand 2 pins and 3.3 which is what we'd need over here however that doesn't help you very much because these little things are not breadboard friendly if you look underneath they're connected with an eight-way dual header pin the anything can plug into that is either these dupont cables which works quite well for testing I'd certainly recommend that or you can go to your your favorite florist and supplier and get an adapter board which once again isn't in the cheapest ring in the world but let me just show this let's go over to the browser window and here we have ad adapter plate it says there let me at two pieces well I got these from banggood it's 2 pounds 18 so a pound the piece a so that's what one dollar 20 25 these days something like that not the cheapest thing in the world but it does work so it means you plug in the actual unit then just switch back here so what we're going to do is take one of these with its but it spins I wouldn't have any DuPont cables and plug them directly into that socket you see there and then what that allows you is that you can put different cables or different cables again but from the top so these are your signal and data lines and here is your power and then you can put 5 volts in here because there's already a 3.3 on here and it says I can get a bigger picture let's have a look we can there we are that's a nice little picture so all the data stickers along here the one you need anyway here's your power a fit Bob's your uncle now as it happens I've got a couple of these on order not for the videos I'm doing today little bit lightning really but I've realized that this is the best I can do for well experimenting but if I really want to put it into a project which I do then this is not really going to be satisfactory so I've got a couple of those on order now if you use the higher power ones let me just show you one of those something like this float see this one here it's got this nice walkie-talkie style aerial now that's three pound 26 there is a little bit some more expensive in it's like a bigger view of that oh yeah now that's probably friendlier to use because it's got these pins on the top but then you are paying a bit more but then the range also on these is significantly more you can actually go up to 100 million watts on these and you would get or easy a couple of kilometers on that so maybe not depending what your need is you have a choice right so as I say let's get back to about the workbench what we're going to do is communicate with these two okay it's only only across six inches but it it works for about a meter and a half a website set up at the moment you need two identical Arduinos and well - everything releasing you got to business is - lots of DuPont cables if you haven't got deep on cables please please please watch the video that I made ages ago about that sort of the basic necessities you need because you can need females at this end and then whatever it is you're connecting - no I haven't got a a genuine Arduino here these are all nanos more clone nanos on the developer mumbles I find it very much easier to use these clone boards because you've got a choice then I've either pins or sockets now the socket is okay you'll get those on the standard one but these pins not it actually and on it is the signal basis a you know D 0 D 1 D 10 a 7 whatever along this side and then you've always got the 5 volt on the ground right next to on the each one so you're never going to route supply pins so that's why I like these and they're nice and big as bigger than Arduino Uno and it all makes it very easy to use right let me move these other bits other way now you've seen those and let's show you the code running and then we'll talk a bit more about the code now remember this is this is transceiver n RF 24 L o1 beginners 101 class the absolute minimum you can get away with just to prove it works because I think once you get this working even the way I've got it working now I think it will become fairly obvious how you'd adapt that to send more data we'll talk look through the code in just a little while now I'm going to show you my monitor now rather than my code window because there's just so much going on we've got first of all we've got two instances here of the standard arduino ide okay there's one here and one here now if you look on the debug window of serial monitor one's running on common v and right and one's running on comm ten here now that's in some ways quite tricky to do you must launch each instance of this directly don't start going to file open or recent or anything like that that just makes another copy within this instance it's really weird how that works go to your desktop and click the shortcut to load up one instance and open this particular sketch though this one I've called test mode one okay that's the transmitting version and this one over here is called test no zero so you open up another instance clicking the desktop shortcut and setting the port number to port in the ones running on port 10 and this one over here is running on port 5 now what you might find is the menu switch one actually changes the other one and that's quite infuriating but persevere and eventually it will give up and let you do it now the other thing to notice if you notice up the top here is that I'm running arduino 1.8 one now at the time of making this video at least so February 2017 we seem to jump from one point 613 we didn't have any one point seven is lower now we went straight to one point eight one and I found these easier to work with when trying to load up two separate instances but maybe that was just pure gains okay so what you can see now is that this sketch is sending something and receiving something whereas this sketch over here is just R ascending in response right so what would right here that is this side of the transmitter this is on comma 5 comma 5 and this one over here is on comm 10 all right so this one's gen transmitting this one is just doing well I thought the knowledge will talk about more about that in the code now the standard example sketch that comes with the library that we're going to use as you see here it says RF 24h you can do either you can you can type something in the serial port window and do either transmit or receive but I found that well I personally found it a little bit confusing and secondly I thought well if I'm finding it a little bit difficult to follow and understand northers complicated I thought well you know we just we can't see the wood for the trees so what I've done is paired these two programs down to the ground as it were one does the transmission one does the reception and then send something back as well so they're both actually are transmitting so let's have a look at the code now that we can see obviously data is being transmitted between the two units and we're sending a number outs just a random number and then we're getting back that number plus 1 so if you look here as being 12 came back 13 sending 44 game back 45 and this is just saying what is sending back ok so it's working so we've got two sketches that's great I'll put these in the video links down below that little gun github and it's kind really just have a look at the sketch itself and of course the RF 24 library the guy does an awful lot with these transmitters you can have many many many transceivers all talking to each other in a very complex manner but needless to say I'm not coming there today but let's go back to the code window now and just look at one of these at a time we'll look at the transmitted first because that's probably the most interesting right here we are then now this is the same one of the example sketches pared down to the ground and I'm not trying to take credit for this by the way we're using as our f24 library so I'll put a link down where we can get that as well let me just show you where that comes from we can get it's all on github as you might expect so it's bring that up or we can get that from so this is the well the username of the person writing TM our h20 whoever he she it may be Ukrainian Canadian programmer and nonetheless though but this is the library one other side there's there's other ones on here you can see where we interest in this TF are 24 ok these all the lines are well slightly more advanced but you might want to rather look at those right so the RF 24 then this is the library just click on the clone clone or download download the zip unzip it rename it to RS 24 when you get it old site on the IRS 24 - master and put that into your standard Arduino libraries folder back to the coding because we're not actually going to look at this oh well apart from in the examples I was telling about getting started this is the one with both are hacked basically to make two versions of that want to transmit one want to receive one even though they're both effectively are doing transmit and receive but if you download my two programs get them working anything great I understand now what it's doing then you could well load this up and it's an identical program so you load the same program up in both instances of your sketch but that's somewhere down the line let's not go there right let's carry on with the the code analysis so we're using standard SPI on there because both these run on SDI of course so net spin eleven twelve and thirteen and not ten actually it's probably me being a bit ambitious everything is going to run more for so it's eleven twelve thirteen plus two further ones that use is to switch the chip in to enable mode transmit mode whatever so we just leave that as is this is the way of well he's chosen this way so to identify which node it is you're talking about and it's the first digit that determines what the actual units going to do whether it's the transmitting bit or the receiving bit so I've just left those as they works it just seemed too complicated change reading so we've got one and two and the beta mode fine okay so on here I've said this is the transmitter code they're going to initializes all the codes everything now if you notice here I've got the PA level this is the transmit level the absolute minimum if you up that there are other options but if you put it any higher you may well get problems in stability or just won't work because the same is that only 3.3 volts your Arduino can probably give you on this in the like 50 milliamps tops so it might appear to work for bitten and fail a lots of people have soldered capacitors and what-have-you across these pins but I just don't want to get into all that at this time it works as it is now on like this without any soldering in capacitors anywhere but putting it down to the minimum I've also set this up here this is the fastest transmission rate it will give you but also the shortest range it can transmit it both one megabits per second and 250 kilobits per second so if you use maximum power here and 250 kilobits per second there you'll get the longest range which will probably give you although perhaps a kilometer or near enough as I say I'm playing about these for a different reason anyway so I look at the shortest distance and for a demo project like this it will serve you well as lying minimum powers would that interfere with anything and the fastest speed now channels these are all operating between 2.4 gigahertz and 2.5 gigahertz yes I hear you say isn't my rooster running at that if it is your wife I will probably run a 2.4 something but it doesn't extend all the way up to 2.5 so these units have effectively got a hundred channels to choose from in one mega Hertz chunks if you like so we can start from 0 to 1 to 4 once you four then won't be affected by microwave ovens and Wi-Fi and stuff that's what I've used here to give you the best possible chance now before you can transmit anything we need to opera open a pipe and the pipe is an address of your notice the addresses 1 & 0 were declared up here okay they're unique and think of them like a well like a port would be if your tcp/ip that is to say the address that your internet connection is to web pages or web browsers go to port 80 then they don't have to it's just convention so anything coming into your home or around your home network will use a network aggressor plus a port so I thought we're doing here we say well we're on this channel here but we're also got a very specific grip and the threat and if it's not that in address it won't respond okay look for this test unit we've only got to one load and two nodes so if you were to point talking they're three knodel's meters wouldn't work okay right now what I'm doing is actually transmitting a random number from one to the other so what I'm just feeding the random number generator at this point okay which you need to deal I will do it the same random numbers generated every time which proves they're not there and of course in there right the main loop what's he doing first of all we're going to get around a number anything between 0 & 2 5 4 we have to say stop listening on the radio the radio remember is the object that was declared way up the top here here okay as part of the RF 24 class but if you don't like Clanton object-oriented just think of this as another variable a little web getting hold of it so the rainy do we have to stop listening and so we stopped listening every time then we transmit whatever it is so the random characters say 56 we say right I want to transmit that value what's the ampersand in front of that that's a variable name because this is in fact a point of reference to it let's not go there just just take it that's what you got to do and it says how big is your data well I could have put one in there but it's all hides too much I think what we're saying is whatever the size is of a none side chart what of course it is one byte so yes all of that comes down to one but you might not want an undefined child in fact the original sketch transmitted a long value which in fact it'd have been milliseconds or microseconds or something so there's many digits long so that would take em 4 bytes which I thought it's just too much so we're going to transmit one data one byte of data that's how it knows how big it is the other thing is whatever the size an unsigned char takes which is one byte and then we listen so having transmitted from here to here the number 56 for example if they right now ie this one here and listening for whatever response you give me now it's not going to listen for ever so in other words it's just hang potentially so we we start we grab whatever the current Milly's is that is how many milliseconds since we switch this little beauty on and if you ask if this good one for 49 days without running out of space to record that and we're saying whilst we haven't got any data so we're still waiting for data to appear here from this one so we're waiting so the same while we still haven't received any we'll just check that we haven't waited for the 200 milliseconds yet and continue to wait and continue to wait continue to wait but the minute we do get some data available it will come out over here and go down to here okay now if we didn't get any data we just return the return from this point is a little bit cheeky doing it like this we're returning out of the loop - whatever called loop and one day I'm going to do a video and he'll this hangs together but just just take it from me that there's something the cause of the loop a shell program if you like is sketch the cause of our sketch so that's just going to go back to it because if it hasn't received a response is nothing because we can stay on screen assuming it does receive a response phone it comes down here says right okay well let's read that response and we're going to get the day so once again what's the ampersand once again we've got this how big is the data coming back well we know we're only sending one byte back there's what I mean keep it simple we say we're sending a byte we're getting the byte and then we're just in this bit here just displaying to the serial port as you've already seen exactly what it is we've got back we should be one more then won't be sent I have nothing checking that out here because you can see it on the serial monitor then we wait for a second and start the whole thing again right so that's how the transmitted works now there's one little bit in here that we sort of glossed over this one here with same right radio right so we're writing the data that's the pointer to a data variable thing is whatever's in that one Megan go and transmit it one bite but this radio dot right returns of value back to say not only did I transmit but I got an acknowledgment and automatic and acknowledgement that the the other end of to whom on transmitting received it so what's happening is this one transmits this one down here says yeah I've got that Thanks and then this one carries on with the code even though we're going to do some more work in here in a minute you get this automatic acknowledgment which of course is brilliant for us because we can transmit stuff and we know the other side's got it without us doing anything if we did have any of this hanging around waiting for another response back in all this it wouldn't matter because we know already that this radio don't like has already confirmed that the other end has received it and not only has it received it but it says I've received it uncorrupted and or if it did receive it corrupted it was incorrect this would retry up to 15 times to send that packet again okay then this is all very clever stuffers them how they crammed in all that electronics ins of this thing is quite amazing so what this is saying here is that if I didn't manage to send and get an acknowledgement back we'll just display a little message but we'll still continue just just in case we missed it we wouldn't have done but Wow okay so that's what this that little bit is quite seven they'll go all these comments in here just so that you can follow it through okay no it's not likely well there's something of that little tiny bit of data obviously this is a very very very simple sketch and we're only sending one character but we could send more we could send an array of characters which I could have been used in my cat one for example the time the date the temperature the humidity whether it's raining or not so forth but that's for another day well this have a very quick look now at the receiving end which is even simpler than this right this is the receiver so this is text node 0 and as it stares down here it says this is a receiver code okay so on their little mock-up here it's this one over on the left hand side running on comm 10 this I hope is yes comm 10 good right so what this is almost a copy more the beginning bit most definitely is a copy of the other code does all the radio setup begin and all the rest of it minimum power to make a bit same channel obviously if you want to talk remember pretend these are walkie-talkies you've got to be talking on the same Colonel how many and you've obviously got to be talking and listening on the same pipe that the other one is listening to so this is the opposite way around the other sketch we are opening a writing pipe on 0 where the other sketch opened it on 1 and we're opening a reading pipe on 1 where's the other one had to be on 0 remember think of these as sort of a a door so we've got the street and these are the doors on those streets you then get the right one nothing about so we start listening because obviously this one is supposed to listen that's the whole point of this and we want to get a single character so we say is there any data can I hear anything while there is data we go and read it and now we're only got one character that's all we can do you'll read one character there will not be any further data available we therefore stop listening I add one to that single character beta plus plus so if we receive 56 you will now go up to 57 and we write that back in exactly the same way the transmitter a really sends it to us and then sends it back we then start listening again now if you notice here we haven't said if not radio right so we just we're just writing out transmitting into these does but it's coming from here to here now if that fails we don't know about it here because we haven't checked then bothered to check out of that way this would have given us an indication if only we bothered to check the return value remember we haven't deliberately doing that we just show you that you don't have to check if you don't want to and then we go back to listening mode to catch the next packet and we just send a little response here to the serial monitor say this is why sent back so having gone through all that let's look at the monitor window again here we are now these are our two outputs so this is sending back the responses we've just look as we've just seen there that's the response that it's sending back and let me just first reveal out this is the transmitter we're sending a value making sure that the other end has actually received it for continuing but then receiving something back as well in fact it's one more so that's it really that's the very simplest sketches I could come up with it seemed to make sense to me for a beginner and you know I'm a beginner in doing this as well really so I think if I can understand this two separate sketches similar but not quite the same and two instances running like this sending staff receiving stuff I reckon that makes sense so the only thing you've got to do if you want to make this way you're thinking edge you know what I like this I know I'm going to do it how are you going to do it right well the first thing you need then two of these NRF lo1 transmitters and I would advise just playing about ways get these one with a built-in aerial until you know that whatever project is you've got in mind is going to work is no point in forking out extra money although stupidly for high-power ones obviously will need to Arduino zuv some shape or form you will need DuPont cables and I would recommend you get the DuPont cables and then use the same color scheme that's and I've chosen here so this is the connection from the top of these okay this thing down here so the and the reason those colors were chosen because I want you to read down here to VCC and if you look at the DuPont cables when they come in on top I've got lots of them here let me just pan back and you can buy these on eBay for very very little money so if you wanted to start with the red as I did you're going to be restricted at night it's going to go red brown black white gray right I was just watching that better video and it's obvious that I've done it the wrong way around let's do that again look this is the DuPont cables and I should have been reading it that way not not the other ways so I've chosen brown reds or browns the ground Reza's positive and then you know orange yellow green in that order anyway and you can literally just peel these off like that and plug them in now as it happens I've just stopped the video just for that I've also received those sockets today learnt so there's a little stockett you see it's just a little thing like this it's now five volts in not three point three which makes it a lot easier for us Adri nights so that get the right voltage that just plugs in there and the cable sequence the coloring sequence if I seem to have so much trouble with has been honored here so they haven't mucked around with the order of the pins in other words so I say it very quick thing like there we are oh one last thing cause it's got a little light on it you see these haven't got lights on this is just shame really they just company tells actually running but anyway that's got a lotta to show you more power to it and well it just makes a lot neater them and they you could literally melt that in the project box or something as it only holds up some very very tiny holes there and uh they you might want to just put those onto a circuit board and solder model stick it down whatever but it's certainly a lot easier doing it like that then it is with the original one like this you can't reuse this one at all you can use this one that's it it's all I wanted to say alright let's get back for the main video so that's that's why these colors are the way they are because that's the way they happen to be on the DuPont cables starting with red and then it goes red black yellow orange blue green content what color gray and purple so that's what is now there isn't that one pin there you'll notice it says irq we don't coming in that at all twenty pin in fact just just to stop the questions coming yes I've connected it to this pin here because that socket on here is not connected to anything on this particular expansion board is not connect rather than just leaving it dangling in midair ready to cut something it shouldn't touch off put on there right so you need two of those and to be quite honest although you can connect to my block there's Matt's grateful demo prototyping I think just to prove point if you've got a project in mind you can do this really easily and it's not going to take you that long a couple of thousand you put a video especially be use the sketches that I've given you and remember to load up two instances directly from the desktop shortcut that's going to be fairly easy but I would recommend if you're going to order these you might as well order the adapter plate to go with them because you will most certainly need those as you go on okay just to refresh your memory let's have a look at those there are that's what they are the two I bought now of course if you buy more of these they do go down in price quite quite quickly and these are the same pound apiece because I bought two but you will tender example again something like sixty pens now if we just have a look here again go back one that's the high power transmitter maybe I look five pieces here a full pound the penny so I'd be about what five dollars for five a dollar apiece that's pretty good isn't it and the actual transmitters is one with one here for one pound 38 if you buy more than that this kind of looks they did them in bulk here I know they do some other hair lot you can get 20 of them and things I believe unlikely you'll need 20 but you might be in a school for example or college you might better share them out with your little schoolmates or something seeing it 24 walk 13 69 which is probably about I know that 16 17 dollars they line up that's a tad more but whatever you can see if the price drops fairly quickly let's just have a little demo now of what happens when it runs out of range so there we are it's all running quite happily well I'm going to take this one the foreign does the desk take it off here and move it look without destroying the setup I'm going to move it far away well there we are no let me just kill that there now look at this you see it saying I haven't added a response now and I'm not getting a response because it's too far it says that range quite limited let me bring it back there now look I've lifted it by my desk and it's quite happy again well sort of happy that's it so now it's probably oh it's probably about 60 centimetres apart two foot apart and it's quite happy I start moving it further it loses that which is great because I want it for that because when minimum power and just for testing purposes I think that's enough if you do want to go up one level in power or put a link to the datasheet or the information in that RF 24 library and then you can move up one level I think I've got it as min power and you can put it up - I think it's low power where do we set that yeah that's here so I've said it's a PA - min you can set it to PA - low but as I said before if you start getting problems with these units because they suddenly stop working on something that's probably because your Arduino just can't supply enough power okay but those little as that two plates will get you round that problem right okay that will do for an intro if you got this far I think you'll probably immediately see how useful it be to transmit more data but that's just to get thing working and already I've got projects in mind that want to do whether it comes to anything well well write down a few months as I think the way my productivity is going in the moment so that was an intro to the NRF 24 l0 1 plus transceiver module I hope you enjoyed the video thanks for watching I hope you're finding these videos useful and interesting that are plenty more videos to choose and a couple is shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 148,817
Rating: 4.9196315 out of 5
Keywords: nRF24L01, nRF24L01+, TMRh20 Library, Transceiver, Arduino, electronics, C++, microcontrollers, programming, gadgets, Transmitter, Receiver, Data, Adapter module, Send, transmit, receive, wireless
Id: JSHJ-RLbNJk
Channel Id: undefined
Length: 37min 9sec (2229 seconds)
Published: Sun Feb 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.