Raspberry Pi 3 Tutorial 14 – Wireless Pi to Arduino Communication with NRF24L01+
Raspberry Pi 3 Tutorial 14 – Wireless Pi to Arduino Communication with NRF24L01+
Difficulty level: Beginner
Approx reading time:
Components Required:
1. Raspberry Pi 3 model B
2. Arduino Uno
3. MicroSD card 8 or 16 GB (Class 4 and above) with Raspbian – 2
4. Windows PC / Linux PC (Tested on Windows 10, Ubuntu 14.04 LTS)
5. Ethernet cable (Category 5 – also called Cat 5)
6. Micro USB cable and USB cable type A/B (for Arduino)
7. Breadboard
8. NRF24L01+ Transceiver – 2
9. Jumper Wires (Male to Female, Male to Male)
Way to go ->
1. Login to both your devices using PuTTy or your Ubuntu terminal.
2. Login to both your devices’ GUIs using VNC server (Linux / Windows).
Do the following for both the devices:
3. In the terminal type:
1
|
sudo raspi–config
|
and press Enter on your PC keyboard.
Follow steps 7 – 9 of RPi 3 Tutorial – 11 for steps 4 – 6.
4. Go to Advanced Options.
5. Go to SPI.
6. Enable SPI Interface by clicking on <Yes>.
7. Reboot the Pi. In the terminal, type:
1
|
sudo reboot
|
8. In the terminal type:
1
|
sudo apt–get update
|
9. After it has rebooted, install Python development tools. In the terminal, type:
1
2
3
|
sudo apt–get install python–dev. # python2
sudo apt–get install python3–dev. # python3
|
10. Download this package. In the terminal type:
1
|
wget https://github.com/Gadgetoid/py-spidev/archive/master.zip
|
11. In the terminal type: ls to check that you have master.zip in your current working directory.
12. To unzip, in the terminal type:
1
|
unzip master.zip
|
13. Remove master.zip:
1
|
rm master.zip
|
14. Navigate to py-spidev-master directory(folder):
1
|
cd py–spidev–master
|
15. Type in the terminal: ls, to view the contents of the directory(folder).
16. In the terminal run the commands:
1
2
3
|
sudo python setup.py install # python2
sudo python3 setup.py install # python3
|
17. Navigate to the desktop by running the following commands. In terminal type:
1
|
cd ..
|
Press Enter on your PC keyboard.
1
|
cd Desktop/
|
Press Enter on your PC keyboard.
18. Create the following directory(folder) and navigate to it. In terminal type:
1
|
mkdir NRF24L01
|
Press Enter on your PC keyboard.
1
|
cd NRF24L01/
|
Press Enter on your PC keyboard.
19. In the terminal type:
1
|
git clone https://github.com/Blavery/lib_nrf24
|
20. Navigate to directory just downloaded. In the terminal type:
1
|
ls
|
Press Enter on your PC keyboard.
1
|
cd libnrf24/
|
Press Enter on your PC keyboard.
1
|
ls
|
Press Enter on your PC keyboard.
21. Copy lib_nrf24.py to the NRF24L01 directory. In the terminal type:
1
|
cp lib_nrf24.py ~/Desktop/NRF24L01/
|
22. Navigate to the NRF24L01 directory:
1
2
3
|
cd..
ls
|
23. In Python 3 (IDLE), create a new file.
24. The name could be: ReceiveArduino.py. Save it in the NRF24L01 directory(folder).
25. In the file ReceiveArduino.py, write the following code with comments (line starting with “#”) for clear understanding and save (press Cntrl + S on your PC keyboard) the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while(1):
# ackPL = [1]
while not radio.available(0):
time.sleep(1 / 100)
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print(“Received: {}”.format(receivedMessage))
print(“Translating the receivedMessage into unicode characters”)
string = “”
for n in receivedMessage:
# Decode into standard unicode set
if (n >= 32 and n <= 126):
string += chr(n)
print(“Out received message decodes to: {}”.format(string))
|
26. Open the Arduino IDE.
27. In the above menu bar go to: Sketch -> Include Library -> Manage Libraries…
28. Search for RF24, select RF24 by TMRh20 Version 1.1.6 and click Install. After installation close the library manage.
29. Write the following program in the Arduino IDE and Upload it to the Arduino. Send.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//Send.ino
#include<SPI.h>
#include<RF24.h>
// ce, csn pins
RF24 radio(9, 10);
void setup(void){
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0x76);
radio.openWritingPipe(0xF0F0F0F0E1LL);
radio.enableDynamicPayloads();
radio.powerUp();
}
void loop(void){
const char text[] = “Hello World is awesome”;
radio.write(&text, sizeof(text));
delay(1000);
}
|
30. Finally, run the script on the Pi by clicking on Run -> Run Module in the menu bar or by pressing F5 on your PC keyboard.
ReceiveArduino.py output
31. For a master/slave setup.
SendReceiveArduino.py(Pi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
# radio.startListening()
message = list(“GETSTRING”)
while len(message) < 32:
message.append(0)
while(1):
start = time.time()
radio.write(message)
print(“Sent the message: {}”.format(message))
radio.startListening()
while not radio.available(0):
time.sleep(1 / 100)
if time.time() – start > 2:
print(“Timed out.”)
break
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print(“Received: {}”.format(receivedMessage))
print(“Translating the receivedMessage into unicode characters”)
string = “”
for n in receivedMessage:
# Decode into standard unicode set
if (n >= 32 and n <= 126):
string += chr(n)
print(“Out received message decodes to: {}”.format(string))
radio.stopListening()
time.sleep(1)
|
SendReceive.ino(Arduino)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
//SendReceive.ino
#include<SPI.h>
#include<RF24.h>
// CE, CSN pins
RF24 radio(9, 10);
void setup(void){
while(!Serial);
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0x76);
radio.openWritingPipe(0xF0F0F0F0E1LL);
const uint64_t pipe = (0xE8E8F0F0E1LL);
radio.openReadingPipe(1, pipe);
radio.enableDynamicPayloads();
radio.powerUp();
}
void loop(void){
radio.startListening();
Serial.println(“Starting loop. Radio on.”);
char receivedMessage[32] = {0};
if(radio.available()){
radio.read(receivedMessage, sizeof(receivedMessage));
Serial.println(receivedMessage);
Serial.println(“Turning off the radio.”);
radio.stopListening();
String stringMessage(receivedMessage);
if(stringMessage == “GETSTRING”){
Serial.println(“Looks like they want a string!”);
const char text[] = “Yo wassup, haha”;
radio.write(text, sizeof(text));
Serial.println(“We sent our message.”);
}
}
delay(100);
}
|
SendReceive.ino(Arduino) output
SendReceiveArduino.py(Pi) output
Hardware Connections ->
1. Raspberry Pi 3 GPIO Header.
2. nRF24L01 pinout
3. nRF24L01 to Pi connections.
4. nRF24L01 to Arduino Uno connections.
Read full tutorial at
Rating widget:
No Comments Yet
Categories: Uncategorized
Leave a Reply
Your email address will not be published.
.END