Learn / Pins: in and out · guide 2 of 10

Blink an LED

The green user LED LD7 blinks, and you know which document told you each line of the code.

Beginner≈ 30 minBefore this:Start here: the board and its documentsTopics:RCCGPIO

The "hello world" of microcontrollers. You find the LED on the board, follow its wire to an MCU pin, switch on the pin’s clock, make it an output and toggle it. Every step points at the page that explains it.

You will learn
  • Find what an on-board part is connected to, using the user manual and the schematic
  • Why a peripheral does nothing until its clock is enabled in the RCC
  • How GPIOx_MODER and GPIOx_BSRR control a pin
  • What active-low means and how to spot it on a schematic
You need
  • STM32H750B-DK and a USB cable to CN14 (the ST-LINK connector)
  • STM32CubeIDE installed

On this board

Pulled from the datasheet tables and the board schematic. Click a pin for everything the finder knows about it.

PinBallBoard netUsed here asAFOpen
PJ2T6LED1LD7, green user LED (active-low)
PI13H2LED2LD6, red user LED (active-low)
1

Find the LED in the user manual

The board has eight LEDs, but most belong to the ST-LINK or the power circuit. The user manual has a table of all of them. The two meant for you are LD6 (red, "USER1") and LD7 (green, "USER2"), driven by PI13 and PJ2.

Whenever you want to use something on the board (a LED, a button, the screen), start in the user manual: it tells you which MCU signal it uses.

2

Confirm it on the schematic

The user manual says "PJ2", but the schematic shows how the LED is wired, and that decides whether you write 0 or 1 to turn it on.

On the MCU sheet, pin PJ2 leaves the chip on net LED1. Follow that net to the small LED circuit on the same sheet: 3V3 → LD7 → R233 (510 Ω) → LED1 → PJ2. The LED’s anode sits on 3.3 V, so current only flows when the pin pulls its side low. That is an active-low LED: pin low = LED on.

Tip. Click the LED1 label in the schematic viewer: it lists every other place that net appears.

3

Turn on the clock of GPIO port J

After reset every peripheral is unclocked to save power, and an unclocked peripheral ignores your writes. GPIO ports hang on the AHB4 bus, so their enable bits are in RCC_AHB4ENR. Port J is bit 9, GPIOJEN.

This is the step beginners forget most. If your pin "does nothing", check the clock enable first.

4

Make PJ2 an output

Each pin has two bits in GPIOx_MODER: 00 input, 01 output, 10 alternate function, 11 analog (the reset value for most pins). For pin 2 these are bits 5:4, so you clear both and set the low one.

The other output settings can stay at their reset values: GPIOx_OTYPER = push-pull and GPIOx_OSPEEDR = low speed, which is plenty for a LED.

5

Toggle the pin

You could write GPIOx_ODR, but that is a read-modify-write and can clash with an interrupt touching the same port. GPIOx_BSRR changes single pins in one write: bit n sets pin n high, bit n+16 drives it low. Because LD7 is active-low, reset turns it on.

#include "stm32h7xx.h"   /* with STM32H750xx defined by the project */

static void delay(volatile uint32_t n) { while (n--) { } }

int main(void)
{
    /* 1. clock for GPIO port J (RCC_AHB4ENR bit 9) */
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIOJEN;
    (void)RCC->AHB4ENR;              /* read back: lets the enable take effect */

    /* 2. PJ2 as general-purpose output: MODER2[1:0] = 01 */
    GPIOJ->MODER = (GPIOJ->MODER & ~GPIO_MODER_MODE2_Msk)
                 | (1U << GPIO_MODER_MODE2_Pos);

    for (;;) {
        GPIOJ->BSRR = GPIO_BSRR_BR2;  /* PJ2 low  -> LD7 on (active-low) */
        delay(2000000);
        GPIOJ->BSRR = GPIO_BSRR_BS2;  /* PJ2 high -> LD7 off */
        delay(2000000);
    }
}

Tip. The busy-wait delay() speed depends on the clock and the compiler optimisation level. For real timing use SysTick or a timer: see the PWM guide.

6

Build, flash, and make it yours

In STM32CubeIDE create a project for the STM32H750B-DK board, put the code in main.c, and press Run: the ST-LINK flashes and starts it. Then try:

  • Blink LD6 (PI13) in turns with LD7. Port I needs its own clock bit.
  • Make a heartbeat pattern: two short blinks, one long pause.
  • Look up GPIOJ in the memory map and compute the address of GPIOJ->BSRR yourself (base + offset 0x18).

Common mistakes

  • Writing 1 to turn the LED on. These LEDs are active-low: 0 = on.
  • Forgetting the RCC clock enable. The registers read back as 0 and writes are lost.
  • Setting MODER with |= only. The reset value of most pins is 11 (analog), so OR-ing in 01 leaves 11. Clear the two bits first.

Check yourself

Try to answer before opening. If you get stuck, the pages linked above have the answer.

Why does a 1 in BSRR bit 2 turn LD7 off?

BS2 drives PJ2 high. The LED’s anode is at 3.3 V, so with the cathode side also high no current flows.

Which register and bit would you need for an LED on PE3 instead?

Port E clock: GPIOEEN in RCC_AHB4ENR, then MODE3 in GPIOE->MODER and BS3/BR3 in GPIOE->BSRR.

What is LD8 connected to, and could you blink it too?

Table 6 in the user manual says LD8 (green, "ARDUINO") is on PD3. It is also Arduino pin D13, so yes, same recipe on port D.

Reading list

Every page this guide sent you to, by document. Reading them in order is a good revision.

UM2488UM2488 STM32H745I/H750B-DK Discovery kit user manualp.8p.16
MB1381MB1381 Discovery board schematic (H750XB variant)sheet 3sheet 3
RM0433RM0433 reference manual (STM32H742/743/753/750)p.130p.373p.457p.533p.534p.540p.540p.542p.543

Search next

Each of these opens the finder with pins, registers, schematic hits and document pages for the term.

Where to go next