Вы находитесь на странице: 1из 72

Arduino

Introduction to Arduino
What is Arduino?

● The Arduino is a pocket-sized computer (called a "microcontroller")


that you can program and use to control circuits.
● It interacts with the outside world through sensors, leds, motors,
speakers. even the internet; this makes it a flexible platform for lots
of creative projects.
● Open Hardware + Open Software.
● You can program it , make projects with it.
Why Arduino?

● It is Open Source, both in terms of Hardware and Software.


● Inexpensive. It is cheap(�500).
● It can communicate with a computer via serial connection over USB.
● It can be powered from USB or standalone DC power.
● It can work with both Digital and Analog electronic signals.
● Simple, clear programming environment: It uses a simplified set of
libraries that are built over C.
 www.arduino.cc
 The Arduino software can run on Windows, Macintosh and Linux.
Arduino

• Arduino is composed of two major parts:


• Arduino Board: piece of hardware where we can build our project
• Arduino Integrated Development Environment or IDE,
the piece of software we run on our computer.
• We use the IDE to create a sketch ( a little computer program) that we
upload to the Arduino board. The sketch tells the board what to do.
• 28 legs are available
• ATmega328, the heart of Arduino board.
Arduino Hardware
ICSP
This is referred to as in-circuit serial programming (ICSP), which represents the
method available for programming Arduino boards. Ordinarily, an Arduino
bootloader program is used to program an Arduino board, but if the bootloader is
missing or damaged, ICSP can be used instead. ICSP can be used to restore a
missing or damaged bootloader. Most Arduino and Arduino compatible boards will
have a 2x3 pin ICSP header on them. It breaks out three of the SPI pins
(MISO, MOSI, SCK), power, ground, and reset.
The two main uses of the ICSP pins are:
•Boot-loading
•SPI communication

Serial Peripheral Interface (SPI) is a synchronous serial data protocol used


by microcontrollers for communicating with one or more peripheral devices
quickly over short distances. It can also be used for communication between
two microcontrollers

We can use another Arduino to be that piece of hardware to program the


bootloader through the ICSP pins.
ICSP
Most Arduino and Arduino compatible boards will have a 2x3 pin ICSP header on
them. It breaks out three of the SPI pins (MISO, MOSI, SCK), power, ground,
and reset.

•MISO (Master In Slave Out) - The Slave line for sending data to the
master,
•MOSI (Master Out Slave In) - The Master line for sending data to the
peripherals,
•SCK (Serial Clock) - The clock pulses which synchronize data
transmission generated by the master
I2C
The I2C communication bus is very popular and broadly used by many electronic devices
because it can be easily implemented in many electronic designs which require
communication between a master and multiple slave devices. The two wires, or lines are
called Serial Clock (or SCL) and Serial Data (or SDA). The SCL line is the clock signal which
synchronize the data transfer between the devices on the I2C bus and it’s generated by
the master device. The other line is the SDA line which carries the data.

The Inter-integrated Circuit (I2C) Protocol is a protocol intended to allow


multiple “slave” digital integrated circuits (“chips”) to communicate with
one or more “master” chips. Like the Serial Peripheral Interface (SPI), it is
only intended for short distance communications within a single device.
Arduino Uno Revision 3 Specifications

 Microcontroller: ATmega328
 Operating Voltage: 5V
 Digital I/O Pins: 14 total – 6 of which can be PWM
 Analog Input Pins: 6
 Maximum DC Current per I/O pin at 5VDC: 40ma
 Flash Memory: 32KB (0.5KB used by boot loader)
 SRAM Memory: 2KB
 EEPROM: 1KB
 Clock Speed: 16 MHz
 USB access to the serial port
 Onboard 5 VDC & 3.3 VDC regulated power
ATmega 328 microcontroller

 Two 8-bit Timer/Counters


 One 16-bit Timer/Counter
 Six PWM channels
 Six channel 10 bit ADC
 Programmable Serial USART
 Programmable Watchdog Timer with Separate
On-chip Oscillator
 On-chip Analog Comparator
Arduino Uno R3 Pin Descriptions
Arduino Uno R3 Pin Descriptions

PORT B (PB0 – PB7) is an 8 bit bidirectional I/O port with internal pull-
ups. Processor pins 14 – 17 bring PB0 to PB5 out.

 PB1 can also be used as a PWM output


 PB2 can also be SPI Bus Master Slave Select (*SS)
