6

I'm planning to count the RPM of a RC Car which is controlled with a PI B+.

But while planning the question occurs, how to count the RPM. My Idea is via a Sensor, which gives a Pulse every Round, and to count that via Software.
My Problem right now is, that I didn't find reliable Information, about how fast I can count GPIO Changes and I don't know hot to do that efficiently.

Right now I would like to use Python with RPI.GPIO, since most of the stuff you find is made like this.

According to this Answer, it is possible to get up to 1 MHz (which should be 1M RPS, which I shouldn't reach).
Others state a max of 9 kHz (9k RPS) which is a completely other Dimension, and I think I've also read lower values while researching...

My Idea of Code yet is to add an event callback to a function, which simply counts up a counter and in the main thread I read and reset that counter about every .5 seconds (and measure the time). Furthermore I would keep the last three values and calculate an average.


So I hope Someone can give me advice, how to get the RPM the best way, and about how many Pulses per Second I can count max/for sure

4

3 Answers 3

8

You have misunderstood the post which mentions 1 MHz. All that is actually said is that the Python time module can return the current time to a microsecond. It says nothing about how many gpio transitions can be detected in a second.

I believe my figure of around 9 K gpio state changes per second is still accurate for Python running on the Pi. As said in the other linked post you can do a lot better (circa 100 K events per second) if you do the event captures on the Pi but off load the processing to a PC.

With C on the Pi you should be able to achieve 250 K events per second or more.

http://www.raspberrypi.org/forums/viewtopic.php?f=37&t=90243&p=632413&hilit=+hall#p632413 shows Python using Hall sensors to count motor revolutions. The maximum RPM was about 9000.


The following is an artificial example but shows pigpio counting 260 k events per second (as well as making a stab at displaying the captured data). The capture (pigpio daemon) is on the Pi. The Python and piscope are running on a laptop.

http://abyz.me.uk/videos/pig_cb.webm

The script sets up a default callback for multiple gpios and starts PWM at the maximum frequency on each. The callbacks count the level transitions to a rising edge by default.

The maximum PWM frequency is set by requesting a too high value of 100000. The closest achievable frequency will be set. The achievable frequencies depend on the sampling rate with which the pigpio daemon was started.

#!/usr/bin/env python

import time

import pigpio

GPIO=[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

NGPIO = len(GPIO)

cb = [0]*NGPIO
last = [0]*NGPIO
now = [0]*NGPIO

pi = pigpio.pi()

for i in range(NGPIO):
   pi.set_PWM_frequency(GPIO[i], 100000) # set maximum frequency.
   pi.set_PWM_dutycycle(GPIO[i], 128) # square wave.
   cb[i] = pi.callback(GPIO[i])
   last[i] = 0

while True:

   time.sleep(1.0)
   total = 0
   for i in range(NGPIO):
      now[i] = cb[i].tally()
      total = total + now[i] - last[i]
      last[i] = now[i]
   print(total)
2
  • 1
    I know that "thank you for this" type stuff isn't really desired on SE, but repeatedly I want to find something in-depth about what I'm trying to personally achieve, and your responses come up time and time again, here, and elsewhere. Cheers.
    – stevieb
    Jul 7, 2017 at 22:28
  • Can you please explain the code, what is purpose of GPIO list, NGPIO and all functions and variables Aug 26, 2019 at 4:22
2
  1. This really isn't a Pi question.
  2. You haven't really defined your problem.

What range are you expecting? What accuracy do you require?

I would just measure the time of each pulse, derive the duration since the previous and invert this.

If the motor runs at 6000rpm this would give 1 pulse every 10mS which should be feasible. You need to know that Linux isn't a real time OS, so interrupts which occur would affect accuracy.

1
  • My Problem is, I don't know ow many pulses the pi can handle per second, and how to count them efficiently. If I'm using my method, I think a few rps could be lost. For my calculations I could think about 8-20kRPM (150-333 RPS I didn't expect it to be that low earlier). (Maybe more, but I don't think I could get over 80kRPM/1333RPS ever)
    – XoMEX
    Dec 21, 2014 at 10:15
1

We have run a relevant benchmark test for both Rpi.GPIO and PIGPIO.

We concluded that both libraries perform well for frequencies of up to 5 KHz with and accuracy above 99%. The accuracy of the Rpi.GPIO library deteriorates over 5 KHz and at 50 KHz it is incapable of performing this task.

The PIGPIO library performs comparably better, with its accuracy being above 99% for frequencies up to 20 KHz. Above that frequency, its performance gradually deteriorates and at 110 KHz it cannot read correctly any phrase at all.

You can read the more about the test, including all required information so as to replicate it, at our blog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.