6 GPIO using STM32Duino
6.1 Objectives
- Understand and experiment with GPIO Digital Output, GPIO Digital Input, and GPIO Analog Input/Output on the STM32 Nucleo-144 F767ZI board using STM32Duino.
- Learn to use basic example sketches in Arduino IDE for controlling LEDs and reading inputs.
6.2 Materials
- STM32 Nucleo-144 F767ZI development board.
- Arduino IDE with STM32Duino configured.
- Jumper wires.
- Potentiometer (for analog input task).
6.3 Part 1: Simple Blink (GPIO Digital Output)
Steps
- In Arduino IDE, go to
File -> Examples -> 01.Basics -> Blink
. A new sketch will open. - Ensure that Nucleo-144 is selected as the board. If it is not selected, choose it from the dropdown menu.
- The code will reference
LED_BUILTIN
, which corresponds to the built-in LED onPB0 (LD1)
on the Nucleo-F767ZI board. - Upload the sketch to the STM32 Nucleo-144 board.
Important
Ensure that the Board part number is set as Nucleo-F767ZI from the Tools menu before uploading.
Expected Output
- The built-in LED on PB0 will blink on and off with a 1-second delay.
Tasks
- Modify the sketch to make all built-in LEDs (on
PB0
,PB7
, andPB14
) blink.
6.4 Part 2: Debounce (GPIO Digital Input)
Steps
- In Arduino IDE, go to
File -> Examples -> 02.Digital -> Debounce
. A new sketch will open. - Ensure the board is correctly selected (Nucleo-144).
- Set
btnPin
toPC13
(the onboard button). - Set
ledPin
to one of the built-in LEDs (e.g.,PB0
). - Upload the sketch to the STM32 board.
Expected Output
- The LED will toggle between ON and OFF each time the button is pressed, with debounce logic to filter out noise from the button.
Tasks
- Modify the sketch so that each button press cycles through turning
PB0
,PB7
, andPB14
on and off in sequence (cycling the built-in LEDs).
6.5 Part 3: Fading (GPIO Analog Output)
Steps
- In Arduino IDE, go to
File -> Examples -> 03.Analog -> Fading
. A new sketch will open. - Use
PB0
for the built-in LED (LED_BUILTIN
). - Upload the sketch to the STM32 board.
Expected Output
- The built-in LED will gradually fade in and fade out.
Task
- Modify the code to alternately fade
PB0
andPB7
so that one fades in while the other fades out, and vice versa.
6.6 Part 4: Calibration (GPIO Analog Input)
Steps
- In Arduino IDE, go to
File -> Examples -> 03.Analog -> Calibration
. A new sketch will open. - Connect a potentiometer to
A0
for analog input. - Use
PB0
for the LED output. - Use
PB7
to indicate calibration period. - This sketch calibrates the sensor (potentiometer) over the first 5 seconds to detect its minimum and maximum values, then maps the sensor readings to adjust the brightness of the LED on
PB0
. - Change the calibration time to 10 seconds and try turning the potentiometer knob to both extends as possible.
- Upload the sketch to the STM32 board.
Code Example (Modified from the calibration example)
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin = PB0; // pin that the LED is attached to
const int indicatorLed = PB7;// pin to indicate the calibration period
// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
void setup() {
.begin(115200);
Serial// turn on LED to signal the start of the calibration period:
(indicatorLed, OUTPUT);
pinMode(indicatorLed, HIGH);
digitalWrite
// calibrate during the first 10 seconds
while (millis() < 10000) {
= analogRead(sensorPin);
sensorValue
// record the maximum sensor value
if (sensorValue > sensorMax) {
= sensorValue;
sensorMax }
// record the minimum sensor value
if (sensorValue < sensorMin) {
= sensorValue;
sensorMin }
.print("sensorMin:");
Serial.print(sensorMin);
Serial.print(',');
Serial.print("sensorMax:");
Serial.print(sensorMax);
Serial.print(',');
Serial.print("sensorValue:");
Serial.println(sensorValue);
Serial}
.println("Calibration Done.");
Serial// signal the end of the calibration period
(indicatorLed, LOW);
digitalWrite}
void loop() {
// read the sensor:
= analogRead(sensorPin);
sensorValue
// in case the sensor value is outside the range seen during calibration
= constrain(sensorValue, sensorMin, sensorMax);
sensorValue
// apply the calibration to the sensor reading
= map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue
// fade the LED using the calibrated value:
(ledPin, sensorValue);
analogWrite}
Expected Output
- During the first 10 seconds, the sensor will calibrate its minimum and maximum values based on the potentiometer input.
- After calibration, the LED brightness will adjust based on the potentiometer’s position, with the sensor values mapped between 0 and 255 to control the PWM output on
P0
.
Tasks
- Try modifying the code to calibrate until the user button is presses instead of using a fixed time window.
6.7 Conclusion
This exercise introduces basic GPIO operations using STM32Duino on the STM32 Nucleo-144 F767ZI board. Students learned how to:
- Control LEDs using digital output (Simple Blink).
- Handle button presses using digital input with debouncing (Debounce).
- Create fading effects with analog output (Fading).
- Read and map analog input data from a potentiometer to control the brightness of an LED (Calibration).
These examples serve as foundational tasks to deepen the understanding of GPIO handling in embedded systems.