Unit 1 Notes
Unit 1 Notes
Best Application Desktop prototyping Low cost, Small High Performance High I/O
& use with Arduino profile, Simple prototyping with requirements with
Shields project superior analog I/O more memory
space
DC Power Jack
Voltage
Regulator USB Port
Reset Button
USB Interface
chip
Crystal
Oscillator Analog Reference
Voltage
Ground
Power Source -
ATmega328 Digital
Pins
Analog pins
Serial
5V ON Indication
in -Circuit Serial
Communication
Programming Header
USB Port
This is a printer USB port used to load a
program from the Arduino IDE onto the Arduino
board. The board can also be powered through
this port.
Power port:
The Arduino board can be powered
through an AC-to-DC adapter or a battery.
The power source can be connected by
plugging in a 2.1mm center-positive plug
into the power jack of the board.
● Flash memory of 32KB. The program loaded from Arduino IDE is stored
here.
● RAM of 2KB
● CPU: It controls everything that goes on within the device. It fetches the
program instructions from flash memory and runs them with the help of
RAM.
● Electrically Erasable Programmable Read Only Memory (EEPROM) of
1KB. This is a type of nonvolatile memory, and it keeps the data even
after device restart and reset.
The Arduino UNO board has 6 analog input pins, labeled “Analog 0
Analog input pins: to 5.” These pins can read the signal from an analog sensor like a
temperature sensor and convert it into a digital value so that the
system understands. These pins just measure voltage and not the
current because they have very high internal resistance. Hence,
only a small amount of current flows through these pins.
Although these pins are labeled analog and are analog input by
default, these pins can also be used for digital input or output.
Digital Pins You can find these pins labeled “Digital 0 to 13.” These pins can be
used as either input or output pins.
When used as output: pins act as a power supply source for the
components connected to them. (supply 4 mA of current at 5 V)
When used as input : they read the signals from the component
connected to them.
Some of the digital pins are labeled with
tilde (~) symbol next to the pin numbers (pin
numbers 3, 5, 6, 9, 10, and 11). These pins
act as normal digital pins but can also be
used for Pulse-Width Modulation (PWM),
which simulates analog output like fading an
LED in and out.
Arduino Board
Programming
What is Arduino
• Open source electronics platform
– Easy to use hardware & software
• Equipped with sets of analog & digital I/Os
• Can be interfaced to extension boards ( shields)
• Features USB based communication interface
• Provided with Integrated Development
Environment
• Aimed to provide low-cost and easy way for
students & professionals
• GNU General Public License
Arduino Board
Hardware Block Diagram
Digital
SPI
I/O
USB
Interface UART
POWER
+5 Volts Clock
+3.3 Reset
Volts
Arduino Uno Pinout
Arduino IDE
• Allows to write program and upload them into the board
• Online Web Editor
• Arduino Desktop IDE
Sketches
• Program written using Arduino Software
• Written in text editor & save with extension of .ino
• Arduino uses this name for a program
• Code which will be uploaded and run on the board
IDE Basic Options
• New
– Creates a new sketch
• Open
– Allows to load the sketch from the system
• Save
– Saves the sketch
• Verify
– Checks the code for errors compiling it
• Upload
– Compiles the code and uploads to configured board
• Serial Monitor
– Opens a serial monitor
• Quit
– Closes all IDE windows
Variables
• a place to store the data
• Has name, value and type
– Example declaration
– int pin = 2;
• Specified only once and can be used many times
• Variable assignment
– pin = 3;
Functions
• Setup
– Called when a sketch starts
– Used to initialize variables, pin modes, start using
libraries
– Will run only once, after each power up / reset of
the board
• Loop
– Called after creating setup function
– Function loops consecutively
– Allows the program to change & respond
– Actively controls the arduino
Digital I/O
• Generic Pin on an IC / Board
• Configured as Input or Output
• Can be enabled / disabled
• Logic Level
– High
– Low
• Input values are readable
• Output values are writable / readable
• Input configured pins also can be used as Interrupt
• No predefined purpose
Digital Pin Functions
• pinMode
– Configures pin as Output or Input
– pinMode(pin, mode)
• pin : pin number
• mode: INPUT, OUTPUT, INPUT_PULLUP
• digitalWrite
– Writes a High or Low value to a digital pin
– digitalWrite(pin, value)
• pin : pin number
• value : HIGH or LOW
Digital Pin Functions…
• digitalRead
– Reads the current value of specified pin
– int digitalRead(pin)
• pin : pin number
• Returns the current value
UART / Serial Port
• Univeral Asynchronuos Receiver / Transmitter
• Supports NRZ encoding format
• 7 or 8 bit data
• 1 or 2 Stop bits
• Hardware Flow Control
– RTS – Request to Send
– CTS – Clear to Send
• Programmable Parity ( even, odd and none )
• RS485, RS232 compatible
UART Interface
TXD
RXD
UART
NXP Serial
IMX6 RTS
Device
CTS
Serial function
• begin
• end
• print
• println
• readBytes
• readString
• write
• setTimeout
• available
• availableForWrite
Inter Integrated Circuit Inteface
• Multi master, multi slave serial interface bus
• Uses only two bi-directional open drain lines
– SDA : Serial Data Line
– SCL : Serial Clock Line
• Since lines are open drain, these pins needs to be pulled high.
– Normally pull up resistor will be 4.7 K / 10K
I2C Model
VCC
R R
SDA
SCL
I2C
I2C I2C I2C
Master
Slave Slave Slave
uC
I2C / Wire Functions
• begin
• receive
• send
• onRequest
• RequestFrom
Serial Peripheral Interface
• Synchronous Serial Communication Interface
• Used for Short distance. Basically with in the
board
• Full duplex mode with master slave architecture
• Also called as four-wire bus
• Pins
– MISO : Master Input Slave Output
– MOSI : Master Output Slave Input
– SCK : Serial Clock
– SS : Slave Select
SPI Model
MOSI
MISO
SPI SPI
Master SCK Slave 1
SS1
SPI
Slave 2
SS2
Architecture ATmega328 and
Ardunio
18ECO108J
Architecture
AVR Architecture
🞂 Clocks and
Power
AVR Architecture
🞂 CPU
🞂 Details
coming
AVR Architecture
🞂 Harvard architecture
🞂 Flash – program
memory
🞂 32K
🞂 SRAM – data
memory
🞂 2K
🞂 EEPROM
🞂 For long-term data
🞂 On I/O data bus
Memory
🞂 Instruction
Fetch and
Decode
AVR C P U
🞂 ALU
Instructions
AVR C P U
🞂 I/O
and
special
functions
AVR Register File
🞂 Program memory –
Flash
🞂 Data memory -
SRAM
Stack Pointer Register
🞂 \
🞂 Status bits set by instructions/Checked by
Branch/Skip instructions
🞂 I – Global interrupt enable
🞂 T – Flag bit
🞂 H – Half carry (BCD arithmetic)
🞂 S – Sign
🞂 V – Overflow
🞂 N – Negative
🞂 Z – Zero
🞂 C – Carry
AVR Architecture
🞂 Three timers
🞂 Very flexible
🞂 Choose clock rate
🞂 Choose “roll-over”
value
🞂 Generate
interrupts
🞂 Generate PWM
signals
🞂 (represent 8-bit value
with using a clock
signal)
Arduino Timing Functions
🞂 delay(ms)
🞂 wait for ms milliseconds before continuing
🞂 delayMicroseconds(us)
🞂 wait for us microseconds before continuing
🞂 unsigned long millis( )
🞂 return number of milliseconds since program
started
🞂 unsigned long micros( )
🞂 return number of microseconds since
program started
🞂 resolution of 4 microseconds
AVR Architecture
🞂 Interface to pins
🞂 Each pin
directly
programmabl
e
🞂 Program
direction
🞂 Program value
🞂 Program pull-
ups
🞂 Some pins are
special
🞂 Analog vs.
Digital
🞂 Clocks
I/O Ports
🞂 Pullups
🞂 If a pin is an input (DDRxi = 0):
🞂 PORTxi = 0 – pin is floating
🞂 PORTxi = 1 – connects a pullup to the pin
Keeps pin from floating if noone driving
Allows wired-OR bus
🞂 Digital pins:
🞂 Pins 0 – 7: PORT D [0:7]
🞂 Pins 8 – 13: PORT B [0:5]
🞂 Pins 14 – 19: PORT C [0:5] (Arduino analog pins
0 – 5)
🞂 digital pins 0 and 1 are RX and TX for serial
communication
🞂 digital pin 13 connected to the base board LED
🞂 Digital Pin I/O Functions
🞂 pinMode(pin, mode)
🞂 Sets pin to INPUT or OUTPUT mode
🞂 Writes 1 bit in the DDRx register
🞂 digitalWrite(pin, value)
🞂 Sets pin value to LO W or HIGH (0 or 1)
🞂 Writes 1 bit in the PORTx register
🞂 int value = digitalRead(pin)
🞂 Reads back pin value (0 or 1)
🞂 Read 1 bit in the PINx register
Arduino Analog I/O
🞂 Analog inputs
🞂 Convert voltage
to a 10-bit digital
value
🞂 Can provide
reference voltages
PWM – Pulse Width Modulation
🞂 Special I/O
support
🞂 Serial protocols
🞂 Uses special pins
🞂 Uses timers
🞂 Beyond scope of
this course
Arduino C Programs
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
Arduino Serial I/O
🞂 Communication with PC via USB
serial line
🞂 Use the Serial Monitor in the IDE
🞂 Or set up a C or Java (or you-
name-it) interface
🞂 Example Serial library calls
🞂 Serial.begin(baud-rate)
🞂 9600 default
🞂 Serial.println(string)
🞂 int foo = Serial.read()
🞂 Read one byte (input data is buffered)
🞂 See documentation for more
Example Program
Pulse Width Modulation Using an
Arduino
Abstract
Pulse Width Modulation is a process that is used in many applications. One of the
easiest ways to implement this is by using an Arduino. The Arduino can do this in a number of
ways.
This application note will look at what Pulse Width Modulation is and will also explain
how to perform Pulse Width Modulation using two different methods.
Keywords
Pulse Width Modulation, Arduino, AnalogWrite, DigitalWrite
66
Table of Contents
Abstract 1
Introduction 3
Objective 4
Methods 4
AnalogWrite 4
DigitalWrite 6
Conclusion 7
Reference 8
67
Introduction
So the larger the duty cycle percentage, the larger the output signal will be. In the picture
above and for the Arduino, it is the voltage that varies.
The Arduino should also be briefly explained before moving on to how to create the
PWM. There are many variations of Arduinos on the market. The Arduino Uno will be the focus
of this application note. The Arduino is a board that allows the user to access, program and
build with a microcontroller. The Arduino has 14 digital input/output pins [1]. Six of these pins
can be used for with the PWM function that is already built into the Arduino. The pins that do
not have this ability can still be used for PWM but must be used in another way. They output
either a High (5V) or Low (0V) depending on what is coded. The coding will be explained in
the sections that describe the different methods for PWM.
68
Objective
Methods
This application note will talk about the two most basic ways of implementing PWM.
These methods are analogWrite and digitalWrite.
AnalogWrite
The Arduino has six pins that can already do PWM without varying much. Some
sample code is provided in Figure 2 below.
Figure 2. analogWrite
The code is broken down into three sections. The first section is where the variables that are
going to be used are initialized. For instance, the line that says “int led = 9;” sets pin 9 on the
Arduino to the variable “led”. The variable “led” can now be used in the code and it will tell
the
69
Arduino that it is using pin 9. This makes the code cleaner and it helps keep track what each
line in the code is actually affecting. The next section of the code is the void setup. This section
is where pins are set as either inputs or outputs. This is done with the pinMode function. The
function takes your pin or variable and then sets it to what you specify, either input or output. In
this example, the variable “led” is being set as an output. The last section of code is the void
loop. This is where the bulk of the code goes. It will run an infinite loop of the code that is put
in this section.
AnalogWrite function generates a square wave that can be varied in the function. The
two things the function needs to run are a pin/variable and a value to set the duty cycle. The
value that gets input for this function has to be between 0 and 255 [3]. A duty cycle of 100%
occurs if the value is set at 255 and a value of 0 gives a duty cycle of 0%. To get a specific
duty cycle a value for analogWrite needs to be calculated. This is shown below.
For example, a duty cycle of 20% would need to be set at a value of 51. The example
code in Figure 2 has code that changes the duty cycle while it is running. The output of this
code is shown in Figures 3, 4, 5 and 6 below. Each figure shows a different time. So the
pictures from left to right shows the code changing the pulse width.
70
DigitalWrite
The digitalWrite function can be used on all 14 digital input/output pins. Unlike
analogWrite, a square wave has to be made manually. This is done by using both the digitalWrite
and delay functions. The digitalWrite function can only provide a High (5V) or a Low (0V) [4].
So manually doing this in the code provides a square wave. Sample code is shown in Figure 7
below.
Figure 7. digitalWrite
Each time the digitalWrite function is used, it needs to be followed with the delay function.
The above code currently has a duty cycle of 50%. To get a desired duty cycle, the math
becomes a little harder than for the analogWrite function.
71
Conclusion
The two methods have their pros and cons. AnalogWrite already generates a square
wave and it is simple to calculate the value needed to get a desired duty cycle. The AnalogWrite
function cannot have a different frequency than what is already set for that pin. The
DigitalWrite function requires more math to get the desired duty cycle but it allows for the
frequency to be changed. More information about both of these functions can be found on the
Arduino website [5]. Arduino is an open source microcontroller board so people are constantly
writing new code to be implemented on these boards. Other methods of generating a PWM
signal are available online but these are the most reliable because they were made available
with the Arduino board.
72
References
1 http://arduino.cc/en/Main/ArduinoBoardUno
2 http://motorcontroller.blogspot.com/2009/07/today-i-looked-over-my-ece-618-junior.html
3 http://arduino.cc/en/Reference/analogWrite
4 http://arduino.cc/en/Reference/digitalWrite
5 http://arduino.cc/
73
Lecture 6 – ATmega328 Timers
and Interrupts
Arduino Digital and Analog I/O Pins
🞂 Digital pins:
🞂 Pins 0 – 7: PORT D [0:7]
🞂 Pins 8 – 13: PORT B [0:5]
🞂 Pins 14 – 19: PORT C [0:5] (Arduino analog pins
0 – 5)
🞂 digital pins 0 and 1 are RX and TX for serial
communication
🞂 digital pin 13 connected to the base board LED
🞂 Digital Pin I/O Functions
🞂 pinMode(pin, mode)
🞂 Sets pin to INPUT or OUTPUT mode
🞂 Writes 1 bit in the DDRx register
🞂 digitalWrite(pin, value)
🞂 Sets pin value to LO W or HIGH (0 or 1)
🞂 Writes 1 bit in the PORTx register
🞂 int value = digitalRead(pin)
🞂 Reads back pin value (0 or 1)
🞂 Read 1 bit in the PINx register
Port Pin Definitions
#define PINB _SFR_IO8(0x03) #define PORTC _SFR_IO8(0x08)
#define PINB0 0 #define PORTC0 0
... ...
#define PINB7 7 #define PORTC6 6
🞂 Timer/Counter Control
Register B
Mode Summary
COM0A[1:0] (COM0B[1:0] similar)
Timer/Counter 0 Interrupts
🞂 Timer/Counter 0 Interrupt
Mask
ISR( _vect) {
timer++;
PORTB = ;
}
void loop()
{
// Nothing to do
}
Timer Example #2
void setup() {
DDRB = ; // Pin 13 OUTPUT
// Using timer 2
// Set to CTC mode, Pin OC0A disconnected
TCCR2A = ;
// Prescale clock by 1 (no prescale)
TCCR2B = ;
ISR( _vect) {
timer++;
PORTB = ;
}
void loop()
{
// Nothing to do
}
External Interrupt Example
#define pinint0 // Print out the information
#define pinint1 void loop()
void setup() {
{ );
pinMode(pinint0, );
pinMode(pinint1,
Serial.begin(9600); Serial.print("X: ");
// External interrupts 0 and Serial.print(percent0);
1 Serial.print(" Y: ");
// Interrupt on rising Serial.println(percent1);
edge EICRA = ; }
// Enable both interrupts
EIMSK = ;
// Turn on global interrupts
sei();
}
ISR( _vect) {
}
ISR( _vect) {