or PWM output
 PB3 can also be or SPI Bus Master Out/Slave In (MOSI)
or PWM output
 PB4 can also be SPI Bus Master In/Slave Out (MISO)
 PB5 can also be SPI Bus Master Clock Input (SCK)
 PB6 and PB7 are brought out on Processor pins 9 and
10 for the crystal clock oscillator
Arduino Uno R3 Pin Descriptions

PORT C (PC0 – PC5) is a 7 bit bidirectional I/O port. Processor pins 23 – 28 bring PC0 to
PC5 out.
 PC0 – PC5 can also be used as A/D inputs
 PC4 and PC5 can also be used as SDA(Serial Data) and SCL(Serial clock) for I2C
 PC6 is brought out on processor pin 1 as reset

PORT D (D0 – D7) is an 8 bit bidirectional I/O port with internal pull-ups. Processor
pins 2 – 6 and 11 – 13 bring all pins out
 PD0 can also be USART Input (RXD)
 PD1 can also be USART Output (TXD)
 PD3 can also be used as a PWM output
 PD5 can also be used as a PWM output
 PD6 can also be used as a PWM output
Arduino Uno R3 Pin Descriptions
Arduino Uno R3 Pin Descriptions

 Pin -1 is the RST (Reset) pin and applying a low level signal for a time longer than the
minimum pulse length will produce a RESET.
 Pin-2 and pin-3 are used in USART for serial communication
 Pin-4 and pin-5 are used as an external interrupt
 Pin-6 and pin-11 are used as timer/counter sources.
 Pin-9 & pin-10 are used as a timer counters oscillators as well as an external
oscillator where the crystal is associated directly with the two pins
 Pin-12 and pin-13 are used as an Analog Comparator i/ps.
 Pin-19 is used as a Master CLK o/p, slave CLK i/p for the SPI-channel.
 Pin-18 is used as Master CLK i/p, slave CLK o/p.
 Pin-17 is used as Master data o/p, slave data i/p for the SPI-channel.
 Pin-16 is used as a slave choice i/p
 Pin-15 can be used as an external o/p of the timer or counter
 Pin-23 to Pins28 have used for ADC (digital value of analog input) channels. Pin-27
can also be used as a serial interface CLK & pin-28 can be used as a serial interface
data
Architecture

ALU – Arithmetic Logic Unit


The ALU operates in direct connection with all the 32 general
purpose working registers. Within a single clock cycle, arithmetic
operations between general purpose registers or between a register
and an immediate data are executed. The ALU operations are
divided into three main categories –
arithmetic, logical, and bit-functions.
Architecture

In-System Reprogrammable Flash


Program Memory
The ATmega328/P contains 32Kbytes On-chip Reprogrammable Flash
memory for program storage. Since all instructions are 16 or 32 bits
wide, the Flash is organized as 16K x 16. For software security, the
Flash Program memory space is divided into two sections - Boot
Loader Section and Application Program Section in the device .
The ATmega328/P Program Counter (PC) is 14 bits wide, thus
addressing the 16K program memory locations.
Architecture
SRAM Data Memory
The first 32 locations address the Register File(0000-001FH), the
next 64 location the standard I/O memory (0020-005FH), 160
Extended I/O Registers (0060-00FFH), and the next 2K locations
address the internal data SRAM(0100-08FFH) .

EEPROM Data Memory


The ATmega328/P contains 1K bytes of data EEPROM memory. It is
organized as a separate data space, in which single bytes can be
read and written. The EEPROM has an endurance of at least 100,000
write/erase cycles. The access between the EEPROM and the CPU is
described in the following, specifying the EEPROM Address Registers
(10 bit, EEPROM Address Register High(bit 9, 8), EEPROM Address
Register Low(bit 7:0), ) , the EEPROM Data Register (8 bit), and the
EEPROM Control Register (8 bit).
Architecture

General Purpose I/O Registers


The device contains three General Purpose I/O Registers, General
Purpose I/O Register 0/1/2 (GPIOR 0/1/2). These registers can be used
for storing any information, and they are particularly useful for storing
global variables.
Architecture

Stack Pointer
The Stack is mainly used for storing temporary data, for storing local variables
and for storing return addresses after interrupts and subroutine calls. The
Stack is implemented as growing from higher to lower memory locations. The
Stack Pointer Register always points to the top of the Stack.
The Stack Pointer points to the data SRAM Stack area where the Subroutine
and Interrupt Stacks are located.

