nrf24l01 is too slow to send a simple string from Raspberry pi to Arduino?

i am using the sample code at

and i am able to use nrf24l01 to communicate raspberry pi with arduino, but the problem is the timing. I found it takes > 20 ms to send a very simple string from raspberry pi to arduino, and this is not acceptable for my applicaiton. I am hoping the timing can be for example < 200us or something closer. You can use the code below to test the timing from raspberry pi to arduino. My test result is ~ 24 ms

===

start = time.time()

radio.write (“A”)

end = time.time()

delta = end - start

print (1000 * delta)

===

if i use nrf24l01 for arduino-arduino communicaiton, i do not have the problem. it take a very short time, but RasPi-arduino communication is far too slow.

any suggestions and solutions? thanks

Its most likley a limitation of the PI due to its time slicing and running a relatively slow CPU. If you can turn off the time slicing, you will get better performance for your application, but I dont know if thats possible with the version of Linux thats being used.

Hi mauried and/or other experts. i used Raspberry PI 3 model B, and i thought it shall run much faster than my arduino Nano, but on the contrary, sending a simple string (from PI --> to Nano) takes much longer than (from Nano --> to Nano). The needed times from PI to Nano also flucturates a little bit. Is this because PI has 4 cores?

The nrf24l01 library is from https://github.com/Blavery/lib_nrf24

The spidev package is at https://github.com/Gadgetoid/py-spidev/archive/master.zip

Detailed codes are at http://invent.module143.com/daskal_tutorial/raspberry-pi-3-wireless-pi-to-arduino-communication-with-nrf24l01/

my simplified python code is below, which is to send only a simple "A" and it takes so long. The long time transmission and unstable timing totally mess up my robot's applicaitons.

thanks alot for any advice.

==========

import RPi.GPIO as GPIO from lib_nrf24 import NRF24 import time import spidev GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM)

talking_pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1]] radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) radio.setChannel(0x76) radio.setDataRate(NRF24.BR_1MBPS) radio.setPALevel(NRF24.PA_MAX) radio.enableDynamicPayloads() radio.openWritingPipe(talking_pipes[0])

message = list("A")

while(1):

time0 = time.time() print (time0) print(" ")

radio.write(message)

time1 = time.time() print (time1) print(" ")

time_difference = time1 - time0 print (time_difference) print(" ")

time.sleep(3) # sleep 3s

Python is an interpreted language which will run very slowly in any micro computer. You really need a C version of the Python code which can be compiled for ARM architecture. There is a version of gcc for the PI, but I havnt as yet used it.

This code for measuring time is all wrong. It is taking into accouunt lots of irrelevant stuff which is probably slow

 time0 = time.time()
    print (time0)
    print("      ")
   
    radio.write(message)
   
    time1 = time.time()
    print (time1)
    print("      ")

    time_difference = time1 - time0
    print (time_difference)
    print("      ")

try this version and see what happens

  time0 = time.time()

    radio.write(message)
   
    time1 = time.time()

    time_difference = time1 - time0
    print (time_difference)
    print("      ")

it would also be wise to try it without the radio.write() line to know how long the timing code overhead takes.

...R

ericcouyang:
and i am able to use nrf24l01 to communicate raspberry pi with arduino, but the problem is the timing. I found it takes > 20 ms to send a very simple string from raspberry pi to arduino, and this is not acceptable for my applicaiton. I am hoping the timing can be for example < 200us or something closer. You can use the code below to test the timing from raspberry pi to arduino. My test result is ~ 24 ms

if i use nrf24l01 for arduino-arduino communicaiton, i do not have the problem. it take a very short time, but RasPi-arduino communication is far too slow.

any suggestions and solutions? thanks

The problem is very likley with the Pi as Python on the Pi is incredibly slow.

So slow that I would just not bother with it at all.

Solution; dont use Python on the Pi.

srnet: The problem is very likley with the Pi as Python on the Pi is incredibly slow.

I don't have an RPi but quick look at the specs for a Raspberry PI 3 model B shows

Quad Core 1.2GHz Broadcom BCM2837 64bit CPU 1GB RAM

That suggests to me it should not be much slower than this laptop which seems to be running at a similar speed.

...R

Python is an interpreted language, so its not compiled. Means it runs much slower than normal programs because of the overhead at run time.

Even for an interpreted language Python on the Pi is very slow.

A while back I timed a program that reads and prints the contents of the SX127x LoRa device registers, there are 128 of these in the SPI device. I was wondering if Pi Python would be a useful hardware development environment.

The timings were;

Arduino Pro Mini 8Mhz, 0.031Seconds Pi 2 900Mhz and MMbasic, 0.19Seconds Pi 2 900Mhz and Python, 7.44Seconds

MMbasic is a port of the same MMBasic used on the Micromite, its interpreted Basic and runs 40 times faster than interpreted Python on the Pi.

I concluded that Pi Python was not suitable for hardware development.

mauried:
Python is an interpreted language, so its not compiled.
Means it runs much slower than normal programs because of the overhead at run time.

I am well aware that Python is an interpreted language and that a program written in Python will not be as fast as one wriiten using a compiled language.

Nevertheless Python is blindingly fast on my PCs and is my “normal” programming language.

I would be interested if someone would run some simple benchmark using Python on an RPi and tell us the results so I can compare it to my PC.

Maybe I will try to find my Yun at the bottom of some drawer and try Python on it - knowing that it will be slower than an RPi.

…R

Yep. Python is painfully slow since it runs through an interpreter. Compared to C++ which gets compiled, you don't have any control over how the code gets executed in python or when. On the other hand, you get all the benefits of the garbage collection and libraries of python. That said, I'd stay away from python if you have strict timing requirements, especially if you're trying to do things on the order of microseconds (or hundreds of microseconds).