0% found this document useful (0 votes)
25 views

Design of IoT Laboratory Exercises

Uploaded by

jpc.freelance19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Design of IoT Laboratory Exercises

Uploaded by

jpc.freelance19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

FACULTY OF INFORMATICS

Design of IoT laboratory exercises

Bachelor’s Thesis

MARTIN MOJŽIŠ

Brno, Spring 2023


FACULTY OF INFORMATICS

Design of IoT laboratory exercises

Bachelor’s Thesis

MARTIN MOJŽIŠ

Advisor: Ing. Václav Oujezský, Ph.D.

Department of Computer Systems and Communications

Brno, Spring 2023


Declaration
Hereby I declare that this paper is my original authorial work, which
I have worked out on my own. All sources, references, and literature
used or excerpted during elaboration of this work are properly cited
and listed in complete reference to the due source.

Martin Mojžiš

Advisor: Ing. Václav Oujezský, Ph.D.

iii
Acknowledgements
I would like to thank my supervisor Ing. Václav Oujezský, Ph.D. for
all the support, time and advice he provided during my work on this
thesis. I am also grateful to my family and friends for supporting me
while I was writing this thesis.

iv
Abstract
In this thesis, laboratory exercises related to Internet of Things are
designed and programmed. Different microcontrollers are used for
programming the exercises, and difficulty of implementation of the
exercises is compared for each of these devices. The used devices are
microcontrollers STM32, Raspberry Pi Pico, and Rock Pi S, which also
are described in this thesis. The designed exercises concern program-
ming of I/O devices, namely rotary encoder, LCD, shift register and
buttons. It is explained how these I/O devices work and how to pro-
gram them. For programming, Arduino IDE and Python was used.
The used microcontrollers are compared regarding their suitability for
programming of the exercises. It was discovered that from the used
microcontrollers, the most suitable for programming these exercises
is the STM32.

Keywords
Exercises, Internet of Things, Lasaris, Microcontroller

v
Contents
Introduction 4

1 Description of microcontrollers 6
1.1 STM32 microcontroller . . . . . . . . . . . . . . . . . . . 6
1.2 Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . 9
1.3 MH-Tinny Attiny88 . . . . . . . . . . . . . . . . . . . . . 11
1.4 Rock Pi S . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.5 Comparison of microcontrollers . . . . . . . . . . . . . . 12

2 Design of exercises 13
2.1 Devices used . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.1 LCD and rotary encoder . . . . . . . . . . . . . . 14
2.2.2 Shift register and buttons . . . . . . . . . . . . . 16

3 Programming of microcontrollers 19
3.1 Programming of STM32 . . . . . . . . . . . . . . . . . . 19
3.2 Programming of Raspberry Pi Pico . . . . . . . . . . . . 22
3.2.1 Arduino IDE . . . . . . . . . . . . . . . . . . . . . 22
3.2.2 MicroPython . . . . . . . . . . . . . . . . . . . . 24
3.3 Programming of Rock Pi S . . . . . . . . . . . . . . . . . 25
3.4 Comparision of complexity of programming exercises
for different microcontrollers . . . . . . . . . . . . . . . 27
3.5 Advantages and disadvantages of individual microcon-
trollers used in practical exercises . . . . . . . . . . . . . 28
3.6 Encountered problems with microcontrollers . . . . . . 29

4 Conclusion 32

Bibliography 34

A Lasaris Boards 37

B Content of the Attachment 39

vi
List of Figures
2.1 STM32 board with LCD. . . . . . . . . . . . . . . . . . . . 15
2.2 STM32 board with shift register. . . . . . . . . . . . . . . . 17
3.1 IDE configuration. . . . . . . . . . . . . . . . . . . . . . . . 20
A.1 STM32 base board [29]. . . . . . . . . . . . . . . . . . . . . 37
A.2 RS485 communication board [29]. . . . . . . . . . . . . . . 37
A.3 LCD application board [29]. . . . . . . . . . . . . . . . . . 38

vii
List of Listings
3.1 Includes and initialization of LCD. . . . . . . . . . . . . 21
3.2 Rotary encoder pins definition and initialzation. . . . . 21
3.3 Import and initialization of LCD controller. . . . . . . . 26
3.4 Example of usage of LCD controller. . . . . . . . . . . . 27
3.5 Finding right I2C address. . . . . . . . . . . . . . . . . . 28

1
Abbreviations
CAN Controller Area Network 7

COM communication port 23

CPU central processing unit 7

GPIO general-purpose input/output 9

I/O input/output 4

I2C Inter-Integrated Circuit 7

IDE integrated development environment 9

IoT Internet of Things 4

IPS in-plane switching 14

KB kilobyte 7

LAN Local Area Network 25

LCD Liquid Crystal Display 6

MCU microcontroller unit 7

MHz Megahertz 7

NVRAM non-volatile random-access memory 7

OLED Organic Light-Emitting Diode 7

PC personal computer 13

PCM pulse-code modulation 11

PIO Programmable I/O 10

PWM pulse-width modulation 7

2
Abbreviations

SBC single-board computer 11

SD Secure Digital 11

SIPO serial-in parallel-out 17

SPI Serial Peripheral Interface 9

TDM time-division multiplexing 11

TFT Thin-Film Transistor 14

TN Twisted-Nematic 14

UART universal asynchronous receiver-transmitter 8

USART universal synchronous asynchronous receiver-transmitter 7

USB Universal Serial Bus 6

Wi-Fi Wireless Fidelity 6

3
Introduction
“The term Internet of Things (IoT) generally refers to scenarios where
network connectivity and computing capability extends to objects, sen-
sors and everyday items not normally considered computers, allowing
these devices to generate, exchange and consume data with minimal
human intervention (1, p. 1)”. The IoT is created by interconnected
devices which have sensors, and processors, and can communicate
with each other. IoT does not have to be connected to the World Wide
Web and has many uses and applications.
It is used by consumers as part of home automation, which can,
for example, help save energy, and sometimes even for assistance to
disabled or elderly people. IoT is also used by organisations, for exam-
ple by Medical and Healthcare ones for data collection, and analysis,
which helps with research. Other Healthcare use is the monitoring of
patients. IoT is also used by organisations for transportation, where it
can help with traffic control, parking, or logistics. Industrial uses of
IoT include automation or semi-automation of manufacturing, which
speeds up manufacturing and helps produce different products faster.
IoT can also be used in agriculture, for monitoring weather, soil con-
tent and pest infestation, these data can increase farming productivity
and help with decisions regarding farming. There are even more uses
of IoT, so it can be seen that IoT is quite useful.
In this thesis, it will be shown how to program some basic in-
put/output (I/O) devices using microcontrollers. The goal of this
thesis is to design two exercises concerning the IoT. These exercises
will however be programmed for three different microcontrollers.
These microcontrollers are on different base boards, made by Lasaris
laboratory, and can be connected to the same I/O devices which are
on another board. The microcontrollers used are STM32, Raspberry
Pi Pico, and, finally, RockPi.
Another goal of this thesis is to compare these microprocessors
regarding their approachability while programming. Questions that
will concern us are: Which microprocessor is the easiest to program
on? Which microprocessor is the easiest to set up? This thesis points
these questions.

4
Introduction

In chapter 1, the basic concept of microcontrollers is described. The


microcontrollers used in this thesis are also described in detail and
compared in this chapter. In chapter 2, IoT exercises for programming
these microcontrollers are designed. Finally, in the last chapter 3, the
process of connecting, programming and launching the exercises is de-
scribed, together with a comparison of complexity and a description of
their advantages and disadvantages. At last, there are also issues that
were encountered while programming microcontrollers mentioned.

