Using the hall sensor from a bldc motor to get rpm on arduino - Page 1
Forum section: electronics / beginners

author: trilerian
author_rank: Regular Contributor
timestamp: May 22, 2023, 07:30:49 pm
content: I have a few projects that I am going to start on. One is to get the rpm data from my sensored bldc motors. I am trying to improve upon the sample rate of the flywheel dyno I have. The flywheel dyno uses a 10hz sample rate with an optical sensor. Due to the low sample rate, the torque curve isn't accurate near 0 rpm. I realize I can't get torque at 0 rpm with a flywheel dyno, but I hope to get closer to 0 than my current setup which starts outputting torque data after about 500 milliseconds. Sense the hall sensors are used to accurately control the speed of the motor, I assume it will be sufficient for a higher sample rate. The motor in question will be a 3 phase 2 pole bldc motor. Max RPM will be about 22k, but I have motors that can get up to 60k. My intention is to read this data with an arduino and hopefully output it to a spread sheet where I can plot the data vs time. Then use the MOI of my flywheel to get mechanical power and torque. I also plan on using ACS772ECB-200B-PFF-T on one of the phases to get the phase current as the sensor on the dyno is connected in series with the battery. I haven't looked into a voltage sensor yet, but I plan on adding that as well.What I am trying to do here is to learn, and test the validity of the dyno I bought. Btw, I am extremely new to arduino, so some of my questions may be basic.The closest I have found to what I am trying to do is from the following link:https://www.utsavrshah.in/Microcontroller%20resources/RPM%20measurement%20using%20Arduino/RPM%20Measurement%20using%20Arduino.htmlI would like to output the data to a csv though.

author: Benta
author_rank: Super Contributor
timestamp: May 22, 2023, 07:55:31 pm
content: Measuring rpm using the Hall sensors on motor is absolutely feasible, but you'll only get one pulse per revolution. If that's OK, go ahead.BTW: the Hall sensors do not "control the speed of the motor", they control the commutation of the stator.

author: trilerian
author_rank: Regular Contributor
timestamp: May 22, 2023, 08:34:15 pm
content: If each phase has its own sensor wire, wouldn't it be 2 pulses per revolution? 6 step commutation? Or am I not thinking about that correctly?

author: Benta
author_rank: Super Contributor
timestamp: May 22, 2023, 08:46:22 pm
content: Well, you can sniff all three Hall sensors if you like, which will give you three pulses/revolution. but as they're all in a fixed 120 degrees relationship it will give no additional information/precision. But yes, the frequency will be 3x higher (demanding 3x higher processing speed without any advantage).

author: trilerian
author_rank: Regular Contributor
timestamp: May 22, 2023, 09:54:36 pm
content: Sniffing all 3 sensors is for another project. That one is going to find the actual angle of the sensors. They are supposed to be 120* off but they vary by a couple of degrees depending on the board. Some are way off.So for more understanding. The poles will pass the sensor and create a voltage above and below a 2.5v reference. We are saying one pulse is peak to peak above 2.5, right? I should be able to calculate rotor strength from this as well?I’m going to measure on my scope and see what I find.

author: langwadt
author_rank: Super Contributor
timestamp: May 22, 2023, 10:08:01 pm
content: Quote from: Benta on May 22, 2023, 07:55:31 pmnumber of pulses will depend on the number of magnets in the motor

author: Circlotron
author_rank: Super Contributor
timestamp: May 22, 2023, 10:17:15 pm
content: Is the BLDC motor driving the dyno flywheel or is it simply being rotated while connected to the flywheel and the hall sensors measuring the rotation?

author: Benta
author_rank: Super Contributor
timestamp: May 22, 2023, 10:40:49 pm
content: Quote from: langwadt on May 22, 2023, 10:08:01 pmTrue. But from the OP's description, it seems to be a normal N-S anchor/rotor.

author: trilerian
author_rank: Regular Contributor
timestamp: May 22, 2023, 10:56:09 pm
content: The rotor is a standard rotor with one pole pair. It is an in runner type, I think that is the correct term. And the motor is driving the flywheel.

author: langwadt
author_rank: Super Contributor
timestamp: May 22, 2023, 10:57:08 pm
content: take the signal from the optical sensor and measure period instead of frequency at low speeds. Maybe add more target for the sensor so you get more than one pulse per revolution

author: trilerian
author_rank: Regular Contributor
timestamp: May 23, 2023, 12:16:39 am
content: The current setup uses 4 read points per rotation for the optical sensor. I don't know how the programming actually reads that data, but it outputs to a csv at 10hz.I put my scope on the sensors and the output works, so that is good. I measure all 3 and they look like they are offset by 120*. I am completely new to all of this and just bought my scope maybe a month or two ago. Would there be a math function to compare the 3 signals? IE, get the actual offset?I am posting a pic of the scope signal. Sorry, I have to learn to capture with the PC still.

