Raspberry Pi Tutorial 34 – Wireless Pi to Arduino Communication with NRF24L01+ Part 1

in this video I’m going to show you how to connect a nrf2 for l0 1 plus transceiver to a Raspberry Pi and an Arduino and establish communication between the two using these transceivers were you going to need to complete this tutorial are two transceivers obviously a Raspberry Pi and Arduino and some jumper cables to be able to connect it all together these transceivers are not necessarily all wired up the same and so you’re going to have to check your data sheet to make sure you know how your pin out is on your particular transceiver I’m going to get started by wiring up this one to my Raspberry Pi VCC is connected to 3.3 volts ground-to-ground CSN to CEO that is GPIO 8c e 2 GPIO 17 m OSI to GPIO 10 SC K 2 GPIO 11 M ISO to GPIO 9 and we will not be using the ir q pin so let’s get started now we’re going to wire up our second transceiver to our arduino VCC will be connected to 3.3 volts ground to ground CSN to digital 10 c e to digital 9 mo SI to digital 11 M ISO to digital 12 and s CK to digital 13 let’s go ahead and get started wiring this up to our Arduino so we’ve wired up our Raspberry Pi to our transceiver and our Arduino to our transceiver first we’re going to start writing the program we need on our Raspberry Pi to receive a message from the Arduino so the Arduino is going to send out a hello world message and the Raspberry Pi is going to receive it and display it so here I have an instance of my Raspberry Pi desktop and over here I have an instance of the terminal opened and we’re going to start on the terminal of the Raspberry Pi so first we need to configure our Raspberry Pi using sudo rasp ID and enabled the SPI on it so go to Advanced Options SPI yes you would like it enabled yes you would like it loaded by default excellent next we’re going to have to update our PI this is so that we can get our packages for actually using the transceivers so sudo apt – get update now that we have updated our PI we can install some packages so this will work for both Python 2 and python 3 and you need to install the appropriate packages to be able to get it to work in both languages so in Python 2 you need two apt-get install Python – dev if you’re running Python 3 you need Python 3 – dev and yes we want to install these packages I already have these packages installed so this won’t take me very long now that we’ve done that we’re going to download a package that allows us to install the ability to use our SPI so you do that by typing in W get HTTP colon slash slash github.com slash gadget toyed with a capital G slash PI SPI dev slash archive slash master dot zip this will download a file called master dot zip we’re going to want to unzip master dot zip which will now create a high SPF – master folder so now that we know that folder is there let’s clean up by removing master dot zip and then we’re going to navigate into our PI SPI dev – master folder in here you’ll see some make files so let’s get some installation happening here if you’re following along in Python – sudo python setup pi install if you’re following along in Python 3 sudo python 3 setup PI install excellent so we’re almost done getting all the packages we need the next thing we need to do is the library use to communicate to our transceiver is a C++ library but we want to write our code in Python because pythons nice and easy and because that’s what all my videos are in so just keeping myself consistent here let’s navigate to our desktop and create a new folder called nrf2 for l01 and let’s navigate to that folder so as I was saying this package if we want to be able to use the transceiver in Python we’re going to have to download another package that allows us to do that so get

