Sie sind auf Seite 1von 63

Lecture 2: Arduino microcontroller,

software and interfacing

1
Objectives
• In this lecturer, we will introduce
– the computer architecture of Arduino system
– Peripheral interfacing
– Programming tool of Arduino

2
History of Arduino system
• It was invented by Massimo Banzi and his team
through the extension of a student project in
2005
• It aims at providing a low cost (~HKD 150) and
easy way for non electronic engineer to create
applicational computer systems
• It is an open source hardware and software
(GNU General Public License)
• It adopts the RSIC architecture

3
Arduino family
Operating/
Analog Digital EEPROM SRAM
Name Processor Input CPU Speed Flash [kB] USB
In/Out IO/PWM [kB] [kB] UART
Voltage

ATmega25 5 V / 7-12
Mega 2560 16 MHz 16/0 54/15 4 8 256 Regular
60 V 4

ATmega32 5 V / 7-12
Leonardo 16 MHz 12/0 20/7 1 2.5 32 Micro
U4 V 1

ATmega32 5 V / 7-12
Uno 16 MHz 6/0 14/6 1 2 32 Regular
8P V 1

ATmega16
8 5 V / 7-9 0.512 1 16
Nano 16 MHz 8/0 14/6 Mini
ATmega32 V 1 2 32 1
8P

4
Arduino UNO
• Equipped with ATmega328p
• 14 digital I/O pins (6 PWM outputs, 6 analog
inputs)
• Run at 16 MHz
• USB connection
• Power jack
• In Circuit Serial
Programming (ICSP)
header
• Reset button
5
Arduino technical specifications (1)

Microcontroller ATmega328P
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V
14 (of which 6 provide PWM
Digital I/O Pins
output)
PWM Digital I/O Pins 6
Analog Input Pins 6
DC Current per I/O Pin 20 mA
DC Current for 3.3V Pin 50 mA

6
Arduino technical specifications (2)

32 KB (ATmega328P) of which
Flash Memory
0.5 KB used by bootloader
SRAM 2 KB (ATmega328P)
EEPROM 1 KB (ATmega328P)
Clock Speed 16 MHz
LED_BUILTIN 13
Length 68.6 mm
Width 53.4 mm
Weight 25 g

7
ATmega328p architecture
• ATmaga328 is a low power CMOS 8 bit
microcontroller based on enhanced Reduced
Instruction Set Computer(RISC) architecture
• It uses Harvard architecture which separates the
memories and buses for program and data in
order to maximize performance and parallelism
• It employs a single level pipelining
– While one instruction is being executed, the next
instruction is pre-fetched from the program memory

8
ATmega328p architecture

9
The basic working of CPU of
ATmega328p
1. The data is uploaded in serial via the port. The
data is decoded and then the instructions are sent
to instruction register and it decodes the
instructions on the same clock pulse.
2. On the next clock pulse the next set of instructions
are loaded in instruction register.
3. In general purpose registers the registers are of
8-bit but there are 3 16-bit registers also.
a. 8-bit registers are used to store data for normal
calculations and results.
b. 16-bit registers are used to store data of timer counter in
2 different register. Eg. X-low & X-high. They are fast, and
are used to store specific hardware functions.

10
The basic working of CPU of
ATmega328
4. EEPROM stores data permanently even if the
power is cut out. Programming inside EEPROM is
slow. It can store the Boot loader.
5. Interrupt Unit checks whether there is an interrupt
for the execution of instruction to be executed in
ISR (Interrupt Service Routine).
6. Serial Peripheral Interface (SPI) is an interface
bus commonly used to send data between
microcontrollers and small peripherals such as
Camera, Display, SD cards, etc. It uses separate
clock and data lines, along with a select line to
choose the device you wish to talk to.

11
The basic working of CPU of
ATmega328
7. Watchdog timer is used to detect and recover
from MCU malfunctioning.
8. Analog comparator compares the input values
on the positive and negative pin, when the
value of positive pin is higher the output is set.
9. Status and control is used to control the flow
of execution of commands by checking other
blocks inside the CPU at regular intervals.

