Monitoring a relay from a button on NUCLEO - F103RB


I am testing a code that suits the fact I can monitor the relay that will light a LED from a button. The matter is that when I push the button, I can see that it does work from the peripheral in the debug mode but physically nothing has changed (the relay does not shift his contacts).

Please find the code and attached hardware design below.

Note: For my design, the Nucleo board requires a 5v power supply (I got 5.40V and I wonder if it is a problem). For that, I shift the jumper to 3 and 2 from JP5-E5V on my Nucleo board and I supply the pins 6 and 8 from CN7 with 5.40V as an extern supply.

#include <STM32f10x.h>

int  main (void )
 GPIOA->CRL = GPIOA->CRL & ~(0xF << 24); *// output push-pull b0001 PA6*
 GPIOA->CRL = GPIOA->CRL | (0x01 << 24); *// output push-pull b0001 PA6*
 GPIOC->CRL = GPIOC->CRL & ~(0xF << 16); *// input floating b0100 PC4*
 GPIOC->CRL = GPIOC->CRL | (0x04 << 16); *// input floating b0100 PC4*

  while(1) {
   if(GPIOC->IDR & (1 << 4)) {
   GPIOA->ODR = GPIOA->ODR ^ (1 << 6);

The LED lights up when you debug, but not when free run, Correct?? What was the state of PA6 and PC4 ODR and IDR Register when debugging? You can also use a multimeter to see the voltage of the across output pin.

the Led doesn’t light up at all neither in when I debug nor when free run.
Well, my output is not the Led but rather the relay. I didn’t see the state of PA6 but just the state of my button.

First of all a 12V relay is an overkill for a LED. You can do this with just the transistor.

Also, you have to check the state of the output pin (input to transistor in your diagram) because how you are going to know that the transistor has an input. Just check the state of ODR register for output pin. Also what type of relay you have? From your diagram it seems like a normally open relay. Can you confirm this??


Hi. You XORed the output while the button Is pressed. May be you must OR, not XOR the output. If you want to toggle the relay you need to use an auxiliary variable to detect rising edge.

I have resolved the issue. One of the component was not wired.

1 Like

I completely agree with @EEngineer. You have to use a multimeter for debugging the physical connections of your circuit. This makes debugging faster.

1 Like