5
1 Description of microcontrollers
Firstly, what are microcontrollers will be explained. A microcontroller
is a chip that has its own processor, and many more components.
These components include flash memory, RAM, digital and analogue
I/O circuits, interrupts, and timer. They can even have other fea-
tures (Gridling; Weiss [2], 2007). Microcontrollers, unlike micropro-
cessors, are used in embedded devices. They have usually low perfor-
mance but are very power efficient because they need to be powered
on all the time when used. Microcontrollers are used in lots of today’s
electronic devices. Some microcontrollers can even communicate with
each other. With this rich connectivity, microcontrollers have a broad
range of use.
The I/O can be analogue or digital and is usually managed through
pins. The pins can therefore be analogue or digital, while more of them
can be used at once to provide one functionality. Thanks to pins, there
are a lot of sensors and I/O devices that can be connected to the micro-
controllers. Most of the pins are exposed for use to connect peripherals
such as buttons, keyboards, environmental sensors, devices for vari-
ous measurements, biometric devices, displays, or devices providing
more storage for microcontrollers. More specific examples are push
buttons, temperature sensors, rotary encoders, proximity sensors, Liq-
uid Crystal Display (LCD) displays, shift registers, or LED diodes. IoT
is a set of microcontrollers which are programmed and interconnected,
and are able to exchange data and cooperate.
Microcontrollers can be programmed from a computer. For con-
nection to the computer, the microcontrollers can support serial port
interface, or even have Universal Serial Bus (USB) port. Some have
an ethernet port, or wireless connections such as Wireless Fidelity
(Wi-Fi) and can be connected to the network. The microcontrollers are
usually pre-programmed with a bootloader that helps with uploading
programs from the computer to the microcontroller’s own memory.

1.1 STM32 microcontroller


As it is stated on the official internet page of STMicroelectronics, STM32
is a family of 32-bit microcontrollers based on the Arm Cortex®-M

6
1. Description of microcontrollers

processor. It is designed for microcontroller unit (MCU) users to offer


them greater flexibility. These microcontrollers combine good perfor-
mance, processing of digital signal, and low-power and low-voltage
operation. They also offer comfortable development and full integra-
tion (3). The STM32 board comes in more configurations regarding
I/O ports, which count ranges from 36 up to 144.
In exercises, the STM32F103C board will be used. This board
has an ARM Cortex-M central processing unit (CPU) running at
72 Megahertz (MHz), up to 128 kilobyte (KB) of flash memory, and up
to 20 KB of RAM. More importantly, regarding I/O, the STM32F103 has
48 pins. Through them, it offers Inter-Integrated Circuit (I2C) for com-
municating with LCD display, Controller Area Network (CAN) 2.0,
up to three universal synchronous asynchronous receiver-transmitter
(USART), one pulse-width modulation (PWM) channel, and USB 2.0
for connection with a computer, according to the official internet page
of its manufacturer (4).
I2C will be used in the exercises done in this thesis, so it will be
looked into a bit more. It is a multimaster serial-ended computer
bus (Mankar; Darode; Trivedi; Kanoje; Shahare [5], 2014). It is used
for slow, short-distance data transfer usually between processors or
microcontrollers and their peripherals. I2C does have low bandwidth,
but is effective and is used because of its simplicity and low cost of
manufacturing. More specific usages of I2C include controlling small
LCD or Organic Light-Emitting Diode (OLED) displays, describing
device configuration to allow usage of plug and play on devices, ac-
cessing non-volatile random-access memory (NVRAM) chips that
hold user settings, changing monitor settings, reading various sensors
data, and so on. In the exercises, the I2C will be used for sending data
to the LCD display.
CAN, according to its ISO standard, is a protocol for serial com-
munication, which offers multiplexing and real-time control in dis-
tributed settings. These include mainly automotive applications and
other control systems (6). In a publication about CAN 2.0, it is stated
that the CAN was created by ROBERT BOSCH GmbH to provide
multi-processor communication. In 1991, CAN Specification 2.0 was
introduced with the added format of messages, which allows messages
to be extended with more message identifiers. It is fully compatible
with CAN 1.2, which is the previous version of CAN (7). This version

7
1. Description of microcontrollers

of CAN offers data transfers as fast as 1 Mbit per Second, maximally.


In one frame, it allows to send up to 8 bytes.
As stated in the article by Garbutt(2003), the main usage of USART
is to transmit serial data, which means to send and receive data in a
serial fashion, one frame consisting of eight or nine bits at a time. It can
also detect errors which may occur during transmission. The USART
can operate in two modes, asynchronously or synchronously. The
synchronous mode of operation uses two lines for sending data, one
for the clock and the other one for data (8). The asynchronous mode
allows it to operate as a universal asynchronous receiver-transmitter
(UART), so without using the clock.
The UART is usually used for communicating with the PC while a
program is running on a microcontroller. It defines a set of rules for
data exchange between two devices. It is a protocol, which describes
serial communication. In the description of the protocol, it is stated
that the UART uses only two channels to send serial data between
two devices. Devices are also connected through the ground. Three
types of communication are possible via UART. These are simplex,
so only one device sends data, other is receiving, half-duplex, both
of the devices can send data but only one at a time, and full-duplex,
when devices can send and receive data at once (9). An article about
UART states that a device implementing UART is an integrated circuit,
which can transform parallel data into serial and vice versa. Therefore,
it includes two converters, which convert data to and from parallel.
Data coming to the computer are converted to parallel and data sent
from it are converted to serial. The UART device also needs to have a
buffer (10). UART is used in RS-232, modems, and so on.
PWM (pulse-width modulation) is a method used by digital pro-
cessors to manage analogue outputs. PWM is basically used to repre-
sent analogue values digitally. This method finds its usage in a wide
spectrum of areas, for example, communication, and power control.
As it is stated in the PWM explanation, PWM uses counters to turn
the output on for a specific fraction of a given period of time. If the
time period is small enough, this method can encode any analogue
value. The value is however still represented as digital in a microcon-
troller because the output is set to either on or off (11). Therefore, the
microcontroller can be used to control analogue devices.

8
1. Description of microcontrollers

This board has no operating system and the only program run-
ning on it will be the one uploaded to it, therefore, it is bare metal
programmable. The program can be written in Arduino integrated
development environment (IDE), compiled there, and uploaded to
the microcontroller by it.
Arduino IDE is a cross-platform software developed by Arduino
Software. As is stated in official internet page of this IDE, in this
application, it is possible to write a program, compile it, and, finally,
upload it to an Arduino board. It has a text editor for writing programs,
a boards manager which allows the user to choose for which board
should the program be compiled, a library manager with various
libraries for controlling I/O devices, a serial monitor for receiving and
sending messages to the connected board, and few other functions
which are similar to other well known IDEs (12).
The board manager provides functionality to select even other
boards, different from Arduino ones. Therefore, we can even program
STM32 with this IDE. In this thesis, version 2.0 of Arduino IDE is used,
which is different from the original one mainly in design. One of the
new functionalities is a debugger, but it will not be needed in this
thesis.

1.2 Raspberry Pi Pico

The second microcontroller that will be used for programming exer-


cises is the Raspberry Pi Pico. It has a similar system on chip to the
STM32, the RP2040, which includes, according to the official datasheet
of this microcontroller, dual-core Arm Cortex-M0+ running at most
at 133 MHz, RAM with a size of 264 KB, and 2 MB of on-chip mem-
ory. Regarding I/O, Raspberry Pi Pico has, based on the datasheet, 40
pins, from which 26 are general-purpose input/output (GPIO) pins
and three pins can be used for analogue input. What is more, it has
rich support for peripherals, which include two UART, two Serial Pe-
ripheral Interface (SPI) controllers, two I2C controllers, sixteen PWM
channels, one USB 1.1 controller with host and device support and
eight programmable I/O state machines (13). There is a version of
Raspberry Pi Pico that even supports Wi-Fi.

