Introduction to
Arduino programming
MARCO BARUZZO
ICTP SCIFABLAB
2
Summary
Introduction
Interacting with the World
Arduino Programming
Arduino Family and other microcontroller
Some projects
Introduction: 3
What is a microcontroller:
Small computing unit equipped with all the required hardware (RAM, ROM,
bus, IO) to perform real-time tasks designed for embedded applications
(It’s different to microprocessor (CPU) which has to be supplied with all the additional hardware)
Introduction: 4
What is a microcontroller:
Small computing unit equipped with all the required hardware (RAM, ROM,
bus, IO) to perform real-time tasks designed for embedded applications
Standard example on the use of microcontrollers:
• Connecting to sensors
• Controlling devices
• Automatizing tasks
• Doing repetitive tasks!
Introduction: 5
Programming microcontroller before Arduino
• Preparing your own board and programming interface with the microcontroller
• Usually assembly coding (not every case)
• Lack of libraries
• Lack of support
Because of these reasons:
• Not really fast for R&D
• Not encouraging for the general public
Introduction: 6
The Arduino breakthrough
• Preparing your own board and programming interface with the microcontroller
• Arduino provides you a standard board with programming interface and
compatible with lots of devices
• Usually assembly coding (not every case)
• Simplified C++ coding
• Lack of libraries
• A HUGE selection of libraries
• Lack of support
• HUGE and supportive community!
Because of these reasons:
• Super fast R&D
• A new gold era of homemade projects
Introduction: 7
Arduino
It is an electronic board hosting a microcontroller.
This board permits you to:
• Connect in a simple way the microprocessor with the PC,
using serial-USB converter, in order to be programmed with
the firmware
• Power electronics
• Connect to the digital and analog pin of the microcontroller
via jumper cable
Introduction: 8
Anatomy of an Arduino board
Programming
USB IO Pins
USB interface
ATMEGA
microcontroller
Power
connector
Major features on board
Introduction: 9
Anatomy of an Arduino board
Reset button LED connected
(restart code) to pin 13
ON indicator
Indicates Rx/Tx
connections on
Serial Port
LED indicator and button
Introduction: 10
Powering Arduino
USB
Connector
Arduino can be powered via:
• Via USB connector (with PC or USB charger)
• Via power connector (connected to internal voltage
regulator input 7-12 V)
• Via Vin pin (only regulated 5 V)
Power
Connector
Vin pin
Interaction with the World: 11
Arduino senses and arms: IO pins
USB
Connector
An I/O pin is an interface between the microcontroller and the
external world, these can be:
• Via USB connector (with PC or USB charger)
• Via power connector (connected to internal voltage
regulator input 7-12 V)
• Via Vin pin (only regulated 5 V)
Power
Connector
Vin pin
Interaction with the World: 12
Arduino senses and arms: IO pins
Digital pins
I/O pins are the interfaces between the microcontroller
and the external world, these can be:
• Input
• Output
• Analogic
• Digital
• PWM
• General Purpose
Analog pins
Interaction with the World: 13
Arduino senses and arms: IO pins
Digital pins
The inputs are the way in which the microcontroller
acquire information and output are used by the
microcontroller to “act” on the external environment
As human we use light and light sensors (eyes) to
acquire most of the information.
The microcontroller uses voltage and voltage meter
Analog pins
Interaction with the World: 14
Arduino senses and arms: IO pins
Voltage can vary continuously, from 0 to 5 Volts, to give
an idea of a continuous value (like the luminosity of a
light source). This is called Analogic value
Or voltage can be set in only two “levels”, High and
Low, 5 Volts and 0 Volts, to say On or Off (there is light,
there’s not light). This is called Digital value
Interaction with the World: 15
Arduino senses and arms: IO pins
Digital IO pins
Digital IO PWM pins
Most Arduinos have a set of analog inputs to measure sensors
and a set of digital inputs that can work also as output, I/O.
Some digital output can also perform a Pulse-Width Modulation
or PWM. This technique simulates an Analog Output.
Pins on most advanced microcontrollers can be used as analog
or digital and are called General Purpose I/O, or GPIO
Analog input pins
Interaction with the World: 16
IO pins recap
Digital IO pins
Digital IO PWM pins
• Analog pins are usually only inputs and can measure, or
convert, a continuous analog signal into a discrete value in a
specific range
• Digital pins usually can be inputs and output. They can
measure if a value is 0 or 5V or they can put out a value of 0
or 5V.
• Analog output are really uncommon and requires parcular
devices. To simulate an analog output we can use a PWM
output.
Analog input pins
Interaction with the World: 17
IO pins recap
Digital IO pins
Digital IO PWM pins
Analog input pins
Interaction with the World: 18
Using pins: connecting devices
• LED =)
• A lot of different types of sensors (Temperature, Light)
• Controllers (Buttons, Analog stick, Touch)
• Motors (DC motors, Steppers, Servo)
• Relays (Microcontroller controlled switches)
• Screens (LCD, OLED)
• Speakers (Not so simple)
• Memory (SD card)
And we can use them automatically
Interaction with the World: 19
Analog sensors
• Temperature
Sensors can be analogic or digital.
• Humidity
• Pressure Analogic means that the sensor will produce a voltage “proportional” to the
• Light value that you are measuring with the sensor.
• Strain
• Weight This means that you have to measure the voltage with an analog pin and
• Accelerometer calculate the proportion to have the value of the temperature.
• Movement Choosing the relation between voltage and temperature is called calibration.
• Distance
• Presence 0V 2.5 V 5V
• Sound (microphone)
• …… 50° C
0° C 100° C
Interaction with the World: 20
Digital sensors
• Temperature
• Humidity Digital sensors have internally another small controller that measure
• Pressure and do all the math required to have the result, but you need to
connect/collect the controller with to the Arduino through a digital.
• Light
• Strain
The values are encoded in a series of 0 and 1 in a particular format that
• Weight you need to decode. This is a communication protocol
• Accelerometer (More on this later)
• Movement
• Distance
• Presence
• Sound (microphone)
• ……
Interaction with the World: 21
LEDs
LED are really simple to be used with Arduino.
To light up a LED you need to:
• Connect the negative wire of the LED to GND on the Arduino
• Connect the positive wire of the LED a digital pin with a
resistor in series (to limit the current flowing into the LED,
usually around 300Ω)
Write the code to use that pin as a digital output and set it to 5V
Tips: if it not working probably you need to invert the LED wires=)
Interaction with the World: 22
Relays
Relays are very useful to control other devices and
more powerful lamps, some motors, or any other
device that can be used with a switch.
Also in this case you can control it directly with a
digital pin
Interaction with the World: 23
Motors
The most common motors to be connected to Arduino are:
• Servomotor: it can do precise repeatable movements and, in case of
small servos, can be powered and used directly with an Arduino
• DC motors: it’s a very fast and powerful motor, but no precise small
movement. It required additional controller circuit (MOSFET, relay,…)
• Stepper motors: powerful, precise and repeatable but it requires
another driver circuit that can be controlled by an Arduino
Arduino Programming: 24
Connecting Arduino to PC: Arduino IDE
Important part of the Arduino ecosystem:
Arduino Integrated Development Environment (IDE).
This is a PC program can:
• Connect to Arduino (simple driver managing)
• Editor where to write your code
• Compile the code for the specific model of Arduino
• Upload the code in the microcontroller
• Monitor in real-time the Arduino Serial Port
• Full list of code examples
You can download it for free from the Arduino webpage
Arduino Programming: 25
Connecting Arduino to PC: Arduino IDE
• Managing driver and compilers for all Arduino
(and not only models)
• Library manager
• Serial Port Monitor
• Editor
• Compiler Output
Arduino Programming: 26
Arduino IDE
Arduino IDE from my PC:
• Adding a new board
• Adding a new library
• Opening a code example
• Compiling
Arduino Programming: 27
Arduino language
Arduino IDE supports a C++ with simplified syntax as programming language
C++
But it has a lot of other functions and variables that are not standard C++, why?
Arduino Programming: 28
Arduino language
Because you have to control pins and other stuff in real-time….
First things to be noted:
• void setup()
This is the first part of the code that the
microprocessor will run only one time when it
is switched on. Usually it runs the initialization
of hardware and objects.
• void loop(pin, value)
This loop contains all the instructions that will
run continuously indefinitely until the
microprocessor will be powered off
Arduino Programming: 29
Arduino language
Because you have to control pins and other stuff in real-time….
There are of course some strange functions:
• pinMode(pin, value)
Used to set one of the Arduino pins in input or
output
• digitalWrite(pin, value)
Used to change the status of the Arduino pin
from HIGH to LOW and vice versa
• delay(value)
Used to wait some time before next instruction
Arduino Programming: 30
Arduino language
Other common Arduino functions are:
• analogRead(pin)
Returns a value proportional to the voltage
applied to the pin
• digitalRead(pin)
Returns 1 or 0 if the voltage applied to the pic
in 5V or 0V
• [Link](baudrate)
Open a Serial COM Port
• [Link](string)
Write on Serial COM Port
• [Link]()
Read on Serial COM Port
Arduino Programming: 31
Arduino language
For example this code:
• In the setup initialize a digital pin to be an
output
• In the main loop:
1. Set the pin to HIGH (5V)
2. Wait 1 second
3. Set the pin to LOW (0V)
4. Wait 1 second
5. Restart forever
This code blink a LED=)
Arduino Programming: 32
Arduino language
For example this code:
• In the setup initialize a Serial COM Port
• In the main loop:
1. Read the value from the Analog Pin
2. Map the value (Calibration)
3. Write the results in the Serial
4. Wait 2 millisecond
5. Restart forever
This is perfect to read an analog
temperature sensor
Arduino Programming: 33
Libraries
I mentioned drivers for motors and connection protocols for devices or sensors.
Of course these requires a specific code to be run.
A communication protocol is a standard set of rules that
describes how two circuits speaks each other.
Common Arduino examples are I2C, SPI, UART, OneWire
Luckily there are library already prepared for our devices.
Arduino Programming: 34
Libraries
Most of the library can be downloaded directly from
the Arduino IDE using the specific tool and searching
the sensor or driver that is required.
For most of the library there are also well commented
example on how to use the various functions.
Arduino Programming: 35
WiFi and Bluetooth
Some Arduinos can be connected via WiFi or Bluetooth.
This can add a lot of features as remote control, remote
data logging, etc
Arduino Programming: 36
New features from Arduino
Arduino App
• Arduino App provides a user-friendly
interface for controlling and monitoring
Arduino projects
• On the App can be added features like data
visualization, remote control, and real-time
feedback
• Develop apps for iOS, Android, or web
platforms to reach a wider audience
Arduino Programming: 37
New features from Arduino
Arduino Cloud
• Simplified IoT Development: user-friendly
platform for creating and managing IoT
projects.
• Remote Monitoring and Control: Access and
control your Arduino devices from remote.
• Data Collection and Analysis
• Integration with Other Services (Google
Assistant, Amazon Alexa)
• Over-the-Air Updates of firmware
Arduino Family and other microcontroller 38
Arduino UNO specifications
Microcontroller: ATmega328P
Digital Pin IO: 14
Analog Pin I: 6
PWM Pin: 6
Working voltage: 5V
Max current on Digital Pin: 20 mA
Flash Memory: 32KB
SRAM Memory: 2KB
EEPROM Memory: 1KB
Clock: 16 MHz
Led built-in: D13
Native USB: no
ADC: 1x 10bit
DAC: no
Time/Counter: 2x 8bit, 1x 16bit
Arduino Family and other microcontroller 39
Arduino UNO specifications
Microcontroller: ATmega328P
Digital Pin IO: 14
Analog Pin I: 6
PWM Pin: 6
Working voltage: 5V
Max current on Digital Pin: 20 mA
Flash Memory: 32KB
SRAM Memory: 2KB
EEPROM Memory: 1KB
Clock: 16 MHz
Led built-in: D13
Native USB: no
ADC: 1x 10bit
DAC: no A lot of data!
Time/Counter: 2x 8bit, 1x 16bit
Do we need all of them?
It depends on our project!
Arduino Family and other microcontroller 40
Arduino UNO specifications
Microcontroller: ATmega328P
Digital Pin IO: 14
Analog Pin I: 6 Most common criteria
PWM Pin: 6
Working voltage: 5V
Max current on Digital Pin: 20 mA
Flash Memory: 32KB
SRAM Memory: 2KB
EEPROM Memory: 1KB
Clock: 16 MHz
Led built-in: D13
Native USB: no
ADC: 1x 10bit
DAC: no
Time/Counter: 2x 8bit, 1x 16bit
What if these IO pins are not enough?
Arduino Family and other microcontroller 41
Arduino Big Family
Uno
Leonardo
Mega
Fio
Nano
LilyPad
Yun
101
Nano 33 BLE
Nano Every
Micro
MKR1000
…and many others!
The list is very long
What are your project requirements?
Arduino Family and other microcontroller 42
Arduino Big Family
An example:
UNO has 13 digital pins
More pins?
Leonardo has
20 digital IO pins
More Pins?
Mega has 54 Digital IO
pins!
Arduino Family and other microcontroller 43
Arduino Big Family
Another example:
UNO can be connected to
the PC only via USB (serial
port)
Other connection?
Ethernet!
Cannot run the cable?
Uno WIFI!
Arduino Family and other microcontroller 44
More features? Arduino shields!
Try a shield!
Stackable electronic board that can
be connected on the Arduino
board!
Different versions for different
requirements:
Screens
Controllers
Motor drivers
Sensors
Connections
Arduino Family and other microcontroller 45
This was the basic Arduino
The Arduino that I showed you are the basics…
They are our allies in our small project.
What if I need something more powerful?
Arduino Family and other microcontroller 46
Arduino MKR
Small Arduino board with more advanced
micro than the other
Designed for IOT
Different sensors or receiver (GPS)
They have some type of wireless
connection
Arduino Family and other microcontroller 47
Arduino Portenta
Powerful SOC (system on chip)
It is like a tiny computer (it also runs Linux)
You can run AI stuff and other programs that require more resources
Developed for fast industrial R&D
Arduino Family and other microcontroller 48
Arduino Clones
Arduino hardware is mostly opensource/openhardware,
It can be made by everyone!
Arduino Family and other microcontroller 49
Arduino Competitors
Nowadays there are a lot of competitors
with different spec.
The most common is the ESP32
Arduino Family and other microcontroller 50
ESP32
It is a SOC microcontroller very common,
with a very large community and very
cheap (10$).
It has:
X32 core (some version dual)
WiFi
Bluetooth
Up to 34 GPIO
Arduino Family and other microcontroller 51
To be mentioned: TTGO
Based on ESP32, it is a series of board
which comprehend different versions with
different setup:
Built in OLEO screen
Power circuit
LoRa chips
GPS receiver
SD card reader
Arduino Family and other microcontroller 52
I heard about Raspberry Pi
It is another known brand, they produce two main
categories of of board:
Raspberry Pi Pico – RP2040
Raspberry Pi 3 – 4 – 5 - Zero
Arduino Family and other microcontroller 53
Raspberry PI 3B-4-5-Zero
These are like real computer, they run Linux and are
useful for an incredible number of applications, like:
Low cost desktop computer
Video/multimedia player
Small server
….
I use them to collect data for tracking planes=)
Arduino Family and other microcontroller 54
Raspberry Pi Pico
It is like an Arduino,
in some way similar to a MKR
Arduino Family and other microcontroller 55
Other Arduino programming platforms
The three most used platforms for programming:
• Arduino IDE
• Arduino Web Editor
• PlatformIO
56
Arduino Family and other microcontroller
Micropython
There are different boards which support also
micropython and the code is uploaded to the
board via a simple file copy/paste. Then, it is
interpreted by the firmware running on the
microprocessor
57
Some Arduino projects
Robotic arm
3D printer
Watering system
Weather station
Air pollution monitor
Robots
…..
58
Some other projects
LoRa satellite receiver ([Link])
Weather balloons receiver ([Link])
Cloud Chamber =)
59
Some other side
Wokwi – Arduino code/project tester
60
Questions?
61
Thank you for your attention