The Stack Pointer is implemented as two 8-bit registers in the I/O space (SPH , SPL).
Stack Pointer Register High byte:Bits 2:0 – (SP[10:8]) SPH,
Stack Pointer Register Low byte:Bits 7:0 – (SP[7:0]) SPL
Status Register
Bit 7 6 5 4 3 2 1 0

I T H S V N Z C

Access R/W R/W R/W R/W R/W R/W R/W R/W

Reset 0 0 0 0 0 0 0 0

Bit 7 – I: Global Interrupt Enable


Bit 6 – T: Copy Storage
Bit 5 – H: Half Carry Flag

Bit 4 – S: Sign Flag, S = N ㊉ V


Bit 3 – V: Two’s Complement Overflow Flag
Bit 2 – N: Negative Flag
Bit 1 – Z: Zero Flag
Bit 0 – C: Carry Flag
General Purpose Register
Total 32 general purpose registers

R0 0x00
R1
0x01
R2
0x02

R13

R14

R15

R16

R17

R26

R27

R28

R29

R30 0x1E Z-registe r Low Byte

R31 0x1F Z-registe r High Byte


The X-register, Y-register, and Z-register

15 XH XL 0

X-register 7 0 7 0

R27 R26

15 YH YL 0

Y-register 7 0 7 0

R29 R28

15 ZH ZL 0

Z-register 7 0 7 0

R31 R30
Arduino Programming
Configuring Arduino

Download the Arduino IDE software

● This is the software used to write


programs for the Arduino.

Go to http://arduino.cc/en/Main/Software and
download the Arduino Software for your
Windows/Linux.

The Arduino runs a simplified version of the C


Programming language, with some extensions
for accessing the hardware

Programs are created in the Arduino development


environment and then downloaded to the Arduino board.
Configuring Arduino

Initial Setup

● We need to setup the environment to Tools menu and select Board.


● Then select the type of Arduino you want to program, in our case it’s the Arduino Uno.
Writing and Downloading Code

Write sketch on pc

Download sketch to Arduino


Running Code While Tethered

Run sketch on Aurdino and


send data back to PC. Arduino interact with its
environment .

Serial communication back to


host.
Arduino workflow

Create electronics
circuit UNPLUG
Then go back
and check your
connect Arduino code, circuit and
to circuit Arduino to
circuit
connection
create Arduino
code

plug in Arduino, set serial


port, board type and NO
upload code

does it perform the YES Great.!


intended task?
Running the Arduino Software

• Now that our software is installed and our Arduino is setup, let’s
verify everything is working. The easiest way to do this is by using
the “Blink” sample application.
 Open the Arduino Software by Double-clicking the Arduino
Application).
 Make sure the board is still connected to your computer.
 Open the LED blink example sketch: File > Examples > 1.Basics >
Blink.
 You should see the code for the application open and it should look
like this:
Let’s examine the code in blink sketch.
Structure of a sketch

Each Arduino program (often called a sketch) has two required


functions (also called routines).

void setup()
{
// write your code here..!!
}
void loop()
{
// write your code here..!!
}
Structure of a sketch

 void setup()

All the code between the two curly brackets will be run once when your
Arduino program first runs.

 void loop()

This function is run after setup has finished.After it has run once it will
be run again, and again, until power is removed.
Structure of a sketch

int ledPin = 13;


The onboard LED we want to control
void setup( ) { is on pin 13. So initialized variable
ledPin with 13.
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
}
Structure of a sketch

int ledPin = 13;


void setup( ) {
pinMode(ledPin, OUTPUT);
}
void loop() { pinMode(pin, mode);
Used to set a pins mode, pin is the pin
digitalWrite(ledPin, HIGH);
number you would like to address. the
delay(2000); mode can either be INPUT or OUTPUT.

digitalWrite(ledPin, LOW);
delay(2000);
}
Structure of a sketch

int ledPin = 13;


int digitalWrite(pin);
void setup( ) { Write a HIGH or a LOW value to a digital pin.If
pinMode(ledPin, OUTPUT); the pin has been configured as an OUTPUT
with pinMode(), its voltage will be set to the
} corresponding value: 5V (or 3.3V on 3.3V
boards) for HIGH, 0V (ground) for LOW.
void loop() {
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
}
Structure of a sketch

int ledPin = 13;


void setup( ) {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); Which takes an integer of a time interval in
milliseconds, And tells microcontroller to do
delay(2000); nothing for that interval,1000 ms = 1 s

