Communication Practice Between Raspberry Pi and Arduino

Communication Practice Between Raspberry Pi and Arduino

Limited-time resource download: Reply “Tutorial” to get the microcontroller e-book, replySimulation to get Proteus simulation materials, Baido cloud group sharing link update time: 2016-03-26, if invalid, please leave a message at the end of the article, do not leave a message in the background

Recently, I need to transmit data between Arduinos and between Arduino and the host computer (Raspberry Pi). Although the original APC220 device is usable and convenient, the cost is too high and not easy to mass-produce, so I sought other solutions.

1. Solution Selection

Based on search results and previous experience, the following feasible solutions are available:

  • nRF24L01+ (RF)

  • ESP8266 (WIFI)

  • XBee (ZigBee)

  • ENC28J60 (LAN)

  • W5100,W5500 (LAN)

Among them, Xbee is the best choice, but the cost is too high. The LAN solution is inconvenient, the WIFI solution has high power consumption, and the Bluetooth solution has a short transmission distance, so the RF solution is considered, which balances cost and effect well. The nRF24L01+ is cheap (about 5 dollars, the one with good workmanship and antenna is around 15), programming is simple, and there is an RF24 library that supports Raspberry Pi, Arduino, and Linux simultaneously. The Git link is: https://github.com/TMRh20/RF24.git

Cheap (generally made) nRF24L01 development board:

Communication Practice Between Raspberry Pi and Arduino

2. Wiring

1. nRF24L01+ Pin DiagramCommunication Practice Between Raspberry Pi and Arduino– 1: Ground – 2: 3.3V (must not connect to 5V, it will burn) – 3: CE (RF read and write control pin) – 4: CSN (chip select pin) – 5: SCK (SPI clock) – 6: MOSI (SPI master out slave in) – 7: MISO (SPI master in slave out) – 8: IRQ (external interrupt)

2. Wiring Method

Number nRF24L01 Arduino Mega Arduino UNO Rpi (Physical Pin)
1 GND 9
2 VCC 1
3 CE 7 (customizable) 7 (customizable) 15 (GPIO22)
4 CSN 8 (customizable) 8 (customizable) 24 (SPI0CS1)
5 SCK 52 13 23
6 MOSI 51 11 19
7 MISO 53 12 21
8 IRQ

Wiring Diagram:Arduino UNOCommunication Practice Between Raspberry Pi and Arduino

Arduino MegaCommunication Practice Between Raspberry Pi and Arduino

RaspberryPi3Communication Practice Between Raspberry Pi and Arduino

3. Code & Running

The GettingStarted example included in the RF24 library is very convenient, its code includes both sending and receiving types, defaults to receiving mode, switches to sending when inputting T, switches to receiving mode when inputting R, and has simple timeout judgment. For better understanding, the code can be simply modified to let the receiver return an incrementing number.

1. Arduino1) Install the RF24 library by downloading RF24 from https://github.com/TMRh20/RF24.git, and copy it to the libraries directory under the Arduino installation directory. After starting ArduinoIDE, select RF24->GettingStarted from examples.

2) The sending end code does not need modification, just compile and upload it directly. (Note the selection of UNO and Mega and the serial port selection) Change the radioNumber in the receiving end code from the default 0 to 1. As follows:

123 bool radioNumber = 0; (self is 2Node, send to 1Node) ->bool radioNumber = 1; (self is 1Node, send to 2Node)

In short, 1Node is the receiving end, and 2Node is the sending end. Suggestion: The got_time in the original code is not easy to observe and understand, you can assign a static incrementing value to the got_time in the receiving end before sending.

3) After starting the sending end, input T to enter sending mode. The receiving end can start without inputting R. (Defaults to R receiving mode) If the wiring is normal as mentioned above, the output of the sender and receiver can be seen in the Serial Monitor, roughly as follows: sending end diagram (static incrementing variable):Communication Practice Between Raspberry Pi and Arduino

2. Raspberry Pi In this article, the Raspberry Pi used is the RPi3 B model released in 2016, and its pin diagram is as follows:Communication Practice Between Raspberry Pi and Arduino

1) Install the RF library by copying the RF24 library to the Raspberry Pi (or get it directly via git). Enter the RF24 directory and execute the following command to compile and install (select SPI mode)

12 ./configure --driver=SPIDEVsudo make install -B

2) Modify the system configuration

12345 Modify /etc/modprobe.d/raspi-blacklist.conf, if there isblacklist spi-bcm2708, comment it out. Modify the /etc/modules file, add a line to enable SPI.spidev

After rebooting the Raspberry Pi, two device files spidev0.0 and spidev0.1 will be added under /dev.

3) Code modification Modify the RF24/example_linux/GettingStarted.cpp file, like the Arduino above, the sending end does not need modification, the receiving end changes radioNumber from the default 0 to 1, and it is recommended to change the timestamp data returned to an incrementing number. Execute make in the current directory to generate the binary file for GettingStarted.

4) Run using sudo ./ GettingStarted and input 0 to enter receiving mode. If the configuration and operation of the Arduino sending end are normal, it will respond normally. Roughly as follows (incrementing variable version):Communication Practice Between Raspberry Pi and Arduino

4. Notes & Insights

The cheap version of nRF24L01 has a general effect and is easily interfered with. The one with an antenna is better. Do not skimp on the project. Wiring must be accurate, and the SPI principle should be understood.

