Read time between rise on 3 separate input pins

Another concern I have is how long the interrupts will take. If you look at the the scope output you can see that there are 3 events that will trigger an interrupt pretty much at the same time...

ok. i must have missed the discussion describing the use of comparators.

how many BEMF inputs are there?

what is the expected relationship between a BEMF comparator output and any of the waveforms events?

why is the comparator output (yellow) high while the bemf appears near zero(?) left of the dashed vertical marker line but then go low when the bemf also appears near zero just to the right of the dashed vertical line and then go high again as the bemf appears to increase in value?

3 phase bldc motor, so 3 bemf signals. 3 hall effect sensors. Need time between each hall effect sensor and the bemf signal that corresponds to that phase. Hall effect sensors can be out of alignment, ie they are not a perfect 120* apart. The purpose of this is to show how far out of alignment each sensor is and to show the advanced can timing of the motor. Rotor asymmetry can affect the readings as well.

The external comparator output is bad, I was showing it as a reason why I am using the internal comparator of the arduino.

EDIT:

If it is easier, I supposed I could measure just one phase for timing and then measure the time between rising edges of the hall effect sensors for alignment...

looking at the original specification I doubt if you will be able to get anywhere near a tenth of of degree even with fast microcontrollers such as the ESP32.
at 1KHz a tenth of a degree I make to be 0.277uSec

this simpler version looks for time difference in rising edges in loop() by reading the state of the pins

// ESP32 determine time between two pulses rising edge

#define RXPIN 16
#define RXPIN2 17

volatile int pin = 0;
volatile long timer = 0, start;

void setup() {
  Serial.begin(115200);
  pinMode(RXPIN, INPUT_PULLDOWN);
  pinMode(RXPIN2, INPUT_PULLDOWN);
}

// calculat pulse widtch in microseconds
void loop() {
  static int lastPin = -1;
  static long lastTimer = 0, counter = 0;
  static long printer = millis(), second = millis();
  static bool pin1 = 0, oldPin1=0, pin2 = 0, oldPin2=0;
  static long average = 0;

  // look for rising edge on pin1 - note time
  pin1=digitalRead(RXPIN) ;
  if (pin1 == 0) oldPin1=0;
  else if(oldPin1==0) {
    oldPin1 = 1;
    start = micros();
  } 

// look for rising edge on pin1 - get time difference
  pin2=digitalRead(RXPIN2) ;
  if (pin2 == 0) oldPin2=0;
  else if(oldPin2==0) {
    oldPin2 = 1;    
   // timer = micros() - start;  // time between rising edges
    average += micros() - start;  // time between rising edges
    counter++;
  }

  // after 10 seconds print average time difference
  if (millis() - printer > 10000) {
    Serial.printf("*** average %f.2  *****\n", (float)average / counter);  // print average pulse width
     counter = average = 0;                                   // reset initialse values
    printer = millis();
  }
}

on serial monitor 60degree phase difference gives

*** average 166.761520.2  *****
*** average 166.924606.2  *****
*** average 166.478058.2  *****
*** average 166.493744.2  *****
*** average 165.672440.2  *****
*** average 166.488953.2  *****

Yeah, I'm starting to wonder about the 1/10 of a degree myself. Maybe the units on the market are just using an average and with that and the calculation are getting 1/10 of a degree. That code does look much simpler though.

what do the commercial units cost?
could be using much faster processors or an FPGA?
Edit: Raspberry Pi 4 running RTOS?

The units range from $100 - $460 depending on the accuracy. The $460 unit uses an XMega256 A3U.

???

don't know how to evaluate ...

is that delta 460 usec?

and is this the BEMF time measurement due to the waveform changing polarity

... or should the BEMF constant, proportional to motor speed?