digitalWrite(ledPin, LOW);
delay(2000);
}
Upload the sketch
Now, simply click the “Upload” button in the environment. Wait a few seconds
– you should see the RX and TX LEDs on the Arduino flashing. If the upload is
successful, the message “Done uploading.” will appear in the status bar.
Variables
•Variables are allocated by declaring them in the program. If a variable
is declared outside the braces of a function, it can be seen everywhere in
the program. If it is declared inside the braces of a function, the variable
can only be seen within that function. Variables come in several flavors
including byte (8-bit, unsigned, 0 to 255), word (16-bit,unsigned, 0 to
65,536), int (16-bit, signed, -32,768 to 32,767), and long (32-bit,signed, -
2,147,483,648 to 2,147,483,647). Variable declarations generally appear
at the top of the program
•byte i;
•word k;
•int length;
•int width;
Math

•Symbol Description
+ addition
- subtraction
* multiplication
/ division
% modulus (division remainder)
<<left bit shift
>>right bit shift
& bitwise AND
| bitwise OR
Serial.print

The Serial.print command is used to see what's going on


inside the Arduino from your computer.
For the command to work, the command Serial.begin(9600)
must be placed in the setup()function. After the program is
uploaded, we must open the Serial Monitor window to see
the response.
•void setup()
•{
•Serial.begin(9600);
•Serial.println("Hello World");
•}
Common Coding Errors

•Forgetting the semi-colon at the end of a statement


•Misspelling a command
•Omitting opening or closing braces.
Arduino Programming Basics

COMMAND DESCRIPTION

pinMode(n,INPUT) Set pin n to act as an input. One-time command at top


of program
pinMode(n,OUTPUT) Set pin n to act as an output.

digitalWrite(n,HIGH) Set pin n to 5V

digitalWrite(n,LOW) Set pin n to 0V

digitalRead(pin) Reads the value from a specified digital pin,


either HIGH or LOW
delay(x) Pause program for x millisec, x = 0 to 65,535

for() Loop. Example: for (i=0;i<3;i++){} Do the instructions enclosed


by {} three times
if (expr) {} Conditional branch. If expr true, do instructions enclosed by {}

while (expr) {} While expr is true, repeat instructions in {} indefinitely


Arduino Programming Basics

COMMAND DESCRIPTION

lcd.begin(cols, rows) Initializes the interface to the LCD screen, and specifies
the dimensions (width and height) of the display.
lcd.print(data) Prints text to the LCD.
lcd.print(data, BASE) BASE (optional): BIN for binary, DEC for decimal, OCT
for octal, HEX for hexadecimal
lcd.setCursor(col, row) Position the LCD cursor

lcd.rightToLeft(); causes text to flow to the left from the cursor, as if the
display is right-justified.
lcd.leftToRight() causes text to flow to the right from the cursor, as if the
display is left-justified.
lcd.clear(); clear the screen

Serial.read() accepts serial input from a computer and displays it on


the LCD
Arduino Programming Basics

COMMAND DESCRIPTION

analogWrite(n,d) Writes an analog value (PWM wave ) to a pin, here n is


the pin number, d is duty cycle between 0(always off)
and 255(always on)
analogRead() converts the input voltage range, 0 to 5 volts, to a
digital value between 0 and 1023.
tone(n,f,d) Play tone of frequency f Hz for d millisec on speaker
attached to pin n

Reference:
https://www.arduino.cc/en/Reference/HomePage
Examples 1

1. Turn on LED connected to Pin 2 for 1 s.


void setup() {
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
}
void loop()
{}
Examples 2

2. Flash LED connected to Pin 2 at 1 Hz forever.


void setup() {
pinMode(2,OUTPUT);
}
void loop() {
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
delay(500);
}
Examples 3

3. Turn on motor connected to Pin 4 for 1 s.


