Teach Remote lab lessons

Teach lesson

STM32 with Arduino API (2/4): sequence and buttons

Students extend an STM32 Arduino sketch with multiple LEDs and button input, then explain how sequence timing and button state affect output.

  • STM32 Nucleo (Arduino)
  • 55 min
  • Grade 10-12 / introductory vocational electronics
  • English
  • Embedded systems
STM32 Nucleo (Arduino)
STM32 Nucleo (Arduino)

Learning Outcomes

  • Create an output sequence using a list of pins.

  • Read an active-LOW button and use it to change behavior.

  • Record two test states and explain the program decision.

Student activity preview

Activity Content

Preview only. In a class session, students can fill in responses and submit their work to the teacher.

1

An active-LOW button

8 min

In this setup, the button we will use is connected to pin PC5. It is an
active-LOW button: when it is pressed, digitalRead(PC5) returns LOW;
when it is not pressed, it returns HIGH.

If the button is pressed and the program runs digitalRead(BUTTON_1), which value should it compare against: HIGH or LOW?

2

Sequence with fast mode

27 min

The program uses three RGB outputs on the board. First it prepares them in
setup(). Then, in loop(), it chooses the wait time according to the button
state.

Remote STM32 Nucleo WB55RG lab setup with board, inputs, and outputs available.

View of the lab setup. In this session you do not change wires: you only use
pins that are already available from the program.

  1. Open the same STM32 Arduino lab from the previous session.

  2. Open main.ino and copy the reference program.

  3. Compile and upload the program.

  4. Observe the sequence without pressing the button.

  5. Press button 1 in the lab interface and observe again.

  6. Change the fast value from 150 ms to another visible value, compile, upload, and test.

#include <Arduino.h>

const int LEDS[] = {PA8, PA9, PA10};
const int TOTAL_LEDS = 3;
const int BUTTON_1 = PC5;  // Active LOW: pressed == LOW

void turnOffAll() {
  for (int i = 0; i < TOTAL_LEDS; i++) {
    analogWrite(LEDS[i], 0);
  }
}

void setup() {
  pinMode(BUTTON_1, INPUT);
  for (int i = 0; i < TOTAL_LEDS; i++) {
    pinMode(LEDS[i], OUTPUT);
  }
}

void loop() {
  bool pressed = digitalRead(BUTTON_1) == LOW;
  int waitTime = pressed ? 150 : 500;

  for (int i = 0; i < TOTAL_LEDS; i++) {
    turnOffAll();
    analogWrite(LEDS[i], 180);
    delay(waitTime);
  }
}

Fill in the table after uploading the program and testing the button. Use only
two rows and leave the others blank if Teach shows more:

- Row 1: button not pressed.
- Row 2: button pressed.
- In Expected digitalRead value, write HIGH or LOW according to the
active-LOW logic.
- In Wait used, record the value the program applies in that case.
- In Brief explanation, connect the button state with the observed speed.

Button state Expected digitalRead value Wait used Observed sequence Brief explanation

Explain how you know the button changed the program behavior. Use your two table rows as evidence.

3

Modify one rule

10 min

Choose one small modification:

- Change the speed of fast mode.
- Change the speed of normal mode.
- Turn on two colors at once in part of the sequence.
- Invert the logic so the button makes the sequence slower.

What modification did you make? Which line did you change, and what did you observe after compiling and uploading?

4

Submit your code

10 min

Submit your final main.ino

Save main.ino before attaching it. The snapshot must show the final version with the button reading and the modified sequence you want your teacher to review.

Why can an active-LOW button be confusing at first? Write one short rule that would help you avoid mistakes.