#38 Advanced Tutorial for NRF24L01 and Arduino #2 Reliable Connections

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
create see youtubers here is the guy with a Swiss accent again in the first video about the NRF 24 lo1 modules we were able to establish a physical connection between the two modules on the 2.4 gigahertz band if you missed this episode it might be good to view part 1 first in this video we will concentrate on the software part how to make the connection reliable how to pack our data in a simple and efficient way and of course how to establish the be directional data transfer at the end we will have to working NRF 24:01 nodes which are able to transport commands in one direction and receive telemetry data in the other to create telemetry data I use a TC s 3200 color sensor this is by far not an ideal color sensor because all three sensors also measure infrared light this effect makes that the measured values of all three colors will change even if I change only one color of the neopixels but for today I just use it to create some telemetry data and to prove that the overall system works but let's now continue with communication we saw in the first video that wireless communication easily can be disturbed by other devices or noise on the same frequency because this is unavoidable we have to build our devices that they can cope with this situation already in the early days of Morse a communication the operators established a procedure to solve this problem the sending operator sent a few words and stopped the receiving operator whether the content of the message he heard was correct if so he sent a short acknowledged signal back to the sending operator if the frequency was noisy the receiving person would not hear the message or not correctly then he would not acknowledge it and the sender after a short time repeated the same message again it could also happened that the message was received correctly but the acknowledged signal was somehow disturbed this had the same effect on the sender he had no acknowledge and had to repeat the message again in these days the messages were usually clear text and the receiver was able to judge whether the content was okay or not but of course only if he spoke the language of the transmitted message in my case this was definitely not true when I had to transmit Arabic names of refugees from Damascus to Geneva during the Lebanon War in 1982 and today the sender for sure cannot judge whether the message is right or wrong this is why checksums were invented at the end of each message a unique checksum is added to the message because the receiver knows how this checksum was calculated he can check if either the message or the checksum was correctly received these principles are implemented in the NRF 24 lo1 chip if you enable this function the sender adds a checksum and the receiver acknowledges the correct reception of the message back to the sender if the sender gets this acknowledge it continues with sending the next package if not it repeats the same package till it gets the acknowledged after a certain number of retries it stops and informs the sketch about the failure all this is done without your intervention by the chips itself pretty neat you can even set the maximal numbers of retries here we use 15 if we do not use this function data can be lost on the way from the sender to the receiver and we do not know about it so now we have a reliable connection between our two devices in the next step we have to find a way to package our data to a format which can be transported by the NRF chips the maximal package size between 2 and RF 24 lo1 chips is 32 bytes this means it could for example transfer the sentence the quick brown fox in one package because the sentence consists of 19 ASCII characters however it could not transfer the sentence the quick brown fox jumps over a lazy dog because this sentence has 42 bytes and is too long if you want to transfer this sentence you have to divide it into two parts this is not done by the chips for our example today we only need to transfer three integer readings of the potentiometers in one and three floating point readouts of the TCS 3200 into the other direction so we have to package these numbers into the 32 byte data package in order to keep it simple and stupid I use data structures a structure is a simple but very powerful thing in the arduino ide as we saw before the data structure in one direction is called all pack and in the reverse direction the data is packaged in the structure called display pack first you have to define the content of these structures let's use the control pack structure as an example the structure has to carry the integer values of the readings of a 0 a 1 and a 2 so we define the structure controlled F in a first step and in the next step we have to define the variable control pack using the newly created type this statement looks exactly the same as if you would define a variable as integer the only difference is that the type integer is predefined in the Arduino language a structure is stored in memory as consecutive bytes if we want to transfer it over our communication channel we just have to provide the memory address of the first byte of the structure and its length to the sending routine addresses in Arduino language are identified by an ampersand our structures are much smaller than the 32 byte maximum and we would have plenty of room for other data to add data just include its definition and name in the structure and the rest is taken care by the IDE and the NRF chips again very neat just for information a n RF 24 L or one chip can receive data from six different senders at the same time each pipe has to have a different address of course and keep in mind all these devices have to operate on the same Channel now we have everything to establish a reliable connection and can move on to the sketch in this example I use the same sketch for both arduino z' each arduino gets its role by reading an input pin during set-up if this pin is high the module executes the roll potentiometer and if it's low it executes the roll neopixel a simple jumper is used to select the roll the sketch starts with the initialization of the RF 24 and the neopixel objects and with the definition of the two pipe addresses in the setup we start the NRF module with a command radio dot begin and open the two pipes according to the assigned roles and set all the different parameters we discussed above the statement radio dot print details sends the actual configuration to the serial monitor here we can check if all the prerequisites like channels and transmission speeds are the same for the sender and the receiver very important for debugging then we set the devices in the listening mode which is default only if we send something we change the mode for a short period because we do not want to block the channel more than necessary in the loop part of the sketch we decide on the role we execute the role potentiometer reads the potentiometers and puts them into the control pack structure this is done with a statement control pack dot 0 equals analog read of a 0 after that we send the structure to the receiving station and wait for the acknowledge if we do not get it even after fifteen retries we know that the communication was not successful in our case we do not care and continue anyway we're listening to the response from the opposite side if a display pack arrived and is available we read it and update the statistics on the serial monitor if we have to wait too long we know that the communication was not successful and increase the error count if we are in the role neopixel we check if a control pack is available and if so we read it and display its values through the neopixels if not we read the color values with the TCS 3200 and transmitted it let's now check the system I have the neopixels put on the floor and the potentiometers on my table if I turn the potentiometers the color of the neopixels change success and the values returned from the color sensors also change success again let's now look at the success statistics it shows how many of the last 10 transmissions were successful with this setup you can now start your own tests I moved one of the devices into another room upstairs just to show you how important such tests are if I leave the NRF module alone the transmission is not working at all all 10 packets are lost all the time if I take the NRF module and move it just a few centimeters the success rate is 100% and the communication works perfectly this is because we work with microwaves and the wavelength is only a few centimeters you can get the NRF 24 lo1 chips on modules with a power amplifier and longer antennas because they are pin and software compatible you can just replace the modules and check the performance if I only change one transceiver it does not help a lot only if I change both the communication works if you are interested in radio frequencies you even can add lambdak water Grande planes or build a dipole antenna to increase performance of your devices but this is a whole other story I hope this episode was useful or at least interesting for you bye
Info
Channel: Andreas Spiess
Views: 43,059
Rating: undefined out of 5
Keywords: NRF24L01, Arduino, ESP8266, diy, electronics, wireless
Id: a2FBa4MK66M
Channel Id: undefined
Length: 13min 47sec (827 seconds)
Published: Thu Jan 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.