12
The basic working of CPU of
ATmega328
10.ALU (Arithmetic and Logical unit)The high
performance AVR ALU operates in direct
connection with all the 32 general purpose
working registers. Within a single clock cycle,
arithmetic operations between general purpose
registers are executed. The ALU operations are
divided into 3 main categories – arithmetic,
logical and bit-function.

13
The basic working of CPU of
ATmega328
11.I/O pins The digital inputs and outputs (digital
I/O) on the Arduino are what allow you to
connect the Arduino sensors, actuators, and
other ICs, such as reading switch inputs,
lighting indicators, and controlling relay outputs.

14
ATmega328 memory
• ATmega328 has three types of memory:
– Flash memory: 32KB nonvolatile memory. This is
used for storing application, which explains why you
don't need to upload your application every time you
unplug Arduino from its power source.
– SRAM memory: 2KB volatile memory. This is used
for storing variables used by the application while it's
running.
– EEPROM memory: 1KB nonvolatile memory. This
can be used to store data that must be available even
after the board is powered down and then powered
up again.
15
ATmega328 power and I/O ports
• The MCU accepts
supply voltages from
1.8 to 5.5 V. However,
there are restrictions
on the operating
frequency;
• This MCU has three
ports: PORTC,
PORTB, and PORTD.
All pins of these ports
can be used for
general-purpose digital
I/O or for the alternate
functions 16
17
Pin assignments
• General pin functions
– LED: There is a built-in LED driven by digital pin 13.
When the pin is high value, the LED is on, when the
pin is low, it is off.
– VIN: The input voltage to the Arduino board when it is
using an external power source (as opposed to 5
volts from the USB connection or other regulated
power source). You can supply voltage through this
pin, or, if supplying voltage via the power jack, access
it through this pin.

18
Pin assignments
• General pin functions
– 5V: This pin outputs a regulated 5V from the regulator
on the board. The board can be supplied with power
either from the DC power jack (7 - 20V), the USB
connector (5V), or the VIN pin of the board (7-20V).
Supplying voltage via the 5V or 3.3V pins bypasses
the regulator, and can damage the board.
– 3V3: A 3.3 volt supply generated by the on-board
regulator. Maximum current draw is 50 mA.

19
Pin assignments
• General pin functions
– GND: Ground pins.
– IOREF: This pin on the Arduino board provides the
voltage reference with which the microcontroller
operates. A properly configured shield can read the
IOREF pin voltage and select the appropriate power
source, or enable voltage translators on the outputs to
work with the 5V or 3.3V.
– Reset: Typically used to add a reset button to shields
that block the one on the board.

20
Pin assignments
• Special pin functions
– Each of the 14 digital pins and 6 analog pins on the
UNO can be used as an input or output.
– The Uno has 6 analog inputs, labeled A0 through A5;
each provides 10 bits of resolution which measures
from ground to 5 volts
– Serial / UART: pins 0 (RX) and 1 (TX). Used to
receive (RX) and transmit (TX) TTL serial data. These
pins are connected to the corresponding pins of the
ATmega8U2 USB-to-TTL serial chip.

21
Pin assignments
• Special pin functions
– External interrupts: pins 2 and 3. These pins can be
configured to trigger an interrupt on a low value, a rising or
falling edge, or a change in value.
– PWM (pulse-width modulation): pins 3, 5, 6, 9, 10, and 11.
Can provide 8-bit PWM output.
– SPI (Serial Peripheral Interface): pins 10 (SS), 11 (MOSI),
12 (MISO), and 13 (SCK). These pins support SPI
communication using the SPI library.
– TWI (two-wire interface) / I²C: pin SDA (A4) and pin SCL
(A5). Support TWI communication using the Wire library.
– AREF (analog reference): Reference voltage for the
analog inputs.

