cancel
Showing results for 
Search instead for 
Did you mean: 

Switching relay causes hardFault on MCU

SSone.1
Associate III

I am switching 5V relay with BJT using MCU(STM32G030) pin. When clock speed is 64MHz(max speed) and 1ms timer update interrupt is enabled, not every 

time I switched relay but sometimes MCU goes into hardFault handler. But if I disable timer update interrupt or reduce the clock speed to 16MHz or 

dismount the BJT that I use to switch relay, hardFault doesn't occur.

Code below executed in 1ms timer update interrupt;

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)

{

uint8_t var1 = 0;

TIM1->SR &= 0xFFFE; //clear pending bit

for(var1; var1 < 150; var1 ++) //pretend doing something

{

__NOP();

__NOP();

__NOP();

__NOP();

__NOP();

__NOP();

__NOP();

}

}

I have another interrupt from sysTick timer and it has same priority with timer update interrupt(from timer1 of MCU). They both generates 1ms interrupts.

Relay switches mains but there is no load connected and I have freewheeling diode on relay.  

STM32G030 doesn't have HardFault Status Register so I can't look for hardFault source. I couldn't figure out the relation between switching relay, clock speed and interrupt that causing hardFault together.

By the way in hardFault function I am checking ECC flags to see if flash corruption occured and rarely ECCC or ECCD flag set.  

In main code I read one hardware pin coming from ESP8266 that telling make relay on or make relay off. According to this pin's status I switch relay. Code is below:

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_TIM1_Init();

 /* USER CODE BEGIN 2 */

 HAL_TIM_Base_Start_IT(&htim1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 if(HAL_GPIO_ReadPin(relayOnOff_GPIO_Port, relayOnOff_Pin))

 {

 HAL_GPIO_WritePin(RelayPin_GPIO_Port, RelayPin_Pin, GPIO_PIN_SET);

 }

 else

 {

 HAL_GPIO_WritePin(RelayPin_GPIO_Port, RelayPin_Pin, GPIO_PIN_RESET);

 }

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

6 REPLIES 6
LCE
Principal

When switching the relay, check the power supply voltage in general, and close to the STM32.

SSone.1
Associate III

Thanks for answer. I have connected external power supply to be sure not brownout occuring but result was same.

ONadr.1
Senior III

Is power suply distribution blocked by capacitors? How far is relay form MCU? You can also insert some choke into relay power suply. Inductive load, as relay coil, can cause high glitches and affect,for example, the oscilator,.

Using an external supply does not mean that it's well layout and buffered on the PCB, see what ONadr.1 wrote.

So better grab a scope and check the power supply on the board, at several points, especially close to the MCU.

Aleks
Associate III

The problem is 100% in hardware (scheme, PCB) design. So read this first for example: https://resources.altium.com/p/using-flyback-diodes-relays-prevents-electrical-noise-your-http://www.fordemc.com/docs/download/EMC%20Design%20Guide%20for%20PCB.pdf

and ... a lot of other about EMC and EMI.

Paul1
Lead

Suggest you post schematic of circuits for Relay, and STM32 Power and control of relay.

  1. How is STM protected from Relay? (buffer circuit = Resistors, BJT, Hi side, low side, pullup, pulldown, etc.). For relays I like to use these as they have flyback circuits: ULN2003A, NUD3105LT1G, NUD3160DMT1G, TBD62083A, TBD62783A
  2. Does relay have a Flyback diode across Coil for when turn off relay?
  3. What is current consumption of relay?
  4. Decoupling capacitors where?
  5. Is the power: Diode > Big Cap+Small Cap > Reg/DCDC > BigCap+SmallCap > MCU? Small caps parallel to big caps because big caps have poor High Freq response due to ESR.

.

Include the schematic.

Paul