The nRF24L01 - Wireless Joystick for Arduino Robot Car with nRF24L01+

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll be working with the nrf2 for l01 transceiver modules we'll learn how these devices work and how we can use the Radiohead library to communicate between two Arduino with them we'll even build a wireless joystick for our robot car we're working without a wire today so welcome to the workshop [Music] hey welcome to the workshop today I'm going to be working with some devices that many of you have asked me about these are the nrf2 for l01 transceiver modules now for those of you not familiar with these modules these are little RF transmitter receivers that you can use with microcontrollers like the Arduino or micro computers like there as Barry Pi in order to exchange data at a reasonable speed over some fairly reasonable distances in fact with the correct antenna you can even approach a kilometer under the right conditions now I'm going to discuss how these modules work and show you a library that we have used before that you can use to communicate with these modules but I'm also going to show you a project that many people have asked me about and it's this you might remember I did a video on the L 298 n H bridge motor controller and in that video I connected an L 298 and H bridge and an Arduino up to a joystick and used it to drive two motors on a robot car base and I was able to steer the car by pushing the joystick forward to go forward pulling it back to go backwards and when I pressed it toward the side the car would steer off to that side now in that video I just wired it directly to the Arduino with a big long wire but many people have asked about doing a remote control project with that so today I'm going to do exactly that I'm going to take this little robot car now you might remember that video in which we built a robot car base that had a couple of speed sensors on it I'm taking the same robot car I'm adding it nrf2 for l01 into the system so that I can control this with a remote joystick and so I'll show you exactly how to build that so stay tuned there's a lot to cover today now before we start working with the nrf2 for l01 transceiver modules I want to say a couple of words about this robot car base now for those of you who haven't seen the video in the robot car base video I showed you how to assemble one of these car bases from an inexpensive little kit and I also showed you how to use these little speed sensors these little black disks that come with these kits we use them with a set of opto isolator z' in order to send signals to the arduino and we use our dwee no interrupts in order to measure those signals and measure the speed of the wheels and therefore control the car so it's a very useful video to watch if you haven't seen it it'll show you how to use the robot car base and also you'll learn a bit about arduino interrupts if you're not familiar with that concept already now having said that we are not going to be using the speed sensors in today's design however i'm not going to be removing them if you have seen the video and if you have built the robot car base based on the video first of all thank you very much for doing that but secondly you should know that i have a couple of wiring changes I needed to make specifically the changes going to the L 298 and H bridge motor controller the reason is that some of the pins I use conflicted with some of the pins I needed for the nrf2 for l01 transceivers so I had to move some of the wiring around and I also used a pin that you might not expect me to have used so you'll be learning another Arduino design technique that you might not know so if you have built the base pay particular attention to the wiring changes you're going to need to make having said all that now let's go and learn a little bit about the nrf2 4 l 0 1 RF transceiver modules the nrf2 for l01 is an inexpensive radio transceiver module based upon a chip designed by Nordic semiconductor it operates in two 2.4 to 2.5 gigahertz industrial scientific and medical or is M band it connects to external devices using a serial peripheral interface or SPI port it's used in many applications such as wireless mice remote controls and many industrial designs there are many configurations of the nrf2 for l01 today we'll be dealing with two popular styles one is a module with an integrated antenna the other is a module with the low noise amplifier and external antenna for improved performance now here are the pin outs of the nrf2 for l01 note that I'm showing the module with the internal antenna but the one with the external antenna has the identical pin ups this is also the top view of the module now pin 1 which is indicated with a square around the pin is the ground pin pin 2 is VCC this can be any voltage between 1.9 and 3 point 6 volts please note that the nrf2 for l01 cannot be powered directly with a 5 volt power supply pin 3 is the chip enable pin this is an act of high pin and when it's activated tells the tip to either transmit or receive depending on upon which mode it is currently in pin 4 is kip select not this is tied to the spi bus slave select and is an active low pin when it is activated it indicates that this particular module has been selected on the spi bus pin 5 is the s CK or clock pin this is the clock signal from the master on the spi bus pin 6 is master out slave in this is the output from the master which in our case is the Arduino or the input to the NRF 2 for L 0 1 pin 7 is master in slave out this is the output from the NRF 2 for L 0 1 pin 8 is the IR Q or interrupt pin this is an active low pin that sends interrupts generated by the NRF 2 for L 0 1 now I showed you in the animation there are a couple of different styles of nrf2 for l01 i wanted to show them to you on the workbench here right now this is the standard module that you'll pick up it's a very inexpensive module and I'm not sure if you can see this correctly but over here it's got a very squiggly little line on the printed circuit board that's actually an integrated antenna for the module so no external antenna is necessary for this this module is also an nrf2 for l01 as you can see it has an external antenna and that antenna is actually connected you can remove it if you wish I'll remove that now and this uses what's called an SMA connector which is a standard connector for antenna so you can connect another antenna to this as well in addition you might notice an additional tip on here and this is the low noise amplifier what this does is it takes the receiving signal and it amplifies it it doesn't do anything that a transmitting signal the transmitting signal is the same as the other module except it's enhanced by the better antenna but the receive signal goes through this low noise amplifier making it a lot stronger allowing this to communicate more reliably over greater distances now this third thing over here I'm going to show you in a moment it's a very handy little circuit board that you can use when you're working with the nrf2 for l01 what you do is you plug your nrf2 for l01 into the connector over here and then you can use these connectors to go out to your Arduino or Raspberry Pi or whatever it is you happen to be wiring to the key thing about this board is it also has a 3.3 volt voltage regulator on it so you power it with 5 volts instead of the 3.3 volts and as I'll get into in a moment there are a lot of advantages to doing this so I would strongly recommend that you pick a couple of these up as well it'll make your life a lot easier now before we begin our experiments I want to give you a few tips for working with the nrf2 for l01 because although these mod rules are very reliable and dependable they can be a bit difficult to get them working at times and most of those difficulties will boil down to two things either wiring or power supply now wiring simply means you have to make certain that you've got the right connectors going to the right pins on your Arduino and while that may appear obvious it often isn't you'll have to remember that different libraries for the Arduino use different pins for the nrf2 for l01 so you need to pay attention to which library you're using in your code and why are the module accordingly so if you're trying different sketches that use different libraries make sure you look at the wiring another thing is if you use one of these adaptor devices which I do recommend some of them including the ones I bought have mislabelling on them and they've taken the input at the output pin and reversed them so if you're having trouble to get things to work and you're using one of these devices check that out your input and output pins may just simply be mislabeled and you'll have to reverse the wiring on that now another problem with these devices though is power supply the nrf2 for l01 uses a 3.3 volt power supply as opposed to the 5 volt supply you're probably used to using now if you're using one of these modules it's great because you apply 5 volts to it it's got an internal voltage regulator and a couple of filter capacitors and it powers a module perfectly however you may be tempted to wire it directly to your Arduino and use the 3.3 volt output on the Arduino and in some cases that will work and in some cases it won't the reason is many Arduino clones including a lot of Arduino mega clones do not have sufficient current on the 3.3 volt output they use a very small voltage regulator and you'll find you have problems running these modules because the transmissions can sometimes take Peaks that exceed the value that those devices can provide now you can sometimes rectify that problem by putting a filter capacitor directly across the power supply leads on your RF something about a hundred microfarads or so has been proven to work well but again I would recommend just using one of these little adapters you can just wire it up to five volts and aside from that little pin mislabeling these things work wonderfully and will make it so much easier for you to experiment with these modules and he's only cost about a dollar or so they're not very expensive at all so having said that let's get started with our experiments now here are the connections for our demonstrations note that you will need to build two of these circuits one to act as a server and one to act as a client the connections on both are identical now we'll start with an Arduino Uno of course and I'm going to be using the nrf2 for l01 adapter now if you aren't using the adapter you can wire directly to the nrf2 for l01 but you'll have to pay attention to the power supply connection so we'll start off with the power supply connection if you're using the adapter connect the 5 volts to the 5 volt input on the adapter and the ground to the ground pin now if you're using the nrf2 4 l 0 1 by itself connect 3.3 volts to the NRF 2 4 l 0 1 VCC connection and the ground to the ground you'll also probably need to use a decoupling capacitor such as a 100 micro farad capacitor across the power supply line you may also find that that'll work better when it's powered with an external power supply such as a battery as opposed to being powered by the USB port if you're not using the adapter for both the adapter and the nrf2 4 L 0 1 the remaining connections are as follows the sieep in which is pin 3 on the NRF 2 for L 0 1 is connected to Arduino digital i/o pin 8 the CSN pin which is pin 4 on the NRF 2 for L 0 1 is connected to Arduino pin 10 the clock pin which is pin 5 on the NRF - for lo1 is connected to pin 13 the master out slave in pin which is pin 6 on the NRF - for l01 is connected to pin 11 the master in slave out pin which is pin 7 on the NRF - for l01 is connected to pin 12 note we are not using the interrupt pin from the NRF - for l01 in our experiments so now that we have two arduino z' wired up to nrf2 for l01 we're ready to begin our experiments now you may recall that when i was talking about wiring the nrf2 for ll 1 up to an arduino i mentioned that the pin outs would be different depending upon which library you chose to use now I've wired these up to use the radiohead library if you followed my video on using the 433 megahertz RF modules then you've already installed their radio head librarian to your arduino ide but if you haven't then you're going to need to do that and i'll show you how to do that in a minute it basically involves just downloading a zip file and installing it from that now radiohead not to be confused with the band that wrote the okay computer album is a library that was written by Mike McCauley for a company called airspace and it's an excellent library for all sorts of RF modules including the nrf2 for l01 and the library comes with a number of example sketches and we're going to be following some of these example sketches now in order to do this I'm transmitting the vast distance between this work bench over here and this work bench over here now this is all of about two meters or so and you could use either of the nrf2 for l01 modules you could use these small ones that have the integrated antenna or you could use the ones with the external antenna which is what I've chosen to use now even over this distance I found that using these modules with the integrated antenna although they worked fine when I got in between the two modules my body was actually a to block the signal and while I'd like to think that might be due to my magnetic personality it's actually true of any human body because after all the human body is mostly composed of water and water is pretty good at attenuating RF signals now using these antennas however I found that could transmit from any two points in my house so as an example my workshop is down in the basement in the very corner of the basement and I went to the second floor to a room on the opposite corner of the house and had no trouble doing the transmission going through all the walls and the floors and also the interference from the Wi-Fi router that I've got the next room from the Wi-Fi access point I have upstairs from a number of cordless phones in the house so it's really quite reliable using these ones with the antenna connection now you'll also notice I'm using two computers through these experiments and that's kind of ideal because I've got one for the server and one for the client now I realize that many of you won't have two computers but that isn't going to prevent you from doing the experiments what you'll need to do is compile and download the code individually onto each Arduino and then power them both off of batteries and use your computer to monitor one of the arduino x' and monitor the serial port so you can see what's going on so you can do this quite easily with just one computer but because I have a computer and each workbench here I'm going to do that so let's take a look at our first experiment using the Radiohead library so first we'll need to install the Radiohead library now as I said if you had seen the video I did on the 433 megahertz RF modules then you may have installed this library into your arduino ide already but if you haven't you'll first need to visit the Radiohead webpage and get the library I have a link to the page and the article associated with this video now at the very top of the page you'll see a link over here to a zip file with the latest version of the Radiohead library click on that and download the zip file to a location your computer that you're going to remember now go into your arduino ide and go up the sketch go to include library and do add zip library navigate to the location where you downloaded your library includes the library and click OK the library has now been added to your Arduino IDE now some people say that you need to restart the IDE in order to activate it but I haven't found that to be true however if you have odd results you might want to give that a try now the library itself comes with a number of example sketches and we're going to use a couple of them one for the receiver and one for the transmitter or in other words the server and the client so let's go and take a look at those sketches right now go under file go to examples and then scroll down in examples until you get examples from custom libraries and you should see Radiohead Radiohead is a number of sub menus so go to NRF 24 and let's take a look at the NRF 24 client let's expand that admit now the sketch starts off by including a couple of libraries the SPI library because as you recall we will be using the SPI interface to talk to our nrf2 4 l0 once it also includes their Radiohead NRF 24 library create an instance of that which we're calling an RF 24 in the setup we set that instance up so basically as long as it it initializes we'll set it up for a default of 2.40 2 gigahertz which is channel 2 then we'll change that over to channel 1 it's set up to a default data rate of 2 megabits per second and a transmit power of 0 DBM now bear in mind that with these modules if you reduce the data rate you can increase the distance that you're sending now log to the loop now recall that this is the client so we'll go into the serial monitor and we'll say we're sending something to the server and we're going to send a message to the server now we'll use an unsigned 8-bit integer in an array called data and we'll set that to the value of hello world and so that's the data that we're going to do and so we are going to send that data using a command that sends the data plus it needs to know the size of the data which we just use Arduino sizeof command to get so now that the packet has been sent we wait for a reply we use the buffer command to get the maximum message length believe is 32 bits minus a few overhead bits so it works out to be 28 bits and then we set the length of our buffer to the size of the buffer then we rate for a reply we take a look at the buffer that has that correct length inside it if we get that we say got reply and we print out our reply in the serial monitor if we don't get a reply after a timeout period then we'll print receive failed and so this will happen if the transmitter and receiver are too far from each other or if like I said if you get in the way of the two of them if we don't get any kind of a reply at all we'll print this no reply is the NRF 24 server right then we delay the whole thing for 400 milliseconds and start the loop over and over again so in our serial monitor we should observe the text that we are sending data and we're waiting for a reply and if we do get the reply we'll print it out so now that we've seen the client side let's take a look at the server side so we'll go back into examples go back to Radiohead go to NRF 24 I will do the NRF 24 server the complementary sketch to this one now this will be loaded on to our second Arduino so it starts off the same way by loading the SPI in the rh + RF 24 libraries and we set up an instance called n RF 24 this the setup is exactly the same we set up our serial monitor and we set up the NRF 24 we initialize it and set the value we change to channel the channel 1 you can experiment by painting these cattle numbers by the way you may find another channel works better in your location just make certain to change the channel on the receiver and the transmitter to match and after that it's very similar we are looking to see if we have a message inside the buffer and if we do have a message we will print got request and we'll print it and then we'll send the reply again we make an array and we say and hello back to you and we send that out as a packet and we print that we have sent the reply if we don't get anything from the other unit we put receive failed over here so load these up to the to arduino z' and then we'll take a look at the serial monitors and watch our server and client transmit and receive between each other alright I've got my demonstration running right now I've got it on two computers the computer on this workbench is my server computer and the computer on this workbench is the client computer and I've got the two serial monitors up so you can see them as well and as you can see from the monitors the server is sending a reply and got a request hello world sent to reply the client is saying sending to NRF 24 server it's got a reply and hello back to you or it's asking is the server running and it's doing that because I'm blocking the signal with my body as I mentioned before that will happen I'm using the small modules with the integrative antennas and that's the reason I'm getting these errors I actually wanted to do this so that I could show you the errors if I were to substitute these modules the ones with the long antennas there doesn't seem to be anything I can do to induce errors I even tried putting a metal pot over the top of the entire circuit board to block the signal and that wasn't even enough to block out the signal so these make a very strong signal so as you can see this works we're sending data back and forth between the two devices replying to the data when we get it on this device and so the first experiment works quite well so let's move on to another Radiohead library demonstration so as you saw with our experiment using the nrf2 for l01 with the Radiohead library is quite a simple thing to do and you can experiment by taking one side let's say the server side and powering it up with a battery and moving it around your home or if you can go outside taking it outside and seeing what kind of rain you can achieve if you're using the modules that have the external antenna you can achieve a fairly decent range but even with the other modules you shouldn't be able to get at least a couple of meters and you can also experiment with combinations where one unit uses the module with the external antenna and the other unit uses the module with the integrative antenna and see what kind of ranges you achieve in your environment now I want to move on to another sketch you'll probably have noticed there were a few example sketches in the RF 24 section of their Radiohead library and the one I'm going to use this time it's called the reliable Datagram server and client now what is a reliable Datagram well consider this and the sketch we just ran when one side transmits to the other side it has no idea if the other side successfully received the result it just goes and repeats transmitting but it doesn't know nor does it care whether the other side is actually receiving and for a lot of applications this is this fine for example if you're sending data from a temperature and humidity sensor to a remote location the temperature and humidity sensor doesn't really care if the remote location received it because it's going to be sending it again in a few seconds anyway so if it dropped off one hopefully it'll pick up the next time but if sending critical data such as an actual file such as a video file or something then you need a method so that the transmitting side knows if the receiving side has received a packet successfully and if it hasn't it'll retransmit it in fact if you're watching this on YouTube right now as I suspect you are this is going on on the video that you're watching because the tcp/ip protocol that we use on the Internet does exactly that it divides data into packets and sends them and if the packet isn't received by the other end it resends it now the reliable Datagram server and client experiment does exactly that it sets up what is called a reliable Datagram which has an address from both the client and the server and once one side receives a packet it sends an acknowledgement to the other side to let it know that that packet has been received if the other side doesn't receive that packet acknowledgement then it resends the packet so let's take a look at the reliable Datagram example very quickly because we're going to build our remote control for our robot car based upon this example so let's take a quick look at the reliable Datagram examples used in the Radiohead library now once again we go into file we go to examples and scroll down until we see Radiohead go into NRF 24 and we'll look at the reliable Datagram client first now I'll just quickly go over this to show you some of the differences between this and the two sketches you just read now the first thing is you'll notice is there's an additional library over here in addition to the two libraries that we ran before we also take the Radiohead reliable Datagram library then we define two things a client address and a server address now do not confuse these with channel numbers these are the addresses used within Datagram packets it self then after that we set up an instance of the driver like we did before and we set up an instance of the reliable Datagram that we're going to call manager and we set it with the driver and the client address because this is the client search site we'll do the same with the server address and the other sketch we initialize everything to the default and so we're sending everything on channel two now you could use some of the same elements as the last sketch to change the channel if you wished and then we basically put some data into an unsigned integer array again good calling our data hello world we set up a buffer and we open our serial monitor send out to the reliable Datagram server and we send a message to the server now this is the client side that we're talking about right now so we use the reliable Datagram Manager instance we use a send to wait command and we send the data to the server address then we wait for a reply as long as the manager receives an acknowledged one then we print out what the reply is if we don't get an acknowledged reply we send out no reply now remember all of the abdominal area itself we don't have to code for that so the code here is pretty simple if none of this works we'll just print out that the send to weight failed that we never received anything will delay for half a second and do it over and over again now let's take a look at the server side because you can imagine is quite similar so again we'll go into examples go to radio head and RF 24 reliable Datagram server which we will run on our other Arduino open that up once again we use the same libraries the same defaults through the client and server address set everything up again we will now be using the server address instead of the client address because we are the server side otherwise the code is basically the same as before our message this time is in hello back to you and if the manager is available again we wait for a message from the client if we receive the message from the client we'll print the message and then we'll send the message back to the server if nothing works over here this if this fails then we'll say send and wait has failed on our serve on our serial monitor so we've taken a quick look at this so now let's take a look at it in action okay time to test the reliable Datagram setup now once again this is my server and this is my client and I'll this get the serial monitor started on the server and as you can see the server is getting a request from the client with his address once got hello world and it's sending a reply to the client and hello back to you which is saying sending to NRF 24 and once again it's bouncing data back and forth remember in this particular case it's connecting to see if the packets of data actually arrived that they haven't arrived it's resending them so as you can see the reliable Datagram is very reliable and we will be using the reliable Datagram setup in order to build the remote joystick for our little robot car so having said that let's move on to the joystick demonstration now I realize that many of you are anxious to start working with the remote controlled robot car but before we do that I want to do a demonstration just involving the joystick the reason for doing this is because I want to illustrate how we can send data using the nrf2 for l01 now in the previous demonstrations the data we've been sending is all vin text like hello world and hello back to you but I want to show you how you can take data from a sensor and send it over at nrf2 for lo1 using the reliable Datagram method in the Radiohead library and so we're going to connect the joystick and send its values between one Arduino and the other ones so let's take a look at the joystick connections we'll need to make now adding a joystick is very simple we're going to take one of the Arduino as we've been experimenting with and leave the nrf2 for l01 attacked as it was before we will of course be requiring a joystick we'll take the VCC from the joystick and connect it to the 5 volt output of the Arduino we'll take the joysticks ground and we'll connect it to the Arduino ground we'll take the X or horizontal output of the joystick and connect it to analog input a 0 and we will take the Y or vertical output of the joystick and connect it to analog input a one on the other Arduino of course we will not be making any changes at all so now that we've seen how the joystick is hooked up let's take a look at the sketches we'll be using to demonstrate its operation so here's the sketch we'll be running on the Arduino which we connected the joystick to this is based upon the reliable datagram example which we just saw and you'll notice that a lot of it is very similar so it won't go into that in too much detail we start off as we did before by including the two libraries from Radiohead the reliable Datagram library and the NRF 24 library and then we include arduino spi library because these libraries are dependent upon that next we define the connections we made for our joystick so joystick X pin is connected to analog 0 input er a 0 and joystick Y pin is connected to a 1 once again we set up client addresses for the radiogram packets so client addresses 1 and server addresses 2 and we create an instance of the radio driver as we did before now we set the radio driver to the client address so we call it radio Manager set it to radio driver with our client address and now here's the key to sending some data we are going to send the data within an array so we define an unsigned 8-bit array which I'm calling joystick and I'm giving it three elements one is going to be for the x-axis reading one is going to be for the y-axis and the third one in this sketch is sort of a dummy one is just there to show you how you can send some additional data but in the final project the one with the remote-controlled car when we'll be using the third light in order to send Direction data then once again this is identical to the reliable Datagram example we define the message buffer now we go into our setup will set up our serial monitor and we initialize the radio Manager and we're using the default channel to two megabit per second 0 DBM now we go into the loop and then the loop will start off the serial monitor by printing out that we're reading the joystick values and then we're going to get those joystick values and put them into the elements of the array and we do this as follows joystick zero which is the first element of the array is going to be the value of an analog read from the joystick X pin so this is the x-axis now this will give us a value from 0 to 1023 because it's a 10 bit analog to digital converter so we're using the map command to map the value of 0 to 1023 to a value 0 to 255 so joystick 0 will end up being a value of 0 to 255 depending upon where we have the joystick positioned joystick 1 is the same thing for the y-axis and joystick 2 as I said we're just using a dummy value so I just put 100 in there you could use any value from 0 to 255 and that would be fine we're just going to constantly send that value to show how we send yet another value of data then some more stuff for the serial monitor we're just going to print those values and joystick 0 and joystick 1 value so we can watch them and compare them to their l results we get on the other end and then the rest of this is very similar to the reliable Datagram server example we saw the key over here is that when we are sending the packet you're sending it with joystick joystick is the variable and you remember that this is an array that contains all of our data so this is the key over here is to define an array and then to send the array over here the rest of this is identical to what we saw in a reliable Datagram server example we'll print out the reply we get from the other end if we don't get anything we'll ask if that other server is running and then we go through the loop and do it all over and over again reading the joystick values printing them out in our serial monitor and sending the array with joystick values to the other end so now let's take a look at that other end this was the transmitter side so now let's go and take a look at the receiver side now the receiver side is just about identical to the reliable Datagram example in fact there's only a couple of little modifications we've made to it and so we basically start off by including the libraries that are required defining the addresses to the client and server side within the radiogram setting up the instances that we set before right now we're setting this to the server address because this is the server side and message that we are going to be returning if we get valid data and in this case is joystick data received we define the message buffer and now we go into the set up and set up we start our serial monitor and we initialized a radio Manager again with the same defaults we did on the other side now we go into the loop and if the radio manager is available we wait for the message and we get the message inside the buffer and then we go and print it and this is where you'll see how we extract our data the buffer now contains an array so the x value is going to be the buffer value 0 the Y value is going to be buffer value 1 and Z value and the Z value is just a value that we used as a dummy value 100 in this particular case so the Z or Zed as you prefer value is going to be buffer number 2 and then we'll send our message back to the originator saying that we sieved it and then we go through the loop and do it all over and over again so we'll load our sketches on to our two respective arduino x' and we'll take a look at this working in action all right I'm writing the demo right now I've got the joystick on this side and I've got the receiver and on this side and we'll bring the serial monitors up so you can see the values on both monitors and observe what happens when I move the joystick around now on both Arduino Zhan using the modules the nrf2 for l0 ones that have the integrated antennas not the ones with the external antennas but when I do build the robot car I'm going to switch to the models with the external antennas because I want to get better performance and I don't want to get the dropouts that will occasionally get on the setup of God right now so observe what happens when I move the joystick I'm going to move the x-axis first and I've moved the x-axis all the way up to one end right now and as you can see the x-axis value right now is 254 255 and you can see that on both sides now I'm going to move it down and the x-axis is dropped to zero the y-axis is pretty steady in the center now I'll work with the y-axis I've moved it to the middle again moving the Y to one side right now and Y is equal to zero on both ends I'll move Y to the other end and Y is equal to 255 right now you'll notice the Z or Zed if you prefer value is always set at 100 and that's because I use the constant of 100 and so that's the only value being transmitted but what this illustrates is how we can send data from a sensor in this particular case the sensor being a joystick between two devices using an nrf2 for l01 and the radiohead reliable Datagram library so now that we've covered that there's nothing left to do except actually put together our remote-controlled robot car so let's take a look at what we need to do to do that so as I mentioned at the beginning of the video I'm going to be using the robot car base that I used in a previous video the one which I showed you how to use the speed sensors and you may want to take a look at that video if you want to assemble a similar robot car base you could actually use any base though it doesn't have to be this one as long as it's got two wheels and a base to hold the Arduino and a place to hold some batteries and the L 298 and motor controller you could really use any base that you wanted to but I'm basing this project around the last one now the last project used a couple of speed sensors in order to sense the wheel rotation they aren't going to be used in this project but if you did build the last project don't remove them or unwire them we're just not going to make use of them right now now one thing about this car base if you have built the previous one is that you're going to need to make some wiring changes specifically to the L 298 and motor controller a few of the wires on that conflicted with the wires I needed for the NRF 2 for L 0 1 so pay attention to the schematic diagram that I'm about to show you in order to see what those wiring changes are another thing you might notice is that I've used the style of nrf2 for lo1 that has the external antenna and I did that for a liability you could use the other one if you wanted to but I think it's far more reliable to use these especially if you want to drive your car at a reasonable distance and I also did this on the joystick side now for the joystick side of things there are no changes to make from our previous experiments so just leave your joystick wired up to the arduino and again on that side i replaced the NRF 2 for ello one with one of these ones with the external antenna you could do that as well if you wish for the optimum performance so now that we've talked about this car base let's take a look at the wiring we're going to have to use in order to wire all of this up and then I'll show you the sketches we're going to use and then finally we'll actually drive it around the room here's what we'll need to get our carb base running we'll need an Arduino Uno an l2 ninety-eight an h-bridge motor controller the nrf2 for l01 now I'm filling an adapter and I strongly recommend that you also use the adapter to make things so much easier with its onboard voltage regulator however you could use the nrf2 for l01 on its own if you insist you'll also need a couple of DC motors minor rated at 6 volts DC you'll need a power supply for those motors that is about one point four volts higher than the motor rating this compensates for the voltage drop in the l2 98n I'm using a 7 point 5 volt supply consisting of five double-a batteries I also used a nine volt battery to power the Arduino although you could use another power supply for the Arduino if you wish such as a USB power bank now here's how we're going to wire all of this up run the 5 volts from the Arduino to the 5 volt input on the l2 98n we also run the 5 volts up to the 5 volt input on the nrf2 for l01 adapter now if you're using an nrf2 for hello 1 by itself then you'll need to supply it with 3.3 volts instead do not connect 5 volts directly to an nrf2 for l01 the ground from the Arduino will go to the ground of the l2 98n and the ground of the nrf2 for l01 we'll connect our 7 point 5 volt power supply to the high voltage input on the l2 98n we'll connect both of our motors to the l2 98n the connections of the nrf2 for l01 are identical to what they have been in our previous experiments pin 3 the chip enable of the NRF 2 for lo1 will go to pin 8 of the Arduino pin for the chip select not of the NRF 2 for l0 1 we'll go to pin 10 of the Arduino pin 5 which is the clock input of the nrf2 4 hello 1 will go to pin 13 of the Arduino pin 6 which is the master out serial in will go to pin 11 on the Arduino and pin 7 which is the master in serial out we'll go to pin 12 on the Arduino now we'll connect that l2 98n notice if you use the previous project that some of these connections will be different the enable a line on the l2 98 n will go to pin 9 on the arduino now input one of the l2 98n will actually connect to analog input a 0 this is not an error input two of the l2 98n will connect to arduino output number for input 3 of the l2 98n will connect to arduino output number seven input four of the l2 98n will connect to our dueƱo pin number 6 and the enable b line on the l2 98n will connect to arduino digital i/o pin 5 finally the 9 volt battery will connect to the coaxial power input on the Arduino Uno and that's the wiring for our robot car base now let's take a look at the sketches will be using for both the joystick and the car base we'll begin by looking at the sketch for the joystick side of a remote-controlled car now this is very similar to the sketch we saw during the joystick demonstration and it's been amalgamated with a sketch that I did when I did the L 298 and motor controller video where I drove the car with a joystick but in that particular case it was disconnected with a wire so I've used a lot of the same code I used to determine forward and back direction by using the joystick and those of you who saw that video will probably notice that code within this code so we start by including our Radiohead libraries and the dependent SPI library then we define the connections we've made to our joystick a 0 and a 1 we also define a couple of integers to hold those joystick values now remember the Arduino uses a 10 bit analog to digital converter so these values lend up from 0 to 1023 so 512 pretty well puts it in the middle position we define the address for the radio channels and again connect create an instance of the radio driver and we set the radio driver to the client address then we declare an array that we're going to be using to hold the data that we're going to be sending to the car now we have 3 bytes declared so it's a 3 element array to the bytes are going to be the values of each of the motor speeds and the third byte is going to be the motor direction whether we're going forwards or backwards and then we define the message buffer as we did earlier now in the set up we set up a serial monitor which we can use to monitor on this side to see what our joystick values are and the troubleshoot if anything is going wrong and initialize the radio Manager again we also set the initial motor direction as forward so in our array motor control element 2 defines the direction 0 equals forward and a 1 will equal reverse so we set this up as a zero to set our motors going forward initially now we start off our loop by printing to the serial monitor so reading motor control values we send that up there and then we actually read those values so we assign joy pause vert and joy pause to the analog read values of the two pins the two analog pins now this will give them a value again of 0 to 1023 now we need to determine if it's a forward or backward motion and we do it by reading the vertical you if we're pressing the vertical toward the top then we're going forward if we're pulling it back we're going in Reverse so we want to leave a little bit of leeway because none of these joysticks is absolutely perfect so if the joy pause vert is less than 460 we're pulling back on it we're saying this is going backwards so we'll set the motor direction as backwards motor control to now equals one if it's I'll get to this bit in a moment but I just want to show you if it's joint pause vert is over five sixty four then we're going forwards and we'll set motor control two forward and if it's in the middle the motor control is stopped and also all of the variables are set to zero because the motor stopped and we just set the the direction is forward again okay so back in here if we're going backwards we want to determine the speed so we take these two variables motor control one and two and we map them to the speed now since we're going in Reverse we're doing the mapping and reverse so we're looking for values from 460 down to zero and mapping them in Reverse from zero to 255 now if we're going forward we do the opposite we take the values from five 64 to ten 23 and map them to a range of 0 to 255 okay now for the staring we do a similar thing again we establish 460 and 564 as the two limits and anything in between just means joysticks in the middle and we're going to pad the motor control values that we had earlier so again we're going to use a map command now if we're going left we're reversing the the reading so again from 460 down to 0 we go 0 to 255 and then on motor control 0 we're going to minus that value for motor control 0 to slow down that motor and motor control 1 we're going to add that value to speed the motor up and this way our car can steer to one side now we also want to make sure that we don't exceed 255 or go below zero so these statements take care of that now if we push the joystick in the other direction we do exactly the same thing only in Reverse we're going to math 564 to 1023 from 0 to 255 and the math here is this backwards from the math in the previous section and again we make sure we don't exceed exceed the range of 0 to 255 now this I just added at the end as I found any motor control value from 1 to 8 would cause the motors to make a buzzing sound so if the motor control value is less than 8 we already know it can't go below 0 then we'll just set it to zero we're going to display those values in the serial monitor and then the rest of this is identical to the joystick demonstration sketch that we did we are going to send motor control out in our packets and we're going to see if we got a response from the other end now at the end over here I want to mention this delay there's a delay over here to allow everything to take care of itself before we do the next transmission and you can experiment with this delay you might find a little bit of latency in this system and you can reduce that to a degree by reducing the value of this delay but you can only reduce it so much before you actually start to see transmissions drop off and everything because you're basically sending the data too fast so I urge you to experiment with this number if you want to so now that we've seen the transmitter side let's jump over and look at our receiver side so here's the receiver side the side that's going to run on the robot car itself now again we start everything off attentively to before with the same libraries and defined in the client addresses we also need to define the connections to the two motors now motor a is rather interesting enable a is this line number nine and the enables by the way both have to be on lines that the Arduino is capable of pulse width modulation on so pins 9 and pin 5 are capable of PWM and they weren't being used by the nrf2 for l01 you recall I had to change the connections from motor a from the original design while motor B stayed the same now this one might strike you as odd I have pin 14 here and those of you who know the Arduino Uno will say well wait a second it only goes to digital pin 13 this is where I use the analog 0 pin because the analog pins as it turns out can also be used as digital pins and analog 0 is pin 14 analog 1 is pin 15 out of love to pin 16 etc etc up to pin 19 on an Arduino Uno you could also have put a 0 over here and you might want to do that if you're going to build your Kinnear car with something other than an Arduino Uno just because the pin numbers might be different also keep remembering if you're using an Arduino mega there is actually a pin 14 you could have connected to but pin 14 is the same as analog 0 so I just wanted to point that out so we basically define the connections to the motor once again we're doing the same thing as we did before creating an instance of the radio driver and setting it up for the server address which is transmitting us our joystick data and we have a received message that we could send back joystick data received in future videos I'm going to show you other data that we can send back from our remote control car but for now we're just going to send this message just so the other end knows receive something and we set up the message buffer as we did before now we set up a serial monitor over here now and normal operation of course you won't have a serial monitor connected to your car so this is just for troubleshooting purposes and you could eliminate the serial commands later on in the final design if you wish we also need to set up all of the pins that were using as output so we do that and we initialize the radio manager as before ok now in the loop now if the radio manager is available we wait for a message and make sure we get it into the buffer the same as we did in the last demo now you might notice all of these have been remarked out right now this is just for troubleshooting purpose because again you normally won't have a serial monitor connected to the car and I wanted to eliminate these statements in the final code because they're dis kind of pointless and they take up CPU cycles that you could be using for driving your car but if you have trouble getting this to work just take the remark statements off of these ones over here and look at everything while you're connected to the serial monitor to make sure you're actually getting data from the joystick so we're going to look for buffer number two in our in our array it's a three three bit array and number two indicates the motor direction as you recall one is backwards and zero is forwards so if it's equal to one we set our motors backwards by writing to these four pins on the L 298 and to set everything to go backwards if it's not one then it must be zero so we're going to set everything forward and then after that it's a simple matter of driving the motors we drive the enable lines with the analog write commands with buffer one for a buffer zero for B and we'll send the return message back and keep going through the loop over and over again so actually the receive side is relatively simple when compared to the transmit side so now that you've seen both of these sketch let's go and take a look at our robot car in action and so now finally the moment you've been waiting for the robot car with the remote joystick is ready to test out now I've got it here in the workbench I'm going to lift it off of the workbench so it doesn't start driving all over the place and careening off the workbench and destroying itself but let's just check to make sure the motors are actually working so I'll push the joystick forward and then motors move I pull it backwards and they move in the other direct you know put it all to one side I have one motor moving the other side the other motors moving so it looks like it's functional but of course the true test is to give it a drive so I'm going to put it down on the floor and do exactly that [Music] [Applause] [Music] [Applause] [Music] [Applause] [Music] [Applause] [Music] well at last we've made it to the end of the video and I know this has been a particularly long video so if you have made it all the way through this video I must congratulate you I think you actually deserves some sort of reward now hopefully that reward is that you now know how to use the nrf2 for l01 radio modules and by combining them with the Radiohead library and some Arduino zs-- you've got all sorts of remote-control projects that you're dreaming up and that's a pretty good reward in itself now I would really urge you if you haven't already to subscribe to the channel it would mean a lot to me and would also mean that you would get notification every time I make a new video and I've got lots of new videos planned for you so stay tuned for those so in the meantime take care of yourself enjoy your robot cars and I'll see you again very soon here in the workshop good bye for now [Music] [Applause] [Music] [Applause] [Music] [Applause] [Music] you
Info
Channel: DroneBot Workshop
Views: 241,528
Rating: 4.9368176 out of 5
Keywords: NRF24L01+, wireless, Arduino, 2.4 GHz transceiver, nrf24l01 arduino tutorial, tutorial for Transceiver and arduino, RF24 Library, 2.4 GHz Wireless Communications, nrf24l01 tutorial for beginners, Arduino wireless, Arduino wireless tutorial, Arduino transceiver
Id: lhGXAJj8rJw
Channel Id: undefined
Length: 63min 46sec (3826 seconds)
Published: Sat Mar 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.