Overview

uint32_t millis();

Returns the milliseconds since the microcontroller started. Overflows in 50 days.

uint32_t micros();

Returns the microseconds since the microcontroller started. Overflows in 70 minutes.

delay(uint32_t millis);

Delays the execution by the given milliseconds.

delayMicroseconds(uint32_t micros);

Delays the execution by the given microseconds.

Example: Print the milliseconds on Serial, every second:

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println(millis());
  delay(1000);
}

Uses STM32 SysTick, set to interrupt 1ms intervals.

Naming the pins in code:

Generally you should stick to naming the pin the same as they appear on the board to reduce confusion. If on the board you see ABC, use digitalWrite(ABC, HIGH);

pinMode(uint8_t pin, uint8_t mode);

Sets the pin for digital reading / writing

Mode Meaning
OUTPUT 0V - 3.3V
INPUT The pin does not have a pull down or pull up resistor
INPUT_PULLUP Weak pull up resistor to default to HIGH if the pin is not connected to anything
INPUT_PULLDOWN Weak pull down resistor to default to LOW if the pin is not connected to anything

pinMode() is needed for digitalRead() and digitalWrite() functions.

The system automatically sets the pinMode when using a peripheral library like analogRead(), analogWrite(), SPI or I2C, so you don't have to.

digitalRead(uint8_t pin);

Read the voltage level on the specified pin. Returns LOW(0) if it is 0V, HIGH(1) if it is 3.3V

All pins can be used as digital input.

Not all pins are 5V tolerant. Please check the chip reference manual Pinouts and pin description section.

digitalWrite(uint8_t pin, uint8_t value);

If value if LOW(0), sets the pin to 0V. If value is HIGH(1), 3.3V. All pins can be used as digital output.

Example: Blink the LED on the board:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  delay(1000);
}

int analogRead(uint8_t pin);

Read the voltage level on the pin, and map the 0..3.3V range to 0..1023 range by default.

Not all pins can be used as analog input! Please check the Boards page.

analogReadResolution()

TODO not yet implemented

analogWrite(uint8_t pin, int value)

Set PWM to the specified pin.

All pins can be used as analog/PWM output.

Uses the TIM2 clock interrupt with software PWM implementation to allow PWM on all pins.

analogWriteResolution()

TODO not yet implemented

Example: Dim the LED based on the voltage value on PA0

void loop() {
    analogWrite(LED_BUILTIN, analogRead(PA0));
    delay(10);
}

void attachInterrupt(uint8_t, callback, int mode);

Setup callback on the specified pin. Mode can be: CHANGE: When pin changes from LOW to HIGH, or HIGH to LOW, the callback will be called. RISING: When pin changes from LOW to HIGH the callback will be called. * FALLING: When pin changes from HIGH to LOW, the callback will be called.

Same pins from different ports cannot be used. For exaple PA3 and PB3 cannot be used as interrupt simultaneously.

Predefined pin constants

The following constants are defined in the variant.h file for each variant:

Constant Meaning
LED_BUILTIN The primary LED on board, if there is any
STM32_LED_BUILTIN_ACTIVE_LOW If set, the onboard LED lights up when output is set to LOW
MOSI MOSI pin of the first SPI
MISO MISO pin of the first SPI
SCK SCK pin of the first SPI
SDA SDA pin of the first I2C
SCL SCL pin of the first I2C