0% found this document useful (0 votes)
26 views64 pages

Arduino UNO R4 Overview and Examples

The document provides an overview of the Arduino UNO R4, detailing its two versions: Minima and WiFi, along with comparisons to the previous R3 model. It includes sections on programming basics using the Arduino IDE, LED matrix functionalities, and various examples of digital and analog pin usage, including PWM and DAC capabilities. Additionally, it covers WiFi communication and offers code snippets for practical applications.

Uploaded by

steaward61
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)
26 views64 pages

Arduino UNO R4 Overview and Examples

The document provides an overview of the Arduino UNO R4, detailing its two versions: Minima and WiFi, along with comparisons to the previous R3 model. It includes sections on programming basics using the Arduino IDE, LED matrix functionalities, and various examples of digital and analog pin usage, including PWM and DAC capabilities. Additionally, it covers WiFi communication and offers code snippets for practical applications.

Uploaded by

steaward61
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

[Link]

blog

Arduino UNO R4
Arduino UNO R4 Minima and Arduino UNO R4 WiFi

Hans-Petter Halvorsen
Contents
• Introduction to Arduino UNO R4
• Arduino IDE and some basic Arduino Programming
• LED Matrix with Code Examples
• Arduino Pins and Code Examples
– External LED (Digital Out)
– PWM (Pulse Width Modulation)
– Analog Out (DAC)
– Analog In (ADC)
– TMP36 (Analog in/ADC)
• WiFi Communication
[Link]

Introduction to
Arduino UNO R4
Hans-Petter Halvorsen Table of Contents
Arduino UNO R4
• In mid 2023 a new version of the popular
Arduino UNO R3 was released
• Arduino UNO R4 comes in 2 different
versions:
– Arduino UNO R4 Minima (about €18)
– Arduino UNO R4 WiFi (about €25)
Arduino UNO R4 Minima vs WiFi
[Link]

If you compare the feature list and the price difference between Minima and
the WiFi edition, I see no reasons why you should not buy the WiFi edition
Arduino UNO R3 vs R4
Parameter Arduino UNO R3 Arduino UNO R4
Microcontroller (CPU) 8-bit 32-bit

WiFi No Arduino UNO R4 WiFi

Bluetooth NO Arduino UNO R4 WiFi

Memory 2KB SRAM, 32KB FLASH 256 kB Flash, 32 kB RAM

Input voltage (VIN) 6-20 V 6-24 V

Digital I/O pins 14 14

Analog input pins 6(10-bit) 6(14-bit)

PWM pins 6(12-bit) 6(12-bit)

DAC pin 0 1

USN port USB-B USB-C

CAN bus 0 1

Qwiic connector No Arduino UNO R4 WiFi

LED matrix No 12x8 LED matrix on Arduino UNO R4 WiFi

Maximum Pin Current 20mA 8mA


Improvements Arduino UNO R3 vs R4
Main differences:
• 32-bit CPU instead of 8-bit CPU
• 256kB flash memory instead 32kB
• 32 kB RAM instead of 2kB SRAM
• USB-C instead of USB-B port
• 12-bit DAC, while R3 has only PWM
•…
Arduino UNO R4 Minima

[Link]
Arduino UNO R4 WiFi
• WiFi
• Bluetooth
€18 • LED Matrix

[Link]
[Link]

Arduino IDE
Hans-Petter Halvorsen Table of Contents
Arduino IDE

[Link]
Install Arduino UNO R4 Board
Select Arduino UNO R4 Board
Select Port
Start creating Arduino Programs
All Arduino programs must follow the following main structure:
// Initialization, define variables, etc.

void setup()
{
// Initialization
...
}
void loop()
{
//Main Program
...
}
Built-in LED Example
Built-in LED Example
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
[Link]

LED Matrix
Hans-Petter Halvorsen Table of Contents
LED Matrix (Arduino UNO R4 WiFi)
The Arduino UNO R4 WiFi comes with a built
in 12x8 LED Matrix, that is available to be
programmed to display graphics, animations,
act as an interface, or even play games on
(e.g., Tetris or Snake?).