22
ADC inputs
• The MCU has six channels—PORTC0 to
PORTC5—with 10-bit resolution A/D converter.
These pins are connected to the analog header
on the Arduino board
– AVCC: The power pin for the A/D unit.
– AREF: The input pin used optionally if you want to
use an external voltage reference for ADC rather than
the internal Vref. You can configure that using an
internal register.

23
UART Peripheral
• A UART (Universal Asynchronous
Receiver/Transmitter) is a serial interface. The
ATmega328 has only one UART module.
• The pins (RX, TX) of the UART are connected to
a USB-to-UART converter circuit and also
connected to pin0 and pin1 in the digital header.
You must avoid using the UART if you’re already
using it to send/receive data over USB.

24
SPI Peripheral
• The SPI (Serial Peripheral Interface) is another serial
interface but it is synchronous. The ATmega328 has only one
SPI module.
• Besides using it as a serial interface, it can also be used to
program the MCU using a standalone programmer. You can
reach the SPI's pins from the header next to the MCU in the
Arduino UNO board or from the digital header as below:
10 –> Slave Select (SS)
11->Master Output Slave Input (MOSI)
12<-Master Input Slave Output (MISO)
13->Serial Clock (SCK)
• Use for data transfer to SD card or LCD.

25
SPI data transmission
1. To begin communication, the bus master
configures the clock, using a frequency
supported by the slave device
2. The master then selects the slave device with a
logic level 0 on the select line
– If a waiting period is required, such as for an analog-
to-digital conversion, the master must wait for at least
that period of time before issuing clock cycles

26
SPI data transmission
3. During each SPI clock cycle, a full-duplex data
transmission occurs. The master sends a bit on
the MOSI line and the slave reads it, while the
slave sends a bit on the MISO line and the
master reads it. This sequence is maintained
even when only one-directional data transfer is
intended

27
SPI clock polarity and phase
• The master must configure the clock polarity and
phase using either leading edge or trailing edge

28
SPI Daisy chain configuration
• The SPI bus can operate
with a single master device
and with one or more slave
devices.
• In a daisy chain configuration,
the first slave output being
connected to the second
slave input, etc.
• The whole chain acts as a
communication shift register
29
Two Wire Interface Peripheral
• Two Wire Interface (TWI) or Inter-Integrated
Circuit (I2C) is a synchronous serial interface
consisting of only two wires, serial data line
(SDA), and a serial clock line (SCL)
• It is appropriate for peripherals where simplicity
and low manufacturing cost are more important
than speed

30
I2C Reference Design
• It has wires for SCL and SDA with 7-bit
addressing for selecting slaves connected to the
bus
• The bus has two nodes
– Master node – node that generates the clock and
initiates communication with slaves
– Slave node – node that receives the clock and
responds when addressed by the master

31
I2C Reference Design
• The bus is a multi-master bus, which means that
any number of master nodes can be present.
Additionally, master and slave roles may be
changed between messages (after a STOP is sent)
• There may be four potential modes of operation for
a given bus device
– Master transmit – master node is sending data to a slave
– master receive – master node is receiving data from a
slave
– slave transmit – slave node is sending data to the master
– slave receive – slave node is receiving data from the
master

32
I2C Operation
1. The master is initially in master transmit mode
by sending a START followed by the 7-bit
address of the slave it wishes to communicate
with, which is finally followed by a single bit
representing whether it wishes to write (0) to or
read (1) from the slave
2. If the slave exists on the bus then it will
respond with an ACK bit (active low for
acknowledged) for that address

33
I2C Operation
3. The master then continues in either transmit or
receive mode (according to the read/write bit it
sent), and the slave continues in the
complementary mode (receive or transmit,
respectively)
4. The address and the data bytes are sent most
significant bit first. The start condition is indicated
by a high-to-low transition of SDA with SCL high;
the stop condition is indicated by a low-to-high
transition of SDA with SCL high. All other
transitions of SDA take place with SCL low