clone HTTP colon slash slash github.com slash b el capital b and i’ll a free /lib underscore NRF 24 so this creates a folder in our NRI folder here called Lib underscore 24-hour Lib underscore n RF 24 and you could more or less stop here because it has a whole bunch of example files if you want to poke around in them but what’s really important here is this file lip underscore n RF 24 if we’re going to write a script we’re going to import this module and set up our transceiver from it so I’m just going to separate that by copying it over into our n RF 24 master folder that we have and navigate back to that n RF 24 folder so now we have the folder here which we could delete and we have this Python file which we need to keep before we can start writing our Python script so over on the desktop of the Raspberry Pi I’m going to open up an instance of Idol 3 and we’re going to create a new file and I’m going to save this folder I mean I’m going to save this file on the NRF 24 l01 folder and I’m going to call it receive Arduino dot pi you can probably guess what this script is going to do so as I said we’re going to send out a message on Arduino that says hello world and we’re going to receive it on the Raspberry Pi first things first we’re going to have to get some packages imported so import our Pi GPIO as GPIO we’re going to from that module blip underscore n RF 24 we’re going to import an RF 24 we’re going to import time and we’re going to import our SPI we’re going to set up our GPIO so GPIO dot set mode GPIO dot DCM and this way when we when we put in the values for our pin pin connections to the transceiver will it’ll know what we’re talking about and then we’re going to set up addresses for our transceivers like a like a send and receive address and this has to be an array of bytes so I’m going to be creating two addresses here and the reason I’m doing that is ultimately in the next video I’m going to show you how you can establish back-and-forth communication which goes a bit above and beyond simple one-way communication that I’m demonstrating in this tutorial so we’re going to need a send and a receive address so we’re creating one address here and we will create another address here all right so we have our two unique addresses populated in a value here called pipes and now we can set up our radio now this is going to seem like a lot and that’s because it is but once we have it set up we’re pretty much ready to rock and the rest will be simple Python and actually using the radio so don’t get too scared we’re going to create an instance of our radio and then we’re going to begin the radio what we’re passing in here is the ce0 value that’s GPIO eight this is for your CSN pin and we’re going to pass in the c e value which is GPIO 17 now we have a bunch of options that we can play with such as setting up the payload size the maximum size you can set is 32 so let’s set it to 32 as in 32 bytes we can set the channel so you can have multiple devices connected and you have different channels on which you can communicate in this example we’ll be using channel 76 because why not we can also set up our data rate and in this example we’ll be using one of the slower but more secure data rates that’s the one megabit per second so the slower you go the better your range is generally speaking it doesn’t really matter because we’re on the desktop here and the the receivers are literally four inches apart but you know work with me here and we can set up our power level and this is important if you want better range or if you want to conserve your battery so right now we will be going minimum because they are so close together then you can set up things like Auto acknowledgments so Auto

AK will set auto Act to true and we can enable dynamic payload so let’s do that Micke payloads and ultimately we want to enable an acknowledgment payload so when you send a message out you can send back hey yes I actually received this message from you and that’s important because you want to know that you’re heard right now since this is a receiving setup that I’m setting here we’re going to be opening up a reading pipe and that reading pipe is going to be the second entry in our pipes address here so this F 0 F 0 F 0 e 1 address that we specify here and then we’re going to print the details and what this allows you to do is like when you execute this in terminal it will print the details that you can then review these settings that I’m setting up here have to match most of them have to match whatever is on the Arduino so payload size channel data rate power application doesn’t setting up if you if you’re working with your acknowledgments you’ll have to do that and your address is of course will have to be the same now since this is the Raspberry Pi that will be listening so we’re going to start listening for that hello world message we might as well start listening now the rest of this will be simple Python so we’re going to set up a while true loop here and what’s going to happen is it’s going to sit and it’s going to wait and it’s going to wait until a message is actually delivered so this while not loop here is going to check if it’s received anything and if it hasn’t it goes back to sleep until something actually happens at which point it breaks out of this a while true loop then we will be receiving our message so let’s create an instance of a received message that will be an empty list and we are then going to read from our radio and populate our received message with whatever we get from our radio and then we need to get the dynamic payload size so we’ve received our message and let’s just print a dummy statement here that says we received this and what did we receive we received our best received message right great now the thing is is the data that is sent across this radio is a byte like single byte at a time up to 32 byte payload and a byte is made up of 8 bits and 8 bits can only yield the number between 0 and 255 so anything that we get will be 32 numbers between 0 and 255 well how do you get a letter right like let’s say we want to send a string how do we get a string well you can encode your string characters like each character has like a Unicode equivalent byte value associated with it and since we’ll be sending strings here we need to decode whatever we get in our received message into strings so that you and I can understand what it is that’s going on so let’s print a statement here that says translating our received message into unicode characters and then we’re going to create an instance of an empty string to which we will then go append whatever the characters from our received message are so for whatever value we have in receive message so iterate over the length of received message we are going to check if and because we could receive a null byte for example so we need to not necessarily append our null byte so if n is greater than or equal to 32 and n is less than or equal to 126 which is an arbitrary range that I chose based off of the charts that I looked at for unicode characters we are going to append to our string the character equivalent of n so it will decode the byte value n into whatever letter or symbol it represents and then we can say our receive message decodes to this and that will be string all right perfect and that’s it for the Raspberry Pi Python portion so now we can give that a save and start programming our Arduino I will be programming the Arduino on my Mac side here now it doesn’t matter if your PC Mac or Linux you are going to be able to do this regardless as long as you have the arduino ide installed which is needed to actually program the Arduino we’re also going to have to download some packages so over here on

