0% found this document useful (0 votes)
12 views

Week 8 AVR Basics

avr series description using servo, and dusplay

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 8 AVR Basics

avr series description using servo, and dusplay

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Stanford d.

School

1
Microcontroller Based Design

Dr Anjum Naeem Malik

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.

These are modified Harvard


architecture 8-bit RISC
single-chip microcontrollers.

AVR was one of the first


microcontroller families to
use on-chip flash memory for
program storage, as opposed
to one-time programmable
ROM, EPROM, or EEPROM
• NAND-type flash memory may be written and read in blocks
used by other • NOR-type flash allows a single machine word (byte) to be written/
microcontrollers at the time. Erased 5
• SRAM / DRAM. SRAM – Fast, Cache. DRAM – Main Memory
What is an Arduino ?
 Open Source electronic prototyping platform based on
flexible easy to use hardware and software.

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()

if() statements / Boolean

analogRead()

Serial communication
21
https://www.tinkercad.com/circuits

22
https://www.tinkercad.com/

23
Comments, Comments, Comments

 Comments are for you – the programmer and your


friends…or anyone else human that might read your code.

 // this is for single line comments


 // it’s good to put a description at the
top and before anything ‘tricky’
 /* this is for multi-line comments
 Like this…
 And this….
 */

24
Programming Concepts: Variable Types
 Variable Types:

8 bits 16 bits 32 bits

byte int long


char unsigned int unsigned long
float
25
Arduino Program Development
• Based on C++ without 80% of the instructions.
• A handful of new commands.
• Programs are called 'sketches'.
• Sketches need two functions:
– void setup( )
– void loop( )

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

 loop : The loop functions runs continuously


till the device is powered off. The main logic
of the code goes here. Similar to while (1) for
micro-controller programming.

29
 A pin on arduino can be set as input or
output by using pinMode function.

 pinMode(13, OUTPUT); // sets pin 13 as


output pin

 pinMode(13, INPUT); // sets pin 13 as input


pin

// NOTE: -> commands are CASE-sensitive

30
 digitalWrite(13, LOW); // Makes the output
voltage on pin 13 , 0V

 digitalWrite(13, HIGH); // Makes the output


voltage on pin 13 , 5V

 int buttonState = digitalRead(2); // reads the


value of pin 2 in buttonState

31
 What is analog ?
 It is continuous range of voltage values (not
just 0 or 5V)

 Why convert to digital ?


 Because our microcontroller only understands
digital.

32
33
34
35
 The Arduino Uno board contains 6 pins for
ADC

 10-bit analog to digital converter

 This means that it will map input voltages


between 0 and 5 volts into integer values
between 0 and 1023

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

This example shows how to fade an LED on pin 9 using the


analogWrite() function.

This example code is in the public domain.


*/

int led = 9; // the pin that the LED is attached to

int brightness = 0; // how bright the LED is


int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:


void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

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);

// change the brightness for next time through the loop:


brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:


if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

45
Digital Sensors (a.k.a. Switches)
Add an indicator LED to Pin 13
This is just like our
1st circuit!

Creative Commons License

Creative Commons Attribution-ShareAlike 3.0 United States License


46
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States License.
47
Dealing with switch bounce

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

As far as the microcontroller is concerned, each ‘bounce’ is equivalent to one


press and release of an ‘ideal’ switch. Without appropriate software
design, this can give rise to a number of problems, not least:

• Rather than reading ‘A’ from a keypad, we may read ‘AAAAA’


• Counting the number of times that a switch is pressed becomes extremely
difficult.
• If a switch is depressed once, and then released some time later, the
‘bounce’ may make it appear as if the switch has been pressed again (at
the time of release).

49
Dealing with switch bounce

Creating some simple software to check for a valid switch input is


straightforward:

1. We read the relevant port pin.


2. If we think we have detected a switch depression, we wait for 20 ms and
then read the pin again.
3. If the second reading confirms the first reading, we assume the switch
really has been depressed.

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

For 8051 and Arduino

53
LCD
Hitachi 44780

54
LCD
Liquid Crystal Display

 Lines
 Data Lines
 Control Lines

 Connected through ports of controller


 Parallel Interfacing

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

 Connect data lines of LCD (8) with any port of Arduino/


8051

 Connect Control pins of LCD with another port pins

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

 Check busy flag etc

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

*detail in LCD Command Set

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

 Read the data on D0 – D7

 Set E Low, to finish write cycle

68
Interfacing with Servo Motor

69
Servo Motor
Sensor

DC Motor

Servo Motor = Motor + Feedback (sensor)


70
Incremental Encoder

 Optical Encoder

 Light Source

 Disc with cuts

 Light Sensor

 2 lines of cuts and 2 outputs

71
Incremental Encoder

 Optical Encoder

 Light Source

 Disc with cuts

 Light Sensor

 2 lines of cuts and 2 outputs

72
Incremental Encoder
 Position Measurement
 Incrementing with pulse count

 Direction Knowledge
 Using ‘Phase’ knowledge

 How to measure speed?


 By counting pulses and dividing
by time

73

You might also like