Hello,
I'll do my best trying to explain this scenario so that any of you give me some advice.
I'm controlling a brushed DC motor via PWM at 5 kHz. Picture attached. Also, I've attached my motor's nameplate. The circuit attached is already a PCB. As you can see, this time, instead of using a shunt resistor, I decided to experiment with the
TLI4970-D050T4 current sensor. The microcontroller which is sending the PWM and reading the sensor via SPI is a STM32L432KC Nucleo running at 2 MHz. Of course, I can increase this core frequency by far if needed. The SPI clock is prescaled to 1 MHz. Attached you can also find the code snippet I used to calculate the current. I used STMStudio to monitor the current variable. I built the code with HAL Library for a fast test.
I tried the circuit with the DC motor, and also with a light bulb. I also used a Fluke 175 RMS multimeter (in DC current mode) to compare with the current measures that I get from the sensor.
First test: With the light bulb1- With a duty cycle of 40%, the multimeter says about 0.7A continuously. The current sensor throws an inconsistent value of 1.2A as it is changing very fast.
Second test: With the motor1- With a duty cycle of 40%, the meter says about 5A continuously. The current sensor shows changing values between 0 and 6A.
So basically there some factors that got me confused:
I'm measuring current in a switching element. So I think the meter is giving me the average current, and the sensor is giving the instantaneous values at a certain state of the transistor (ON/OFF).
Furthermore, I don't know if I should read the SPI in main loop as shown in the code, or sample the current at a certain frequency.
I have confirmed that my SPI interface is well configured. It has the correct clock polarity and phase. The current sensor has the default setting parameters, i.e, 18 kHz bandwidth. The overcurrent detection triggers an interrupt routine.
I thought of putting the sensor in series with motor. But it's too late. I'll do it for a next design. In the meantime, I want to see what I can achieve with this setup.
I hope I've given enough details. Let me know if you need more. I have some previous posts about this project, but still have issues. So I decided to write this post with more info.
To sum up, what do you think I should improve in this actual scenario?
Code snippet
while (1)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
HAL_SPI_Receive(&hspi1, (uint8_t *)&SPIRx, 2, 10);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(10);
motorCurrent=motor_current();
float motor_current() {
float Iout;
float BIout;
int16_t Bit15;
Bit15 = SPIRx&0x8000;
if(Bit15==0x8000){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
}
else {
BIout= SPIRx&0x1FFF;
Iout = (BIout-4096)/80;
}
return Iout;
}