2024 STM32 - Multi Learning Shield
2024 STM32 - Multi Learning Shield
by:
Ts. Abd Rahim Kasiman
Department of Skills and Professional
Faculty of Technical and Vocational Education,
Universiti Tun Hussein Onn Malaysia (UTHM)
Tentative Program
-
08.00 09.00 Introduction to STM32 environment
-
09.00 09.30 Refreshment
-
09.30 10.30 Software setup for STM32CubeIDE & Tera Term
1st Day
-
10.30 13.00 Digital Output Control and Task Scheduling
-
13.00 14.00 Lunch + Photoshoot
-
14.00 15.30 SysTick timer concept
-
15.30 17.00 Basic Control Structure and Function Flow
-
08.00 09.00 Digital Input + External Interrupt (EXTI)
-
09.00 09.30 Refreshment
-
09.30 11.00 External Interrupt (EXTI)
2nd Day
-
11.00 13.00 Setting IOC for Arduino Multi Learning Shield on STM32L053
Tentative Program
-
08.00 09.00 Basic control on 7 segment using serial concept
-
09.00 09.30 Refreshment
-
09.30 10.30 7-segment using 74HC595 shift register concept
3rd Day
-
10.30 13.00 Programming 7-segment to display 4-digit display + Timer concept
-
13.00 14.00 Lunch
-
14.00 15.30 Analog concept on STM32L053
-
15.30 17.00 Programming for single and multiple-channel analog input
-
08.00 09.00 Review on WSMB 2023 Pre-Qualification questions for ESP module
-
09.00 09.30 Refreshment
-
09.30 12.00 Answer review on WSMB 2023 Pre-Qualification
4th Day
-
12.00 13.00 Review on KVSkill 2023 Final questions for ESP module
Pre-Training Survey Question
ISMT
ISMPV
Tuition Fees for Bachelor's Degree Program
Post Graduate Programme
Overview of Nucleo-L053R8
• The STM32 Nucleo board is a low-cost and easy-to-
use development platform used to quickly evaluate
and st ar t developm ent wit h an STM32
microcontroller in the LQFP64 package
User Manual
Nucleo board
Nucleo-L053R8 Features
• Common features
• STM32 microcontroller in LQFP64 or LQFP48 package
• 1 user LED shared with ARDUINO®
• 1 user and 1 reset push-buttons
• 32.768 kHz crystal oscillator
• Board connectors:
• ARDUINO® Uno V3 expansion connector
• ST morpho extension pin headers for full access to all STM32 I/Os
• Flexible power-supply options: ST-LINK USB VBUS or external sources
• On-board ST-LINK debugger/programmer with USB re-enumeration capability: mass storage, Virtual COM port, and debug
port
• Comprehensive free software libraries and examples available with the STM32Cube MCU Package
• Support of a wide choice of Integrated Development Environments (IDEs) including IAR Embedded Workbench®, MDK-ARM,
and STM32CubeIDE
• Board-specific features
• External SMPS to generate Vcore logic supply
• 24 MHz or 48 MHz HSE
• Board connectors:
• External SMPS experimentation dedicated connector
• Micro-B or Mini-B USB connector for the ST-LINK
• MIPI® debug connector
Hardware layout and config
Hardware layout and config
Hardware layout and config
Arduino connectors on Nucleo-L053R8
ST morpho connectors on Nucleo-L053R8
Schematic for
Nucleo L053R8
Nucleo-L053R8 schematic pg 2 (MCU)
Nucleo-L053R8 schematic pg 3 (STLINK-V2/1)
Nucleo-L053R8 schematic pg 4 (STLINK-V2/1)
Nucleo-L053R8 schematic pg 4 (POWER)
Nucleo-L053R8 schematic pg 4 (EXT CONN)
Installing STM32 on PC
• STM32CubeIDE is an advanced C/C++ development platform with peripheral conf iguration,
code generation, code compilation, and debug features for STM32 microcontrollers and
microprocessors. It is based on the Eclipse®/CDT™ framework and GCC toolchain for the
development, and GDB for the debugging. It allows the integration of the hundreds of
existing plugins that complete the features of the Eclipse® IDE.
Download
STM32CubeIDE
Installing Tera Term
• Tera Term (alternatively TeraTerm) is an open-source, free, software implemented,
terminal emulator (communications) program. It emulates different types of computer
terminals and supports telnet, SSH 1 & 2 and serial port connections.
Download
Tera Term
Hello World in STM…
Open STM32CubeIDE
Hello World in STM…
1 1. Select MCU/MPU selector
2. Type “STM32L053R8” in
2 commercial part number
3. Select STM32L053R8T6 from
list
4. Click Next
4
Hello World in STM…
1. Type the project’s name
2. Click Finish
1
2
Hello World in STM…
1. Right click PA5 and choose
GPIO_OUTPUT
1
2. Right click PC13 and choose
GPIO_INPUT
2
PA5
2
PC13
1
Hello World in STM…
Enabling USART2
1. Click connectivity in Categories
3 2. Choose USART2
3. Choose Asynchronous at Mode
4. Click NVIC Settings at Configuration
1 5. Tick global interrupt
5
2
Hello World in STM…
4
1
Clock Configuration
1. Click Clock Configuration Tab
2. Change from MSI to PLLCLK
3. Ensure 24MHz at HCLK
4. Click Device Config Tool Code Generation
3
2
Hello World in STM…
You will be presented with this display.
Follow instruction if your display is still in IOC
1 1. At Project Explorer, click Core and Src
2
3 2. Double click on main.c
Hello World in STM…
• Always write code in between the BEGIN and END comment
• NEVER write code outside these boundaries, as it will be omitted during the code generation of
IOC
Hello World in STM…
• Write the codes below according to the comment title
• Click icon to build the project (check for errors and warnings)
• If connected to hardware, click to download directly to board.
Hello World in STM…
• Once the code is downloaded, view the output on Tera Term
• Open Tera Term and choose STMicroelectronics on Serial
1 2
3
Hello World in STM…
• Go to Setup and choose Serial port…
• Change baudrate from 9600 to 115200 and click New setting
2 1
Hello World in STM…
• Click the black button on STM32 (RESET) and a text will appear on Tera
Term which is “HELLO WORLD!”
1
2
3
Double Click
-2,147,483,648 to
int Whole numbers -2, 0, 42
2,147,483,647 (32-bit)
±1.7E-308 to ±1.7E+308
float Numbers with fractional parts 3.14, -0.001
(double precision)
• GPIO_PIN_SET is HIGH
• GPIO_PIN_RESET is LOW
DIGITAL OUTPUT
• All items below are considered as digital output devices
• However, some of them can be controlled by the PWM technique
Blinking LED (A5)
OR
Warning – unused variable
• Observe that a warning will be indicated by a
yellow line marked on the right side of the main
window.
• Clicking the yellow line will bring you to the
problematic line. Put your arrow on top of the
exclamation mark to read the message.
• This warning is for unused variables.
• It can be omitted.
Blinking LED with condition
void setup()
{ pinMode (led, OUTPUT);
• loop() subroutine
} will run once
void loop()
{ digitalWrite(led, HIGH);
delay(2000); • kelip2() will loop
digitalWrite(led, LOW);
delay(500); forever
kelip2();
}
void kelip2()
{ while(1)
{ digitalWrite(led, HIGH); delay(100);
digitalWrite(led, LOW); delay(100);
}
}
Control Structure: WHILE statement
int limitSwitch,motor=13;
void setup() • motorUp() subroutine will
{ pinMode(motor,OUTPUT);
pinMode(12,INPUT_PULLUP); run once
digitalWrite(motor,HIGH); //OFF
}
void motorUp()
{ digitalWrite(motor,LOW); //ON
• RETURN will invoke last
while(1) subroutine and enter infinite
{ limitSwitch = digitalRead(12);
if(limitSwitch==0) //pressed loop without any
{ digitalWrite(motor,HIGH); //OFF commands
return;
}
}
}
Control Structure: LOOP conclusion
Video
Programming Basics - Comments
• Ev e ry p ro g ram c an hav e a c o m m e nt d e p e nd i ng o n the
programmer itself.
Comments is not considered a
• Two ways of comment: programming instruction.
Therefore it can be omitted.
• Single line statement
//This is a single line comment
void setup()
{ // initializes digital pin LED_BUILTIN as an output
pinMode (LED_BUILTIN, OUTPUT);
}
Single line statement
// the loop function runs over and over again forever
void loop()
{ digitalWrite(LED_BUILTIN,HIGH); //turn the LED ON
delay(1000); //wait for a second
digitalWrite(LED_BUILTIN,LOW); //turn the LED OFF
delay(1000); //wait for a second
}
Programming Basics - Indentation
• Indentation is a good programming behaviour to make reading
and troubleshooting of a program much easier and faster.
• To emphasize structured programming.
void setup() { void setup()
pinMode (LED_BUILTIN, OUTPUT); { pinMode (LED_BUILTIN, OUTPUT);
} }
void loop()
{ for(int i=0;i<3;i++)
{ digitalWrite(13,HIGH); delay(500);
digitalWrite(13,LOW); delay(500);
} Bracket for FOR structure
for(int j=0;j<5;j++)
{ digitalWrite(12,HIGH); delay(500);
digitalWrite(12,LOW); delay(500); Bracket for FOR structure
while(1)
{ digitalWrite(11,HIGH);
}
Bracket for WHILE structure
}
}
Bracket for LOOP subroutine
Programming Basics - Indent
Before formatting After formatting
void setup() void setup()
{pinMode(13, OUTPUT); { pinMode(13, OUTPUT);
} }
void setup()
• Setup subroutine is being read once.
{ Serial.begin(9600);
} • In Loop subroutine, it calls upon tambah
void loop()
subroutine.
{ tambah();
} • The Tambah subroutine will process the
void tambah() variable value and print it.
{ cnt++;
delay(1000); • “return” will invoke the subroutine caller
Serial.println(cnt);
return; and loop these processes as it is the
} nature of the loop subroutine.
STATU
Subroutine Feature
S
Send data
NO
(argument)
Return data NO
Programming Basics – Subroutines 1
int cnt=13;
void loop()
{ tambah(); Callee function no argument received
}
void tambah()
{ cnt++;
delay(1000);
Return value no return value
Serial.println(cnt);
return;
}
Programming Basics – Subroutines 2
int cnt=5;
• In the Loop subroutine, it calls upon the
void setup()
{ Serial.begin(9600); t a m ba h s u br o u t i n e w i t h a s i n gl e
}
argument of data passed.
void loop()
{ tambah(15); • The Tambah subroutine will receive the
}
data while declaring a data type.
void tambah(int no1)
{ cnt = cnt+no1; • After that, it will process the variable
Serial.println(cnt);
return; value and print it.
}
• “return” will invoke the subroutine caller
STATU
Subroutine Feature without any value returned.
S
Send data
YES
(argument)
Return data NO
Process data YES
Programming Basics – Subroutines 2
int cnt=0,nombor=120;
void setup()
{ Serial.begin(9600);
STATU
} Subroutine Feature
S
void loop() Send data
{ tambah(15,20,nombor); YES
(argument)
}
Return data NO
void tambah(int no1, int no2, int no3) Process data YES
{ cnt = no3 / no2 + no1;
Serial.println(cnt);
return;
}
Current Sourcing /
PULL_DOWN
Current Sinking /
PULL_UP
PB on Nucleo
PB on Nucleo
• At the IOC, ensure the digital pin has been declared.
• The PB on PC13 is wired as Active LOW, giving a reading of 0 (LOW)
if it is being pushed.
• If the PB is not pushed (idle mode), it will give a value of 1 (HIGH).
PB on Nucleo
DIGITAL INPUT: Debouncing
• Bouncing is the tendency of two metal contacts in an electronic
device to generate multiple signals as the contacts close or open.
Video
Bouncing on Nucleo
When PB is pushed and released, the cnt
should be incremented by 1 but the result
differs because of the bouncing!
Debouncing on Nucleo
We have debounced the signal. However, when PB is
pushed & HOLD, the value still increases at an interval
of 0.2s. How to solve?
Debouncing on Nucleo
This program ensures that even though the
push button is pushed and hold, the cnt
value only increase by +1
Debouncing on Nucleo
Same function but different technique!
EXTI on STM32
• An external interrupt (EXTI) is a computer system interrupt that
happens as a result of outside interference, whether that's from the
user, from peripherals, from other hardware devices or through a
network.
EXTI on STM32
• We are going to make a NEW program which will:
1. Display cnt value which is incremented every 0.1 second.
2. Toggle LED on PA5 when PB1 (PC13) is pushed via EXTI
EXTI setting on IOC
2
6
3
1
5
EXTI setting on IOC
• Passive buzzers:
• Needs an AC signal to produce sound.
• Downside: need more complex circuitry to control them, like an oscillating
555 timer or a programmable microcontroller for PWM
Timer concept
2
2
b. Counter Period : 100-1
6
6
Programming PWM on buzzer
1 2
1 2
33
AML IOC on STM32CubeIDE
AML IOC on STM32CubeIDE
1. Connectivity
a) USART2 = Mode: Asynchronous
b) NVIC setting: Tick Global Interrupt
2. Clock Config
a) Tick from MSI to PLLCLK.
b) Ensure 24MHz on HCLK
Change GPIO input to Pull-up for PA1, PA4 & PB0 (digital input)
AML IOC on STM32CubeIDE
Change GPIO output to Open Drain for PA6 & PA7 & PB6 (digital output)
Then, click Device Config Tool Code Generator
AML LEDs
1 4
D1
D4 D3 D2 D1
D2 0 1 0 1
D3
01012 = 510
D4 You can turn on any LED using any numbering system as follows…
Basic Numbering System
Numbering System LEDs
Decimal Binary Code Hex Code D4 D3 D2 D1
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
AML LEDs
From the table, we can use any numbering system to light any LED for any sequence
or or
Any numbering system chosen above will produce the same result, which is:
D4 D3 D2 D1
Programming Basics - Subroutines
• A subroutine (also called a subprogram) is an abstraction of a
process that is called which can:
• Receive data,
• Process data, and
• Return a value.
void loop()
{ tambah(); Callee function no argument received
}
void tambah()
{ cnt++;
delay(1000);
Return value no return value
Serial.println(cnt);
return;
}
Programming Basics – Subroutines 2
int cnt=5;
• In Loop subroutine, it calls upon tambah
void setup()
{ Serial.begin(9600);
subroutine with a single argument of
} data passed.
void loop()
{ tambah(15);
• Tambah subroutine will receive the data
} while declaring a data type.
void tambah(int no1)
{ cnt = cnt+no1;
• After that, it will process the variable
Serial.println(cnt); value and print it.
return;
}
STATU • “return” will invoke the subroutine caller
Subroutine Feature
S without any value returned.
Send data
YES
(argument)
Return data NO
Process data YES
Programming Basics – Subroutines 2
int cnt=0,nombor=120;
• PTD
How to solve?
• PV
• BEGIN 0
AML Push Button
AML LEDs + PB Exercise
• Make a program to:
1. Blink LED on PA5 at an interval of 1s.
2. Display value of time interval on serial.
3. If S3 is pressed, it will decrease the interval by -0.2s
4. If S1 is pressed, it will increase the interval by 1s
5. Lowest time interval is 0.2s and highest is 3s
6. If S2 is pressed, it will reset the time interval to 1s
7. If the time interval is 0.2s and the user press the PB for 3s, it will display on
serial “HI RAHIM”
AML Buzzer
• Your buzzer is wired on B3 as Active HIGH interfaced with the PNP
transistor as load switching.
• Add buzzer by going to IOC, declaring it as GPIO_OUTPUT, and
renaming it BUZZER.
• Set GPIO mode as Open Drain and generate code.
• Add the necessary code as follows:
AML Buzzer AMLBuzz
Now, every time you press PB, a beeping sound will occur.
Make a subroutine that beeps according to the argument value
AML Buzzer
Every time you press PB, two beeping sounds will occur!
AML 7 Segment
• The 7 segment LED is interfaced with 74HC595 which is an 8 bit
shift register.
• In order to control the 7-segment, we need to understand the
working principle of a shift register.
• It uses a SIPO register where it stands for Serial Input, Parallel
Output.
• In general, the input data is given bit by bit serially.
• Once the Latch pin is enabled, the content of shift register is copied
to 2 register which is storage/latch register that is connected to Q
nd
0
–Q 7
74595 concept on LEDs
7-segment CA
DE
dp g f e d c b a BIN HEX
C
1111111
1 1 1 1 1 1 1 1 nil FF
1
1100000
1 1 0 0 0 0 0 0 0 C0
0
1111100
1 1 1 1 1 0 0 1 1 F9
1
1010010
1 0 1 0 0 1 0 0 2 A4
0
1011000
1 0 1 1 0 0 0 0 3 B0
0
1001100
1 0 0 1 1 0 0 1 4 99
1
1001001
1 0 0 1 0 0 1 0 5 92
0
1000001
1 0 0 0 0 0 1 0 6 82
74595 concept on 7-segment CA
74595 on 2 units of 7-segment
• The Q7’ of the 1st 74595 is fed to Data Pin
on 2nd 74595
• Other pin such as Latch, Clk & reset on 2nd
74595 is shared with the 1st pin of 74595
7-segment circuit on AML
This circuit is triggered manually.
Let’s wire each PB to signal pins.
00100101 00101111
LS
MSB B
7-segment circuit on AML
How to program?
Programming 7-segment on AML pg1
Programming 7-segment on AML pg2
0 1 2 3 4 5 6 7 8 9
7-segment program explanation
If x = 4536;
0 1 2 3 4 5 6 7 8 9
3.3
1
11
0.825
00
Analog Pins on STM32L053
ADC_IN10 -
ADC_IN11 -
ADC_IN12 -
ADC_IN13 -
ADC_IN0 -
ADC_IN1 -
ADC_IN2 -
ADC_IN3 -
ADC_IN4 -
ADC_IN5 -
ADC_IN6 -
ADC_IN7 -
ADC_IN14 -
ADC_IN15 -
ADC_IN8 -
ADC_IN9 -
IOC for Analog on STM32
1. Open Analog in Categories.
2. Select ADC.
3. Tick IN0 as our variable resistor is
wired to PA0.
4. In parameter setting, change:
a. Resolution : 10-bit
Insert GetTick in t2
Reading multiple ADC on STM32
• The previous program only reads single ADC at IN0.
• What if there are 3 analog inputs to be read?
• We will read each channel and save them in an array before displaying it
on USART.
• Connect LM35 and a variable resistor to the label PA4 and PA5 on AML
shield according to the picture.
LM35
• Arduino A4 = PC1
(ADC_IN11)
• Arduino A5 = PC0
(ADC_IN10)
IOC setup for multiple ADC
0°C = 0V
1°C = 10mV = 0.01V
10°C = 100mV = 0.1V
100°C = 1000mV = 1.0V
150°C = 1500mV = 1.5V
LM35: Exercise
What is the voltage for these readings?
8
Displaying temperature AMLanC
• From the previous slides, there are exercise that states if 0.17V =
17⁰C
• From observation, the voltage is multiplied with 100 to obtain the
temperature in celcius
ANALOG CASE STUDY
Analog Input: Analog in Linear Graph
Flame Sensor Module
• Used to detect fire or other light source with wavelength of 760nm ~ 1100nm light.
20cm 1V (20,1)
Step 2: Graph
Y (volt)
4.8V
1V
X (dist)
20cm 100cm
Analog Input: Analog in Linear Graph
a) 25cm
b) 105cm
Analog Input: Analog in Linear Graph
Anemometer
• Output signal range: 0-5V
• Supply Voltage: DC12-24V
• Resolution: 0.1m/s
• Effective range: 0-30m/s
• Type: 3 cups
• System error: ±3%
• Connection mode: 3 wires
• Wiring map: 12V, Gnd, analog