PI Control DRV8302

Hi everyone, i have arduino due, drv8302, and BLDC motor with hall sensor. I would like to applied Proportional Integral (PI) control to controlling the speed of BLDC Motor.

I have succeed to spin the BLDC motor using switching sequence (by giving pwm signal on each mosfet based on hall sensor combination). I have some question on my mind. Here it is :

  1. To applied PI Control do i need to spin the motor clockwise and counter clock wise? Can i just use one of it?

  2. Is there any pin of DRV8302 that works as direction (CW/CCW) pin?

  3. Is there any library that contain PI control to control speed (rpm or any units) in simpleFoc that maybe i can try first?

Sorry if my question are out of topics.

Ps : i’ll share my arduino code later.

Hi, @zulfiar-am,

We support velocity control (PID) and angle control (P only) in the SimpleFOC library.

Please take a look at our documentation and examples, I think they will answer your questions.

Hi @runger thanks for the answer, I have tried the code example and modified it so it can work with DRV8302 but i found this error

I have installed PciManager.h library via Library Manager of Arduino but it still won’t work. can you please help me? here are my full code

Also i found out that the PID to control velocity have code like this


but i dont know where’s the “output” variable goes to. i think it should be goes to analogWrite(output); but i didn’t found any code that consist of analogWrite to write the pwm. could you please explain to me?

Sorry if i am asking to many question.
Thank you very much!

As far as I can see this has to do with the PciManager library and not simepleFoc so I don’t now if this forum is the right place to ask for help with that error.

Hi @bartios & @runger i have fixed the error already, the error above occured because i wrote it as

PciListener ImplistenerIndex(encoder.index_pin, doIndex);

i have revised it like the example code to be like this :

PciListenerImp listenerIndex(encoder.index_pin, doIndex);

but still i found error like this,

first i thought it was the library version, i already downgrade the version from 2.00 to the latest version, but it still won’t work. At this time i dont know yet how to fix it. so i think i’ll comment it and try it to my device first.

i ask here because the library are used in velocity motion control using encoder example.

If you try to compile just the example code from github without any changes does it compile or do you get errors?

yes it show the same error, thats why i need some explanation or detail from anyone who have tried it.

Looks like the doIndex() function should take a byte parameter, like this:

void doIndex(byte param0){ encoder.handleIndex(); }

Not sure why, I don’t use the PciManager really…

hi @runger could you please answer these question? i am confused right now

i have already read the documentation of velocity close loop control but still that’s not answer my question.

Hi @zulfiar-am,

The analogWrite() function is usually not sufficient for BLDC motor control. SimpleFOC has its own PWM driver code for each MCU type that it supports…

You can turn the direction either clockwise or anti-clockwise, but you don’t have to. It’s your choice. Positive target values turn in one direction, while negative ones turn in the other…

The DRV8302 does not have a direction pin. As a 3-PWM or 6-PWM brushless driver it lets the controller directly set the phase voltages/currents (via PWM) and does not support speed/direction control. You’d need a different type of driver for that, but that type will not support FOC (field oriented control).

Hi @runger based on your answer “As a 3-PWM or 6-PWM brushless driver it lets the controller directly set the phase voltages/currents (via PWM) and does not support speed”

isn’t this means that the higher voltage and current that the driver generate, the speed/spin of the BLDC Motor also goes faster too?

i have tried to analogWrite each switching pin (INH_A, INH_B, INH_C, INL_A, INL_B, INL_C) of DRV8302 with switching combination from some journal and paper, and it turns out that the higher value of pwm it makes the speed/spin of BLDC motor faster too.

also where can i find the code of this? i think i need to learn it

It’s in the drivers subdirectory in our library…

Hi @runger i found out that to spin brushless dc motors we need to switching the mosfet, there are many combination of switching sequence methods such as

i would like to know what combination of switching that simpleFOC library used? where do i can look of its code and its switching sequence?


I suggest you start with our documentation site - we have an area called the “theory corner” which describes exactly the control algorithms used, and goes into some detail on field-oriented control.

We also link to some resources on the subject.

But it’s not the simplest concepts, and the code behind the switching of the MOSFETs (via an appropriate driver) is fairly complicated, and depends also on the MCU type used. You can find it all in the drivers subdirectory in our library, but it might be difficult to follow it.