Brushless DC Motor RPM Read

Hello Guys

Your help is greatly appreciated.

I am trying to read the rpm of a brushless DC motor, with a dedicated driver datasheet of which is linked below.

I have a Nextion display attached and I want to display motor RPM on screen but I am new to Arduino and failed to do so, using simple analogRead code. Could you please guide me how to?

On datasheet it says as following, but I can’t figure out what to do.

Motor Speed Signal Output:
Connecting SPEED and COM to get pulse output F=N*P/60
F—Pulse output frequency
P—Pole number of BLDC motor ( has 4 poles)

I’ve tried the code attached to no avail. Please take it as simple as possible as I’ve been on Arduino for two weeks only :slight_smile:

sketch_feb13a.ino (821 Bytes)

analogRead() won't read pulses. It will either get an on or off (most likely off) depending on if it randomly measures at the same time there is a pulse.

If your program has nothing better to do, use pulseIn() to read the duration of the pulses. Be aware that it can lock up your code for as much as a second if there's no pulses to measure.

If you do have real work to do, then either find a library such as FreqMeasure or write your own interrupt code. attachInterrupt() is pretty easy to use.

P may be the number of pole pairs, not the number of poles, so be aware of that.

volatile long pulses = 0 ;  // must be volatile if used in ISR

void int_func ()
  pulses ++ ;

void get_pulsecount ()  // safely read pulses when its not changing
  noInterrupts () ;
  long res = pulses ;  // don't zero the count, just sample it, less scope for bugs (easy debug)
  interrupts () ;
  return res ; 

void setup ()
  pinMode (2, INPUT_PULLUP) ; // just in case its open-collector
  attachInterrupt (0, int_func, RISING) ;  // interrupt channel 0 is pin 2 (for Uno etc)

unsigned long timestamp = 0L ;
long prev_pulses = 0L ;

void loop ()
  if (millis () - timestamp >= 1000)  // triggered every second
    timestamp += 1000 ;  // setup for next time

    long now_pulses = get_pulsecount () ;  // sample the count
    long count = now_pulses - prev_pulses ; // take difference from last sample
    prev_pulses = now_pulses ;  // update the previous value

    Serial.println (4 * count / 60.0) ;

Hello Mark,

Thank you very much for your reply. Here i get an error as following ;

error: void value not ignored as it ought to be

long pulses = get_pulsecount () ; // sample the count

Yes, I made an error in dashing out a quick example - that function ought to return long


Mark your input is very much appreciated, I am a newbie like the friend who has the original post here.

Would you mind posting the correct code? I tried to fix it, I just can’t do.

Just change the return type to long from void. If you don't know how to do that, you will benefit from learning how to, you won't get far without understanding "return type" or "long" or "void" - I've given you the clues, its not rocket science...