9
1. Description of microcontrollers

The Programmable I/O (PIO) is a feature that is not present in


STM32 and offers a way to communicate via more protocols than the
board has hardware support for, not only SPI, I2C and UART. The PIO
can emulate other hardware interfaces. According to the authors of
MicroPython, this can be done thanks to the RP2040 chip which has
special PIO blocks so the main cores are not slowed down by the use
of PIO (14).
SPI is a communication protocol1 . It uses four lines for communi-
cation and works on the single-controller principle. As it is stated in
an article about SPI, the lines consist of two data lines (for in and out
communication), a line for clock signals, for synchronization, and a
line for selecting whom the SPI controller wants to communicate with,
named peripheral select signal. Because it is a single controller, the
controller device is required to start communication with other pe-
ripheral devices. When it wants to communicate, meaning to send or
receive data, the controller device sets the clock signal to a frequency
compatible with both the controller and desired peripheral, and sets
the peripheral select line, the one which corresponds to the periph-
eral, to low. Communication can happen in one of four modes. The
peripherals can differ and use different modes so the controller has
to change its configuration accordingly when it is needed because of
peripheral (15). Otherwise, the SPI does not define anything more.
The Raspberry Pi Pico board can be programmed in C language,
for example in Arduino IDE, and also in MicroPython. According to its
official internet page, the MicroPython is a version of Python 3 which
includes just a part of the whole Python 3 library and is primarily
made for microcontrollers, therefore, it is optimised to use less memory
and CPU power. In MicroPython, important functions from Python 3
are; however, available. These include for example interactive Python
console, handling of exceptions and list comprehension (16). It also has
its own specific libraries for communication with I/O devices, which
means it supports GPIO, SPI, PWM and even Bluetooth. MicroPython
can be used on many types of microcontrollers and even on devices
with Linux or Windows. On the GitLab page of the MicroPython, it is
stated that the Raspberry Pi Pico is supported (17).

1. https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all

10
1. Description of microcontrollers

1.3 MH-Tinny Attiny88


The MH-Tinny Attiny88 microcontroller is based on the ATtiny88 chip,
which is based on the AVR® enhanced RISC architecture. It is also pro-
grammable in Arduino IDE and is most similar to the more common
Arduino Nano. The main difference between these two is that the MH-
Tinny is less powerful, but on the other hand, its biggest advantage is
its power efficiency. The chip in the Attiny88 is also slower than the one
in STM32, running at only 16 MHz. Based on its official datasheet, the
Attinny88 microcontroller has 8 KB of programmable flash memory
and regarding IO, it has 28 programmable pins, supporting SPI and
L2C (18). This microcontroller is also bare metal programmable.

1.4 Rock Pi S
The last microcontroller for which the exercises will be programmed
is Rock Pi S. This microcontroller is a type of microcontroller which
is also called a single-board computer (SBC). This means it has an
operating system running on it, which is usually Linux.
The Rock Pi S is similar to Raspberry Pi Zero, which is a more well-
known SBC. Rock Pi S is also the most powerful of the microcontrollers
used for the exercises, which will be programmed in another part of
this thesis. In fact, it has a 64-bit quad-core processor, ARM Cortex-A35
CPU, which is capable of audio processing and use for other digital
multimedia, and 256 MB of RAM.
Based on the microcontroller’s official website, it also has 52 pins,
including analogue and digital pins, an ethernet port, and a Micro
Secure Digital (SD) card reader (19, 20). It is also stated that its pro-
cessor supports audio interfaces, for example, pulse-code modulation
(PCM), time-division multiplexing (TDM) and so on. The pins on
the board support three PWM, two SPI, two I2C and three UART. On
the board, there is also a USB 2.0 (20). PCM is used for representing
audio streams digitally. According to the explanation of it, it was in-
vented in 1939 and was designed to allow communication in digital
form to be sent by analogue media. This method; however, also works
for representing audio in digital form, by means of sampling and
quantizing. There are multiple variants of PCM, depending on the

11
1. Description of microcontrollers

method used to quantize the audio data. The PCM is mainly used in
CDs and other forms of audio. Variants with compression are used
in telephony (21). Rock Pi S supports PCM, therefore, it can process
audio data. A Micro SD card is also needed for working with a micro-
controller because the Micro SD card contains a bootable operating
system which runs on the board. An operating system is needed to
programme the board. The Rock Pi S can be programmed with the
help of libraries, such as libmraa, which will be used in this thesis and
needs to be installed on the operating system of the board, for working
with I/O. The libmraa is a C/C++ library which can be used also with
Python and Java. According to the official libmraa site, it provides
functions to communicate with I/O devices. GPIO, I2C, PWM, SPI,
and more can be controlled by this library. What is more, the libmraa
supports many different boards, such as Raspberry Pi, Intel Edison,
and Rock Pi 4 (22). This makes code more portable and is good for
sensor developers and programmers. Languages used for program-
ming are for example C language or Python, but any language which
is supported by the libmraa library can be used.

1.5 Comparison of microcontrollers


From information about these microcontrollers, it can be seen that MH-
Tinny and STM32 are very similar, while STM32 has more processing
power and few more I/O options, while both can be programmed in
C language and are bare metal programmable. Raspberry Pi Pico is
more powerful than both of them. Furthermore, it also has better I/O
and supports programming not only in C language in Arduino IDE
but also in MicroPython. At last, the Rock Pi is, with its quad-core
processor, the most powerful of these microcontrollers. The Rock Pi S
also has the most I/O options. Thanks to the operating system running
on it, Rock Pi S is programmable in more languages than the other
microcontrollers, although in this thesis, only Python will be used for
programming it. One of its disadvantages is the need to install special
libraries to control I/O ports. Because of the similarity of MH-Tinny
and STM32, it was decided to program the exercises just for STM32,
Raspberry Pi Pico and Rock Pi S. Programming MH-Tinny would be
similar to programming the STM32.

12
2 Design of exercises
I/O devices will be used in the upcoming exercises. In the first exercise,
an LCD and a rotary encoder will be used together. In the second
exercise, buttons and a shift register will be used. The goal of the
exercises is to understand how to use the peripheral devices connected
to microcontrollers, which means writing programs which will use
them.
Programs do not have to be very complicated, because these exer-
cises are focused mainly on programming the microcontrollers. After
doing the upcoming exercises, the student should be able to use the mi-
crocontroller and peripherals connected to it such as LCD and rotary
encoder or the buttons and shift register, even for larger projects.

2.1 Devices used


The devices used in this these exercises are boards made by Lasaris
laboratory. These boards are made for the subject Introduction to IoT,
the same for which the exercises are designed. There are three types
of these boards.
First of them are base boards, which include the microcontroller,
power supply, and basic I/O for connection to personal computer (PC),
as can be seen in Figure A.1.
These boards can have different microcontrollers installed on them,
which were mentioned before. To use more I/O devices with the base
boards, on the top of them, a communication board has to be connected.
The communication board can be seen in Figure A.2, and can have
RS485 or CAN bus installed on it for example. The communication
board will not be programmed in the exercises; however, it is needed
for connecting the base board with the application board.
On the top of the communication board, an application board can
be connected, as can be seen in Figure A.3. The application boards
can have various I/O devices installed on them, such as LCD together
with rotary encoder, buttons and shift register, meteorological sensors,
or even OLED display with an SD card reader.
Three types of boards, base board, communication board, and
application board, are connected one above another one and provide a

13
2. Design of exercises