the Left I have the link HTTP colon slash github.com slash t mr h 20 / RF 24 go there and you can download this zip file oh and then extract it or you can do it the easy way which is opening up an instance of your terminal I navigated to my desktop here and then you can just type in git clone HTTP colon slash slash github.com / TM RH 20 / RF 24 and this will download the package directly to your desktop excellent so it says it’s done let’s take a look at our desktop here and we have our folder RF 24 right here with a bunch of stuff inside of it great so now we’re going to have to actually import this library into our we know I IDE so here using the arduino ide it’s a little bit different on pc but it’s more or less the same you’re going to import a library you’re going to add your library and then you’re going to point to that folder that is on your desktop the RF 24 folder here and you’ll choose that I have it already installed here because I’ve done this before so import library there it is RF 24 so you’re going to need this installed for your Arduino and work cool alrighty so let’s get to write in the program I’m going to save this file in that RF 24 folder just because then it is all there together and I’m going to call it send send yeah there you go so this will be sending the hello world example so let’s let’s get started importing the library so we’re going to need our SPI because this is how the transceiver communicates we’re going to need to include our RF 24 library that we just downloaded because duh and now we will begin our radio so RF 24 radio and I’m specifying 9 and 10 here and these are the digital pens to which your c e and c SN are connected to so I said Digital 9 digital 10 so that’s how it knows what’s what and then we are going to set up our setup so set up its board and we are going to begin our radio we’re going to set the power amplification level and this does not have to match our Raspberry Pi but you know this is what you choose if you are concerned about how much power you really want to deliver you’re going to set the channel now this does have to match what’s on your Raspberry Pi and if you’re following along that was channel 76 we are going to open up a writing pipe since we will be writing to our Raspberry Pi and the address will be F 0 F 0 F 0 F 0 o 0 e 1ll and this matches the one in the Raspberry Pi the FC here your five entries here F 0 F 0 F 0 of F 0 e 1 and the ll on the end there and then we are going to enable our dynamic payloads and then we are going to power up our radio simple right and it’s very similar to what we actually wrote on the Raspberry Pi which makes things a little bit more convenient because you don’t have to remember quite as much we’re then going to set up an hour loop here and it will be a very simple loop and all that we’re going to do is set up a character called text which will be our message hello world so we have our hello world statement there and then we’re just going to write it we’re going to write our text message there and we have to send over the size of our text message and then because I don’t want to spam the Raspberry Pi too much I’m going to set up a one-second delay here so let’s give this a save and let’s plug in our Arduino now so our Reno is wired in and let’s upload this let’s end with a semicolon on our delay there before we save and then let’s upload this sketch alright cool so it’s uploaded now and every second it is sending out the message hello world or at least that’s the idea over here on our Raspberry Pi

we have our receive Arduino dot I file so to execute that type in sudo python 3 receive arduino pi launch that and scrolling up here a bit we get our initial like setup data so we have our two addresses that we specified and then we have our bitrate and our model what it is this sort of information and then here we have it updating saying what is going on so we received this message which are the bytes representing our decoded message which is hello world so there you have it hello world from the arduino to the Raspberry Pi using a nrf2 for l0 1 plus transceiver sending over hello world is nice and all but one way communication can only take us so far let’s say our Raspberry Pi was a master and our Arduino was a slave with sensors connected to it maybe we want to ask the arduino for our a temperature reading and then later an ultrasound distance reading well we’re going to have to send a message to the arduino which the art we know then interprets so it must receive it interpret it and then send something back to the Raspberry Pi simple decision-making type stuff so I’ll be covering that in the next tutorial see you then