Book Arduino Uno R4
Book Arduino Uno R4
books books
books
Mastering the
Mastering the
Arduino Uno R4 Arduino Uno R4
… all in creative and educational ways with the project operation and
associated software explained in great detail.
Dogan Ibrahim
Cover Mastering the Arduino Uno - UK.indd Alle pagina's 13-09-2023 11:20
Mastering the Arduino Uno R4
Programming and Projects for the Minima and WiFi
Dogan Ibrahim
● 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.
● Declaration
The author, editor, 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 cause.
All the programs given in the book are Copyright of the Author and Elektor International Media. These programs
may only be used for educational purposes. Written permission from the Author or Elektor must be obtained before
any of these programs can be used for commercial purposes.
Elektor is 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
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
●5
3.11 Project 10: Controlling the LED flashing rate - external interrupts . . . . . . . . . . . . 80
3.17 Project 16: Using the 74HC595 shift register – binary up counter . . . . . . . . . . . 100
3.18 Project 17: Using the 74HC595 shift register - random flashing 8 LEDs . . . . . . . 103
3.19 Project 18: Using the 74HC595 shift register - chasing LEDs . . . . . . . . . . . . . . 104
3.20 Project 19: Using the 74HC595 shift register - turn ON a specified LED . . . . . . . 105
3.21 Project 20: Using the 74HC595 shift register - turn ON specified LEDs . . . . . . . 107
4.7 Project 5: 7-Segment 4-digit multiplexed LED display - reaction timer . . . . . . . . 128
●6
5.9 Project 5: LCD based accurate clock using timer interrupts . . . . . . . . . . . . . . . . 149
6.8 Project 9: Sound detection sensor — control the relay by clapping hands . . . . . . 175
6.9 Project 10: Flame sensor - fire detection with relay output . . . . . . . . . . . . . . . . 177
8.2 Project 1: Display the pressed key code on the Serial Monitor . . . . . . . . . . . . . . 195
●7
9.5 Project 3: Temperature and humidity display with time stamping . . . . . . . . . . . . 213
12.3.1 Project 4: Rotate the motor clockwise and then anticlockwise . . . . . . . . . . . . 238
●8
13.3.3 Project 4: Generate sine wave whose frequency changes with potentiometer . 245
Chapter 14 ● Using the EEPROM, the Human Interface Device, and PWM . . . . . . 248
15.3.4 Project 5: Controlling the Arduino Uno R4 WiFi on-board LED from
a smartphone using UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
●9
18.4 Project 1: Arduino Uno R4 WiFi to Arduino Uno R4 Minima CAN bus
communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
18.5 Project 2: Sending the temperature readings over the CAN bus . . . . . . . . . . . . 306
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
● 10
The Arduino Uno R3 board probably ranks as the most popular Arduino family member
to date and has been with us for many years. Based on the low-cost 8-bit ATmega328P
processor, the Uno R3 has been used by students and hobbyists in many beginning and
intermediate-level, low-speed projects requiring small to medium amounts of memory.
Perhaps one of the attractive points of the Uno R3 was its powerful IDE and the simplicity
of using it to develop projects in relatively short times.
Recently, the new Arduino Uno R4 was announced. This new board is compatible with the
earlier Uno R3 but offers highly improved specifications compared to Uno R3. The new
Arduino Uno R4 is based on a 48 MHz 32-bit Cortex-M4 processor with a large amount of
SRAM and flash memory. Additionally, a higher-precision ADC and a new DAC are added to
the design. The contemporary design also supports the CAN bus interface. Two versions of
the board are available: Uno R4 Minima, and Uno R4 WiFi.
This book is about using these new boards to develop many different and interesting pro-
jects. The projects given in the book have been fully tested with just a handful of parts
and external modules, which are available as a kit from Elektor. The block diagrams, circuit
diagrams, full program listings, and complete program descriptions are given for all the
projects in the book. You should find it easy to build the project hardware and then follow
the software descriptions given for the projects. It should also be relatively easy to modify
the hardware and software for your own project applications.
I hope that you enjoy reading the book and at the same time learn how to use the Arduino
Uno R4 Minima or the R4 WiFi models in your innovative projects.
Dogan Ibrahim
London, 2023
Publisher's Notice: All programs discussed in this Guide are contained in an archive file
you can download free of charge from the Elektor website. Head to www.elektor.com/
books and enter the book title in the Search box.
● 11
1.1 Overview
The Arduino project started out as a tool for students at the Interactive Design Insti-
tute, Ivrea in Italy back in 2005. The aim of this project was to provide low-cost and easy-
to-use hardware and software to beginner students and hobbyists to create simple projects
using sensors and actuators. The name "Arduino" comes from the bar named Arduin of
Ivrea where the project's founders used to meet for drinks. The name of this bar came from
the Margrave Arduin of Ivrea, who was King of Italy from 1002 to 1014.
The initial Arduino project team consisted of Massimo Banzi, David Cuartielles, Tom Igoe,
Gianluca Martino, and David Mellis. In 2003 Hernando Barragan created the development
platform Wiring as a master's thesis at the Institute Ivrea. Wiring was an open-source
electronics platform consisting of a programming language, an integrated development
environment (IDE), and a single-board microcontroller. This project was developed under
the supervision of Massimo Banzi and Casey Reas. The Wiring platform included a printed
circuit board with an ATmega128 microcontroller, an IDE, and some library functions. Later
in 2005, Massimo Banzi, David Mellis, and David Cuartielles extended the Wiring platform
by adding support for the ATmega8 microcontroller which was cheaper. This new project
was named Arduino. The project was so successful that after developing less expensive
versions, by mid-2011 over 300,000 copies of Arduino were produced commercially and by
2013 this number increased to over 800,000.
Arduino is an open-source hardware where the designs are distributed under a Creative
Commons license and are freely available. The only point is that the developers are re-
quested the name Arduino to be reserved for the official product and not be used for similar
copy work without permission.
One of the nice features of the Arduino series is that a pre-programmed boot loader is
used on the on-board processor. Users can develop their programs using the IDE and then
upload their programs to the Arduino processor with the help of this boot loader program.
The I/O pins are available at female headers located on either side of the board. This makes
the hardware development very easy as jumper wires can be used to make connections to
the board.
The original Arduino board was manufactured by the Italian company called Smart Projects.
Many versions of the Arduino have been developed over the years by several companies.
Some versions are:
• Arduino Diecimila
• Arduino Uno R2
• Arduino Leonardo
• Arduino RS232
• Arduino Pro
• Arduino Mega
• Arduino LilyPad
● 12
• Arduino Robot
• Arduino Esplora
• Arduino Yun
• Arduino Fio
• Arduino Ethernet
• Arduino Due
• Arduino Nano
• Arduino Uno SMD R3
• Arduino Uno R3
• Arduino MKR Zero
• Arduino Zero
• … and many more
The Arduino family has been so popular that in 2022, its revenue amounted to over US$237
million, including a large portion of online sales via the Internet.
Two new versions of the Arduino have recently been announced: Arduino Uno R4 Mini-
ma, and Arduino Uno R4 WiFi. In this book, you will be developing projects using both
the Arduino Uno R4 Minima and the Arduino Uno R4 WiFi. The new two boards are similar
to the very popular Arduino Uno R3 board but they have been expanded in many ways.
Word length 8 32
SRAM 2 KB 32 KB
EEPROM 1 KB 8 KB
Operating voltage 5V 5V
Timers 3 10
SPI 1 1
● 13
I2C 1 2
Operating voltage 5V 5V
Op Amp None 1
Analog inputs 6 6
PWM pins 6 6
The Arduino Uno R4 features the Renesas RA4M1 processor, which is an Arm 32-bit Cor-
tex-M4 processor, running at 48 MHz. The Uno R3 had an ATmega328P processor with only
a 16 MHz clock. This is a 3 times increase of the clock speed over the Uno R3. Additionally,
Uno R4 has 32 KB SRAM memory compared to 2 KB on the Uno R3. The flash memory of
the R4 is 256 KB, compared to only 32 KB on the Uno R3. As a result, more complex pro-
jects requiring more memory can be developed with the Uno R4. The USB port on the Uno
R3 has been replaced with the currently standard USB-C and the maximum power supply
voltage has been increased to 24 V with improved thermal design. The processor operating
voltage is still 5 V. Arduino Uno R4 provides a CAN bus interface, allowing devices to be
connected and programmed on a CAN bus environment. The ADC converter capacity has
been increased from 12 bits to 14 bits on the Uno R4. SPI and I2C bus interfaces have been
increased from 1 to 2. The Uno R4 supports Human Interface Device (HID) which enables
users to simulate a mouse or keyboard when connected to a computer via a USB cable.
Users can send mouse movements or keystrokes to a computer. Additionally, Uno R4 in-
cludes a true 12-bit DAC converter. The analogWave library was added to make using the
DAC easy. Generating a sine, saw or square wave is as easy as calling a library function.
Of course, you can do much more with it. The Uno R4 PCB is hardware compatible with the
Uno R3. The pinout, voltage, and form factor are unchanged so that the Uno R4 can easily
replace designs that use the Uno R3. The software IDE is also compatible between the Uno
R3 and Uno R4, where an effort was made to maximize backward compatibility of the Ar-
duino libraries so that users can use the existing libraries without any modifications. Some
libraries which depend heavily on the AVR architecture may need to be re-loaded into the
IDE. A public list of such libraries will be provided by Arduino.
● 14
Compared to other Cortex-based boards such as the Raspberry Pi Pico, which uses the
Cortex M0+, the Cortex-M4 performance is about 6 times better (just to remind you, the
Raspberry Pi Pico clock runs at 125 MHz by default). As a result, the Arduino Uno R4 can be
used in highly complex real-time projects (e.g. digital control, DSP, AI, etc.) requiring large
memory and fast throughput.
As mentioned earlier, Arduino Uno R4 is available in two versions: WiFi and Minima. The
WiFi version is equipped with an Espressif S3 Wi-Fi module, making the board ideal in IoT-
based network and Bluetooth BLE applications, as well as in applications requiring Internet
connectivity. Additionally, a 128×8 LED matrix is included on the board. The Minima version
offers a cost-effective option with no WiFi or Bluetooth connectivity. In this book, all the
projects compile and run on both versions, except that the WiFi and LED Matrix
projects only compile and run on the R4 WiFi version.
In summary, the Uno R4 is a giant leap forward for Arduino as it is a truly remarkable board
that will take your microcontroller project experience to the next level. Perhaps one disad-
vantage of the Uno R4 compared to Uno R3 is its increased price.
● 15
On the two sides of the PCB, you have the header connectors as in the Uno R3. The headers
have the following pins (see Figure 1.2):
ICSP connector:
• See Table 1.2
SWD/JTAG connector:
• See Table 1.3
Table 1.2: ICSP connector pins (source: Product Reference Manual, SKU: ABX00080).
● 16
Figure 1.2: Header pins (source: Product Reference Manual, SKU: ABX00080).
The recommended operating conditions are shown in Table 1.4. Schottky diodes are used
for overvoltage and reverse polarity protection. Power can either be supplied via the VIN
pin, the barrel jack (DC jack), or via USB-C connector. If power is supplied via VIN, a
● 17
buck converter steps the voltage down to 5 V. Power via USB supplies about 4.7 V (due
to Schottky barrier forward bias) to the RA4M1 microcontroller. The RA4M1 processor can
operate from +1.6 to +5.5 V and is connected to +5 V on the Arduino board. The digital
GPIOs on the RA4M1 microcontroller can handle currents of IOH = 4 mA and IOL
= 8 mA (assuming middle pin drive) current. Remember that IOL is the current
into the pin (sinking) when the pin is at logic 0, and IOH is the current from the
pin (sourcing) when the pin is at logic 1. Care must be taken not to exceed the
recommended current drives of the GPIO ports. Figure 1.3 shows a simplified power
supply connection of the Arduino Uno R4.
Figure 1.4 shows the pin layout of the Arduino Uno R4 Minima board. The component layout
is shown in Figure 1.5 (taken from Product Reference Manual: SKU: ABX00080) with the
component descriptions shown in Table 1.5.
● 18
● 19
● 20
• 1× PS2 Joystick
• 5× 1 kΩ resistor
• 5× 10 kΩ resistor
• 5× 220Ω resistor
• 1× 4×4 keypad module
• 1× SG90 Servo
• 1× RFID card
• 1× RGB module
• 2× jumper cap
• 1× 9 V Battery DC clip-on cable
The kit is supplied in a plastic box with a lid as shown in Figure 1.6 (note: the actual pack-
aging and contents of the kit as received may differ from the photo).
Figure 1.7 shows the supplied components and the Arduino Uno R4, which is not included
and must be purchased separately. A close-up picture of the included sensors, actuators,
and displays is shown in Figure 1.8. Figure 1.9 shows a close-up picture of the supplied
LEDs, resistors, buttons, buzzers, breadboard and wire jumpers.
● 21
Figure 1.9: Close-up picture of the LEDs, resistors, buttons, wires, etc.
● 22
2.1 Overview
The Arduino code is written in the C++ language, which is currently one of the most popu-
lar languages used to program microcontrollers. Arduino can be programmed using either
the Desktop Arduino IDE or the Arduino Web Editor. The IDE requires the software to
be installed on your PC. This is an Integrated Development Environment (IDE) that consists
of an editor, compiler, debugger, and tools to upload the compiled code to the processor
on the development board. An Arduino program is called a sketch, which is compiled
into machine code and uploaded to the target processor. Arduino Web Editor enables the
programmer to develop, compile, and upload programs using an online browser with the
advantage that the online tool is always up-to-date and includes the latest libraries and
features. In this book, you will be using the IDE. Interested readers can search for Arduino
Web Editor in Google and create an account to sign in and use it.
The Arduino IDE has been developed over a decade and there are several versions of it.
The latest stable version is 2.1 released in March 2022. Version 1.8.19 has been popular for
many years and is still used by many programmers. New version 2.1.0 is the recommended
version since it is faster and easier to use than version 1.8.19. The author has used version
2.1.0 in all the projects in this book. Readers may prefer to use the same version in their
projects.
In this chapter, you will learn how to install version 2.1.0 of the IDE, which was the latest
version at the time of authoring this book. Simple software-only programs are given in this
chapter to review the principles of programming using the Arduino IDE. In the next chap-
ters, you will be using the newly released Arduino Uno R4 Minima/WiFi development
boards together with the supplied components of the bundle in many real-time project
applications.
Further information on Arduino IDE, Web Editor, and related tools can be obtained from the
links given in Table 2.1.
● 23
https://www.arduino.cc/en/software
Select your processor from the DOWNLOAD OPTIONS at the right (Figure 2.1). Click
JUST DOWNLOAD (unless you want to support by contributing). The author installed it on
a Windows 10 laptop and at the time of drafting this book the latest version file name was:
Arduino-ide_2.1.0-Windows_64bit.exe.
You now have to install the Board Package for your Arduino Uno R4 Minima. The steps are:
• Click to Open the Boards Manager at the top left of the screen (Figure 2.2).
• Search for ARDUINO UNO R4 (Figure 2.3) and click INSTALL to install it. At the
time of drafting this book, the version was: 1.0.1.
● 24
• You should be able to select the Arduino Uno R4 board from the board selector
at the top left of the screen (Figure 2.4). Connect your Arduino Uno R4 Minima
(or WiFi) to your PC via a USB-C cable.
• Click Tools Port and select the serial port connected to your Uno R4.
You are now ready to develop programs and upload them to your Arduino Uno R4 Minima/
WiFi processor on your development board.
Before looking at some example programs, it is worthwhile to learn some of the commonly
used menu options offered by the IDE.
File: with this menu option you can open existing programs, save programs, open example
programs, and set the IDE working environment (e.g., Preferences…).
Edit: with this menu option you can cut, paste, select, go to a specified line, change inden-
tation and font size, and find text in a file.
Sketch: with this menu option you can compile your program, upload the compiled pro-
gram to the target processor, include libraries, add files and some other options that you
will not be using.
● 25
Tools: with this menu option you can manage libraries, configure the serial monitor and
serial plotter, select and configure the development board that you will be using, and burn
a new bootloader.
Help: this menu option displays various help items on selected topics. Additionally, it dis-
plays the version number of the currently used IDE (Figure 2.5). e.g., Version: 2.1.0
Solution 1
Figure 2.6 shows the program listing (Program: sumN). Comments are used at the be-
ginning of the program to describe the function of the program. Also, the names of the
author and the program and the date of development of the program are all listed here.
It is strongly recommended by the author to include comments in your programs to make
them easy to follow and also easy to modify in the future.
The setup() function is executed only once at the beginning of the program. Inside this
function, the Serial Monitor is configured to run at 9600 baud (you may choose a different
baud rate if you wish).
The main program runs inside the function loop(). Here, variables i, N, and Sum are
declared as integers and Sum is cleared to 0. The program prompts the user to enter N
which is read using the built-in function parseInt(). The program checks if data is availa-
ble before reading from the keyboard. Then, a for loop is formed where the sum of all the
integer numbers from 1 to N are calculated and stored in variable Sum. The sum is finally
● 26
displayed as an integer using a println() function. Notice that the println() function prints
a carriage return and line feed after displaying the data. The program is stopped by using a
while() statement at the end, otherwise the loop() function will repeat forever.
• Connect your Arduino Uno R4 development board to the PC and configure the
serial link.
• Type your program as in Figure 2.6 (or load from the Elektor website of the
book) and then save it with a suitable name, e.g., sumN.
• Click Sketch Upload to upload the correctly compiled code to the processor
on your development board. You should see the message Done uploading at
the bottom part of the screen.
• Make sure the Baud rate is set to 9600. Click Serial Monitor at the top of the
bottom panel. (Figure 2.8). If Serial Monitor is not available, click the Serial
Monitor icon at the top right corner of the display.
//--------------------------------------------------------------------
// SUM OF INTEGER NUMBERS FROM 1 to N
// ==================================
//
// This program calculates and displays the sum of integer numbers
//
// Author: Dogan Ibrahim
// File : sumN
// Date : June, 2023
//--------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
delay(5000);
}
void loop()
{
int i, N, Sum = 0;
● 27
• You should see the text How many numbers are there ? displayed at the
Serial Monitor window. For example, enter 10 where it says: Message (Enter
to send message to 'Arduino UNO R4' Minima…) and press the Enter key
on your PC. You should see 55 displayed which is the sum of integer numbers
from 1 to 10 as shown in Figure 2.9.
You should follow the steps given in this program in order to test the other programs given
in the following sections of this chapter.
● 28
Solution 2
Figure 2.10 shows the program listing (Program: squares). The Serial Monitor is initialized
as in the previous example. A for loop is set up in the main program loop, which tabulates
the squares of numbers from 1 to 10. The display items are separated with a tab (i.e. "\t").
//--------------------------------------------------------------------
// TABLE OF SQUARES
// =================
//
// This program displays table of squares of integers from 1 to 10
//
// Author: Dogan Ibrahim
// File : squares
// Date : June, 2023
//--------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
delay(5000);
}
void loop()
{
int i, N;
● 29
Figure 2.11 shows the output from the program, displayed on the Serial Monitor.
Solution 3
The volume of a cylinder whose radius and height are r and h respectively is given by:
Area = π r2 h
Figure 2.12 shows the program listing (Program: cylarea). Function area receives the
radius and height of the cylinder as floating point numbers and returns the volume to the
main program which then displays the volume on the Serial Monitor.
//--------------------------------------------------------------------
// VOLUME OF A CYLINDER
// ====================
//
// This program calculates and displays the volume of a cylinder
//
// Author: Dogan Ibrahim
// File : cylarea
// Date : June, 2023
//--------------------------------------------------------------------
#define pi 3.14159
float r = 10.0;
float h = 15.0;
void setup()
{
Serial.begin(9600);
● 30
delay(5000);
}
//
// Function to calculate the volume
//
float volume(float radius, float height)
{
float vol;
vol = pi * radius * radius * height;
return vol;
}
void loop()
{
float cylinder;
Figure 2.13 shows the output from the program where the radius and height are set to 10
cm and 15 cm, respectively at the beginning of the program.
● 31
Solution 4
Given the temperature in degrees C, it can be converted into degrees F using the following
formula:
F = 1.8 × C + 32
Figure 2.14 shows the program listing (Program: CtoF). Function ToF receives degrees C
as its argument, converts it in degrees F and returns to the main program. The temperature
in degrees Centigrade is read from the keyboard.
//--------------------------------------------------------------------
// CELSIUS TO FAHRENHEIT
// =====================
//
// This program converts Celsius to Fahrenheit
//
// Author: Dogan Ibrahim
// File : CtoF
// Date : June, 2023
//--------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
delay(5000);
}
//
// Function to convert Degrees C to Degrees F
//
float ToF(float C)
{
return (1.8 * C + 32);
}
void loop()
{
float F;
int C;
F = ToF(C); // COnvert to F
Serial.print(C);
Serial.print(" Degreec C = ");
● 32
Serial.print(F);
Serial.println(" Degrees F");
Figure 2.15 shows the output from the program where 100 degrees centigrade is converted
into Fahrenheit and displayed on the Serial Monitor.
Solution 5
Figure 2.16 shows the program listing (Program: times). An integer number is read from
the keyboard and stored in variable N. Then a for loop is set up that runs from 1 to 12.
Number N is multiplied by 1 to 12 inside this loop and is then displayed on the Serial Mon-
itor.
//----------------------------------------------------------------------
// TIMES TABLE
// ===========
//
// This program reads an integer number and then tabulates the time table
//
// Author: Dogan Ibrahim
// File : times
// Date : June, 2023
//----------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
delay(5000);
}
void loop()
{
int N, i;
● 33