connection of the devices on each board with the base board. Therefore,
there are connecting pins, which interconnect the boards. Because of
that, the pins do not have to be connected manually to the devices by
the user.
Base boards and application boards will be used in this thesis for
programming exercises. What is more, these boards can also be re-
placed by different ones. This will be taken advantage of, and different
application boards and base boards will be used in exercises.
One of the used application boards has an LCD display and rotary
encoder, while another one has a shift register and buttons. Base boards
have microcontrollers from the first chapter(1) on them.

2.2 Exercises

In this section, exercises using the mentioned devices will be described.


Firstly, exercise using application board with LCD and rotary encoder
will be explained, with brief explanation of rotary encoder and LCD.
Secondly, exercise which uses shift register and buttons will be de-
scribed. How the shift register works will be explained too.

2.2.1 LCD and rotary encoder

LCD and a rotary encoder will be used in this exercise. Microcontroller


will communicate with the LCD using I2C. A library will be used for
this communication. Used LCD has 2 rows with 16 columns in each,
to display characters.
A simplified scheme of connection of the boards, STM32 as base board
and application board with LCD and rotary encoder is shown in Fig-
ure 2.1.
The LCD is a flat-panel display, which was invented in 1964 and
went through great development since. There are various modes of
operation of LCD, for example, in-plane switching (IPS), Thin-Film
Transistor (TFT), Twisted-Nematic (TN), and others. Every type has
its pros and cons, so the LCD has a great variety of uses. LCDs are
nowadays widely used in various electronic devices such as watches,
laptops, mobile phones, calculators, and televisions.

14
2. Design of exercises

RS-485
Rotary
encoder

STM32

I2C LCD

Power
supply IO

Figure 2.1: STM32 board with LCD.

The rotary encoder is a device which converts a rotation of an axle or


shaft to digital or analogue signals which are then used to determine
the direction and speed of rotation and the change of position of the
knob. Rotary encoders are widely used, for example as volume control
knobs but also in printers, robotics and photographic lenses.
According to the internet page of their manufacturer, two different
types of rotary encoders exist, incremental and absolute. In absolute,
the exact position of the rotary encoder is known and based on it, the
rotation, its direction and angle, can be known. On the other hand,
on the incremental type, only immediate changes in the position of
the knob are reported, so the absolute position of the knob is un-
known (Rotary Encoders [23], 2023).
In this exercise, the incremental type of rotary encoder will be used,
so it is needed to remember the position of the knob. The used rotary
encoder also has an integrated button, which will send a signal when
pressed.

15
2. Design of exercises

To use a rotary encoder in an exercise, we need to know how it


works, because we will need to determine the direction of its rotation.
As is stated in the internet page of manufacturer of rotary encoders,
inside the rotating knob, there is a disc that is connected to the common
ground pin and therefore can pass signals. This disc rotates together
with the knob. The rotary encoder also contains two pins, which are
static, which means they are not moving with the knob. When the
knob rotates, these two pins come into contact with the ground pin
one after another (Rotary Encoders [23], 2023). The static pins are not
in the same place, they are shifted a bit from each other. Therefore,
one of the static pins connects and disconnects from a ground pin
sooner when rotating to the right and the other static pin is connected
and disconnected sooner when rotating to the left. The direction of
rotation can therefore be determined based on the order of contact
of the ground pin with two static pins. The rotary encoder has also
button, which can be pressed.
In this exercise, also the LCD display will be used, to display the
counting of rotations of the rotary encoder and the direction of the ro-
tation. To program this, one of two static pins on the knob of the rotary
encoder will be watched for changes. Meanwhile, the information will
be sent to LCD by I2C. When the rotary encoder sends a signal, the
other static pin will be checked and the direction of rotation will be
decided based on whether the checked pin is connected to the ground
pin. A press of the button on the rotary encoder will also be displayed.
When the press occurs, it is recommended to remember the time of
the press, to check the time against the time of the next press of the
button, to not display the same press multiple times.

2.2.2 Shift register and buttons


A simplified scheme of connection of the boards, STM32 as base board
and application board with a shift register and buttons is shown in
Figure 2.2.
In this exercise, shift register and buttons will be used and pro-
grammed.
A shift register is a type of digital circuit. The shift registers are
mostly used for conversion between serial and parallel signals. Serial
communication means that communication is done bit by bit, one bit

16
2. Design of exercises

RS-485
Buttons

STM32

Shift register

Power
supply IO

Figure 2.2: STM32 board with shift register.

at a time. The information is sent sequentially. On the other hand,


parallel communication allows sending more data at once. Multiple
channels are used when using parallel communication.
In this exercise, the shift register will be used to convert serial
communication to parallel communication. The shift register used in
this exercise is 74HC595. It has 8 output pins and signals will be sent
to it by 3 pins.
The 74HC595 shift register is serial-in parallel-out (SIPO). As it can
be seen in the datasheet of this shift register, it has a storage register
with the size of 8 bits, which is the same as the number of output pins.
In this storage register, data sent to the shift register can be stored
while they are being sent. The 3 input pins on the shift register are
the clock pin, for the timing of sending the data, the latch pin, for
unloading data from the storage register to the shift register, and the
data pin, for sending data to the shift register (24).
In the datasheet of this shift register, it is also explained how to
communicate with it. To send data to the shift register, it is needed to

17
2. Design of exercises

set the clock pin to 1 and then set it back to 0. Every time the clock
pin is set to 1, the data saved to the storage register are shifted by one,
and on the new place, which is freed by this shift, the state of the data
pin is stored. After each store of data, the clock pin has to be set back
to 0. After storing data in the storage register like this, the latch pin
is used to send the data from it to the output pins. The latch pin has
to be set to 1 and then the clock pin from 0 to 1 to do this. After this,
the clock pin is recommended to be set to 0, to prepare it for the next
action which will use it. After the data are sent to the output pins,
we can continue by sending data to the storage register of the shift
register (24).
This shift register also has LED diodes on it, when some pins are
activated, the led diodes connected to them turn on. When they are
deactivated, LEDs turn off. Thanks to this, what is sent to the shift
register, and which pins are turned on can be seen.
Other peripheral devices used in this exercise are buttons. Buttons
are input devices and have two logical states, high or low. To avoid
randomness in case of a disconnected button or strong interference
from the environment, the input_pullup mode will be used in our
program. The state of the button is defaultly high, and when the button
is pressed down, its status becomes low. This mode of working with
buttons uses internal resistors on the microcontroller for cancelling
out false signals.
The goal of this exercise is to write a program which will use both
devices, buttons and also the shift register. Such a program is one that
will set the pins at the shift register to different states when different
buttons are pressed. For example, all pins on the shift register will be
set to 1 when the first button will be pressed. Secondly, pins will be
turned on one by one when the second button will be pressed, and
finally, every other pin will be turned on when the third button will
be pressed. After every press of a button that causes a change to the
shift register, the program will wait for the end of the action caused
by this press. Basically, the program will not allow other input while
the shift register is set to wanted state for specific time. For example,
turning all of the pins on for half a second, after which all the pins
will be set back to 0. This waiting will be applied to all of the actions
with buttons. This is the exercise that will be programmed for the shift
register and the buttons.