void setup() {
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
delay(1000);
digitalWrite(4,LOW);
}
void loop()
{}
Examples 4
int i;
void setup()
{pinMode(2,OUTPUT);
Serial.begin(9600);
Serial.println("Welcome to my program");
delay(1000);
flasher(); // call flasher function
Serial.println("I hope you like flashing");
delay(1000);
flasher(); // call flasher again
Serial.println("Here it is one more time");
delay(1000); contd..
Examples(4)Contd…

flasher();
}
void loop() {}
void flasher()
{
for(i=0;i<3;i++) {
digitalWrite(2,HIGH);
delay(250);
digitalWrite(2,LOW);
delay(250);
}
}
Sample Program(LED ON/OFF)
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
Sample Program(PWM)
int ledPin = 9; // LED connected to PWM pin 9
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop()
{
analogWrite(ledPin, 100); // sets the LED on

}
Sample Program(Serial data transfer)
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(2));
delay(1000);
}
Sample Program(Serial data transfer)
int i,j,k;
void setup()
{
Serial.begin(9600);
i=21;
j=20;
k=i+j;
Serial.flush();
Serial.print(k);
}
void loop() {}
Sample Program(LM35 ,Temp. Sensor)
float temp;
int tempPin = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
temp = analogRead(tempPin);
temp = temp * 0.48828125;
Serial.print("TEMPRATURE = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();
delay(10000);
}
Measure Room Temperature

1. Arduino board
2. Breadboard
3. Temperature Sensors
4. Jumper wires
Connect the sensor to your Arduino board as shown on this picture:

● Create a arduino sketch and add below code .


Room Temperature : Sketch
const int sensorPin = A0;
void setup() { Analog input pin
Serial.begin(9600);
}
void loop(){
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0) * 5.0;
float temperature = (voltage - .5) * 100;
Serial.print(", degrees C: ");
Serial.println(temperature);
delay(1000);
}
Room Temperature : Sketch
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop(){ This opens a connection between
the arduino and the computer. so
int sensorVal = analogRead(sensorPin); we can see the values from
float voltage = (sensorVal / 1024.0) * 5.0; analog input on your computer.
9600 bit/sec rate at which
float temperature = (voltage - .5) * 100; arduino will communicate .
Serial.print(", degrees C: ");
Serial.println(temperature);
delay(1000);
}
Room Temperature : Sketch
const int sensorPin = A0; To get the value from the sensor
void setup() { ,call analogRead() that take one
argument what pin it should take a
Serial.begin(9600); voltage reading on.The value which
} is return is between 0 and 1023.

void loop(){
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0) * 5.0;
float temperature = (voltage - .5) * 100;
Serial.print(", degrees C: ");
Serial.println(temperature);
delay(1000);
}
Room Temperature : Sketch
const int sensorPin = A0;
void setup() { Converting ADC reading to voltage. The
voltage will be a value between 0 and 5
Serial.begin(9600); volts ,So divide sensorVal by 1024.0
} and multiply by 5.0. this new number
represents voltage on the pin.
void loop(){
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0) * 5.0;
float temperature = (voltage - .5) * 100;
Serial.print(", degrees C: ");
Serial.println(temperature);
delay(1000);
}
Room Temperature : Sketch
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop(){
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0) * 5.0;
float temperature = (voltage - .5) * 100;
Serial.print(", degrees C: ");
Serial.println(temperature); Converting voltage to temperature .
converting from 10 mv per degree
delay(1000); with 500 mV offset to degrees
} ((voltage - 500mV) times 100).
Room Temperature : Sketch
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop(){
int sensorVal = analogRead(sensorPin); Serial.print() sends information
from the arduino to connected
float voltage = (sensorVal / 1024.0) * 5.0; computer .
float temperature = (voltage - .5) * 100;
Serial.print(", degrees C: ");
Serial.println(temperature);
delay(1000);
}
Room Temperature : Uploading Sketch
Now upload the sketch to the arduino, after finishing uploading click
the serial monitor
Serial monitor
Room Temperature : Serial Monitor
Interfacing LCD with Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); include the library
code (header file)
void setup() {
lcd.begin(16, 2);
lcd.print(“Arduino Training”);
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
Interfacing LCD with Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2); Initialize the library with the
lcd.print("hello, world!"); numbers of the interface pins
}
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
void loop() {
lcd.setCursor(0, 1); LCD D4 pin to digital pin 5
lcd.print(millis() / 1000); LCD D5 pin to digital pin 4
} LCD D6 pin to digital pin 3
LCD D7 pin to digital pin 2
Interfacing LCD with Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
set up the LCD's
void setup() {
number of columns
lcd.begin(16, 2); and rows
lcd.print("hello, world!");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
Interfacing LCD with Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2); Print message to the LCD
lcd.print("hello, world!");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
Interfacing LCD with Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
}
set the cursor to
column 0, line 1
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
Interfacing LCD with Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
}
print the number of
void loop() { seconds since reset
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
Thank you

Оценить