Hello,
I want to limit or stop the current flow inside the windings of a BLDC motor through programming.
I am driving a BLDC motor using two MOSFETs, where the gate of the MOSFETs is connected to the pins(3rd and 9th Pin) of the Arduino Pro Mini. A Hall sensor is used to identify the position of the rotor. The hall sensor is connected to pin no. 2 of the Arduino ProMini. The circuit diagram is shown in the image. The pins are turned high and low in accordance with the output of the Hall sensor.
I am attempting a test where I am trying to limit or stop the current flowing through the windings of the motor when the rotor is blocked. I am trying to achieve this through programming. I have attached an external interrupt at pin no 2. The interrupt will be triggered when there will be a rise in the output pulse of the Hall sensor. A millis() is used to count from the Rise time of the pulse and if that time is greater than a specified value, the MOSFETs will be turned off for '3 seconds' by giving a low pulse to the respective gates. I also want the rotor to start rotating once unblocked. So I introduce a down counter using millis() which will keep the power to the motor turned ON for '30 milliseconds' and if the motor is unblocked in that 30 milliseconds the rotor will start moving again.
But the code I've written fails to do this process. I am not an expert and would like some help with my code.
//BLDC Motor driver program with Over and Under voltage protection.
const int Hall_IC = 2; // Hall Sensor
const int VCC_AI = A0; //for UV-OV protection
const int WhiteLED = 3; //MOSFET gate 1.
const int GreenLED = 9; //MOSFET gate 2.
int Reading; //To read the output from the Hall sensor.
//Over and Under voltage protection value.
const int VCC_UV_TH = 244; // below 6 V
const int VCC_NORMAL_UVTH = 327; // Above 8 V
const int VCC_OV_TH = 810; // Above 20 V
const int VCC_NORMAL_OVTH = 728; // Above 18 V
int VCC;// Variables to store new UV-OV values
uint32_t period; //unsigned 32-bit integer variable
uint32_t tStart; //unsigned 32-bit integer variable
byte fault; // Flag - Set if the voltage crosses prescribed limit.
int rotor_block=3; // 3 milliseconds timer.
volatile unsigned long value;
void setup() {
Serial.begin(9600);
pinMode(Hall_IC, INPUT);
pinMode(WhiteLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
}
void loop(){
attachInterrupt(0, rising, RISING); //External interrupt on the rising edge of the Hall sensor Pulse.
Reading = digitalRead(Hall_IC);
VCC = analogRead(Reading);
//UV-OV with hysteresis.
if((VCC>=VCC_NORMAL_UVTH)&&(VCC<=VCC_NORMAL_OVTH)) fault=0;
if((VCC<=VCC_UV_TH)||(VCC>=VCC_OV_TH)) fault=1;
if(!fault)
{
if(Reading)
{digitalWrite(GreenLED, LOW);
digitalWrite(WhiteLED, HIGH);}
else
{digitalWrite(WhiteLED, LOW);
digitalWrite(GreenLED, HIGH);}
}
else{digitalWrite(GreenLED, LOW);
digitalWrite(WhiteLED, LOW);}
if((millis()-value) >= 15) //If HIGH pulse is greater than 15 milliseconds
{if((millis()-rotor_block)>=1)//Keep the current flow for 3 milliseconds.
{rotor_block-=1;
if(!rotor_block)
{rotor_block=30;
period = 30 * 1L; // 3 second turn off period.
for(tStart = millis(); (millis()-tStart)<period; ){
digitalWrite(GreenLED, LOW);
digitalWrite(WhiteLED, LOW);}
}
}
}
}
//ISR
void rising(){
value = millis();
attachInterrupt(0, rising, RISING);
}
The circuit diagram annotations:
A - +ve terminal of Supply is connected here.
Yellow and Orange - Connected to Pin no 3 and 9 of the Arduino.
White and Blue - Connected to Motor windings.
