Raspberry Pi Pico: nRF24L01 over SPI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks my name is tim and welcome back to my channel so before i begin today's video i just wanted to take a moment to say thank you to everyone who's liked my videos or taken the time to leave comments and feedback because i really appreciate that this is a very young channel so in particular i'm grateful to everyone who subscribed that really helps me thank you all very much for taking the time to do that it really is appreciated and in fact today's video is actually something of a request because i had a few comments on my previous video around the esp-01 module asking for a video on the nrf24l01 that's uh this little module here so today we're going to take a look at this thing and learn how to use it so on my desk here i have two raspberry pi picos connected identically to two small buttons and two nrf24l01 wireless modules now if i hold down the button on this first picot here you can see the led on both of these picots turns on and when i release the button they both turn off now if i do the same on the other picot over here well the same thing happens and that's because these two picos are communicating wirelessly using these nrf24l01 modules we have a two directional wireless communication mechanism so let's take a look at how this module works and how the demo was put together the nrf24l01 is an easily obtainable wireless transmitter and receiver module that operates in the frequency of 2.4 gigahertz the data sheet for it is readily available online and the module claims a maximum range of around 100 meters although this will depend on the quality and configuration of the modules you're using in my demo here i'm using these little modules which feature built-in antennas making them quite small and compact but you can get modules like this one which have an external aerial connector which should give them a longer range so there's some degree of variety to these things and obviously pick whichever one is most appropriate for your application since the nrf24l01 is connected over the serial peripheral interface or spi the first thing we need to understand in order to use one is how spi works however for those of you who already have a good understanding of spi i've added some timestamps in the description of this video so you can just skip this part if you'd like to okay so spi was created by motorola back in the 1980s and much like the i squared c interface that we've previously looked at spi is a pretty ubiquitous mechanism for interfacing with microcontrollers it's a full duplex synchronous serial communication interface and that means as with any serial interface spi writes one data bit out at a time over a single communication line however it's also a full duplex interface that can both send and receive concurrently so it actually uses two communication lines one for sending and one for receiving since it's a synchronous interface it also requires a clock signal to synchronize communications between controllers and peripherals spi can communicate between multiple peripherals but unlike i squared c it doesn't use addresses instead it requires a gpio pin to be connected to each individual peripheral and this pin is used to select the desired communication target so as a minimum we need four connections to make spi work and we need an additional connection for each subsequent peripheral that we'd like to add this of course means the number of available gpio pins on our controller is a limiting factor when using spi however it can run much faster than i squared c making it an attractive choice for some applications so let's talk about terminology since spi is an old standard it uses the terms master and slave to identify the communicating devices and label the pins used obviously that's not something we find acceptable today and there have been several efforts to remove that sort of language from technical specifications for spi in particular the open source hardware association have proposed an alternative naming convention that replaces the term master with controller and slave with peripheral and i'll be using those names throughout the rest of this video first though let's take a look at how the pins are labeled using both the old and newly proposed names so using the old naming scheme we have the clock signal labeled sclk for serial clock next we have the mozzie and miso pins mozzy stands for master out slave in and miso stands for master in slave out and these are the spi data lines finally we have the slave select pin labeled ss and that's used to select the device we want to talk to in the newly proposed naming scheme these labels change to the following mozzy becomes copy for controller out peripheral in miso becomes kippo for controller in peripheral out and ss becomes cs for chip select so with our terminology established there's one more thing we need to talk about before we can start using the interface sbi is what's called a de facto standard and that means it became a standard due to wide scale adoption by vendors rather than through any formal agreement and this has led to a number of variations on the theme of spi briefly these tend to be around the clock pulse polarity that is whether the clock is considered active high or active low and around the clock phase or timing and this is where the data is transmitted on the leading edge of the clock pulse or on its trailing edge of course the combination of these differences gives rise to a number of permutations so it's often necessary to check this with your peripherals data sheet before you can start using it luckily software libraries exist for many common peripherals that take care of all these details and this is indeed the case for the nrf24l01 so now that we understand the basics of spi let's look at how to wire up the nrf24l01 module specifically as this is a 3.3 volt module we'll be powering it from the raspberry pi pico's on-board 3.3 volt regulator broken out at physical pin 36 next we need a ground connection and i'm using physical pim38 for this simply because it's conveniently close to the 3.3 volt supply pin next we connect the csn or chip select not pin to gpio pin 15 and this is the spi chip select pin for the nrf24l01 chip select is active low so when that pin is driven low the module becomes the active peripheral for spi communication next to the csn pin we have a pin labeled ce for chip enable and this is used to control the transmitter in the nrf24l01 when this pin is set high the transmitter is enabled and messages can be sent and received when the chip enable is set low the module enters a lower power standby mode next we have the copy pin or mozzy pin in the old naming scheme and that's connected to gpio pin 7 on the pico labeled spi-02 or transmit after this we have our clock signal and this is connected to the spi zero clock pin on the pico at gpio pin number six the next pin labelled is provided to let the peripheral raise and interrupt on the controller so it can respond to important events but we don't need to use this today so we're not going to connect it finally we come to our kippo pin or miso pin in the old scheme and this is connected to the picos spi zero rx or receive pin and that's broken out at gpio pin number four okay so that's all there is to connecting up this module purely for the purposes of our demo the little push button is connected between gpio pin number 28 and the 3.3 volt supply okay with the hardware side of things covered now let's take a look at the software conveniently there exists a micropython driver for the nrf24l01 and you can find it in the micropython git repository on github under the drivers folder and i'll leave a link in the description now this driver is going to make working with the nrf24l01 much easier as it takes care of all the spi interface specifics we just need to tell it what to send and when however if you're interested in what's going on under the hood then this code is actually very easy to follow and it's well worth reading over in order for our demo software to import this driver module we need to place it into the pico storage to do that you just need to load it up in thoni go to save as select the pico from the pop-up and then save the file using the name nrf24l01.py and this only needs to be done once unless you need to modify the driver for some reason right so now let's take a look at our demo software so as usual we begin by importing the necessary modules that we're going to need we're obviously going to need the pin module to define some gpio pins the spi module because we're using an spi based peripheral and the struct module because we're going to pack some binary data after that we then go ahead and import the nrf24l01 driver module that we previously prepared with our imports completed we define a block of pins so the first pin is used to control the onboard led the second pin is used to read back the push button status and then we have the csn and ce pins and these pins are needed to tell the nrf24l01 driver which pin we have connected to the chip select not input and which pin we have connected to the ce or chip enable input right so with our pins defined we then need to go ahead and define two pipeline addresses now these are needed because each nrf24l01 module can communicate with up to six other peers and this is achieved by using the concept of pipelines each pipe has a unique address and can be used to either send or receive messages if each peer listens on its own specific pipe then it will only ever get the messages that were intended for it since in our demo here we're doing two-way communication we need to define two pipeline addresses one for messages destined for the first picot and one for messages destined for the second and in fact the only difference in the software running on the first picot versus that running on the second picot is the order of these pipeline addresses because obviously on one of those picos we need to flip these addresses so the receiving pipe becomes the transmit pipe and the transmit pipe becomes the receiving pipe so with our initialization completed we encounter the first of our functions and that's this little setup function here now this function is just going to create an instance of the nrf24l01 driver to do that we need to provide the spi interface we'd like to use now the raspberry pi pico actually has two interfaces so here we're providing the first one because that's the one we've wired up we also need to provide the csn pin object and the ce pin object finally we also need to provide a payload size and that's because the nrf24l01 transmits fixed sized packets now for our demo we're just sending over a 32-bit integer value and that's of course four bytes in size so we say we'd like a payload size of four bytes please so with the driver object constructed we can then go ahead and open our transmission pipe and our receiving pipe and then we just start listening and at this point the nrf24l01 is actually ready to start receiving messages finally before we return from our setup function we make sure that our led is initially turned off right so that's all there is to the setup function after that we just have our little demo function here and this is the main body of this program so all we're doing here is initializing a state value to zero and then entering the main loop so within this loop we have two sections the first section here is concerned with checking the local state of our button so we compare the button state to the previously stored state and if they don't match then we know the button has either been pressed or released so we'll record that state and set the value of the led accordingly but of course we also need to transmit this information over to our counterpart pico so that's what this little bit of code does here initially we need to stop listening because now we're going to transmit we then attempt to transmit by using nrf.send and then struct.pack and finally the state so we pack the state into a little binary object and we use nrf.send to transmit it now this is wrapped inside of a try block because the nrf24l01 actually expects to use a reliable communication mechanism now reliable in this context doesn't mean that your messages are always guaranteed to arrive this is a wireless communication after all and messages can get lost but what it means is that when a message is received by the recipient that message should be acknowledged so a little response message should come back to say yep i got your message everything is fine now if the driver doesn't get that response message back in time it raises an os error exception to suggest that the message may not have arrived and that gives us an opportunity to do something sensible like maybe re-transmit the message if we care enough however when i first set up this demo i was always getting this error message no matter what i did and i could see that the message was received successfully but the driver didn't seem to think so now i got around that problem by enabling something called auto acknowledgement but we'll take a look at that in just a moment anyway once we've finished with our transmission we need to return to the listening state in case the other picot has a message that it wants to send to us and that's what this second part of the loop actually takes care of so here we're saying if nrf.any and this means if we have any data to read then go ahead and read it so we use nrf.receive to read back the message we use struct.unpack to return the integer that we previously packed and then we just set the led status to whatever that message contained and that's how we're able to switch the led on on both picots when we press the button on either of them right so let's talk about the auto acknowledgement feature that i mentioned a moment ago while reading through the data sheet i came across something called enhanced shock burst and this seems to be a proprietary name for a selection of useful features and functions that are built into the hardware for dealing with things like automatically acknowledging transmitted messages and automatically resending failed transmissions however these features are not enabled by default in the nrf24l01 driver that i downloaded from micropython which suggests that perhaps this enhanced shock burst stuff isn't built into every nrf24l01 module it is available on the modules that i'm using so i've just turned on the auto acknowledgement feature because it's very convenient so to do that i've created this little function here so what this does is to write into one of the registers of the nrf24l01 and of course that's done over the spi interface now the driver actually provides this little reg right function to take care of the specifics of doing that so we just need to provide the address of the register we'd like to write to and a value that we'd like to place into it so that's what i've done here so this address here 0 1 is the register address of the auto acknowledgement feature and this value here is actually a series of flags so the first five bits are used to enable auto acknowledgement on the different pipelines that the nrf24l01 might have and the final three bits here are currently reserved and must be set to zero so this little function here basically turns on auto acknowledgement for all available pipelines pretty handy in fact i think i'll probably end up writing a little patch for the micro python driver to expose these features in case they're useful for anyone else anyway so that's the source code for our program to run it we just call the setup function to build our little driver object then we enable our auto acknowledgement mechanism and finally we call the demo function which is of course the main loop of the program and at that point both picots are now listening for messages and monitoring their buttons when a button press comes along they stop listening and they transmit nice and simple so there we go that's how to use the nrf24 module with the raspberry pi pico and micro python now as always i'll upload all of this code to github so feel free to go grab it and check it out and of course if you want the nrf24l01 driver you'll have to go and get that from the micro python repository link in the description thank you for watching and i hope this has been useful
Info
Channel: Tinker Tech Trove
Views: 28,885
Rating: undefined out of 5
Keywords:
Id: aP8rSN-1eT0
Channel Id: undefined
Length: 19min 4sec (1144 seconds)
Published: Sat Apr 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.