34
I2C Operation
5. If the master wishes to write to the slave, then it
repeatedly sends a byte with the slave sending
an ACK bit. (In this situation, the master is in
master transmit mode, and the slave is in slave
receive mode.)
6. If the master wishes to read from the slave,
then it repeatedly receives a byte from the
slave, the master sending an ACK bit after
every byte except the last one. (In this situation,
the master is in master receive mode, and the
slave is in slave transmit mode.)
35
I2C Operation
7. An I²C transaction may consist of multiple
messages. The master terminates a message
with a STOP condition if this is the end of the
transaction or it may send another START
condition to retain control of the bus for another
message (a "combined format" transaction)

36
I2C Timing diagram
1. Data transfer is initiated with a start condition (S) signaled by
SDA being pulled low while SCL stays high.
2. SCL is pulled low, and SDA sets the first data bit level while
keeping SCL low (during blue bar time).
3. The data are sampled (received) when SCL rises for the first
bit (B1). For a bit to be valid, SDA must not change between
a rising edge of SCL and the subsequent falling edge (the
entire green bar time).
4. This process repeats, SDA transitioning while SCL is low,
and the data being read while SCL is high (B2, ...Bn).
5. The final bit is followed by a clock pulse, during which SDA
is pulled low in preparation for the stop bit.
6. A stop condition (P) is signaled when SCL rises, followed by
SDA rising.

37
Pulse Width Modulation (PWM)
• PWM is a technique for getting analog results with
digital means (averaging the digital signal over a
period)
• Digital control is used to create a square wave (on-
off wave)
– The on-off pattern can simulate voltages in between full on
(5 Volts) and off (0 Volts) by changing the portion of the
time the signal spends on versus the time that the signal
spends off
– The duration of "on time" is called the pulse width. To get
varying analog values, you change, or modulate, that
pulse width
– It can control the brightness of LEDs or speed of motors

38
Pulse Width Modulation (PWM)
• Examples

39
Interrupts in Arduino
• Arduino has three
different sources of
Interrupts:
– Timer Interrupts
• are caused by the Arduino
Timers
– External Interrupts
• are due to external events
i.e. a change in state of any
of the External Interrupt Pins
– Pin-Change Interrupts
• can be enabled when a
change in state of a group of 40
Pins is detected
Sensors and Actuators
• A sensor is a device that detects events or
changes in its states and converts them into
electrical signals
• An actuator is a device that acts upon its
environment through motion or control
• They act as the input devices and output devices
of the Arduino which processes the signals
received from sensors and correspondingly
controls the actuators

41
Environmental sensors

Motion sensors
Wireless modules

User interface modules


Physical sensors
Example of Interfacing LCD to
Arduino