author: Benta
author_rank: Super Contributor
timestamp: May 23, 2023, 12:46:43 am
content: 'Scope picture looks perfect. Nice edges, 120 degrees apart, what more can you wish for?

author: trilerian
author_rank: Regular Contributor
timestamp: May 23, 2023, 01:12:22 am
content: Well... This motor is a "stock" class motor. We look for every little bit to get the edge. Having the sensors be perfect is the goal. Also I have a device that measures the sensor angles, just curious if I could do that with the scope to verify the accuracy. Also all this is learning for me, and it is quite fun and I get an idea in my head and now I am trying to see it.

author: Circlotron
author_rank: Super Contributor
timestamp: May 23, 2023, 02:50:23 am
content: Set rpm and scope sweep speed so that channel 1 first rising edge is on the first graticule line and it's next rising edge is on the 6th graticule line. Then channel 2 should be 2 divisions across from channel 1 and channel 3 should be another 2 divisions across from channel 2.

author: JustMeHere
author_rank: Frequent Contributor
timestamp: May 23, 2023, 07:11:10 am
content: Check out these videos covering how to do this with hardware timers, input compare, and interrupts.

author: trilerian
author_rank: Regular Contributor
timestamp: May 23, 2023, 07:47:09 pm
content: Those videos are great, thanks! I will get to the interrupts. But first I I connected everything together just to read the rpm sensor with the arduino.I am starting off pretty basic with a 16x2 lcd without a backlight. So, sorry. This really is a learning process for everything. I have 16x2 with the i2c, but I need to figure out the code with that.I measured the sensor A with the arduino and sensor C with my scope. They were within a couple of Hz. Also I am including a pic of the optic sensor output from the dyno.

author: trilerian
author_rank: Regular Contributor
timestamp: May 23, 2023, 07:49:33 pm
content: And…

author: trilerian
author_rank: Regular Contributor
timestamp: May 23, 2023, 09:23:27 pm
content: I was able to edit the code to use an lcd with the i2c. Now I am running into another issue. Ideally I would like to power this setup off 2 power supplies that I have (with the help of this forum) modified for a lower output voltage. A single PS didn't provide enough amperage so I have 2 in parallel. However, because the ESC uses the DC power from the power supply to feed the sensor board, it makes the output noisy. While the scope doesn't have any issue with it, the arduino can add extra counts to the rise because of the noise. If I run the motor off a battery the issue goes away. So please bear with me here, I know I am new. Can I make an RC filter for this noise?Edit: I used my scope to look at the frequency of the noise. It looks to be around 600khz to 700khz on the bottom and 150khz to 400khz on the top. The frequency gets higher as the frequency it is riding on gets higher.

author: Benta
author_rank: Super Contributor
timestamp: May 23, 2023, 09:59:36 pm
content: The best way is to run the power stage and the control electronics from two separate power supplies. As you've noticed, the power stage can generate a lot of noise. The main challenge is keeping the power and signal grounds apart (but still connected).

author: trilerian
author_rank: Regular Contributor
timestamp: May 24, 2023, 12:23:35 pm
content: So how do I achieve this? For right now, the arduino is being powered by my laptop, while the dyno setup is powered by a dc source. Either a power supply which connects to an esc which connects to the bldc motor, or a battery that connects to the esc... Their is a six pin sensor harness that goes from the motor to the esc. The pin outs are common ground, sensor C, sensor B, sensor A, temperature, 5v. So this powers the sensor board and the pulses come back on it to the esc. When using the scope I use the common ground and one of the sensor pinouts. But for the pinout going to the arduino I just use the sensor. Should I be putting the common ground from the sensor board to the ground pin on the arduino? I know this would be the case if the entire system was running from the same source, but it isn't. Btw, I do have a 10k resister in series between the sensor lead and the arduino. That helps a lot with the miscounts. It was very random before putting that resistor in.

author: trilerian
author_rank: Regular Contributor
timestamp: May 29, 2023, 09:16:06 pm
content: So, I am trying to get this setup to read the initial value faster. There seems to be a refresh that happens until the the unit starts reading, then everything is fine. I would like the initial refresh to be faster so I can get the frequency closer to 0. Right now I don't start picking it up till around 33Hz.Here is the arduino code I am using. I scraped this together from different sources.I have print to lcd because when I am not running the terminal emulator I want an output. I will need to make changes so the lcd can display data at a lower rate than the serial, right now the data goes faster than the lcd can print.#include <Wire.h>#include <hd44780.h> // main hd44780 header#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class headerhd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chipint Htime; //integer for storing high timeint Ltime; //integer for storing low timefloat Ttime; // integer for storing total time of a cyclefloat frequency; //storing frequency// LCD geometryconst int LCD_COLS = 16;const int LCD_ROWS = 2;void setup(){pinMode(8,INPUT);lcd.begin(LCD_COLS, LCD_ROWS);Serial.begin(115200);}void loop(){lcd.clear();lcd.setCursor(0,0);lcd.print("Frequency of signal");Htime=pulseIn(8,HIGH); //read high timeLtime=pulseIn(8,LOW); //read low timeTtime = Htime+Ltime;frequency=1000000/Ttime; //getting frequency with Ttime is in Micro secondslcd.setCursor(0,1);lcd.print(frequency);lcd.print(" Hz");Serial.println (frequency*60);delay(10);}