18
3 Programming of microcontrollers
For programming STM32 and Raspberry Pi Pico, the Arduino IDE
was used. In the IDE, the right model of the microcontroller for which
the program is being written needs to be selected. Another thing to
be taken into account is that the I/O pins for communication with
peripheral devices are usually differently named for each microcon-
troller. Because of that, the documentation of each board is needed to
be checked and the right pins used.
The programming in Arduino IDE is done in C language. When
programming microcontrollers in Arduino IDE, there are always 2
functions. These are setup(){} and loop(){}. setup(){} is done only
once, at the beginning of the program. loop(){} is done after setup
infinitely, until the microcontroller is turned off or does not have power.
In the setup(){} function, usually the pins used in loop(){} are set up
and variables declared. loop(){} is basically an infinite loop and is
used like the main body of a program. In exercises, these functions
will be treated as described.
In the exercise with LCD and rotary encoder, the text on LCD will
be changed at the same time as the status of rotary encoder or its
button. In this exercise, interrupts can be used. The updates of the
LCD can be launched only when some signal change occurs on one of
the static pins on the rotary encoder. Using interrupts has advantages
over using just the loop(){} function. To know whether something
has happened with the rotary encoder, the device does not have to be
looping through the loop(){} function. Therefore, the processor will
be less utilized. With the use of interrupts, the loop function can be
left empty and what the LCD displays will be changed only when a
signal from the encoder will be received. The loop function can then
be used to do other necessary steps of the program faster.

3.1 Programming of STM32

To program STM32, multiple prerequisites need to be met. On the PC


from which STM32 will be programmed, Arduino IDE, and drivers
for communicating with STLink need to be installed. Also, the STLink

19
3. Programming of microcontrollers

will be needed. It is a programmer for STM32 and can be used for


communication between PC and STM32.
For programming STM32, as mentioned earlier, the Arduino IDE
will be used, on the Windows 10 system. To use it with the STM32
microcontroller, a specific library which will make the Arduino IDE
work with STM32 is needed. In the Arduino IDE, different boards than
the ones provided in the IDE can be installed and selected. In IDE, an
additional boards manager has to be added in the IDE preferences.
To add the STM32, it is needed to paste the link http://dan.drown.
org/stm32duino/package_STM32duino_index.json into the IDE, as
is shown in Figure 3.1.

Figure 3.1: IDE configuration.

Then, the STM32 boards will be available in boards manager and it


is just needed to select the right one. To upload a program to our
STM32F1 board, the “Generic STM32F103C series” board needs to be
selected.
For uploading, the STLink interface is used, which has to be con-
nected to the USB port of the computer. To upload a program, STLink
needs to be connected to the PC as well as to the board. Lastly, the

20
3. Programming of microcontrollers

STLink has to be selected as an upload method in the IDE. With all


this done, the uploading of programs to the STM32 board works just
fine.
For programming the exercises, libraries were used for commu-
nication with LCD and other peripherals. The libraries used were
LiquidCrystal_I2C.h for I2C communication with LCD and Wire.h to
use I2C communication. To use the LCD library, the library had to be
initialised, so the right size of the LCD had to be passed to it and the
right I2C address, which is 0x3F in this case, for the STM32. This can
be seen in Listing 3.1.

Listing 3.1: Includes and initialization of LCD.


# include < Wire .h >
# include < LiquidCrystal_I2C .h >

// define LCD - type from library


LiquidCrystal_I2C oLcd (0 x3F , 16 , 2) ;

It has already been explained how to read information from the ro-
tary encoder at section 2.2.1, so the program works according to the
explanation. The reading of info from rotary encoder in the sample
program is inspired by (25). Pins had to be defined by the function
pinMode, which is built in Arduino IDE. The definition of pins for the
rotary encoder and their initialization can be seen in Listing 3.2.

Listing 3.2: Rotary encoder pins definition and initialzation.

// rotary encoder inputs - according to hardware


description
# define CLK PB4
# define DT PB5
# define BTN PA15

