Blinking a LED with a push-button on a breadboard from STM32F103RB nucleo board

Hello Dears,

I use an STM32F103RB. I have recently learned some ways to blink a led (the one that is on the Nucleo board). Today I want to blink a given led with a push-button on a breadboard.

I have tried but it does not work at all and I want to know what is going on and how I can fix it.

#include <STM32f10x.h>

int main (void)
{
	
RCC->APB2ENR = RCC->APB2ENR | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;

GPIOA->CRL = GPIOA->CRL & ~(0xF << 24); // output push-pull b0001 PA6
GPIOA->CRL = GPIOA->CRL | (0x01 << 24); // output push-pull b0001 PA6

GPIOC->CRH = GPIOC->CRL & ~(0xF << 16);  // input floating b0100 PC4
GPIOC->CRH = GPIOC->CRL | (0x04 <<  16); // input floating b0100 PC4

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

I have used a resistance of 270 ohm, The blue wire is connected on the ground pin of the Nucleo board to the negative of my breadboard, a brown wire is connected from the PA3 of the pin number 37 the resistance on my breadboard, My push-button is powered by a 3v3 from my microcontroller (red wire) and the orange wire is also connected to my push-button from the pin number 27 what represents the PB4.

1 Like

Please post your whole code instead of image.

#include <STM32f10x.h>

int main (void)
{
	
RCC->APB2ENR = RCC->APB2ENR | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;

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);
       }

     }
 }

I find two issues with your configuration. First of all the pin PA3 of your STM32 board is used for communicating with the ST-LINK. It’s already mentioned by @Prototype in one of your previous topics. You may use some other pins for your project. If you still want to use pin PA3 then you have to configure the solder bridges as mentioned in the below screenshot.

The second issue is with the configuration of the switches. The switch must be in such a way that the microcontroller can either detect an active low signal or an active high signal. You may use any of the two schematics from below image but your code should be accordingly.

Thank you @Administrator for your response, well I just forget to edit my post but I will do that.

First please comsider that I already took into consideration @Prototype remarks, I kept the previous post despite the fact that I thought I changed it.

Well, please find abowve my real code : I use other pins for output and input.

I have used PA6 as an output and PC4 as an input

Dear @Administrator thank to your “Active High” circuit, my code works well.