Introduction to Arduino
What is Arduino?
•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.
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
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.
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
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
Reset 0 0 0 0 0 0 0 0
R0 0x00
R1
0x01
R2
0x02
…
R13
R14
R15
R16
R17
R26
R27
R28
R29
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
Go to http://arduino.cc/en/Main/Software and
download the Arduino Software for your
Windows/Linux.
Initial Setup
Write sketch on pc
Create electronics
circuit UNPLUG
Then go back
and check your
connect Arduino code, circuit and
to circuit Arduino to
circuit
connection
create Arduino
code
• 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
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
digitalWrite(ledPin, LOW);
delay(2000);
}
Structure of a sketch
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
COMMAND DESCRIPTION
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
COMMAND DESCRIPTION
Reference:
https://www.arduino.cc/en/Reference/HomePage
Examples 1
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:
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