NanoframeWork And NRF24L01_PA_LNA And ESP32

0 11423 Medium

Send And Receive Data With NanoframeWork And NRF24L01_PA_LNA

projectImage

Things used in this project

Ā 

Hardware components

HARDWARE LIST
1 DFRobot FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
1 NRF24L01_PA_LNA
1 Male/Female Jumper Wires
1 CP2120

Software apps and online services

Ā 

NET nanoFramework Visual Studio extension

Ā 

visual studio 2019

Story

Ā 

Hi friends :)

Ā 

I want to share another Nanoframework project with you.

Ā 

let’s start with the commutations Device!

Ā 

Today commutations are a very important concept.

Ā 

We have many devices and protocols to commutate with other things, for example, Bluetooth or Wi-Fi or wire….

Specially Commutation is important in IoT Projects when we want to control a remote car or lamp or ….

projectImage

I worked before with esp32 Wi-Fi and simple wire.

Ā 

So I decided to work with the new Module that name is NRF24L01_PA_LNA

NRF24L01_PA_LNA is a Wireless Module that works with SPI

projectImage

I prepared a base Nanoframework library to control NRF24L01_PA_LNA. (AP.NanoFrameWork.NRF24L01PALNA)

Actually, I’m working on it and I will continue developing this library for Nanoframework.

Ā 

Ok, let’s show you my library usage with a sample project:

Ā 

First, we need two ā€œesp32 wroom32ā€ develop kits. (I have a develop kit v1 and develop kit v4)

projectImage
projectImage

we need two NRF24L01_PA_LNA Modules

Ā 

Step1: Connect NRF24L01_PA_LNA to esp32 develop kits.

Ā 

GND to GND

Ā 

VCC to VCC 3.3

Ā 

IRQ to GPIO 17

Ā 

CE to GPIO 16

Ā 

MOSI to MOSI (GPIO 23 or GPIO 13 ??)

Ā 

MISO to MISO (GPIO 19 or GPIO 12 ??)

Ā 

SS to SS (GPIO 15 or GPIO 5 ??)

projectImage

When we look at esp32 pinouts, we find out it has 2 interfaces for SPI (VSPI, HSPI), The question here is that which interface must be connected to NRF24L01_PA_LNA ??!!!

projectImage

To find the answer, let’s take a look at the Nanoframework spi samples

projectImage

Hmm, with this sample I can’t understand which interface Nanoframework spi library is performed?!

Ā 

After much R&D I found it; Nanoframework SPI library works with SPI1. So, we need to set PinFunction for SPI1.

I used VSPI. So, I should set PinFunction like this:

projectImage
projectImage

Now, we should initial SPI with This Parameter:

projectImage

Note: this parameter is initialized according to NRF24L01PALNA Document

Ā 

Then, with AP.NanoFrameWork.NRF24L01PALNA, we can send and receive data easily:

Ā 

Initial:

projectImage

Note: You can set PA and Data Rate on both sender and receiver

Ā 

Sender:

Ā 

Initial TX

projectImage

or

projectImage

Send Data

projectImage

Receive:

Ā 

Initial RX

projectImage

Begin Receive

projectImage

Some parameters are hardcoding in my library, but I will develop it that and make it more flexible

projectImage
projectImage
projectImage

Tx

