💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Preparation
  • GPIO Digital In/Out
  • pinMode()
  • Example
  • External Interrupt
  • attachInterrupt()
  • Example
  • Exercise
  • Ticker (SysTick interrupt)
  • Add STM32Timer Interrupt Library
  • STM32Timer class
  • Example
  • Exercise

Was this helpful?

  1. EC Course
  2. Tutorial
  3. Tutorial: arduino-stm32

Tutorial: arduino-stm32 Part1

PreviousTutorial: arduino-stm32 InstallationNextTutorial: arduino-stm32 Part2

Last updated 1 year ago

Was this helpful?

Preparation

MCU board: Nucleo-F401RE

GPIO Digital In/Out

We are going to create a simple program that turns LED(LD2) on and off by pressing the user button(BT1).

Configures the specified pin to behave either as an input or an output.

pinMode(pin, mode)
  • pin: the pin number to set the model of.

  • mode: INPUT, OUTPUT or INPUT_PULLUP.

Look up for pinMode() function in arduino reference for detail description.

Example

Create a new program named as ‘TU_arduino_GPIO_LED_button’.

Write the following source code: .

const int btnPin = 3;
const int ledPin = 13;

int btnState = HIGH;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT);
}

void loop() {
  btnState = digitalRead(btnPin);

  if (btnState == HIGH) 
    digitalWrite(ledPin, LOW);
  
  else 
    digitalWrite(ledPin, HIGH);
  
}

The user button pin is PC13, but this pin cannot be used in arduino. So, you should connect PC13 to pinName D3 by using wire.

Click on upload button. Push the reset button(black) and check the performance. The LED(LD2) should be turned on when the button is pressed.

External Interrupt

We are going to create a simple program that turns LED(LD2) on triggered by External Interrupt of user button(BT1).

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
  • digitalPinToInterrupt(pin): translate the digital pin to the specific interrupt number.

  • ISR: a function called whenever the interrupt occurs.

  • mode: defines when the interrupt should be trggered. (LOW, CHANGE, RISING, FALLING)

Example

Create new program as ‘TU_arduino_ExtIn’.

Write the following code.

const int btnPin = 3;
const int ledPin =  13; 

int btnState = HIGH;

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(btnPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(btnPin), blink, CHANGE);
}

void loop() {
}

void blink(){
    btnState = digitalRead(btnPin);

    if (btnState == HIGH)
        digitalWrite(ledPin, LOW);
    else 
        digitalWrite(ledPin, HIGH);
}

Click on upload button.

Whenever the user button(BT1) is pressed (at fall), then the LED should be ON. When the button is released then the LED should be off.

Exercise

Motion Detection Exercise

  • Use External interrupt to get the digital in data from the motion sensor

  • When the userbutton is pressed, it should turn-off the LED.

The experiment kit has IR motion sensor(HD-SEN0018) that detects a motion of an object nearby. It is often used in automatic lighting system at the front door. It is connected to PinName D5 as DigitalIn.

// set pin numbers
const int motionPin = 5;    // the number of the Motion detection sensor
const int btnPin = 3;       // the number of the push button pin
const int ledPin = 13;      // the number of the LED pin

void setup() {
  // initialize the LED pin as an output:
  // your code 
  
  // initialize the Motion detection sensor pin as an interrupt input:
  // your code

  // initialize the push button pin as an interrupt input:
  // your code
}

void loop() {

}

void motionDetected(){
  // LED on
  // your code
}

void btnPressed(){
  // LED off
  // your code
}

Ticker (SysTick interrupt)

We are going to create a simple program that uses System Timer Tick Interrupt that occurs periodically. Lets turn LED on and off at 1 sec of period.

Add STM32Timer Interrupt Library

Download the library as lastest version.

Add the library as .zip file.

On the menu bar, select sketch > Include Library > Add .ZIP Library. Then, open 'STM32_TimerInterrupt-(version).zip'.

STM32Timer class

  • STM32Timer

    STM32Timer ITimer(TIMx);
    ITimer.attachInterruptInterval(period_us, TimerHandler)
    • TIMx : Timer number (TIM1, TIM2, ...)

    • period_us : Period of Timer (unit: microseconds)

    • TimerHandler : a function handling the timers run

  • STM32_ISR_Timer

    STM32_ISR_Timer ISR_Timer;
    ISR_Timer.setInterval(period_ms, timerInterrupt);
    • period_ms : Period of ISR Timer (unit: miliseconds)

    • timerInterrupt : a function excuted whenever the period of ISR Timer.

Use the STM32TimerInterrupt interface to set up a recurring interrupt; it calls a function repeatedly and at a specified rate.

Example

Create a new program named as ‘TU_arduino_TimerInterrupt’.

You can make LED blink every second, even though there is no infinite loop. This is also called as the ‘Timer interrupt’ or ‘SysTick interrupt’.

#include "STM32TimerInterrupt.h"

#define HW_TIMER_INTERVAL_MS  1000

STM32Timer ITimer(TIM1);    // Init STM32 timer TIM1

const int ledPin =  13;

void setup() {
  pinMode(ledPin, OUTPUT);

  ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, timerInterrupt);
}

void loop(){
}

void timerInterrupt(){
  digitalWrite(ledPin, !digitalRead(ledPin));
}

Click on Upload button.

LED(LD2) should blink every second.

Exercise

Buzz output exercise

  • Buzz the sound for about 1 second that repeats for every 3 seconds.

  • e.g: 1 sec of buzzing then 2 secs of silence and repeat

This experiment kit has a digital buzzer (MCKPI-G1410).

It is connected at DigitalOut PA13. (But, you should wire PA13 to pinName D8.)

A simple example of using a buzzer:

void loop(){
  if (buzzState == HIGH)
    tone(buzzPin, 100);
    
  else if (buzzState == LOW)
    noTone(buzzPin);
}
#include "STM32TimerInterrupt.h"

#define HW_TIMER_INTERVAL_MS  1000

STM32Timer ITimer(TIM1);    // Init STM32 timer TIM1

// constants won't change. They're used here to set pin numbers:
const int ledPin =  13;   // the number of the LED pin
const int buzzPin = 8;    // the number of the buzzer pin

// variables will change:
int buzzState = LOW;
int cnt = 0;

void setup() {
  // initialize the LED pin as an output:
  // your code

  // Interval in microsecs
  // your code

  // Timer interrupt every 1sec
  // your code
}

void loop(){
  if (buzzState == HIGH)
    tone(buzzPin, 100);
    
  else if (buzzState == LOW)
    noTone(buzzPin);
}

// Whenever ISR Timer interrupts, this function is excuted.
void timerInterrupt(){
  // Use 'cnt' and 'buzzState' variables
  // your code 
  
}
button pin connection

image

buzzer pin connection

attachInterrupt()
Hint:
STM32Timer Interrupt Library
Hint:
pinMode()
source code
HUINS experiment kit