author: JustMeHere
author_rank: Frequent Contributor
timestamp: June 03, 2023, 03:40:44 pm
content: Your algorithm isn't really sampling frequency. It's looking at one up and one down pulse. Then it sleeps. You should be counting the number of pulses that happens over a period of time. Ex: Sample the signal for 100ms and count 10 pulses, you get a frequency of 10 Hz (note my math may be wrong).This is best done using the hardware and interrupts I mentioned above. Trying to count this in software will be problematic because you also end up timing the execution speed of your program.

author: langwadt
author_rank: Super Contributor
timestamp: June 03, 2023, 05:49:06 pm
content: Quote from: JustMeHere on June 03, 2023, 03:40:44 pm~100Hzat low frequencies it is much more accurate to measure the period

author: Siwastaja
author_rank: Super Contributor
timestamp: June 03, 2023, 06:18:52 pm
content: Quote from: Benta on May 22, 2023, 08:46:22 pmWith the usual configuration, it's six, not three pulses perelectricalrevolution so 60 degree accuracy, and this is electrical degrees which equals mechanical degrees only in 1 pole pair per phase motors. OP is talking about RPM somechanicalrevolutions, which is then any integer multiple of six - usually at least 12 because I think 1-pole-pair-per-phase BLDC motors are quite rare.Quote from: Benta on May 22, 2023, 07:55:31 pmOf course hall sensors can be used for speed loop, too, why not; that's pretty usual thing to do. Using them primarily for commutation does not exclude other types of uses for the same data.

author: JustMeHere
author_rank: Frequent Contributor
timestamp: June 04, 2023, 07:03:00 pm
content: Any chance you're using voltage to control the speed of the fan? The pulse Vmax is the same as the voltage driving the fan (at least the one I played with on my scope did this.) You may be generating runt pulses.

author: Benta
author_rank: Super Contributor
timestamp: June 04, 2023, 08:32:05 pm
content: Quote from: Siwastaja on June 03, 2023, 06:18:52 pmPlease do not misrepresent what I wrote. The speed loop has nothing to do with commutation, and of course you can use the Hall sensor information for the speed loop as well. I never said otherwise.Is this kind of hyperbole your standard form?

author: trilerian
author_rank: Regular Contributor
timestamp: June 05, 2023, 03:29:46 pm
content: Quote from: JustMeHere on June 04, 2023, 07:03:00 pmNo, the signal is coming from a 540 sized motor for RC racing. The voltage driving the motor will be around 8.0v.I put together a code with interrupts, but it was just as slow to start recording as the first way. Ultimately I don't start getting readings until 30-40Hz. I changed the interrupts to gather more and less counts for the average, but then accuracy goes out the window. It is kind of funny really, the dyno works best at 10Hz sampling with whatever software it is written with. Using interrupts I calculated that 10Hz gave good results for the arduino.Regardless, using either method works to get the rpm with ok accuracy. It just doesn't start outputting data quickly enough to use it for my original intent. But I will use it for another project.

author: Siwastaja
author_rank: Super Contributor
timestamp: June 05, 2023, 05:02:15 pm
content: Quote from: Benta on June 04, 2023, 08:32:05 pmI frankly did not understand what you meant by"the Hall sensors do not "control the speed of the motor", they control the commutation of the stator.", and I still don't. Quite obviously, hall sensors do not directly and literally control either the speed or commutation of the motor, but their data can be used as inputs for circuits that control commutation, speed or both. If we assume by "control" OP means "using as an input for control", then your remark is off because hall sensors can equally well "control" both commutation and speed, so that kind of "not X but Y" when both X and Y are true would be wrong.That is the assumption I made; Instead of asking for clarification as I should have, I elaborated on that assumed meaning, on the basis that OP might have misunderstood it the same way as I did, in order to be helpful and correct the misunderstanding, even if it was not what you meant. Sorry 'bout that. Now it's your turn to explain A) what you actually meant, B) why do you think it's helpful.

author: Benta
author_rank: Super Contributor
timestamp: June 05, 2023, 09:03:56 pm
content: Quote from: Siwastaja on June 05, 2023, 05:02:15 pmI don't see any ambiguity at all. On BLDCs the stator (in classic PMDCs "anchor") is fixed, no matter whether it's an inrunner or an outrunner. The PMs move.That the Hall sensors can be used for an additional speed control loop is obvious, but not their prime purpose.