0
\$\begingroup\$

I'm a beginner, and I'm using an STM32 Blue Pill board for the first time. I completed a basic tutorial on how to activate the PC13 pin (on-board LED), but now that I'm trying to get an external LED to blink on a breadboard, I'm having issues.

I'm using the STM32CubeIDE, and allowed the IDE to autogenerate the configuration code that I need to use pin PA0 as GPIO output:

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);

  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

Within my loop, I'm simply running

while (1)
  {
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, 1);
      HAL_Delay(500);
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, 0);
      HAL_Delay(500);
  }

Despite this simple code, I can't get the LED to blink. Here's a picture of my circuit:

To summarize, I have the 3.3v and ground pins connected to the breadboard power rails at the bottom (with a red power LED). I have a jumper cable connecting the A0 pin to row 25, and a 330Ω resistor in series with a 5mm LED with a forward voltage of 3.0V and a forward current of 20mA. I then connect the cathode of the LED to the negative power rail on the breadboard (attached to board ground).

I've tried swapping jumper cables, output pins, LEDs, resistance, but nothing has worked. I connected the pin jumper cable to the positive power rail, and the LED lit up (as expected), so I don't think there's something wrong with my circuit. I've also re-programmed the MCU to make sure my program was uploading correctly. Perhaps I'm misunderstanding the relationship between the pin and ground? Any help would be appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ The third argument of HAL_GPIO_Write_Pin() is an ENUM. It is supposed to be either GPIO_PIN_RESET or GPIO_PIN_SET. C doesn't mind if you replace an enum with a constant integer. But I suppose it is not the best form to do so. There is also a HAL_GPIO_TogglePin() function. FYI. \$\endgroup\$ – mkeith Jan 18 at 21:01
  • 1
    \$\begingroup\$ Unless that RED LED of yours has a built-in resistor it's going to let the magic smoke out soon. You have it wired directly between the supply rails... \$\endgroup\$ – brhans Jan 18 at 21:24
2
\$\begingroup\$

You want to toggle Port A pin 0, but you are actually toggling Port C pin 0.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Oh dear, there goes several hours of my life I'll never get back. Thank you. \$\endgroup\$ – DonutGaz Jan 18 at 20:58
  • 1
    \$\begingroup\$ @DonutGaz welcome to the club. I wish I had a dollar for every hour I spent chasing some "bug" that was actually just a minor mistake on my part like this one. LOL. \$\endgroup\$ – mkeith Jan 18 at 21:03
  • \$\begingroup\$ Yes, after a couple of these, you start to get the hang of what to check and in which order. Good thing is you started to think it might be hardware error too. In case of not having colleagues around to point out errors, you can go through the code and explain what it does to a virtual teammate what your code does, which forces you to read everything and at the moment you say (in your mind) "here we are toggling Port A" but on screen it's actually C you can find these yourself. Or, use the debugger, and expect GPIOA pin 0 to toggle when single-stepping, but it doesn't, while GPIOC has change. \$\endgroup\$ – Justme Jan 18 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.