Hall sensor + esp32

Hello everyone, I am having problems controlling a BLDC motor. Specifically, when I checked the hallsensor, I found it was not working. Below is the code I use:
#include <SimpleFOC.h>
// #include <PciManager.h>
// #include <PciListenerImp.h>

HallSensor sensor = HallSensor(25, 26,13, 15);
// interrupt routine initialisation
void doA(){sensor.handleA();}
void doB(){sensor.handleB();}
void doC(){sensor.handleC();}
//PciListenerImp listenC(sensor.pinC, doC);

void setup() {
// monitoring port
//sensor.pullup = Pullup::USE_EXTERN;

// initialise encoder hardware
// hardware interrupt enabled
sensor.enableInterrupts(doA, doB, doC);

Serial.println(“Encoder ready”);

void loop() {
// read sensor and update the internal variables
// display the angle and the angular velocity to the terminal

When I turn the wheel the angle is not right


What do you get if you replace the interrupt functions with:

void doA(){Serial.print("a");}
void doB(){Serial.print("b");}
void doC(){Serial.print("c");}

If you don’t see anything printed then the interrupts aren’t interrupting.

It is a bit naughty printing to serial in an interrupt but it usually works for a character or two.

Also might be worth setting pinMode for your hall pins:

pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(13, INPUT);

Do you actually have external pullups? A strong pull up resistor on each hall might be required for the halls to work, e.g. for hoverboard motors you might need a 1.5k resistor pulling up to 3.3v.

How do I set up the pull-up resistor?

Each of the three hall sensor output wires need a separate pullup. Connect them between the yellow, blue, green wires and 3.3V (red)

1 Like

Whilst it is possible to configure the esp32 hall pins as ‘internal’ pullups:

pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);

The internall pullup resistors are typically weak e.g. 30K ohms or more.

You might find that using input_pullup will work when the motor isn’t running (i.e. turning the motor by hand), but when you start the motor (which introduces noise) you get a lot of ghost interrupts - which gives glitchy velocity. So using an external pullup of 1 to 5K ohms will ensure a nice step transition as each hall passes through the magnetic fields of the permanent magnets of the rotor. If you have a 11pp motor then you’ll have 22 pole transitions which each hall can detect. So there should be 66 interrupts per rotation. If there is noise from the motor you can some times see 100s or 1000s of false interrupts happening. A strong pull up (e.g. a lower 1.5K resistor attached between 3.3v and each hall) will ensure that you get nice steps.

The motor I use has 15 pairs of poles, the pull-up resistor I use is 10k ohms. When the wheel rotates, it jerks. Is it because my PID parameters are not appropriate or due to some other reason?
motor.PID_velocity.P = 0.3f;
motor.PID_velocity.I = 0.3f;
motor.PID_velocity.limit = 0.04f;

Are you seeing stable/correct angles when you turn the motor? Are the angles correct i.e 1 rotation = 2pi?

Those pullups (at 10k) are still a bit weak, and may be prone to noise. For hoverboard motor halls i found 10k to glitch when motor was running and settled on 1.5k.

In terms of tuning, I’d start with zero for I and D and slowly increase P. You should initially see the motor speed lagging what you ask for and then when you find a value of P that gets close to your desired speed (70%) you might start to see speed wobbles. Only then would i play with D (to dampen wobbles) and i (to remove steady state error).

Note the d term is often a small value, much less than 1

1 Like

Thank you bro, I will do that

When I control using closeloop, I set the velocity to 5 rad/s, then change the PID parameters. Initially I gave I = 0 then changed P. I realized at motor.PID_velocity.P = 0.2; The motor started to accelerate but I noticed on the serial screen that the voltage gradually increased to 1 value and the wheel stopped rotating.
When tested with higher P values, the wheel rotates unevenly. It seems like it only rotates for a while and then spins again, causing the wheel to jerk.
Below is a picture of the voltage in the second column. When the voltage is < 0.8, the wheel still rotates, but when it is greater than 0.8V, the wheel stops.
I use esp32S3.

Hi @Khoa_Bach , I think @dekutree64 and @Candas1 have answered you in the other thread.

lets keep the conversation to one thread, if possible, otherwise people will have trouble following your problem and you will get less help.

From the way you describe the problem, I would say you are losing track of the motor’s position - something is not working right with your hall sensors.
Are you powering them from 5V or 3.3V? Have you looked at the signals from the HallSensor with an oscilloscope, and do you get a good signal, or is there maybe a problem with the voltage when the problem occurs?

When you run the motor in torque-voltage mode (no PID for velocity), does it run well? Can you run it at voltages >1V in torque voltage mode?

Thank you, sorry I’ve been in so many topics.
I set it to 3.3V, I don’t have an oscilloscope, I only tested the hallsensor in the FOC example

From the sounds of things, I think you will need some debugging equipment to solve the problem.

But for sure you should try the simpler examples first:

  • sensor test code - to make sure the sensor is working correctly - you can print the angles to the console, and check the sensor is returning 2PI (6.28) for a full revolution of the motor when you turn it by hand
  • torque voltage mode - make sure this is working well before you try any PID tuning

if the sensor is working by hand but not working well when the motor is powered then it is probably an electrical problem with the hall sensors or the power supply. Then you will need some debugging equipment like a scope to look for the problems.

Thank you, but I think the variable doesn’t work well when powered. I took this motor from the slider and I used the DRV8302 with the Esp32S3 to connect, it seems like I’m missing something.

Thank you, I was able to fix it because the microcontroller I connected was having problems

1 Like