void setup () {
// initialize input pins
pinMode ( CLK , INPUT ) ;
pinMode ( DT , INPUT ) ;
pinMode ( BTN , INPUT_PULLUP ) ;

21
3. Programming of microcontrollers

The pins can be defined based on the hardware description docu-


ment, in which pins on the boards used in this thesis are described.
Using a library for LCD, characters can be just sent to the library and
it enables displaying them on the LCD. The task for exercise is to
program the exercise explained in 2.2.1.Library for LCD is used and
explanation of how the rotary encoder works is provided.
The program for working with buttons and shift register used
the pinMode function for reading from buttons too. INPUT_PULLUP
mode was used this time, as mentioned earlier. Another library was
used for controlling the shift register, the ShiftRegister74HC595.h.
This library provides high-level functions which can be used to set all
of the pins on the shift register to the same state, to set one of them,
or even set multiple at once to the required state. The exercise is to
program the program described in 2.2.2 with use of the libraries.
Sample solution programs and presentations explaining these ex-
ercises can be found in B.

3.2 Programming of Raspberry Pi Pico

Raspberry Pi Pico can be programmed via Arduino IDE or MicroPy-


thon. For programming, Arduino IDE is needed, similar to STM32,
and for programming in MicroPython, some Python IDE, such as
Thonny is needed. For programming and connecting the Raspberry
Pi Pico to the PC, the micro USB port on the microcontroller can be
used to connect it to PC, with USB A to micro USB cable, therefore
one USB A to micro USB cable is needed. For the first connection, it
is recommended to hold the Bootsel button on the board while con-
necting it to the PC, in order to reset the board. In the next sections,
the programming of Raspberry Pi Pico in Arduino IDE and then in
MicroPython will be described.

3.2.1 Arduino IDE

Raspberry Pi Pico can be programmed via Arduino IDE. Similarly to


STM32, the right board needs to be installed and chosen in the IDE.
Right after the connection of the board to the PC, when the board was

22
3. Programming of microcontrollers

reset, the File Manager window should pop up. It can be ignored if the
Arduino IDE is used to write the program which will be uploaded.
When a program is uploaded for the first time, it is needed to select
Raspberry Pi Pico as the board for which to compile a program in IDE.
To upload a program again, the microcontroller has to be either reset
or selected in the IDE as a device on a port.
To reset the board, it is needed to disconnect it from the PC and hold
the Bootsel button on it while connecting it to PC. However, after the
first upload, the Arduino IDE should recognize the connected device
automatically. If it does not, the device has to be selected manually. To
select it, it is needed to know how it is connected to the PC. It should
be visible in the device manager (in Windows) under Ports, while
the device should be connected to one of the communication port
(COM)s. The microcontroller is connected to one of the COM ports,
the same one as in the device manager so that port should be selected
in the IDE. Finally, a program can be compiled and uploaded to the
board, in a similar fashion as on STM32.
If the microcontroller is connected to the PC via a micro USB cable,
the serial monitor functionality in the Arduino IDE can be used. This
functionality allows the PC to communicate with the microcontroller
and vice versa. This can be used even while a program is running on
the microcontroller.
To use the serial monitor, the microcontroller has to be connected
to the IDE, which means it must be selected there as a device on
a port. Then, we can open a serial monitor in the IDE and send or
receive messages from the microcontroller in the program uploaded
to Raspberry Pi Pico. This can be used for sending info from the rotary
encoder to the PC.
The programs programmed in Arduino IDE are similar to the ones
programmed for STM32. One of the exceptions is the pins used. The
pins have to be redefined from the ones used on the STM32, to the
pins which are on the Raspberry Pi Pico, corresponding to the ones on
the STM32 on the Lasaris boards. They can be found in the Hardware
description document. When using the pins in the Arduino IDE, just
the number of pins has to be written, for example, not GP11 as is
written in the Hardware description, but only 11.

23
3. Programming of microcontrollers

Other difference is usage of interrupts, which are not used on


Raspberry Pi Pico in Arduino IDE. This applies to the exercise with
rotary encoder.
Working with the serial monitor is added to the exercise with rotary
encoder as a task for Raspberry Pi Pico. In the exercise with serial
monitor, checking the time of button presses is needed, to not display
the same press multiple times. To get current time, used for checking,
a function millis(); available in Arduino IDE can be used. Otherwise,
the exercises to program on the Raspberry Pi Pico in the Arduino IDE
are the same as on the STM32.

3.2.2 MicroPython
Raspberry Pi Pico can be programmed in MicroPython too. MicroPy-
thon does not support the full Python 3 library, but, on the other hand,
it is less hardware-demanding. To make the MicroPython work, just
one file with MicroPython is needed to be uploaded to the board after
connecting it to PC, which can be found at MicroPython webpage (16).
As it has been mentioned, the program can be uploaded to the
board thanks to the mode in which the board is right after it has been
connected to the PC, holding the Bootsel button while connecting. An
File explorer window should pop up. After uploading the right file,
the board will restart and connect to the PC under one of the COM
ports.
A connection with the board can then be established from some
serial communication client such as Putty, or some IDE can be con-
nected to the board. The preferred method of connection is to use
the board from IDE such as Thonny. In Thonny IDE, the board needs
to be selected as an interpreter. This can be set in Thonny under the
option to Configure interpreter. There, Micropython for Raspberry Pi
Pico needs to be selected. The right port has also to be selected, but
IDE can detect on which port the board is connected automatically, so
this option will be used. After these steps, programs can be written
in Thonny and launched in IDE, although they will be executed on
Raspberry.
In MicroPython, libraries from Arduino IDE are not available, so
the LCD will not be used from MicroPython. However, in MicroPy-
thon, the print() function works as expected, so it is possible to write

24
3. Programming of microcontrollers

info from the rotary encoder to the console in the IDE, to which the
Raspberry Pi Pico is connected. This will be used in the exercise re-
garding the rotary encoder. The MicroPython can also be used for
working with shift register. How to do it is explained in next section
and this task is optional in exercises.
Again, sample solution programs and presentations explaining
these exercises can be found in B.

3.3 Programming of Rock Pi S


For programming the Rock Pi S, an SD card and ethernet cable con-
nected to the router are used. On the SD card, there is an operating
system. In this case, it is Debian Linux. When the SD card is in and
Rock Pi boots, it can be connected to the PC via serial console or SSH.
For connection via serial console, USB to TTL cable needs to be
connected, to both, Rock Pi S and PC. Secondly, Putty on PC needs
to be launched, and the right port needs to be selected. This port will
be COM+”some number”, and also, the speed of connection needs
to be set to 1500000 or 9600, based on the settings of the specific Rock
Pi S. This connection, via serial console, is not preferred because of
limited speed. Therefore, unexpected problems can occur, such as the
copy and paste not working as expected. For example, pasted text is
not always pasted completely. Also, Putty sometimes crashes and has
to be restarted. Therefore, using SSH is recommended.
To use SSH, the board has to be connected to an ethernet cable. The
cable has to be connected to the same router as our PC. That means it
has to be on the same Local Area Network (LAN). When the board is
connected, its local IP address is needed. To get it, some IP address and
port scanner software, such as an Angry IP scanner can be used. The
address can be got by trying to connect via SSH to addresses found
by the scanner. It is recommended to launch the IP scanner before
connecting the Rock Pi S to the LAN and launch it again after the
connection to get its IP address. After a successful SSH connection,
Rock Pi S is controlled as any other Linux terminal.
For programming the exercises, a library is needed, for commu-
nication with pins. The Raxda libmraa library can be used for this.
Programming is then done in Python. Programs are similar to the ones

25
3. Programming of microcontrollers

from Arduino IDE. They had to be rewritten to the Python language,


and pins had to be changed accordingly to the hardware description.
Because there are fewer libraries available for communication with
peripheral I/O devices for programming in Python, one new function
for updating the pins on the shift register has to be used. This function
works according to the explanation of working with shift register in
Chapter 2.2.2. Its functionality is similar to the library which was
available for Arduino IDE. However, this is just one function, not a
whole library. A string with values needs to be passed to the function.
This string represents values to which the shift register should then
be set. Therefore only setting all pins at once is possible, but it can
be used to program the same program as with Arduino IDE. This
function can be also used with MicroPython on the Raspberry Pi Pico,
with pins changed accordingly to the Hardware description.
To make the LCD exercise work, a program to control the LCD
display is needed. This program will have to use libmraa library and
I2C interface to communicate with the LCD. The program provides
functions for writing strings to the LCD, turning the LCD backlight
on and off, and clearing the text from the LCD.
This program is similar to a part of a library which can be found
at (26). This library can be used only with Raspberry Pi (version with
Linux, not one used in this thesis), so not with Rock Pi S used in this
thesis. Moreover, it is based on a different library for I2C communica-
tion, not the libmraa, used on Rock Pi S. Therefore, it could not be used
as it is and had to be changed to use libmraa. With this change, and
using right pins and I2C channel, the program which will be used for
controlling the LCD from Rock Pi S was made. Unfortunately, this pro-
gram can be used only with LCDs with LCM1602 I2C converter, which
is one used in this thesis too. Another prerequisite is to have libmraa
installed on board. To use it with other devices than the boards used
in this thesis, the I2C bus and address need to be changed accordingly.
Listing 3.3: Import and initialization of LCD controller.
import i2c_LCD

lcd = i2c_LCD . lcd ()

26
3. Programming of microcontrollers

Listing 3.4: Example of usage of LCD controller.


lcd . clear ()
lcd . display_string ( " Hello ␣ world " , 1)

The program to control the LCD has to be imported to the program


solving the rotary encoder and LCD exercise. Then, the object that
can be used to control the LCD can be instantiated, as in Listing 3.3,
and used in the main program, as in Listing 3.4. This program can
be found at B, as can be sample solution programs for these exercises
and presentations explaining them.
A thing to be aware of is to launch the programs with root privi-
leges because unless they are launched under root, they won’t work.

3.4 Comparision of complexity of programming


exercises for different microcontrollers

The programming of STM32 was pretty straightforward. Once the pro-


gram was written and the pins were set up according to the hardware
description, everything worked flawlessly.
Programming Raspberry Pi Pico was similar. Raspberry Pi Pico
could be programmed in Arduino IDE, as explained earlier. Programs
were the same as for STM32, just the pins were changed. The board
could be connected to the PC only once and different programs could
be uploaded as needed. After disconnecting and connecting again,
the board was recognized by IDE.
MicroPython can be also used for programming this board. The
disadvantage of MicroPython is that libraries are not so easily avail-
able. In Arduino IDE, there are lots of libraries to choose from for
working with peripheral devices. For MicroPython, it is needed to
look for them on the internet and upload them to the device, whereas
in Arduino IDE, the same library can be used for all microcontrollers.
This is because, in the Arduino IDE, a board has to be selected for
which a program should be compiled. Therefore, the libraries are not
dependent on the device for which the program is being compiled. So
all of the libraries available for Arduino IDE can be used even with
other devices which are supported by Arduino IDE.

27
3. Programming of microcontrollers

Rock Pi could not be programmed in Arduino IDE. As already


mentioned, there is a Linux OS running on Rock Pi. Therefore, Python
was used to program exercises for Rock Pi. For working with pins, the
libmraa library was used, as it has been mentioned earlier. To use the
LCD library, the I2C bus and address were needed. These could be
found with the help of libmraa library, as can be seen in Listing 3.5.

Listing 3.5: Finding right I2C address.


rock@rockpis : ~ / python_examples$ sudo mraa−i 2 c detect 1
0 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−
1 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−
2 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−
3 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− 3 f
4 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−
5 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−
6 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−
7 0 : −− −− −− −− −− −− −− −− −− −− −− −− −− −− −− −−

3.5 Advantages and disadvantages of individual


microcontrollers used in practical exercises
Firstly, STM32 could be programmed easily and worked flawlessly.
A minor disadvantage is the need to have a special STLink connector
for programming and also the need to install special drivers to PC. In
the Arduino IDE, the board is also not supported right from the start,
so a library is needed to be added to program it. Connecting to PC
worked as expected and the same goes for uploading programs.
What is also good, is that there is also another development tool
available for STM32, called STM32CubeIDE. This tool was not used
but is available for all widely used operating systems. Furthermore, it
is supported by the manufacturer of STM32 microcontrollers. In this
IDE, programs for STM32 microcontrollers can be written, and specific
languages available are C or C++. On the official STM page, it is stated
that this software offers also peripheral configuration, code compila-
tion and even debugging of microcontrollers. Also, because it is based
on the Eclipse®/CDT™ framework, it supports a lot of plugins from
Eclipse® IDE to enhance the attributes of the IDE. The STM32CubeIDE

28
3. Programming of microcontrollers

supposedly offers everything needed to develop projects for STM32,


so, for example, the selection of used pins or configuration of the used
device. The whole project can be organised within it. What is more,
the configuration can be changed anytime in the process of writing a
project (27).
What can be taken as a disadvantage of this board is that it has less
processing power than the other boards used in this thesis, and has
less functions available through I/O pins.
Secondly, Raspberry Pi Pico can be programmed in Arduino IDE
and with MicroPython, both are suitable. In the Arduino IDE, this time,
no other libraries than those available right in the IDE are needed.
However, when using Arduino IDE with this microcontroller, some
issues were encountered. Firstly, a programmer must be aware of using
right pins, because if wrong are used, the microcontroller has to be
reset. Secondly, when interrupts are used in a program, it does not
work as expected, so it is not recommended to use them.
With MicroPython, there were no issues. For working with Mi-
croPython on this microcontroller, there are even some tutorials avail-
able right on the official Raspberry page. Working with MicroPython
on this microcontroller was, therefore, more pleasant, and fixing prob-
lems was easier. Also, for connection to PC, this microcontroller has
built-in micro USB, so only USB-A to micro USB cable was needed.
Finally, working with Rock Pi was, in my experience, the most
difficult. Mainly setting the board up for programming, which is de-
scribed in section 3.3. After the setup, the libraries which can be used
in Arduino IDE were not available. Python or another language has to
be used and a search for other libraries for working with I/O devices
must be done. Therefore, I consider this board the least suitable for
programming these exercises. However, this board has Linux running
on it, more pins, and much more processing power, so the overall
usability for bigger projects is better.

3.6 Encountered problems with microcontrollers


After programming Raspberry Pi Pico in Arduino IDE, it was discov-
ered that on the board which was used, some pins were disconnected
from the base board and therefore did not make any contact with it.

29
3. Programming of microcontrollers

Basically, the pins did not work. This was an issue because the board
behaved unexpectedly. When uploading simple programs, such as a
program for blinking the built-in led on the board, everything worked
flawlessly. The program could be uploaded, worked and when another
program had to be uploaded, it was possible too; however, when the
pins which were not working were used, the program got uploaded,
but it was not possible to communicate with the board from the PC or
upload another program. This occurred only when using the Arduino
IDE for programming. To get the board to work again, it had to be
reset. When only working pins were used, everything worked well.
When MicroPython was used for programming, if a program did not
work, the IDE used for programming would just throw an error and
programming could be continued. This issue can be resolved by reset-
ting board every time after unsuccessful upload from Arduino IDE to
it, or by using MicroPython for programming.
The same thing was happening when interrupts were used in Ar-
duino IDE with Raspberry Pi Pico. Namely, in the exercise using rotary
encoder, where interrupts were used for launching updates on LCD
and serial monitor when change on rotary encoder or button occurred.
After entering interrupt handler, the board became unresponsive. It
was not found out why this happens; however, this issue can be solved
by using loop function instead of interrupts.
Another issue encountered with Raspberry Pi Pico is that at the
time of writing this thesis, I2C did not work on the board used, or LCD
was not connected properly. Multiple I2C addresses were tested and it
was tried to look for them with the help of I2C scanner program, which
did not find any. Because of this, LCD is not tested with Raspberry Pi
Pico; however, after finding the right I2C address for connected LCD,
the program should work.
When programming the Rock Pi S, the shift register could not
have been controlled from it. When the pins used for controlling shift
register were set according to the hardware description, libmraa li-
brary threw an error saying that some of the pins were incorrectly
defined. After doing some research about libmraa library, how pins
are identified by this library, and studying how the Lasaris boards are
connected, it was found out that the pins in the hardware description
document were shifted by 4, because it included RJ45 pins to counting,
which are not included by libmraa. In hardware description, all the

30
3. Programming of microcontrollers

pins after the pin 26 are shifted by 4 because of this. It was stated that
the pins 47, 48, and 49 were the ones supposedly connected to the
shift register, so it was assumed that the pins 43, 44, and 45 are right.
When these were used, the program did not throw an error but did
not work either. After trying to fix it by changing the configuration of
the libmraa library, trying even different pins, testing with different
base boards (both with Rock Pi S), and programming the program
for controlling the shift register in C language, it was found that the
problem probably lies in the hardware. Therefore, the program using
a shift register and buttons was not tested with the Rock Pi S, the
buttons however work fine.
For I2C communication with LCD on the Rock Pi S, I tried to install
and use the eclipse upm library (28), which is based on libmraa and
provides abstraction from libmraa library to work with peripherals
using simple functions. It could not be installed simply with the help of
a package manager, apt in this case, so the library had to be built on the
Rock Pi S. However, the build failed because of needed prerequisites.
Even with them installed correctly, other errors when building the
library occurred. Therefore, the program mentioned in 3.3 was used,
which works and controls LCD.

31
4 Conclusion
In this thesis, two exercises were designed and programmed for three
different microcontrollers. These exercises used I/O devices, specifi-
cally LCD display, rotary encoder, buttons and shift register. The I/O
devices were connected to the microcontrollers by the boards made
by Lasaris laboratory.
The first exercise is to use the LCD display to display the direction
and the count of rotation of the rotary encoder. The second one is to
write a program which uses buttons to get input and controls the shift
register according to it.
The first microcontroller programmed is STM32, for which the
exercises are programmed using Arduino IDE and libraries provided
by this IDE. The second microcontroller used is Raspberry Pi Pico,
for which exercises are programmed in Arduino IDE and in MicroPy-
thon, although LCD is not used in MicroPython. The shift register
was programmed using pins in MicroPython. How the shift register
works is explained and the exercise can be programmed according to it.
The last microcontroller used for the exercises is the Rock Pi S, which
is programmed in Python, using libmraa library for communication
with pins. For this microcontroller, the shift register is programmed
similarly as for the Raspberry when using MicroPython. For program-
ming the LCD, a program with class which provides functions for
controlling the LCD is provided and can be used.
The second goal of this thesis was to compare microcontrollers. The
findings of this thesis are that the most approachable microcontroller
from those used is the STM32. This is because it is easiest to set up,
and libraries for I/O devices are available in the Arduino IDE. Thanks
to that, the programming on it is user friendly.
The second best microcontroller regarding approachability and
ease of use for these exercises is Raspberry Pi Pico. It can be pro-
grammed with Arduino IDE too; however, when some mistake re-
garding used pins was made in a program, the board had to be reset
manually. On the other hand, this microcontroller is more versatile,
because it can be programmed in MicroPython, which is based on
Python and is easier to program in than in C language.

32
4. Conclusion

The Rock Pi S can be considered the least easy microcontroller


to program the exercises on. The set up of this microcontroller went
without a problem. The libraries for I/O devices were not as available
as with Arduino IDE, although a library for using pins for I2C and
GPIO communication was available, Raxda libmraa. The libmraa had
to be used for working with devices. The programs which work as
controllers for the I/O devices can be used with this library. These
programs had to be found and rewritten to work with Rock Pi S. On
the other hand, the Rock Pi S can be programmed in various languages
and has more I/O options than the other two microcontrollers, which
means it is suitable even for larger projects than the exercises, with
the right libraries used.
Overall, 6 exercises were created, which I hope are contribution for
the subject Introduction to IoT. The provided presentations can be used
in this subject too. The microcontrollers were compared, and results
can be helpful with later design of exercises or decisions regarding
which microcontroller to use for what type of project.

33
Bibliography
1. ROSE, Karen; ELDRIDGE, Scott; CHAPIN, Lyman. The internet
of things: An overview. The internet society (ISOC). 2015, vol. 80,
pp. 1–50.
2. GRIDLING, Gunther; WEISS, Bettina. Introduction to microcon-
trollers. Vienna University of Technology Institute of Computer Engi-
neering Embedded Computing Systems Group. 2007.
3. STM32 32-bit Arm Cortex MCUs. Geneva: STMicroelectronics
N.V., 2023. Available also from: https : / / www . st . com / en /
microcontrollers - microprocessors / stm32 - 32 - bit - arm -
cortex-mcus.html.
4. STM32F103CB. Geneva: STMicroelectronics N.V., 2023. Avail-
able also from: https : / / www . st . com / content / st _ com / en /
products / microcontrollers - microprocessors / stm32 - 32 -
bit - arm - cortex - mcus / stm32 - mainstream - mcus / stm32f1 -
series/stm32f103/stm32f103cb.html.
5. MANKAR, Jayant; DARODE, Chaitali; TRIVEDI, Komal; KANOJE,
Madhura; SHAHARE, Prachi. Review of I2C protocol. Interna-
tional Journal of Research in Advent Technology. 2014, vol. 2, no.
1.
6. Road vehicles — Controller area network (CAN) — Part 1: Data link
layer and physical signalling. Geneva, CH, 2015. Standard. Interna-
tional Organization for Standardization.
7. SZYDLOWSKI, C. CAN specification 2.0: Protocol and implementa-
tions. 1992. Tech. rep. SAE Technical Paper 921603. Available also
from: https://doi.org/10.4271/921603.
8. GARBUTT, Mike. Asynchronous Communications with the PICmi-
cro® USART. Microchip Technology, AN774. 2003.
9. Understanding UART. Columbia, MD: Rohde & Schwarz USA,
Inc., 2023. Available also from: https://www.rohde- schwarz.
com/us/products/test-and-measurement/essentials-test-
equipment / digital - oscilloscopes / understanding - uart _
254524.html.

34
BIBLIOGRAPHY

10. LADDHA, Neha R; THAKARE, AP. A review on serial com-


munication by UART. International journal of advanced research in
computer science and software engineering. 2013, vol. 3, no. 1.
11. Introduction to Pulse Width Modulation (PWM). California: Barr
Group, 2023. Available also from: https : / / barrgroup . com /
Embedded-Systems/How-To/PWM-Pulse-Width-Modulation.
12. Overview of the Arduino IDE 1. Chiasso: Arduino, 2023. Available
also from: https : / / docs . arduino . cc / software / ide - v1 /
tutorials/Environment.
13. Raspberry Pi Pico. Cambridge: Raspberry Pi Ltd, 2022. Available
also from: https://datasheets.raspberrypi.com/pico/pico-
product-brief.pdf.
14. Programmable IO. Cambridge: George Robotics Limited, 2023.
Available also from: https : / / docs . micropython . org / en /
latest/rp2/tutorial/pio.html.
15. LEENS, Frédéric. An introduction to I2C and SPI protocols. IEEE
Instrumentation & Measurement Magazine. 2009, vol. 12, no. 1, pp. 8–
13. Available from doi: 10.1109/MIM.2009.4762946.
16. MicroPython. Cambridge: George Robotics Limited, 2023. Avail-
able also from: https://micropython.org/.
17. GEORGE, Damien. The MicroPython project. San Francisco: GitHub,
2023. Available also from: https://github.com/micropython/
micropython.
18. ATtiny88 Automotive [DATASHEET]: 8-bit AVR Microcontroller
with 8K Bytes In-System Programmable Flash. 1st. San Jose: Atmel
Corporation, 2014. Available also from: https://ww1.microchip.
com / downloads / en / / / DeviceDoc / atmel - 9157 - automotive -
microcontrollers-attiny88_datasheet.pdf.
19. ROCK Pi S > Hardware > GPIO. Shenzhen: Radxa Limited, 2021.
Available also from: wiki.radxa.com/RockpiS/hardware/gpio.
20. ROCK Pi S > Hardware > Main Board. Shenzhen: Radxa Limited,
2021. Available also from: https://wiki.radxa.com/RockpiS/
hardware/rockpiS.

35
BIBLIOGRAPHY

21. PCM, Pulse Code Modulated Audio. Washington, DC: The Library
of Congress, 2023. Available also from: https://www.loc.gov/
preservation/digital/formats/fdd/fdd000016.shtml.
22. Mraa 2.0.0. California: Intel Corporation, 2018. Available also
from: https://iotdk.intel.com/docs/master/mraa/.
23. Rotary Encoders. Kyoto: OMRON Corporation, 2023. Available
also from: https://www.ia.omron.com/support/guide/34/
introduction.html.
24. 74HC595; 74HCT595: 8-bit serial-in, serial or parallel-out shift register
with output latches; 3-state. Eindhoven: NXP B.V., 2011. Available
also from: https://www.electroschematics.com/wp-content/
uploads/2013/08/74HC_HCT595-datasheet.pdf.
25. How Rotary Encoder Works and Interface It with Arduino. Toronto:
Last Minute Engineers, 2023. Available also from: https : / /
lastminuteengineers.com/rotary-encoder-arduino-tutorial/.
26. Python-i2c-lcd. San Francisco: GitHub, 2023. Available also from:
https://github.com/sweetpi/python-i2c-lcd.
27. Integrated Development Environment for STM32. Geneva: STMicro-
electronics N.V., 2023. Available also from: https : / / www . st .
com/en/development-tools/stm32cubeide.html.
28. Eclipse UPM Sensor and Actuator Repository. San Francisco: GitHub,
2023. Available also from: https://github.com/eclipse/upm.
29. Lasaris. Brno: Faculty of Informatics, Masaryk University, 2023.
Available also from: https://lasaris.fi.muni.cz/.

36
A Lasaris Boards

Figure A.1: STM32 base board [29].

Figure A.2: RS485 communication board [29].

37
A. Lasaris Boards

Figure A.3: LCD application board [29].

38
B Content of the Attachment
In the attachment, there is zip file with sample solutions for exercises,
program for controlling LCD from Rock Pi S, and presentations about
exercises. The .ino is format for Arduino IDE. This IDE also needs to
have these files in folders with the same name. Its content is as can be
seen below:

Presentations
Raspberry Pi Pico with LCD and rotary encoder
Raspberry Pi Pico with shift register
Rock Pi S with LCD and rotary encoder
Rock Pi S with shift register
STM32 with LCD and rotary encoder
STM32 with shift register
STM32
LCD_with_rotary_encoder
LCD_with_rotary_encoder.ino
shift_register_and_buttons
shift_register_and_buttons.ino
Raspberry Pi Pico
Arduino IDE
LCD_and_rotary_encoder
LCD_and_rotary_encoder.ino
shift_register_and_buttons
shift_register_and_buttons.ino
MicroPython
rotary_encoder.py
shift_register_and_buttons.py
Rock Pi S
rotary_encoder_and_LCD..py
shift_register_and_buttons.py
i2cLCDlib.py

39

You might also like