[Link]
#include "Arduino_LED_Matrix.h"

uint8_t heart[8][12] = { This Example shows a


{ 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0 }, Heart on the LED Matrix on
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 }, the Arduino UNO R4 WiFi
{ 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

ArduinoLEDMatrix matrix;

void setup()
{
[Link](115200);
[Link]();
[Link](heart, 8, 12);
}

void loop()
{

}
LED Matrix Editor

const uint32_t heart[] =


{
0x3184a444,
0x44042081,
0x100a0040
};
[Link]
#include "Arduino_LED_Matrix.h"

const uint32_t heart[] = {


0x3184a444,
0x44042081, Code created by the
0x100a0040 LED Matrix Editor
};

ArduinoLEDMatrix matrix;

void setup()
{
[Link](115200);
[Link]();
[Link](heart);
} This Example shows a
Heart on the LED Matrix on
void loop() the Arduino UNO R4 WiFi
{

}
#include "Arduino_LED_Matrix.h"

const uint32_t heart[] = {


0x3184a444,
0x44042081,
0x100a0040
}; Code created by the
const uint32_t happy[] = { LED Matrix Editor
0x19819,
0x80000001,
0x81f8000
};

ArduinoLEDMatrix matrix;

void setup()
{
[Link](115200);
[Link]();
This Example switch between
showing a Heart and a Happy
}
face on the LED Matrix on the
void loop()
{
Arduino UNO R4 WiFi
[Link](heart);
delay(1000);
[Link](happy);
delay(1000);
}
More Examples
[Link]

Arduino Pins
Hans-Petter Halvorsen Table of Contents
Arduino Pins
We will provide some basic
• Digital code examples where we
– Digital Out test the different Pins
– Digital In
– PWM
• Analog Out (DAC)
• Analog In
[Link]

External LED
Hans-Petter Halvorsen Table of Contents
External LED Example
What do we need?
• Breadboard
• LED
• Wires
• Resistor (e.g., 𝑅 = 270Ω)
External LED Wiring
Blinking External LED
int ledPin = 8;
void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
[Link]

PWM
Pulse Width Modulation

Hans-Petter Halvorsen Table of Contents


Pulse Width Modulation (PWM)

We can use PWM to


control the brightness of a
LED and many other things

[Link]
Pulse Width Modulation (PWM)
The Raspberry Pi UNO R4 Minima
supports PWM on pins marked with ~
These pins are 3, 5, 6, 9, 10, 11
We use the following Arduino Function for PWM:
analogWrite(pin, value);

Note that despite the function name, the


output is a digital signal, often referred to as a
square wave

[Link]
Pulse Width Modulation (PWM)
The average voltage Level is 0.5V

The average voltage Level is 2.5V

The average voltage Level is 4.5V

[Link]
analogWrite
Arduino can give a signal between 0 and 5𝑉
0.5
0.5𝑉: 100% → 10%
5

0.5V (10% of 255) -> analogWrite(pin, 25)

2.5
2.5𝑉: 100% → 50%
5

2.5V (50% of 255) -> analogWrite(pin, 127)


Arduino syntax: analogWrite(pin, value)
value: the duty cycle: between 0 (always 4.5
4.5𝑉: 100% → 90%
off) and 255 (always on). 5
0-5V -> 0-255
4.5V (90% of 255) -> analogWrite(pin, 229)
Wiring
PWM Example
int ledPin = 3;
int value;

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

void loop()
{
255 -> Max Brightness value = 255;
analogWrite(ledPin, value);
delay(1000);

0 -> Min Brightness -> LED Off value = 0;


analogWrite(ledPin, value);
delay(1000);
}
PWM Example – random()
int ledPin = 3;
int value = 0;

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

void loop()
{
value = random(256);
analogWrite(ledPin, value);
delay(1000);
}
PMM - Voltage
int ledPin = 3;
float voltage = 0;
int value = 0;

Typically, we want to specify the Voltage value void setup()


{
0-5V -> 0-255 pinMode(ledPin, OUTPUT);
[Link](9600);
0V -> 0 }
1V ->
void loop()
{
5V -> 255 voltage = random(0, 501)/100.0;
[Link](voltage);
value = 255*voltage/5;
255 [Link](" - ");
𝑦= 𝑥
5 [Link](value);
analogWrite(ledPin, value);
x: 0-5V delay(1000);
}
y: 0-255
PMM - Voltage
0-5V -> 0-255

0V -> 0
1V ->
..
5V -> 255
This gives this formula that w need to use:

255
𝑦= 𝑥
5

x: 0-5V
y: 0-255
[Link]

Analog Out (DAC)


Hans-Petter Halvorsen Table of Contents
Analog Out
• Arduino UNO R3 has no real Analog
Out pins, only PWM
• The UNO R4 has a DAC with up to 12-
bit resolution

[Link]
Analog Out
We use the analogWrite() function in order to use the DAC:
analogWrite(pin, value);

This DAC pin has a default write


int analogPin = A0; resolution of 8 bits. This means that
int value = 255; values that are written to the pin
analogWrite(analogPin, value); should be between 0-255 (2^8).

You may change this resolution to up to 12


analogWriteResolution(12); bits. The values you write to the pin should
be between 0-4096 (2^12)

[Link]
int analogPin = A0;

void setup()
{
analogWriteResolution(8);
}

void loop()
{
int value = 255;
analogWrite(analogPin, value);
delay(1000);

value = 0;
analogWrite(analogPin, value);
delay(1000);
int analogPin = A0;
float voltvalue;
int dacvalue;

void setup()
{
analogWriteResolution(8);
Typically, we want to deal with voltage
} values instead of DAC values
void loop()
{
voltvalue = 5;
dacvalue = VoltToDac(voltvalue);
analogWrite(analogPin, dacvalue);
delay(1000);

voltvalue = 0;
dacvalue = VoltToDac(voltvalue);
analogWrite(analogPin, dacvalue);
delay(1000);
}

int VoltToDac(float volt)


{ A Function is made to convert from
int dacmax = 255;
int dac = dacmax*volt/5; Voltage (0-5V) to DAC Value (0-255)
return dac;
}
[Link]

Analog In (ADC)
Hans-Petter Halvorsen Table of Contents
Analog In/ADC
• Arduino UNO R4 has 6 Analog In pins
– Use the analogRead(pin) Function
• An analog-to-digital converter (ADC) transforms an
analog signal to a digital one
• By default, the ADC resolution is set to 10 bit
(2^10=1024), i.e., values between 0 and 1023
• It can be updated to 12 bit (2^12=4096) or 14 bit
(2^14=16384) if you need higher accuracy
– Use the analogReadResolution(bit) Function
[Link]
Analog In
int analogReadPin = A1;
int valueAdc = 0;
float valueVoltage = 0;
void setup()
{
analogReadResolution(10);
[Link](9600);
}
Here we have connected a
void loop() 1.5V Battery to one of the
{ Analog Input pins on Arduino.
valueAdc = analogRead(analogReadPin);
We convert the ADC Value (0-
valueVoltage = (5*valueAdc)/1023.0;
[Link](valueVoltage);
1023) to a Voltage Value (0-5V)
delay(5000); and Print the Voltage Value to
} the Serial Monitor
int analogWritePin = A0;

Analog Write and Read Example


int analogReadPin = A1;

int readValue;

void setup()
{
analogWriteResolution(10);
In this example we have
analogReadResolution(10); wired A0 and A1 together
[Link](9600);
}

void loop()
{
int value = 200;
analogWrite(analogWritePin, value);
readValue = analogRead(analogReadPin);
[Link](readValue);
delay(1000);

value = 800;
analogWrite(analogWritePin, value);
readValue = analogRead(analogReadPin);
[Link](readValue);
delay(1000);
}
[Link]

TMP36
Analog In (ADC)

Hans-Petter Halvorsen Table of Contents


Temperature Sensor Example
• In this example we will use a small
temperature sensor to read the
temperature in the room.
• The Temperature Sensor is called
“TMP36”
• In this example we will use one of the
"Analog In" ports on the Arduino board
Necessary Equipment
• Arduino
• Breadboard
• TMP36
• Wires (Jumper Wires)
TMP36

TMP is a small, low-cost temperature sensor and cost about $1 (you can buy it “everywhere”)
Output Voltage vs. Temperature
Datasheet

[Link]
Linear Scaling
Convert form Voltage (V) to degrees Celsius
From the Datasheet we have:

(𝑥! , 𝑦! ) = (0.75𝑉, 25°𝐶)


(𝑥" , 𝑦" ) = (1𝑉, 50°𝐶)

There is a linear relationship between


Voltage and degrees Celsius:
𝑦 = 𝑎𝑥 + 𝑏
This gives:
50 − 25 We can find a and b using the following
𝑦 − 25 = (𝑥 − 0.75) known formula:
1 − 0.75

Then we get the following formula: 𝑦" − 𝑦!


𝑦 − 𝑦! = (𝑥 − 𝑥! )
𝑦 = 100𝑥 − 50 𝑥" − 𝑥!
Wiring
Wiring
Temperature Conversion
We want to present the value from the sensor in degrees
Celsius:
1. The function analogRead() gives a value between 0 and
1023 (Arduino UNO has a built-in 10-bit ADC,
2^10=1024)
2. Then we convert this value to 0-5V.
3. Finally, we convert to degrees Celsius using information
from the Datasheet presented on the previous page
(𝑦 = 100𝑥 − 50)
4. The we can, e.g., show the Temperature value in the
Serial Monitor
Code
const int temperaturePin = 0;

float adcValue;
float voltage;
float degreesC;

void setup()
{
[Link](9600);
}

void loop()
{

adcValue = analogRead(temperaturePin);

voltage = (adcValue*5)/1023;

degreesC = 100*voltage - 50;

[Link]("ADC Value: ");


[Link](adcValue);

[Link](" voltage: ");


[Link](voltage);

[Link](" deg C: ");


[Link](degreesC);

delay(1000);
}
[Link]

WiFi
Connection Arduino UNO R4 WiFi to Internet

Hans-Petter Halvorsen Table of Contents


WiFi
• Arduino UNO R4 WiFi has a built in ESP32-S3
module that enables you to connect to Wi-Fi
networks and perform network operations.
• Wi-Fi support is enabled via the built-in WiFiS3
library that is shipped with the Arduino UNO R4
Core.
• Installing the Arduino UNO R4 Core automatically
installs the WiFiS3 library.
[Link]
void ConnectWiFi()
#include <WiFiS3.h> {
#include "secrets.h" // check for the WiFi module:
if ([Link]() == WL_NO_MODULE) {
[Link]("Communication with WiFi module failed!");
char ssid[] = SECRET_SSID; }
while (true);

char pass[] = SECRET_PASS;


String fv = [Link]();
int status = WL_IDLE_STATUS; if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
[Link]("Please upgrade the firmware");
}
void setup()
{ // Attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
[Link](9600); [Link]("Attempting to connect to WPA SSID: ");
[Link](ssid);
ConnectWiFi(); // Connect to WPA/WPA2 network:
} status = [Link](ssid, pass);

void PrintNetwork() // wait 10 seconds for connection:


void loop() { {
}
delay(10000);
[Link]("WiFi Status: ");
delay(10000); [Link]([Link]());
// You're connected now, so print out the data:
PrintNetwork(); [Link]("You're connected to Wifi");
[Link]("SSID: ");
} [Link]([Link]()); PrintNetwork();
}
IPAddress ip = [Link]();
[Link]("IP Address: ");
[Link](ip);
} “secrets.h”: #define SECRET_SSID "xxx"
#define SECRET_PASS "xxx"
Setting up a Mobile Hotspot WiFi Network

You cannot connect to your Eduroam


Network from Arduino.

You can easily configure a Mobile Hotspot


WiFi Network in Windows 10/11 or on your
smartphone.
Hans-Petter Halvorsen
University of South-Eastern Norway
[Link]

E-mail: [Link]@[Link]
Web: [Link]

You might also like