nRF24L01+ And Raspberry Pi
Further to my previous post I’ve moved onto using the Raspberry Pi as the receiver.
There’s a lot of incorrect documentation, and even incorrect comments in the libraries, so I thought I’d better note down what I’ve done.
First we need to get the Pi version of the RF24 library from GitHub, so on the Pi:
git clone https://github.com/stanleyseow/RF24.git
cd RF24
cd librf24-rpi/librf24
make
sudo make install
I couldn’t get the hardware SPI version to work, so stick with librf24 not librf24-bcm.
I then made the following C++ program for the Pi receiver:
#include <cstdlib>
#include <iostream>
#include "../RF24.h"
using namespace std;
// spi device, spi speed, ce gpio pin
RF24 radio("/dev/spidev0.0",8000000,25);
void setup(void)
{
// init radio for reading
radio.begin();
radio.enableDynamicPayloads();
radio.setAutoAck(1);
radio.setRetries(15,15);
radio.setDataRate(RF24_1MBPS);
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(76);
radio.setCRCLength(RF24_CRC_16);
radio.openReadingPipe(1,0xF0F0F0F0E1LL);
radio.startListening();
}
void loop(void)
{
// 32 byte character array is max payload
char receivePayload[32];
while (radio.available())
{
// read from radio until payload size is reached
uint8_t len = radio.getDynamicPayloadSize();
radio.read(receivePayload, len);
// display payload
cout << receivePayload << endl;
}
}
int main(int argc, char** argv)
{
setup();
while(1)
loop();
return 0;
}
And compile using:
g++ -Wall -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -L../librf24/ -lrf24 pi_rf24ping_rx.cpp -o pi_rf24ping_rx```
Use the following code (slightly modified from last time) on the Arduino transmitter:
```cpp
#include <SPI.h>
#include <RF24.h>
// ce,csn pins
RF24 radio(9,10);
// init counter
unsigned long count = 0;
void setup(void)
{
// init radio for writing on channel 76
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0x4c);
radio.openWritingPipe(0xF0F0F0F0E1LL);
radio.enableDynamicPayloads();
radio.powerUp();
}
void loop(void)
{
// 32 bytes is maximum payload
char outBuffer[32]= "";
// pad numbers and convert to string
sprintf(outBuffer,"%2d",count);
// transmit and increment the counter
radio.write(outBuffer, strlen(outBuffer));
count++;
// pause a second
delay(1000);
}
Wire the Pi (rev2) up like this - note the CE pin has to go to GPIO 25, not 22 and not CSN as stated elsewhere:
radio 7 (miso) to gpio9/bcm21
radio 6 (mosi) to gpio10/bcm19
radio 5 (sck) to gpio11/bcm23
radio 4 (csn) to gpio8/bcm24
radio 3 (ce) to gpio25/bcm22
radio 2 (3.3v) to 3.3v
radio 1 (gnd) to gnd
And on the Arduino (tested on an ATmega328P and Mega2560) like this:
radio 7 (miso) to uno d12 (pin 18) or mega d50
radio 6 (mosi) to uno d11 (pin 17) or mega d51
radio 5 (sck) to uno d13 (pin 19) or mega d52
radio 4 (csn) to uno d10 (pin 16) or mega d48
radio 3 (ce) to uno d9 (pin 15) or mega d49
radio 2 (3.3v) to uno/mega 3.3v
radio 1 (gnd) to uno/mega gnd
It seems to have the same, or maybe slightly less range than the two Arduino’s, i.e. across the house (through 2 walls) and no further, which is why I’m thinking of buying a couple of these modules with a power amplifier and noise suppression circuit (PA+LNA) and an external 2dB antenna, as apparently they increase the range by up to five times - still 1km is bullshit, but apparently 250m is achievable.
I really wish my USBasp would hurry up and arrive as I’m having real problems uploading sketches to the 3.3v Boarduino with the CP2102A, even with caps+crystals.
Next I need to experiment with 3-4 AA batteries powering the ATmega328P, DS18B20, DHT11, BMP085 and nRF24L01+. 3x AA rechargeables should give about 3.6v, which I could drop to around 3v for the Vcc pin on the RF24 using a 1N4001 diode, the rest are 3.0-5.5v tolerant. The alternative is using 4x AA to give 4.8v with a LM1117T regulator to give the whole circuit a steady 3.3v