The expected relationship between a BEMF comparator output and hall effect sensor signal is going to be how much advanced timing the can has. What we are able to do with our motors is twist the end bell which moves the hall effect sensors. Since commutation is determined by the hall effect sensor location this can field weaken the motor to allow for higher rpm. But there is only so much gain you can get before giving up too much efficiency. Hope that makes sense. The BEMF doesn't change no matter where you time the motor, this gives a fixed signal to compare to the hall effect sensor to determine timing. The timing range should be between 20-60*, but we are measuring the rising edge of the hall sensor, which doesn't put the pole in the middle. Also will need to account for any delay. So far my measurements using the internal comparator are about 60* off of what they should be when measuring on my scope. But I wasn't worried about that right this second. That could be a matter of inverting a waveform, it could be a matter of finding the midpoint of the high pulse and comparing to an inverted waveform. I don't know, I haven't experimented enough yet.

At 500hz, the time gap between a sensor and the BEMF should be between 100-330 usec. That corresponds to roughly 20-60* of timing. Then add more time for any offsets.

The timer speed I want is for the resolution, not necessarily for the measurement. But since the measurements will be averaged, the resolution probably doesn't matter, and the micro timer will probably be fine.

Here are a couple more outputs from different scope measurements. In both cases, BEMF is yellow and hall sensor is blue. The first pic is zero timing, the second pic is about 40* timing


at this point, i'm still not sure i understand what you're doing.

i had thought the non bemf measurements were of some waveform applied to the motor. now i realize they froma hall effect devices.

i'm not sure what these are actually sensing. i had thought there was a measurement of the rising and falling edges of the waveform. but not being familiar with hall effect sensors, do they just generate a pulse(?) in the presence of a magnetic field? a spike the the magnetic field reverses?

based on the diagram below (where i assume each pole has a coil on either side of the motor) i'll guess that the hall sensor is positioned near each of the coils that are shown so that they could be used as a timing device for the controller.

bldc

i don't understand what the the bemf compariter is connected to, is it simply the actual coil voltage.

in DC motors, BEMF is used to measure speed. during a very brief measurement period ( ~msec), the drive voltage is supressed and the bemf generated by the motor measured. there is a target bemf used to control the drive voltage.

what does '*" mean: deg, usec?

"*" = degrees

I have no idea why that is turning my asterisk into a period...
That is better

You almost have it. Yes, the hall effect sensors are latching, they provide the pulse while the south pole of the rotor passes them. We can twist the endbell of the motor, which contains the the 3 hall effect sensors. This moves them in relation to the stator coil. The BEMF waveform doesn't change position, we can measure the timing difference because of that.

On a 3 phase bldc motor the drive pulse isn't suppressed, it goes to a different phase. You read the back emf on a phase that isn't being used for commutation. Since it is effectively AC, the signal goes plus and minus, you need zero cross. That is what the internal comparator on the arduino is doing for me.

For comparator connections, I have put a 10k resistor on each phase of the bldc motor and tied the other ends together to create a virtual common ground. That is the + input on the comparator, then each phase goes to the arduino as the alternating.

don't understand -- "isn't being used"? i thought those waveforms from post #1 represented the polarity of a signal on each of 3 coils, + when > baseline and - when at baseline.

Commutation happens on two phases at a time, but really it doesn’t matter here.

The pic you are referring to is just the Hall effect sensors. No bemf. I have posted pics showing a Hall effect sensor with bemf.

How to Connect Brushless DC Motors to an Electronic Controller answered many questions for me

  • that the 3 coils have a common connection and are no individually energized
  • that when coil A is energized, it may initially be connected thru coil B and halfway thru its cycle, to coil C
  • this explains why one coil is not energized during one of the phases and bemf can be measured
  • that the hall sensors are only 60 deg apart

The link below is Wokwi Simulator for 3-Phase Pulse Generator Chip with Arduino MEGA. It may help you to do the simulator for some parts of your project.

Thanks for the information. However I have plenty of experience with how bldc motors work. Again, I don't want to control one with an arduino right this second, just trying to measure time difference between events. And, my application, the hall sensors are 120* apart, not 60*.

This will be useful, I am sure. having a look it is using the mega2560 board with multiple digital input interrupts. It will help though.

I have ordered a function generator to help with testing as well, plus some esp32s since code was given above for those. I have a lot to experiment with to decide how I want to continue.

the link was intended for others watching this thread and trying to make sense of it

Yeah, that is probably helpful...

Actually, to add to that. Here is an interesting link on timing.

[Motor Timing: Theory]