Getting Started with the nRF24L01 Transceiver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello from the fourth Tronics youtube channel welcome to getting started with the n RF 24 l 0 1 plus transceiver and I'm just going to call it the RF 24 for short but what we'll cover here is what is the RF 24 a little bit about how it works and then we'll look at a bi-directional communication example using Arduino let's get started so what is the RF 24 well it's a low-cost wireless transceiver and when I say low-cost we're talking just a couple dollars and since it's a transceiver can do bi-directional communication it works at 2.4 gigahertz it communicates with a microcontroller like Arduino using spy communication it's power supply is fairly flexible it can go up to 3.6 volts so you can use the 3.3 volt output on an Arduino Uno what's nice too is it's spy can handle up to 5 volt logic so you don't have to worry about level shifting and also if you're using 3.3 volt logic it'll work fine as well it has a nice long range I would say as long or longer than Bluetooth now I'm listing a hundred meters but you know in a building it's not going to be quite 100 meters low power consumption so it's great for battery-powered designs it can also be networked you can network up to 6 transmitters and 1 receivers if you want to do a wireless sensor network now of course you could have more than that communicating but it gets a little more tricky on how you handle that but built-in it can handle up to seven modules it has a well-documented Arduino library these modules communicate using packets you can change settings and the registers but we don't have to worry about that because their Arduino library that we're going to use in this example handles all that and just a note I didn't run into this problem in my example but when I was researching this I saw this that they recommend having a capacitor right at its power supply input and the reason is is you know devices like this they use very low power and then when they transmit they use a quick burst of current and sometimes that current can really have a big draw in the power supply so the capacitor can be used as a energy reservoir I have 10 micro farad you probably could use a different value but 10 microfarads is a good common value and then one thing to note and we'll see this in the example but this can do bi-directional communication but it's not as straightforward as Bluetooth you can't just write and read and write and read you have to actually change some addresses and change some settings but it is bi-directional but it's not as simple bi-directional as Bluetooth okay here's a picture of our RF 24 what's nice is even though the module is nice and small it has some pin so it's easy to connect now there's eight pins and the way they're situated it means that you cannot use this in a solderless breadboard because you'll short pins together but as you can see in my picture I have it in that picture I'm connecting it to an Arduino Pro Mini but you can use these cables that connect it you can also you know get a solder breadboard to connect it but once again a solace breadboard is not going to work so here I'm showing the connections for an arduino uno or pro mini and that's what i'm going to use in my example if you have a different arduino the good thing is it's using the standard spy connection so for the clock pin 13 the master out slave in the master and slave ouch is going to be the standard arduino spy connections now there is two other different connections the c ii and the c SN and there's also another one that i'm not showing the irq but the c ii pin is just used to put the module into standby in active modes so this will need to be connected I use pin 9 you can use different pins if you want you never have to worry about this though because this is handled by the Arduino library but it does have to be connected then there's the CS n pin this switches the module between a transmit state or a command state so once again if you're familiar with Bluetooth modules if you want to change a setting inside the module you have to put into command mode that's what this pin is controlling so when it gets spy communication from an Arduino or microcontroller it has to know whether to send that out over the wireless air or to use that as a command to change its setting and that's what this pin is controlling and once again this needs to be connected but you don't have to worry about if you use the Arduino library then the other pin and I will actually connect it but it's the irq pin the irq pin gives indication of some type of action or event so for instance if you get an acknowledged mid packet which we'll talk about from the receiver that have received the transmit packet the irq pin can go high okay here's what we're going to use for our example the the set up and so I have two modules to transceiver modules to our RF 24s I have an Arduino Uno that I'm going to use for my transmit module and I have a charge we know pro mini that I'm going to use for my receiver module so when I transmit a packet I can communicate over that packet and that packet will have a payload or the data I want to transmit so that's pretty straightforward what's nice though is the RF 24 when you send a payload if the payload never reached the receiver or if there was an error and the data the receiver will either send an acknowledgment packet saying that it received the packet it was able to read it or it won't send an acknowledgment packet so you you have an idea if the receiver received it or not now what's nice about the acknowledgment packet is you can either just send it blank as an acknowledgment or you can actually put data into it so that that sort of enables the bi-directional communication so once again it's easy for my transmitter I can just continually send payloads the receiver can send an acknowledgment packet and if the receiver wants to tell the transmit or something it can put a payload in that acknowledgement and we'll show that in this example this is where we get into a little bit about how it's not as flexible as something like bluetooth where you can just communicate back and forth now if I wanted to change roles from receiver to transmitter I can do that this is a transceiver module but both modules have to be aware that you're making this change you can all of a sudden change the receiver to a transmitter and expect the transmitter to know that has to go to receiver so that's where it gets a little tricky on the bi-directional communication once again not a big deal but just something to be aware of and we'll see it in the example so for my example we're going to get to the code in a second but if you want to access the library that I used you can see the link you should be aware though there's a lot of different versions of it so I'm picking one the latest at the time of this video okay let's look at the code so here we are at the transmitter code so as you can see I use the spy library because we're going to spy communication then I'm going to call two two dot H files for the RF 24 and this is included in the library and you can see I have the links there and then finally I have another call that I do to a dot H file that I actually have as part of my sketch so this is actually optional and this dotted dot H file is going to let me print out the status of my module just so you can see the sort of the state of the module once again this is optional from there I declare some variables so I'm going to set my sieep in and my CSN pin once again you can choose the pin you want to use for that I'm going to use nine and ten I set a counter variable to count the number of packets I'm going to send I use this done bowl to know when I'm done transmitting I then make a call to my my RF 24 library object and I name that object wireless spy and then of course you can see this is why the pins you can send what you want it asks for what you want for your c e and c SN pin and then i declare a constant here and this is actually my what they call a pipe I'm calling it P address because really it's just an address it's an it's a unique address for a pipe to communicate over for the transmitter and receiver okay next I get into my setup code I'm going to start my serial communication I'm going to start my printf which once again is optional we're using it to print the details of the module I then start my wireless spy or my RF 24 library object I set my auto acknowledgments which means that I'm saying that if you're a receiver you automatically will send an acknowledgment when you get a payload or packet from the transmitter I also enable the acknowledgment payload which means in my acknowledgement I can actually put data in there I then set retries so for instance if if a packet the transmitter where to send were to fail I'm going to retry it 15 times and I'm going to give a delay between each retry and that's basically 5 times 250 microseconds so it's one millisecond between retries I'm going to open my writing pipe and this is the function that basically turns this module into a transmitter module and you can see I feed in the pipe address I tell it to stop listening this is an optional function but this basically says you're not a receiver so don't listen for packets so once again this is my transmitter I then and this is optional as well I tell it to pray it's detail so it's going to print information about the module okay in my loop I say if I'm not done let's send a packet print my account of my packet I then I'm going to set up a timer so what we're going to do in this example is we're going to see how long it takes to get that packet over to the receiver and then for the receiver to acknowledge it so then I do a write so this is the transmitter I'm writing my count so that's my payload I'm saying that it's one byte in length and so then I send it and I have in this if statement because if it fails meaning the receiver never sends it acknowledgement packet I'm going to print that my packet delivery failed if it works then I do the else statement and you can see I basically grabbed another timer reading in microseconds I then calculate the round-trip time and then I'm going to print that out to my serial monitor and then down here what I'm doing is I'm checking if the acknowledgment packet had a payload so we're going to see with our receiver that at first it's not but in the last one it is so I'm going to run this loop I'm going to send ten different packets then I'm going to wait for the receiver to tell me to stop sending packets and then I'm done so I'll print out the round-trip time for each packet then when the receiver sends me the indication that I'm done I'm going to print that out and then I'm going to set my done variable to true and then it'll stop okay let's look at the receiver sketch this is my receiver sketch so this is what I'm going to upload to the arduino pro mini once again same library calls a lot of the same settings i have the same pipe address remember we have to use the same pipe address if we want to communicate between the modules then i get started my setup code and once again a lot of this is the same except instead of stop listening i'm going to start listening because I'm the receiver I should also mention too that when I open a reading pipe I not only feed in the address but I feed in the pipe number and that is because remember we can have up to six transmitters talking to the same receiver and I'm just telling this transmitter or this receiver I should say that this transmitter is communicating over pipe one okay so if we go to the loop we can see that we have an if statement so once we get up above nine so we get nine different packets or I should say ten packets we're basically going to send an acknowledgment payload to the transmitter telling it that we're done and we can see I'm storing that in this character array so once we're we get 10 packets we're going to tell the transmitter we're done and it will stop transmitting then down here I'm listening so I'm listening for the transmitter send me a packet when there's a packet available I'm going to go in here and I'm going to read it now one thing I didn't mention in the transmitter sketch that I'll mention here is when you pass in the variable to read the payload or even to send it you're basically using this ampersand this is basically saying don't put the variable into this function put the address of the variable so when this reads it it knows where the address of got byte is so it puts the data and got byte then I can use got byte later to print it out there's got byte I'm telling that the read function that it's one byte in length so I read it in I print out that I received packet number and then I print out the packet number so I ran this example on two serial monitors and let's look at the results okay here we are the results I have the receiver on the left and I have the transmitter on the right so I used cool term for the receiver and I used just the Arduino serial monitor for the transmitter now keep in mind you don't have to use both serum owners you can just use one serial monitor on the transmitter receiver everything will work fine but you can see at the top it prints out the details of the module then you can see that the transmitter says now I'm sending out packet one it then gives you the time for the acknowledgment packet so you can see most of them around five hundred forty four or four be something microseconds you can see one of them though jumps up fairly high my guess is there was a problem receiving the package so it had to send it a couple times and then at the very bottom notice I get my done acknowledgement so throughout this the transmitter sending payloads with the packet number the receivers then replying with an acknowledgement packet that it got it but there's nothing in that acknowledgment packet until we get to the very end where we send a character array okay that's it for getting started with the RF 24 transceiver if you want to access the code we went over go to my blog if you like what you saw subscribe to my youtube channel if you have any questions feel free to email me thank you for watching
Info
Channel: ForceTronics
Views: 82,907
Rating: 4.9328537 out of 5
Keywords: Transceiver, Arduino (Brand), nRF24L01, RF24, SPI, low cost, bluetooth, ISM, wireless, Nordic
Id: BjId_6tlYvE
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Mon Feb 16 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.