Week 8 AVR Basics
Week 8 AVR Basics
School
1
Microcontroller Based Design
2
Arduino – Intro and Programming
3
Overview
• Background
– Microcontroller defined/Why Arduino's?
– Types of Arduino microcontrollers
• What To Get (Hardware and Software)
• Arduino C
• Electronic Circuits
• Design Interface Examples
4
AVR
Architecture
AVR is a family of
microcontrollers developed
by Atmel beginning in 1996.
6
Arduino – Official Definition
• Taken from the official web site (arduino.cc):
– Arduino is an open-source electronics prototyping
platform based on flexible, easy-to-use hardware
and software. It's intended for artists, designers,
hobbyists, and anyone interested in creating
interactive objects or environments.
7
Why Arduino?
• Arduino microcontrollers have become the
popular prototyping choice!
– Make Magazine features many projects using
Arduino microcontrollers.
(https://makezine.com/)
• Strives for the balance between ease of use
and usefulness.
– Programming languages seen as major obstacle.
– Arduino C is a greatly simplified version of C++.
• Inexpensive (PKR 1900 @ Electrobes).
8
ATmega328
Block
Diagram
9
ATmega328
Pin Configuration
10
Arduino Types
• Many different versions
– Number of input/output channels
– Form factor
– Processor
• Leonardo
• Due
• Micro
• LilyPad
• Esplora
• Uno
• Nano
11
Arduino
Nano
12
13
Arduino Nano
14
Arduino
Nano
15
Arduino Compiler
• Download current compiler from:
arduino.cc/en/Main/software
• Run the software installer.
• Written in Java, it is fairly slow.
Visit playground.arduino.cc/Main/
DevelopmentTools for alternatives to the
base arduino IDE
16
Compiler Features
• Numerous sample
sketches are included in
the compiler
• Located under File,
Examples
• Once a sketch is
written, it is uploaded
by clicking on File,
Upload, or by pressing
<Ctrl> U
17
Arduino C is Derived from C++
◼ These programs blink an LED on pin 13
• Arduino C
void setup( ) {
pinMode(13, OUTPUT);
}
void loop( ) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
18
Common Sensors
• Dials on a radio are • Infrared sensor & light
simply potentiometers • Hall effect sensor and
• Temperature magnet
• Light • Ball tilt sensor (for
• Angle measuring orientation)
• Switches • Force
– did the user throw a
switch or push a button?
• Accelerometer
(measures motion and
tilt) 19
Getting started with Programming
20
digitalWrite()
BIG 6 CONCEPTS
analogWrite()
digitalRead()
analogRead()
Serial communication
21
https://www.tinkercad.com/circuits
22
https://www.tinkercad.com/
23
Comments, Comments, Comments
24
Programming Concepts: Variable Types
Variable Types:
26
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run
repeatedly:
}
27
comments
28
setup : It is called only when the Arduino is
powered on or reset. It is used to initialize
variables and pin modes
29
A pin on arduino can be set as input or
output by using pinMode function.
30
digitalWrite(13, LOW); // Makes the output
voltage on pin 13 , 0V
31
What is analog ?
It is continuous range of voltage values (not
just 0 or 5V)
32
33
34
35
The Arduino Uno board contains 6 pins for
ADC
36
analogRead(A0); // used to read the analog
value from the pin A0
analogWrite(2,128);
◦ 50% duty cycle PWM signal on pin 2
37
Design and Interface Examples
40
Basic Electric Circuit
• Every circuit (electric or electronic) must have
at least a power source and a load.
• The simplest circuit is a light.
• Plug in the light, and it lights up.
• Unplug it, the light goes out.
• Electricity flows from the power source,
through the load (the light) and then back to
the power source.
41
Basic LED Circuit
• Connect the positive (+) lead of a power
source to the long leg of an LED.
• Connect other leg of the LED to a resistor.
– High resistance means a darker light.
– Low resistance means brighter light.
– No resistance means a burned out LED.
• Connect other leg of the resistor to the
negative lead of the power source.
42
Blink Sketch
void setup( ) {
pinMode(13, OUTPUT);
}
void loop( ) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
43
LED Fade Code
/* Fade
44
LED Fade Code Cont..
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
45
Digital Sensors (a.k.a. Switches)
Add an indicator LED to Pin 13
This is just like our
1st circuit!
In practice, all mechanical switch contacts bounce (that is, turn on and off,
repeatedly, for a short period of time) after the switch is closed or opened.
+5v
Voltage
+5v
t1 t2 Time
48
Dealing with switch bounce
49
Dealing with switch bounce
Note that the figure of ‘20 ms’ will, of course, depend on the switch used.
Reading Assignment:
Check a realistic implementation here and note any differences from above mentioned steps:
https://docs.arduino.cc/built-in-examples/digital/Debounce/
50
Programming: Conditional Statements
if()
51
Project: Mood Lamp / Light Sculpture
52
LCD Interfacing
53
LCD
Hitachi 44780
54
LCD
Liquid Crystal Display
Lines
Data Lines
Control Lines
55
LCD
Cheap and easy way of displaying data
Multi-line display
Commonly 1 to 4 lines
LCDs have
I/O Pins
Display Data RAM (DDRAM)
Character Generator RAM (CGRAM)
Registers
Flags
56
LCD Pins
8 data pins
D0 - D7
RS – Register Select
0 = Command Register Selected
1 = Data Register Selected
R/W – Read / Write
Read / Write pin
0 = Write
1 = Read
E – Enable
High-to-Low edge on E is needed to latch data (on D0 – D7)
57
Hardware Connections
58
Hardware Connections Example
59
LCD Programming
8 data lines
The number on these lines could be
1. Command (when RS = 0)
The number on data lines is a command (for LCD)
2. Data (when RS = 1)
The number on data lines is data (to be displayed on LCD)
60
LCD Command Set
61
LCD Command Set
62
LCD Commands
Command may be
Setting the cursor position
Clear display
63
LCD Programming
Standard steps are
1. Initialize
A. Set data on data lines (D0 – D7)
B. Set Command (RS = 0) or Data (RS=1)
C. Write (R/W = 1) or Read (R/W = 0)
D. Set Falling Edge on E (High-to-Low transition)
E. Repeat
65
Initialization
LCD must be initialized after power-on, before writing any
data
Initialization steps
1. Function Set*
2. Entry Mode Set*
3. Display Control*
4. Clear Display
66
Writing to LCD
Set control lines
RS
0 = Command
1 = Data
R/W = 1
Send the data on D0 – D7
E – Enable
Set E High, to begin write cycle
Wait, to allow LCD to accept the data
Set E Low, to finish write cycle
67
Reading from LCD
Set control lines
RS
1 for display
0 for command
R/W = 0
E – Enable
Set E High, to begin read cycle
Wait, to allow LCD to fetch the data
68
Interfacing with Servo Motor
69
Servo Motor
Sensor
DC Motor
Optical Encoder
Light Source
Light Sensor
71
Incremental Encoder
Optical Encoder
Light Source
Light Sensor
72
Incremental Encoder
Position Measurement
Incrementing with pulse count
Direction Knowledge
Using ‘Phase’ knowledge
73