{"id":175,"date":"2014-12-29T10:48:56","date_gmt":"2014-12-29T10:48:56","guid":{"rendered":"https:\/\/daqdesign.wordpress.com\/?p=175"},"modified":"2025-06-10T08:23:35","modified_gmt":"2025-06-10T08:23:35","slug":"arduino-rf-communications-using-the-nrf24l01","status":"publish","type":"post","link":"https:\/\/daq-design.co.uk\/blog\/?p=175","title":{"rendered":"Arduino RF communications using the nRF24L01+"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The&nbsp;<a title=\"Nordic Semiconductor Rf24\" href=\"http:\/\/www.nordicsemi.com\/eng\/Products\/2.4GHz-RF\/nRF24L01\" target=\"_blank\" rel=\"noopener noreferrer\">nRF24L01+<\/a>&nbsp;is a great little device to allow RF data exchange between two or more arduino devices. They are very inexpensive and you can purchase 4 units from ebay for under \u00a35. There isn&#8217;t a library for the module within the core ardino libraries and the best library I found is written by a guy called manicbug and you can find information on how to download and install the library on his <a title=\"Manicbuf RF24 blog\" href=\"http:\/\/maniacbug.wordpress.com\/2011\/11\/02\/getting-started-rf24\/\" target=\"_blank\" rel=\"noopener noreferrer\">website<\/a>&nbsp;. The rest of this post implies you&#8217;ve installed the library to your ardunio&#8217;s library directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I learnt a lot thanks to manicbug&#8217;s blog and various other posts which I will note at the end of this post if you want further background reading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Regrettably, I could not get any of the examples working correctly despite checking and rechecking the connections, configuration. As the code was a little hard to understand ( I hate classes,pointers and anything OOP) I found I could not penetrate the code successfully. This is a limitation on my part and isn&#8217;t a slight against the library&#8217;s author.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, I went on a search on the <a title=\"RF24 discussion on arduino.cc\" href=\"http:\/\/forum.arduino.cc\/index.php?topic=62222.0\" target=\"_blank\" rel=\"noopener noreferrer\">arduino forum<\/a>\u00a0and towards the end of the discussion there are mentions of powering up and down the radio device to ensure successful transmission. I then implemented the example provided on <a title=\"Simple RF24 Rx\/Tx example\" href=\"http:\/\/shanes.net\/another-nrf24l01-sketch-string-sendreceive\/\" target=\"_blank\" rel=\"noopener noreferrer\">shanes.net<\/a>\u00a0with the mods provided by <a title=\"MLC's radio mod\" href=\"http:\/\/shanes.net\/another-nrf24l01-sketch-string-sendreceive\/#comment-203\" target=\"_blank\" rel=\"noopener noreferrer\">mlc<\/a>\u00a0and the packet exchange was then flawless. My modified code is shown below &#8211; This is the transmitter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;SPI.h>;\n#include &lt;nRF24L01.h>;\n#include &lt;RF24.h>;\n#include &lt;RF24_config.h>;\n\/*\nThis sketch sends a string to a corresponding Arduino\nwith nrf24 attached. It appends a specific value\n(2 in this case) to the end to signify the end of the\nmessage.\n*\/\nint success,fail;\nchar msg&#91;1];\nint i=77; \/\/ Lowercase 'a'\nRF24 radio(9,10);\nconst uint64_t pipe = 0xE8E8F0F0E1LL;\n \nvoid setup(void)\n{\nSerial.begin(9600);\nradio.begin();\nradio.openWritingPipe(pipe);\n}\n \nvoid loop(void){\nradio.powerUp();\ndelay(1);\n\/\/ int messageSize = theMessage.length();\n\/\/ for (int i = 0; i &amp;lt; messageSize; i++) {\n\/\/ int charToSend&#91;1];\n\/\/ charToSend&#91;0] = theMessage.charAt(i);\n\/\/ radio.write(charToSend,1);\n\/\/ }\n\/\/send the 'terminate string' value...\nmsg&#91;0] = 'H';\nbool h=radio.write(msg,1);\nif (h==false)\n{\nSerial.println(\"Failed to tx HIGH \");\nfail++;\nSerial.print(\"failed to send \");\nSerial.print(fail);\nSerial.println(\" packets\");\n \n}\nelse\nSerial.println(\"Sent HIGH OK\");\nradio.powerDown();\ndelay(500);\n \n\/*delay sending for a short period of time. radio.powerDown()\/radio.powerupp\n\/\/with a delay in between have worked well for this purpose(just using delay seems to\n\/\/interrupt the transmission start). However, this method could still be improved\nas I still get the first character 'cut-off' sometimes. I have a 'checksum' function\non the receiver to verify the message was successfully sent.\n*\/\n \nradio.powerUp();\ndelay(1);\nmsg&#91;0] = 'L';\nbool l=radio.write(msg,1);\nif (l==false)\n{\nSerial.println(\"Failed to tx LOW \");\nfail++;\nSerial.print(\"failed to send \");\nSerial.print(fail);\nSerial.println(\" packets\");\n}\nelse\nSerial.println(\"Sent LOW OK\");\ndelay(500);\n \nradio.powerDown();\n\n msg&#91;0]=i;\n radio.powerUp();\n \ndelay(1);\nbool shite=radio.write(msg,1);\ni++; \/\/ incremenet counter to next alphanumeric character\n\nif (i>122) i=77; \/\/ 122='z' reset to 'a'\n\n\n\nif (shite==false)\n{\nSerial.println(\"Failed to tx random char \");\nfail++;\nSerial.print(\"failed to send \");\nSerial.print(fail);\nSerial.println(\" packets\");\n}\nelse\nSerial.println(\"Sent random char OK\");\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Receiver Code.<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;nRF24L01.h>\n#include &lt;RF24.h>\n#include &lt;RF24_config.h>\n#include &lt;SPI.h>\n \n\/*\nThis sketch receives strings from sending unit via nrf24 \nand prints them out via serial.  The sketch waits until\nit receives a specific value (2 in this case), then it \nprints the complete message and clears the message buffer.\n*\/\n \nint msg&#91;1];\nRF24 radio(9,10);\nconst uint64_t pipe = 0xE8E8F0F0E1LL;\n\/\/ int lastmsg = 1;\n\/\/ String theMessage = \"\";=\nvoid setup(void){\n  Serial.begin(9600);\n  pinMode(8,OUTPUT);\n  digitalWrite(8,LOW);\n  radio.begin();\n  radio.openReadingPipe(1,pipe);\n  radio.startListening();\n}\nvoid loop(void){\n  if (radio.available()){\n    bool done = false;  \n      done = radio.read(msg, sizeof(msg)); \/\/1); \n   \/\/   char theChar = msg&#91;0]\n      \n      if (msg&#91;0]==76) \/\/ got character 'L'\n      {\n        digitalWrite(8,LOW);\n        Serial.print(\"rx = \");\n        Serial.print(msg&#91;0]);\n        Serial.println(\"\");\n      }\n      else if (msg&#91;0]==72)  \/\/ got character 'H'\n      {\n        digitalWrite(8,HIGH);\n        Serial.print(\"rx = \");\n        Serial.print(msg&#91;0]);\n        Serial.println(\"\");\n      }\n      \n      else \/\/ a character was received, but it's one we weren't expecting !!!\n      {\n      Serial.print(\"Received invalid data \");\n      Serial.print(msg&#91;0]);\n      char ewwor=msg&#91;0];\n      Serial.print(\" which is the character \");\n      Serial.println(ewwor);\n      }\n   }\n\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">There ya go! Happy new year \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;nRF24L01+&nbsp;is a great little device to allow RF data exchange between two or more arduino devices. They are very inexpensive and you can purchase 4 units from ebay for under \u00a35. There isn&#8217;t a library for the module within the &hellip; <a href=\"https:\/\/daq-design.co.uk\/blog\/?p=175\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[7,8,16,24,28],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-nrf24l01","tag-arduino","tag-manicbug","tag-rf24","tag-uno"],"_links":{"self":[{"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=175"}],"version-history":[{"count":14,"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions\/542"}],"wp:attachment":[{"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daq-design.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}