5. Simple Explanation of RH24 Example Code

Below is the Arduino example included with RH24 (TMRh20), which is simply explained. The version implemented on Raspberry Pi is in C language, the variables and syntax are slightly different, but the logic is basically the same.

  • Variable Definition

12345678910 bool radioNumber = 1; // RF node name determines Flag /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */// Specify CE to use GPIO7, CSN to use GPIO8, needs to match the wiring// If the wiring does not use 7, 8, this code needs to be modified.RF24 radio(7, 8); byte addresses[][6] = {"1Node", "2Node"}; // Two node names bool role = 0; // Sending & receiving mode Flag
  • Initialization Function

1234567891011121314151617181920 void setup() { Serial.begin(115200); Serial.println(F("RF24/examples/GettingStarted")); Serial.println(F("*** PRESS 'T' to begin transmitting to the other node")); radio.begin(); radio.setPALevel(RF24_PA_LOW); // Open a writing and reading pipe on each radio, with opposite addresses if(radioNumber){ radio.openWritingPipe(addresses[1]); radio.openReadingPipe(1,addresses[0]); }else{ radio.openWritingPipe(addresses[0]); radio.openReadingPipe(1,addresses[1]); } // Defaults to listening mode, start listening radio.startListening();}
  • Execution Logic

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 void loop() { /****************** Ping Out Role ***************************/ if (role == 1) { // Sending mode radio.stopListening(); // Must stop listening before sending data Serial.println(F("Now sending")); unsigned long start_time = micros(); // Timestamp to be sent // RF24 will automatically handle the issue of payload and data length mismatch if (!radio.write( &start_time, sizeof(unsigned long) )){ // Send data Serial.println(F("failed")); } radio.startListening(); // After sending data, need to listen for response data unsigned long started_waiting_at = micros(); boolean timeout = false; while ( ! radio.available() ){ // Timeout judgment if (micros() - started_waiting_at > 200000 ){ timeout = true; break; } } if ( timeout ){ Serial.println(F("Failed, response timed out.")); }else{ // Read data and display data and interval time unsigned long got_time; radio.read( &got_time, sizeof(unsigned long) ); unsigned long end_time = micros(); // Output it Serial.print(F("Sent ")); Serial.print(start_time); Serial.print(F(", Got response ")); Serial.print(got_time); Serial.print(F(", Round-trip delay ")); Serial.print(end_time-start_time); Serial.println(F(" microseconds")); } // Try again 1s later delay(1000); } /****************** Pong Back Role ***************************/if ( role == 0 ) { // Receiving mode static long count = 1; // Incrementing count unsigned long got_time = 0; if( radio.available()){ while (radio.available()) { // Read data radio.read( &got_time, sizeof(unsigned long) ); } got_time = count++; // For better understanding, return incrementing count value radio.stopListening(); radio.write( &got_time, sizeof(unsigned long) ); // Write response radio.startListening(); Serial.print(F("Sent response ")); Serial.println(got_time); } } /****************** Change Roles via Serial Commands ***************************/if ( Serial.available() ) { // Sending & receiving mode determined by serial char c = toupper(Serial.read()); if ( c == 'T' && role == 0 ){ // Sending mode Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK")); role = 1; }else if ( c == 'R' && role == 1 ){ // Receiving mode Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK")); role = 0; radio.startListening(); } }} // Loop

5. Reference URL

For detailed reference (in English): http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

Other people’s successful experiences: http://www.cnblogs.com/hangxin1940/archive/2013/05/01/3053467.htmlhttp://www.cnblogs.com/hangxin1940/archive/2013/05/01/3048315.htmlNote: The RF24 library used is not the same

Reprinted from http://blog.csdn.net/ydogg/article/details/53307365

This article comes from: Raspberry Pi Laboratory link: http://shumeipai.nxez.com/2017/03/20/communication-between-arduino-and-raspberry-pi.html

Limited-time resource download: Reply “Tutorial” to get the microcontroller e-book, reply “Simulation” to get Proteus simulation materials.

> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >

How to Share to Moments

Click the three dots in the upper right corner, select “Share to Moments” in the pop-up menu

Microcontroller Subscription Number

WeChat Name: Microcontroller updates daily various knowledge about microcontrollers, electronic DIY, and the latest news in the electronics industry, follow us, great!Communication Practice Between Raspberry Pi and Arduino

Recommended Articles

Reply with the number below or click directly to get related articles:

001:Must-read for Microcontroller Beginners

002:Words from Zhou Lijun to Young People Learning Microcontrollers

003:Discussion: The Difficulty of Software and Hardware Entry and Mastery Time Span

004:Thoughts on Learning 51 Microcontrollers; Recommended Learning Materials; A Few Essential Programs

005:Comparison of Several Microcontrollers Used

006: “ARM+LINUX Learning Route (Learning Sequence, Knowledge Points and Book Recommendations)

007:Differences and Connections between ARM/DSP/FPGA/CPLD/SOPC/SOC

008:Interesting Electronic Production: Food Power Generation in the Hands of Artists – Electronic DIY

009:My Experience: From a Production Line Worker to a Microcontroller Engineer

010:My Friend Spent 200,000 to Bring Back a Toolbox from Germany

Click the bottom left “Read the original text“, enterForum Communication!!!

Leave a Comment

×