Introduction to Microcontrollers: Arduino Tutorial
Vlad−Mihai PL𝐀𝐂𝐈𝐍𝐓𝐀𝟏,𝟐
1. Horia Hulubei National Institute for R&D in Physics and Nuclear Engineering
2. University POLITEHNICA of Bucharest
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html |
Introduction to Microcontrollers: Arduino Tutorial
OUTLINE
➢ Introduction
➢ Microcontrollers Architecture
➢ Arduino environment
➢ Arduino boards
➢ Sensors interface
➢ Application control
➢ Arduino UNO board
➢ Coding example
➢ Applications
➢ Bibliography
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 1
Introduction to Microcontrollers: Arduino Tutorial
Introduction
What is a microcontroller?
❖ Small computers integrated in a single chip:
▪ CPU, RAM, EEPROM and other
Peripherals in the same package.
❖ Excellent for embedded applications;
❖ Low cost and low power consumption;
(ATMEGA328P ~ 1.5 $)
❖ Can work up to tens of MHz as clock frequency;
▪ Can work with low frequency also, few MHz.
❖ Standalone devices;
▪ most of them only need power and a clock
source to run.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 2
Introduction to Microcontrollers: Arduino Tutorial
Introduction
What are they used for?
❖ Data control;
❖ Data Acquisition Systems;
❖ Power control and monitoring;
❖ Motor control applications;
❖ Smartphones;
❖ Automotive industry;
❖ Nuclear instrumentation; https://atmelcorporation.wordpress.com/tag/avr-
❖ Space applications. microcontrollers/
o In general are used in applications where high
processing resources are not required, hence a general
purpose microcontroller is considered suitable due the
low power consumption and without any peripheral and
external memories;
o Also, the cost may be an important factor.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 3
Introduction to Microcontrollers: Arduino Tutorial
Introduction
Famous microcontroller manufacturers:
➢ Microchip;
➢ Atmel (now is part of Microchip);
➢ Intel;
➢ Analog Devices;
➢ MAXIM Integrated;
➢ Renesas Electronics.
A detailed list can be found at:
http://embedeo.org/microcontroller_manufacturers/
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 4
Introduction to Microcontrollers: Arduino Tutorial
Microcontrollers Architecture
I/O PERIPHERALS RAM COMMUNICATION
A/D
INTERFACES
MEMORY
CONVERTER SPI
USART
UART
EEPROM
I/O PINS SPI
CPU I2C
D/A
CONVERTER USB
PWM CLOCK WI-FI
WATCHDOG
BLOCKS SOURCE TIMER
Simplified Architecture
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 5
Introduction to Microcontrollers: Arduino Tutorial
Arduino environment
➢ Open source-source electronics development
board, based on easy-to-use hardware and
software interface;
➢ Very easy to use and its has a broad range of
libraries and example codes;
➢ Arduino language is merely C/C++;
➢ It has embedded an U(S)ART monitor in order to
check the communication with microcontroller;
➢ Using Processing software (embedded in the
latest Arduino compiler version) data can be
displayed, GUIs can be made;
➢ More at: https://www.arduino.cc/ .
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 6
Introduction to Microcontrollers: Arduino Tutorial
Arduino Boards
Arduino UNO Arduino Mega 2560 Arduino Due
Arduino BT Arduino LilyPad Arduino NANO
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 7
Introduction to Microcontrollers: Arduino Tutorial
Arduino UNO Board
Hardware SPI block: Digital I/O PINS
Direct connection to
13=> SCLK Pulse Width Modulation
the hardware UART
12=> MISO (PWM) included (~)
block
11=> MOSI
10=> SS In circuit Serial
programming
USB Port
ATMEGA328P
USB-UART bridge Microcontroller
Input voltage Hardware I2C
(7-12 V recommended) A4 => SDA
A5 => SCL
16 MHz quartz oscillator
Power Supply Distribution Analog inputs
(can be used to power up peripherals) 10-bit within the range 0 – Vref (max. 5 V)
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 8
Introduction to Microcontrollers: Arduino Tutorial
Sensor interface
Analog or digital?
➢ Sensor: an analog/digital device which converts the physical quantity in to an
electrical signal;
➢ All related physical quantities are related to ANALOG interface, where the
quantity can take any value between the hardware minimum and maximum
values;
➢ DIGITAL interface is used to describe the quantity which has only 2 levels, 1 or 0
max logic.
Sine waves are analog,
because the amplitude can
vary between minimum and a
min maximum value
1
Square waves are digital,
because the amplitude can
vary only between 2 values, 0 0
or 1 logic
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 9
Introduction to Microcontrollers: Arduino Tutorial
Sensor interface
Classification
AIR QUALITY LIGHT TEMPERATURE
SENSORS SENSORS SENSORS
HEALTH CARE HUMIDITY
SENSORS SENSORS
PROXIMITY BAROMETRIC
SENSORS SENSORS
ACCOUSTIC VOLTAGE & MEMS
SENSORS CURRENT SENSORS
SENSORS
MEMS => Microelectromechanical systems.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 10
Introduction to Microcontrollers: Arduino Tutorial
Application control
COMPLEX DAQ RELAYS
SYSTEMS ROBOTS
SMART HOUSE TRANSISTORS,
DRONS & UAV
(UNMANNED
AUTOVEHICLES MOTORS AERIAL
VEHICLE)
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 11
Introduction to Microcontrollers: Arduino Tutorial
Arduino UNO Board
❖ 8-bit RISC architecture microcontroller;
❖ 32 KB In-System Self-Programmable Flash program
memory;
❖ 1 KB EEPROM, 2 KB SRAM ;
❖ 6 PWM channels;
❖ 8 x 10-bit resolution ADC inputs; ATMEGA328P
❖ UART, SPI, I2C; Microcontroller
❖ On chip analog comparator;
❖ 3 Timers: 2 x 8-bit and 1 x 16-bit;
❖ Up to 23 Programmable I/O lines;
❖ Write/erase cycles: Up to 10.000 for Flash/ 100.000
for EEPROM;
❖ Speed Grade: 0 - 4 MHz @ 1.8 - 5.5 V, 0 – 10 MHz @ Full datasheet
2.7 – 5.5 V, 0 – 20 MHz @ 4.5 – 5.5 V;
❖ Power consumption: 0.2 mA in Active Mode @ 1
MHz, 1.8 V and 25 °C.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 12
Introduction to Microcontrollers: Arduino Tutorial
Arduino UNO Board
ATMEGA328P
Architecture
Full datasheet
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 13
Introduction to Microcontrollers: Arduino Tutorial
General purpose Input/Output (GPIO)
➢ Are the interface of logic
software with external DDR
hardware; D Registers
➢ Can be programmable as A
Input or Output; T
➢ Can read or write digital PORT
A
signals; Registers
➢ logic 0 = 0 V and logic 1 = Vcc; B Pin
➢ Are controller by 3 registers: U
❖ DDR (Data Direction PIN Registers
S
Register);
❖ PORT (load data when
the pin is set as output);
❖ PIN (load data when the
pin is set as input).
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 14
Introduction to Microcontrollers: Arduino Tutorial
Timers / Counters
➢ Specific internal registers that increment data
and be triggered by:
❖ A clock source : Timer;
❖ An external event : Counter.
Utilities:
❖ Time domain measurements;
❖ Create PWM waveforms.
HC- SR04 ultrasonic distance sensor, it
returns the distance measurement
based on echo time of an ultrasonic
pulse.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 15
Introduction to Microcontrollers: Arduino Tutorial
Interrupts
Main loop {}
➢ Are used to break a routine of an program or
entire program flow in order to handle a instruction 1
specific event. instruction 2
Trigger Interrupt
instruction 3 ISR {}
➢ Are triggered by:
❖ Input pin state change (rise/fall/toggle); instruction 4 instruction
❖ Serial communication (USART, SPI, I2C); instruction 5
❖ ADC state registers;
instruction 6
❖ Analog comparator;
❖ Timers or Counters.
ISR => Interrupt Service Routine
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 16
Introduction to Microcontrollers: Arduino Tutorial
Internal Analog Comparator
V1 + OUTPUT ➢ ANI0 => V1;
V2 - ➢ ANI1 => V2;
➢ Its output value is located in the ACSR register,
named ACO bit;
➢ V2 can be tied to the output of the ADC
1 for V1 > V2 multiplixer, hence can be tied to any of the
OUTPUT
0 for V1 < V2 analog inputs;
➢ As a general information is good to know that
any comparator is an 1-bit ADC;
Utilities:
➢ Compare 2 analog voltages;
➢ Trigger a Timer/Counter1
Input Capture function; ACO => Analog Comparator Output;
➢ Trigger an Interrupt (rise, fall, ACSR => Analog Comparator and Status Register.
toggle).
More information at page 234
|
[email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 17
Introduction to Microcontrollers: Arduino Tutorial
Analog to Digital Converter
(ADC)
➢ 10-bit resolution => 0 – Vref => 0-1023;
➢ Successive-approximation (SAR)
architecture;
➢ 1 ADC with up to 8 multiplexed input
channels => they share the sampling
rate;
➢ one channel is dedicated for internal
temperature monitoring;
➢ Vref can be:
❖ Vcc from power source; (not very
recomended because the power supply
can be noisy)
More about ADC ❖ 1.1 V internal voltage reference;
❖ External, from an external voltage
reference.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 18
Introduction to Microcontrollers: Arduino Tutorial
Pulse width modulation
(PWM)
➢ A square signal can be generated with a
variable duty cycle;
➢ ATMEGA328P has six 8-bit PWM channels;
➢ Can be used for:
❖ To control DC-DC converters;
❖ To control the speed for motors;
❖ To control the luminosity of Leds or
lamps;
❖ To generate an analog voltage direct
proportional with duty cycle value. T
Ton Toff
VCC
𝐓𝐨𝐧 OFF ON OFF ON OFF
Duty cycle = 𝐓
0V
25 % Duty Cycle
|
[email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 19
Introduction to Microcontrollers: Arduino Tutorial
Pulse width modulation
(PWM)
➢ PWM can be used to generate a stable
DC signal if its passed to a low pass
filter, RC passive integrator;
➢ An 8 bit PWM signal will emulate an
8-bit DAC (Digital to Analog
Converter).
Source Read more
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 20
Introduction to Microcontrollers: Arduino Tutorial
Communication interfaces
~USART~
➢ Stands for Universal Synchronous/Asynchronous
Receiver/Transmitter;(one to one communication)
➢ Most common is used UART => Universal
Asynchronous Receiver/Transmitter; UART2
UART1
➢ The data transfer speed is defined as baud rate (bit
rate);
➢ Normally is defines as: TX RX
❖ One start bit;
❖ 8 data bits; RX TX
❖ 1 stop bit.
More at:
http://www.circuitbasics.com/basics-uart-communication/
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 21
Introduction to Microcontrollers: Arduino Tutorial
Communication interfaces
~SPI~
MASTER SLAVE1
➢ Stands for Serial Peripheral Interface, and its
SCLK SCLK describes a serial communication used in
embedded systems;
MOSI MOSI
➢ Support multiple slaves communications, but
MISO MISO only one at time;
➢ Can perform full duplex;
SS1 SS1
➢ One to many communication.
SS2 SLAVE2
SCLK SCLK => Serial Clock;
MOSI => Master Output Slave Input;
MOSI MISO => Master Input Slave Output;
MISO SS => Select Slave.
SS2
Read more about SPI
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 22
Introduction to Microcontrollers: Arduino Tutorial
Communication interfaces
~I2C~
➢ Stands for Inter-Integrated Circuit, also know as Two Wire Interface (TWI);
➢ Serial protocol which allows multiples masters and slaves on the same bus, up to
128; (many to many communication)
➢ Normally each device has an unique 7-bit or 10-bit address in the I2C bus;
➢ Being used in variety of digital sensors, can reach speeds up to 400 kbps. (Arduino
UNO)
SCL
SDA
Master1 Master2 Slave1 Slave2 Slave3 Slave4
SCL=> Serial Clock Line;
SDA=>Serial Data Line. Read more about I2C
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 23
Introduction to Microcontrollers: Arduino Tutorial
Coding example
~blinking a led~
➢ First of all, the latest version of
Arduino compiler must be installed
from: https://www.arduino.cc/ ;
➢ Run the compiler and go to
File=>Examples=>Basics=>Blink ;
➢ A new windows with the related
software should appear on your
screen;
➢ Compile the code and upload it on the
board; (The board new to be
connected to the PC already)
➢ Change the delay value, and observe
what happen.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 24
Introduction to Microcontrollers: Arduino Tutorial
Coding example
~blinking a led~
Simulation was made with
the online simulator available
at: https://circuits.io
Delay value = 1000 ms
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 25
Introduction to Microcontrollers: Arduino Tutorial
Coding example
~blinking a led~
Simulation was made with
the online simulator available
at: https://circuits.io
Delay value = 250 ms
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 26
Introduction to Microcontrollers: Arduino Tutorial
Applications
➢ Temperature & Humidity sensor readout;
➢ Proximity sensors readout => ultrasonic radar;
➢ Leds control;
➢ Light sensor application;
➢ Wearable multi-sensor data acquisition system.
❖ Readout will be made with the Arduino UNO board and data will be send via
UART or Bluetooth to an LabVIEW GUI (Graphical User Interface).
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 27
Introduction to Microcontrollers: Arduino Tutorial
Temperature & Humidity Sensor Readout
➢ Based on low cost DHT11 sensor with 1 wire interface;
➢ Sampling rate need to be lower than 1 Hz (once every
second);
➢ Data is acquired and displayed with a LabVIEW GUI over
UART interface;
More info
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 28
Introduction to Microcontrollers: Arduino Tutorial
Ultrasonic Radar
➢ Provide good measurement between 2 and 400
cm;
➢ Easy to interface with any microcontroller;
➢ Widely used in robots and system were distance to
a specific object is needed to be know.
➢ A good reference article Datasheet
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 29
Introduction to Microcontrollers: Arduino Tutorial
Leds Control
➢ Various Leds turned on and off with Arduino UNO
board using commands received over UART from a
LabVIEW GUI;
➢ Their luminosity will be also controller using PWM
technique by calling the analogWrite function;
➢ ASCII commands are used to control the Arduino
board.
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 30
Introduction to Microcontrollers: Arduino Tutorial
Light sensor application
➢ The total luminous flux incident on a surface can be measured using light
sensors, illuminance, expressed in SI unit as lux (lx);
➢ Ambient Light sensor TEMT6000 is used;
➢ TEMT6000 is a NPN phototransistor sensitive to the visible spectrum;
➢ The collector light current feed a 10 KΩ load resistor;
➢ Voltage drop across resistor can be transformed using a formula from
datasheet.
Datasheet
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 31
Introduction to Microcontrollers: Arduino Tutorial
Smart, low power, wearable multi-sensor data acquisition system for
environmental monitoring
➢ Full article at: https://doi.org/10.1109/ATEE.2017.7905059
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 32
Introduction to Microcontrollers: Arduino Tutorial
Bibliography
1. https://www.arduino.cc/
2. https://forum.arduino.cc/
3. http://www.ladyada.net/learn/arduino/
4. https://learn.sparkfun.com/tutorials/tags/arduino?page=all
5. http://forefront.io/a/beginners-guide-to-arduino/
6. https://programmingelectronics.com/arduino-tutorials-all/
7. http://littlebits.cc/tips-tricks/introduction-to-arduino-programming-1-the-basics
8. https://www.tutorialspoint.com/arduino/arduino_overview.htm
| [email protected] | www.nipne.ro/dpp/Collab/LHCb/upgrade.html | 33