CODE
var gpioController = new GpioController();


            var p1 = Configuration.GetFunctionPin(DeviceFunction.SPI1_CLOCK);

            if (p1 != 18)
            {

                Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
                Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
                Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);

            }

            var irq = gpioController.OpenPin(17);
            irq.SetPinMode(PinMode.InputPullUp);
            irq.ValueChanged += Irq_ValueChanged;

            var ce = gpioController.OpenPin(16);
            ce.SetPinMode(PinMode.Output);

            var csn = gpioController.OpenPin(5, PinMode.Output);


            Thread.Sleep(200);


            SpiDevice spiDevice;
            SpiConnectionSettings connectionSettings;


            SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
            Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
            Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
            Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
            Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");

            foreach (var data in spiBusInfo.SupportedDataBitLengths)
            {
                Debug.WriteLine($"  {data}");
            }

         
            connectionSettings = new SpiConnectionSettings(1, -1);
            connectionSettings.ClockFrequency = 4_000_000;
            connectionSettings.DataBitLength = 8;
            connectionSettings.DataFlow = DataFlow.MsbFirst;
            connectionSettings.Mode = SpiMode.Mode0;


            // Then you create your SPI device by passing your settings
            spiDevice = SpiDevice.Create(connectionSettings);


            Thread.Sleep(50);


            NRFActions nrf24 = new NRFActions(spiDevice, csn, ce);



            ////Read Status
            ////..................


            nrf24.InitialTXMode(NRFActions.PAState.Min, NRFActions.DataRate.d1Mbps);


            Thread.Sleep(100);


            while (true)
            {
                nrf24.SendData("Hi My Name Is Alireza Paridar.)");
                Thread.Sleep(1000);
            }

RX:

CODE
var gpioController = new GpioController();


            var irq = gpioController.OpenPin(17);
            irq.SetPinMode(PinMode.InputPullUp);
            irq.ValueChanged += Irq_ValueChanged;

            var p1 = Configuration.GetFunctionPin(DeviceFunction.SPI1_CLOCK);

            if (p1 != 18)
            {

                Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
                Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
                Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);


            }
      


            var ce = gpioController.OpenPin(16);
            ce.SetPinMode(PinMode.Output);




            // var csn = gpioController.OpenPin(15, PinMode.Output);
            var csn = gpioController.OpenPin(5, PinMode.Output);
     

            Thread.Sleep(200);


            SpiDevice spiDevice;
            SpiConnectionSettings connectionSettings;
            Debug.WriteLine("Hello from sample for System.Device.Spi!");
            // You can get the values of SpiBus
            SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
            Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
            Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
            Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
            Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
            foreach (var data in spiBusInfo.SupportedDataBitLengths)
            {
                Debug.WriteLine($"  {data}");
            }

            connectionSettings = new SpiConnectionSettings(1, -1);


            connectionSettings.ClockFrequency = 4_000_000;//96000;//10_000_000;
            connectionSettings.DataBitLength = 8;
            connectionSettings.DataFlow = DataFlow.MsbFirst;
            connectionSettings.Mode = SpiMode.Mode0;



            // Then you create your SPI device by passing your settings
            spiDevice = SpiDevice.Create(connectionSettings);

            Thread.Sleep(50);


            NRFActions nrf24 = new NRFActions(spiDevice, csn, ce);

    

            nrf24.InitialRXMode(NRFActions.PAState.Min, NRFActions.DataRate.d1Mbps);


            while (true)
            {
                Thread.Sleep(1000);

                var buf = nrf24.ReciveData();

                if(buf==null || buf.Length <= 0)
                {
                    continue;
                }

                string res = "";
                for (int i = 0; i < buf.Length; i++)
                {
                    res += $"{buf[i].ToString()},";
                }

                Debug.WriteLine(res);
                Debug.WriteLine("");

                string txt = System.Text.Encoding.UTF8.GetString(buf, 0, buf.Length);
                Debug.WriteLine(txt);
                Debug.WriteLine("------------------");

            }

I Have many ideas to create many projects with this Module, and I’m working on them. I will share the results when I finish them.

Ā 

If you want, you can join me on GitHub to develop this library.

Ā 

My GitHub (github.com/AlirezaP)

Ā 

AlirezaP/AP.NanoFrameWork.NRF24L01PALNA (github.com)

Ā 

NanoFrameWork makes it easy to deal with the hardware. Forget low-level c or c++ code, just focus on the Ideas ;)

Schematics

projectImage
icon nrf24l01pluss_preliminary_product_specification_v1_0_sU2sqmxyEL.zip 990KB Download(6)

Code

icon AP.NanoFrameWork.NRF24L01PALNA-main.zip 1KB Download(6)

The article was first published in hackster, February 22 2022

cr: https://www.hackster.io/Alirezap/nanoframework-and-nrf24l01-pa-lna-and-esp32-dc510e

author: AlirezaP

License
All Rights
Reserved
licensBg
0