Tool: Fritzing (https://fritzing.org/home/)

45
Arduino programming
• There are some programme languages can be used
to program Arduino
– Drag-and-drop visual programming
• Flags Blockly
• ArduBlock
• Snap4Arduino
– Text based programming
• C/C++
• C#
• Python
• C++ language reference:
https://www.arduino.cc/reference/en/
Arduino Integrated Development
Environment

• Download at Arduino.cc: https://www.arduino.cc/download_handler.php?f=/arduino-


1.8.8-windows.zip
• Unzip the file to an appropriate location
• Run Arduino.exe
Arduino IDE

Menu

Editing area, Arduino code is


referred as sketches

Message area
Arduino libraries
• There are built-in libraries
that provide basic
functionality or import other
libraries to expand the
Arduino board capabilities
• These libraries are roughly
divided into libraries that
interact with a specific
component or those that
implement new functions
• To import a new library, you
need to go to Sketch >
Import Library
49
Arduino program structure
• Its programs consist of two functions: Setup()
and Loop()
• Every Arduino sketch must have a setup function.
This function defines the initial state of the
Arduino upon boot and runs only once.
• Here we’ll define the following:
– Pin functionality
– Initial state of pins
– Initialize classes
– Initialize variables
– Code logic 50
Arduino program structure
• The loop function is also a must for every
Arduino sketch and executes once setup() is
complete. It is the main function and as its name
hints, it runs in a loop over and over again. The
loop describes the main logic of your circuit.

51
How to program Arduino
• The basic Arduino code logic is an “if-then”
structure and can be divided into 4 blocks:
– Setup - will usually be written in the setup section of
the Arduino code, and performs things that need to be
done only once, such as sensor calibration.
– Input - at the beginning of the loop, read the inputs.
These values will be used as conditions (“if”).
– Manipulate Data - this section is used to transform
the data into a more convenient form or perform
calculations.
– Output - this section defines the final outcome of the
logic (“then”) according to the data calculated in the 52
previous step.
From Software to Hardware
• Connecting an Arduino board to computer
through USB cable
– Arduino IDE setting, you need to designate which
Arduino board you’re going to be using. Do this by
click Tools > Board > Your Board
– Choose your processor: Under tools > processor >
select the model you have
– Choose your port: go to tools > Port > COMX Arduino
• Program building process:
– Code → Compile → Upload → Run

53
Arduino debugging
• Arduino debugging is sometimes difficult as it
directly interact with some physical devices and
it does not contain any onboard debugger.
• The failures may come from:
– Hardware fault
– Software fault
Hardware fault
• It may appear when the system is first build or after
software modification.
• When a system does not work, the hardware may
be firstly checked
– Checking wires
– Checking soldering
– Checking power supply
– Checking device
• If a device is considered to be malfunctioned, try to
replace it and test again.
• Multimeter, CRO or the other measurement tools are
used.
Software fault: syntax error

• It may appear when


the system is first
build or after
software
modification.
• The Arduino IDE
can highlight the
error places so that
the errors can be
fixed easily.
Software fault: logical error
• It may appear when some specific event occurs.
• Unfortunately, the Arduino IDE does not support the debugger.
– So, serial print function is used. At the point the it is
suspected a logical error, the serial print function is used to
dump the data for checking.
• E.g. Serial.print(x); //the value of x will be displayed in the message area.
– Or using the Scaffold code.
• E.g. #define DEBUG 100
void setup { … #ifdef DEBUG
Serial.begin(9600);
#endif …}
void loop { … #ifdef DEBUG
Serial.print(x);
#endif …}
• After debugging is completed, the statement #define DEBUG 100 can be
commented out.
Debugging using serial monitor
• The serial monitor is used mainly for interacting
with the Arduino board using the computer, and
is a great tool for real-time monitoring and
debugging
– In order to use the monitor, you’ll need to use the
Serial class

58
Debugging using serial plotter
• Arduino serial plotter is another component of
the Arduino IDE, which allows you to generate a
real-time graph of your serial data

59
External debugging tools
• Visual Micro (https://www.visualmicro.com/)
• Atmel Studio
(https://www.microchip.com/mplab/avr-
support/atmel-studio-7)
• DebugWIRE (https://hackaday.io/project/20629-
debugwire-debugger)
Arduino simulators and emulators

• They emulate the functions of • AutoCAD 123D


the Arduino system
– They are used to test the
• LTSpice Arduino Simulator
software before downloading the
codes to the system.
• PSpice for Arduino
• PaulWare’s Arduino Simulator • Circuit Lab Arduino Simulator
• SimDuino • EasyEDA simulator
• ArduinoSim
• Arduino Simulator for PC
• Circuits-cloud Simulator
• Emulare Arduino Simulator • Systemvision Simulator
• Simulator for Arduino • Proteus by Labcenter
• Yenka for Arduino
• SimulIDE
SimulIDE
(https://sourceforge.net/projects/simulide/ )
Summary
• In this lecture, the Arduino system are
introduced
– Arduino system architecture
– Interfacing techniques
– Arduino programming tools
• In next lecture, assembly programming of
Arduino system will be discussed

END
63

Das könnte Ihnen auch gefallen