Nucleo Boards Programming With The STM32CubeIDE
Nucleo Boards Programming With The STM32CubeIDE
Nucleo Boards
Programming with
the STM32CubeIDE
Hands-on in more than 50 projects
Dogan Ibrahim
Nucleo Boards Programming
with the STM32CubeIDE
Hands-on in more than 50 projects
Dogan Ibrahim
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.
● Acknowledgements
The following figures and pictures in this book, numbered: 1.1, 1.5, 1.6, 1.7, 1.9, 2.2, 2.3, 2.9, 2.10 through 2.20,
2.22 through 2.32, 4.1, 4.2, 6.1, 14.1, 15.1 through 15.10, 16.2, and 16.7 are taken from these STMicroelectronics
sources:
• UM1724 User Manual, STM32 Nucleo-64 Boards (DocID025833 Rev 14);
•R
M0351 Reference Manual STM32L4x5 and STM32L4x6 advanced ARM®-based 32-bit MCUs (DocID024597 Rev 5).
The pictures of Nucleo Expansion Boards in Chapter 16 of the book are taken from the following STMicroelectronics
Internet source:
http://www.st.com/en/evaluation-tools/stm32-nucleo-expansion-boards.html?querycriteria=productId=SC1971.
All the above figures/pictures are used with the written permission of:
© STMicroelectronics. Used with permission.
The author would like to thank to Michael Markowitz of the STMicroelectronics for giving permission to reproduce
the above pictures/figures, as well as for providing sample Nucleo expansion boards for use in the projects
contained in the book. The author is also grateful to Elektor International Media for editing and publishing the book.
● The author and publisher have used their best efforts in ensuring the correctness of the information contained
in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by
errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other
caus
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro
engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops
and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social
media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
●4
Content
PREFACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2.1 Cortex-M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.2.2 Cortex-R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.2.3 Cortex-A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
●5
Nucleo Boards Programming with the STM32CubeIDE
2.3.10 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.3.11 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
CHAPTER 3 • S
TM32 Nucleo Software Development Tools (Toolchains) . . . . . . . . 56
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.5 MDK-ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.6 TrueSTUDIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.8 STM32CubeIDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
CHAPTER 5 • S
TM32CubeIDE Nucleo-L476 Projects . . . . . . . . . . . . . . . . . . . . . . . . 72
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
●6
5.15 Project 14: Interrupt-Based Up/Down Counter with Four-Digit 7-Segment LED
Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
5.16 Project 15: Multiple External Interrupts Sharing the Same Interrupt Line . . . . . 180
6.5 Project 2: 4-Digit 7-Segment LED Up Counter with Timer Interrupts . . . . . . . . . . 195
●7
Nucleo Boards Programming with the STM32CubeIDE
9.2 Project 1: Sawtooth Waveform Generator with Manual DAC Driving . . . . . . . . . . 277
9.5 Project 4: Arbitrary Waveform Generator with Manual DAC Driving . . . . . . . . . . . 287
●8
●9
Nucleo Boards Programming with the STM32CubeIDE
16.5 Motion MEMS and Environmental Sensor Expansion Board (X-NUCLEO-IKS01A2) 461
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
● 10
PREFACE
PREFACE
Arm (previously written as ARM; arm) is a family of processors based on the RISC archi-
tecture that requires fewer transistors than conventional processors. As a result, Arm pro-
cessors cost less, have reduced power consumptions, and are therefore desirable for use
in portable battery-operated devices. Currently, a very large percentage of smartphones,
laptops, tablet computers, and portable games devices are all designed using the Arm ar-
chitecture. Most Arm processors have 32-bit instruction sets with floating point support. It
is estimated that over 100 billion Arm processors were produced in the year 2017.
Arm-Cortex is a family of 32-bit RISC processors consisting of Cortex-M0/M0+/M1/M3
and Cortex-M4. Lately, new models such as Cortex-M323 and Cortex-M33 are available.
Although 8-bit microcontrollers have been very popular in the past, the devices in the Cor-
tex-M series have become widespread replacements.
The STM32 Nucleo family of processors is manufactured by STMicroelectronics. These are
low-cost Arm microcontroller development boards. This book is about developing projects
using the Nucleo-L476RG development board with the STM32CubeIDE software. In the
early chapters of the book the architecture of the Nucleo family is briefly described.
All the projects in the book have been designed using the STM32CubeIDE software devel-
opment tool which can be downloaded free of charge from the STMicroelectronics website.
The book covers many projects using most features of the Nucleo-L476RG development
boards where the full software listing for STM32CubeIDE are given for every project with
the description of each software. The projects range from simple flashing LEDs to more
complex projects using modules and devices such as GPIO, ADC, DAC, I2C, SPI, LCD, power
management, analogue inputs, and others. In addition, several projects are given using the
compatible Nucleo Expansion Boards. These Expansion Boards plug on top of the Nucleo
development boards and provide features such as industrial input/output, DC motor drive,
stepper motor drive, environmental MEMS sensors, accelerometer, gyroscope, Wi-Fi, and
many more.
All the projects in the book have been fully tested and are working. The following sub-head-
ings are given for each project where possible:
• Project Title
• Description
• Aim
• Block Diagram
• Circuit Diagram
• Program Listing (based on STM32CubeIDE)
Complete program listings for each project can be obtained from the book support and
resources page on the Elektor website, www.elektor.com/xxxxxxx.
I hope you find the projects interesting and the book becomes a useful source of reference
for your future STM32 Nucleo projects based on the STM32CubeIDE.
London, 2020
● 11
Nucleo Boards Programming with the STM32CubeIDE
1.1 Overview
This Chapter is about the STM32 Nucleo development boards, and brief specifications of
their various guises are described here. In addition, the popular Nucleo-L476RG board used
in the projects in this book is described in greater detail.
About half of the STM32 Nucleo boards are in the mainstream category. Examples of the
mainstream boards are: Nucleo-F303K8, Nucleo-F042K6, Nucleo-F303RE etc. There are
three sub-categories in the mainstream category:
• F0, Arm Cortex-M0+
• F1, Arm Cortex-M3
• F3, Arm Cortex-M4
The high-performance boards have large memories and faster MCUs. Examples of high-per-
formance boards are: Nucleo-F410RB, Nucleo-F401RE, Nucleo-F722ZE, etc. There are
three subcategories in the high-performance category:
• F2, Arm Cortex-M3
• F4, Arm Cortex-M4
• F7, Arm Cortex-M7
The Nucleo-32 boards are small (50 mm × 19 mm) and Arduino Nano compatible. The
Nucleo-64 and Nucleo-144 are Arduino Uno compatible and they also have ST Morpho ex-
tension connectors which carry the MCU pins. There are large number of Arduino Nano/Uno
compatible shields available in the market and these shields can easily be used with the
● 12
CHAPTER 1 • STM32 Nucleo Development Boards
● 13
Nucleo Boards Programming with the STM32CubeIDE
Where
xx is the processor code as shown in Table 1;
yy is a number;
z is the MCU pin number count as shown in Table 1.2.
● 14
CHAPTER 1 • STM32 Nucleo Development Boards
G 1024
H 1536
I 2048
STM32L031K6T6: L0 family, number 31, 32 pin device, 32 KB flash memory, LQFP type
packaging, –40 to 85 ºC operating temperature range.
● 15
Nucleo Boards Programming with the STM32CubeIDE
● 16
CHAPTER 1 • STM32 Nucleo Development Boards
● 17
Nucleo Boards Programming with the STM32CubeIDE
● 18
CHAPTER 1 • STM32 Nucleo Development Boards
The components on the top side of the board are shown in Figure 1.6. Some of the impor-
tant components that you may need to know their locations on the board are:
• CN1: mini USB socket
• CN2: ST-LINK/Nucleo selector
• B1: User pushbutton
• B2: Reset button
• LD1: red/green communications LED
• LD2: green user LED
• LD3: red power LED
• Arduino connectors
• ST morpho connectors
● 19
Nucleo Boards Programming with the STM32CubeIDE
Figure 1.7: Bottom side of the board. (©STMicroelecronics. Used with permission).
● 20
CHAPTER 1 • STM32 Nucleo Development Boards
Figure 1.8: Powering the board through the mini USB port.
Jumper JP1 on the ST-LINK board can be configured depending on the maximum current
consumption of the MCU board. This jumper should be OFF (default state) for an allowed
current of 300 mA, and it should be ON for an allowable current of 100 mA. If the total cur-
rent consumption of the board exceeds 300 mA then it is important that an external power
supply must be used through VIN or E5V.
VIN can be supplied through pin 8 of connector CN6 or pin 24 of connector CN7. The volt-
age range must be between 7 VDC to 12 VDC. The maximum current capacity should be
800 mA when the input voltage is 7 V, 450 mA when the voltage is between 7 V and 9 V,
and 250 mA when the voltage is greater than 9 V and less than 12 V.
E5V can be supplied through pin 6 of connector CN7. The voltage range is 4.75 V to 5.25 V,
with maximum current capacity of 500 mA.
When power is supplied through VIN or E5V, it is still possible to use the USB connector for
communication, programming or debugging. In this situation, the board must first be pow-
ered using VIN or E5V and then the USB cable should be connected to the PC.
The following procedure should be followed if external power will be used and at the same
time the USB connector will be used for communication/programming/debugging:
• connect pin 2 and pin 3 of jumper JP5;
• remove jumper JP1;
• connect external power VIN or E5V;
• make sure that LD3 is turned ON;
• connect the USB connector to your PC.
● 21
Nucleo Boards Programming with the STM32CubeIDE
LD2 is a green colour user-controlled LED. This LED is connected to pin 11 of connector
CN10 (STM32 I/O pin PA_5) on the Nucleo-L476RG board. The Arduino D13 port pin is also
connected to LD2. Applying logic 1 turns ON the LED.
LD3 is the red power LED that indicates when +5 V power is available on the MCU board.
● 22
CHAPTER 1 • STM32 Nucleo Development Boards
V2-1 functions are enabled for on-board programming. When both CN2 jumpers are OFF
then the ST-LINK/V2-1 functions are enabled for external CN4 connector (SWD supported).
Before connecting the Nucleo-64 board to a Windows PC, a driver for ST-LINK/V2-1 must
be installed. This can be downloaded from the following site. You will have to register at
the site so that you can download the driver. At the time of writing this book the driver was
called en.stsw-link009.zip and found here:
http://www.st.com/en/development-tools/stsw-link009.html#getsoftware-scroll
The Arduino Uno connector pin configuration on the Nucleo-476RG board is shown in
Table 1.6.
● 23
Nucleo Boards Programming with the STM32CubeIDE
● 24
CHAPTER 1 • STM32 Nucleo Development Boards
• check that jumper JP1 is OFF, JP5 is ON, JP6 is ON, and CN2 are ON;
• connect the Nucleo-L476RG board connector CN1 to a PC with a mini USB cable
(Type A to mini-B cable). The red LED LD3 (PWR) and LD1 (COM) should light
up. LD1 (COM) and green LED LD2 should blink;
• press button B1 (left button);
• click button B1 and you should see the blinking rate of the green LED LD2
change accordingly.
If the demonstration software is run successfully then the board should be ready for use
in projects.
1.4 Summary
In this Chapter we have learned about the types of STM Nucleo development boards. In
addition, the specifications and configuration of the popular Nucleo-L476RG board have
been described in greater detail, and the steps to run the demonstration software on the
Nucleo-L476RG are given. This is the development board used in all the projects described
in this book.
In the next Chapter we shall be looking at the STM32 processor architecture.
● 25
Nucleo Boards Programming with the STM32CubeIDE
2.1 Overview
In this Chapter we shall be looking at the internal architecture of the STM32 Nucleo pro-
cessors. This is important for developing efficient programs and projects. The popular STM-
32L476RGT6 microcontroller used in all the projects in this book will be considered as an
example Arm processor.
For example, if you need to develop a battery powered device such as a mobile phone or
a games device, then very high clock speed as well as long battery life are main require-
ments. But if you are developing a traffic lights controller then very high performance is
not a requirement. In general, as the clock speed goes up so does the power consumption
and as a result a trade-off should be made in choosing a microcontroller for a specific ap-
plication.
Arm has been designing 32-bit processors for over 20 years and in the last few years they
have also started to offer 64-bit designs. In actual fact, Arm is a company specialized in
designing the processor architecture and they do not manufacture or sell processor chips.
Arm makes money by licensing their designs to chip manufacturers. The manufacturers
use the core Arm processors (e.g. the core CPU) and integrate with their own peripherals
to end up in a complete microcontroller chip. Arm is then given royalty fees for each chip
manufactured by the third-party companies. Companies that use Arm core processors in-
clude Apple, Atmel, Broadcom, Cypress Semiconductors, Freescale Semiconductors, Analog
Devices, Nvidia, NXP, Samsung Electronics, Texas Instruments, Qualcomm, Renesas, and
many others.
Arm was originally known as the Acorn Computers and they have developed the first Acorn
RISC Machine (Arm) architecture in the 1980s to use in their personal computers. First Arm
processors were co-processor modules used in the BBC Micro series. After failing to find
suitable high-performance microprocessor chips in the market, Acorn decided to design
their own processors. In 1990, the research section of Acorn formed the ARM Ltd.
● 26
CHAPTER 2 • STM32 Nucleo Processor Architecture
Currently Arm is the most widely used processor in terms of quantity manufactured. Over
50 billion Arm processors have been produced as of 2014, where 10 billion were produced
in 2013 only. The Arm 32-bit architecture is the most widely used in mobile devices where
about 98% of all mobile phones sold in the year 2005 used at least one Arm processor. The
Arm architecture is known to offer the best MIPS-to-watts ratio as well as MIPS-to-$ ratio
in the industry, and the smallest CPU size.
The small size, low power consumption, and low cost make Arm an ideal processor in em-
bedded applications. Arm processors are based on an instruction set called Thumb. With
clever design this instruction set takes 32-bit instructions and compresses them down to
16-bits, thus reduces the hardware size, which also reduces the overall cost. The processor
makes use of multistage pipelined architecture that is easier to learn, build, and program.
Arm processors are based on the RISC (Reduced instruction Set Computer) architecture
and are available as 32-bit or 64-bit multi-core structures. RISC processors, as opposed
to CISC (Complex Instruction Set Computer) processors have smaller number of instruc-
tions and fewer transistors (hence smaller die size), as a result they can operate at higher
speeds. Unimportant or not frequently used instructions are removed, and the pathways
are optimized to result in superior performance.
It is important to realize that Arm’s core architecture is only a processor and it does not
include graphics, input-output ports, USB, serial communication, wireless connectivity, or
any other form of peripheral modules. Chip manufacturers build their systems around the
Arm core design and therefore different manufacturers offer different types of Arm based
microcontrollers.
Over the last 20 years or so Arm (ARM) had developed many 32-bit processors. Figure
2.1 shows some of the popular members of the Arm processor family. The first successful
member was the ARM7TDMI which had high code density and low power consumption. This
processor, based on the Von Neumann architecture, was operating at 80 MHz and was used
in early mobile phones. ARM9 was developed in 1997 with Harvard architecture and oper-
ated with 150 MHz, thus offered higher performance. ARM10 and ARM11 were developed
in the years 1999 and 2003, respectively. Both processors were based on the Harvard ar-
chitecture. ARM10 operated at 260 MHz and ARM11 operated at 335 MHz. Around the year
2003 Arm decided to improve their market share by developing new series of high-perfor-
mance processors. As a result, the Cortex family or processors were created. The Cortex
family consists of three processor families: Cortex-M, Cortex-R, and Cortex-A. We shall now
briefly look at these families.
● 27
Nucleo Boards Programming with the STM32CubeIDE
2.2.1 Cortex-M
Cortex-M series are built around the ARMv6-M architecture (Cortex-M0 and Cortex-M0+)
and the ARMv7-M architecture (Cortex-M3 and Cortex-M4). These processors are specif-
ically designed for the microcontroller market, offering quick and deterministic interrupt
responses, low power consumption, low cost, fairly high performance, and ease of use. The
Cortex-M3 and Cortex-M4 are very similar in architecture and have the same instruction set
(Thumb 2) with the difference that the Cortex-M4 offers digital signal processing (DSP) ca-
pability and has optional floating point unit (FPU). Cortex-M4 with its DSP and floating-point
capability is an ideal processor for the IoT and wearable applications. For cost sensitive
and lower performance applications the Cortex-M0 or the Cortex-M0+ can be used. The
Cortex-M0 processor has small gate count (12K gates) and consumes only 12.5 µW/MHz.
The Cortex-M0+ consumes only 9.85 µW/MHz and is based on a subset of the Thumb 2
instruction set and its performance is slightly above that of Cortex-M0 and below that of
the Cortex-M3 and Cortex-M4. Cortex-M7 is a high performance processor that can handle
fast DSP and single or double precision floating point operations and is mainly used in ap-
plications where higher performance than the Cortex-M4 is required.
2.2.2 Cortex-R
Cortex-R series are real-time higher performance processors than the Cortex-M and some
members are designed to operate at high clock rates in excess of 1 GHz. These processors
are commonly used in hard-disk controllers, network devices, automotive applications, and
in specialized high-speed microcontroller applications. Cortex-R4 and Cortex-R5 are the
early members and can be used at clock speeds of up to 600 MHz. Cortex-R7 is a newer
member that incorporates 11-stage pipeline for high performance, and it can operate in
excess of 1 GHz. Although the Cortex-R processors are high performance their architecture
is complex and these processors consume high power, making them unsuitable for use in
mobile battery powered devices.
● 28
CHAPTER 2 • STM32 Nucleo Processor Architecture
2.2.3 Cortex-A
Cortex-A are the highest performance Arm processors designed for use with real-time
operating systems in mobile applications such as in mobile phones, tablets, GPS devices
and so on. These processors support advanced features for operating systems such as An-
droid, ioS, Linux, Windows etc. In addition, advanced memory management is supported
with virtual memory. Early members of the family included processors such as Cortex-A5
to Cortex-A17, based on the ARMv7-A architecture. Latest members of the family are the
Cortex-A50 and Cortex-A72 series designed for low power and very high-performance mo-
bile applications. These processors are built using the ARMv8-A architecture which offers
64-bit energy-efficient operation with the capability of more than 4 GB of physical memory.
Processor Description
Cortex-M7 High performance processor, used in applications where Cortex-M4 is not fast
enough, supports DSP and single and double precision arithmetic
Cortex-M4 Similar architecture as the Cortex-M3 but includes DSP and floating point arith-
metic, used in high-end microcontroller type applications
Cortex-M3 Very popular, low power consumption, medium performance, debug features,
used in microcontroller type applications
Cortex-M1 Designed mainly for programmable gate array applications
Cortex-M0+ Lower power consumption and higher performance than the Cortex-M0
Cortex-M0 Low power consumption, low to medium performance, smallest Arm processor
● 29
Nucleo Boards Programming with the STM32CubeIDE
Table 2.2 shows the CoreMark results for some of the commonly used microcontrollers.
As can be seen from this table, Cortex-M7 achieves 5.01 CoreMark/MHz, while the PIC18
microcontroller achieves only 0.04 CoreMark/MHz.
Processor CoreMark/MHz
Intel Zeus 8.01
Cortex-M7 5.01
Cortex-A9 4.15
Espressif ESP32 4.13
Cortex-M4 3.40
Cortex-M3 3.32
Cortex-M0+ 2.49
Espressif ESP8266 2.38
Cortex-M0 2.33
dsPIC33 1.89
PIC24 1.88
MSP430 1.11
PIC18 0.04
● 30
CHAPTER 2 • STM32 Nucleo Processor Architecture
modules used in most projects, such as I/O, timers, ADC converter and DAC converter,
interrupts, I2C, USART, and so on. Interested readers can get detailed information from
the manufacturer’s datasheets available for download on the Internet. Readers should note
that the structure of the STM32 family of processors is similar and after becoming familiar
with the STM32L476RGT6, it should not be too difficult to learn the structure of the others
in the family.
● 31