Arduino1
Arduino1
Programming Arduino
Agenda
• Introduction to Arduino Board
• Getting started with Arduino IDE
• Arduino Programming and Proteus design
• Working with Sensors and its Interfacing
• Serial Communication feature of Arduino
• SevenSegmentDisplay and LCD Interfacing
• DC Motor and Servo Motor Interfacing
• Bluetooth Interfacing
What is Micro-Controller?
It is a micro-computer. As any
computer it has internal CPU,
RAM, IOs interface.
It is used for control purposes,
and for data analysis.
Famous microcontroller
manufacturers are MicroChip,
Atmel, Intel, Analog devices,
and more.
What is Arduino?
• A microcontroller board, contains on-board power supply,
USB port to communicate with PC, and an Atmel
microcontroller chip.
• It simplify the process of creating any control system by
providing the standard board that can be programmed
and connected to the system without the need to any
sophisticated PCB design and implementation.
– Lights, LED’s
– Motors
– Speakers
– Displays (LCD)
Why Arduino?
• It is Open Source, both in terms of Hardware and Software.
• It is cheap(1300र), the hardware can be built from components or a
prefab board can be purchased for approx. 900र.
• It can communicate with a computer via serial connection over USB.
• It can be powered from USB or standalone DC power.
• It can run standalone from a computer (chip is programmable) and it
has memory (a small amount).
• It can work with both Digital and Analog electronic signals. Sensors
and Actuators.
• You can make cool stuff! Some people are even making simple
robots.
Different types of Arduino boards:
UN Meg LilyPa
O a d
https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
Getting Started (Installing Arduino IDE and Setting up Arduino Board)
IDE =
Integrated Development
Environment
http://www.arduino.cc/en/Guide/Environment
Arduino IDE (Cont..)
Two required functions /
methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
}
Arduino IDE (Cont..)
Your computer
communicates to the
Arduino microcontroller via a
serial port → through a USB-
Serial adapter.
digitalRead(pin);
Reads HIGH or LOW from a pin
Eg3. digitalRead(2);
digitalWrite(pin, value);
Writes HIGH or LOW to a pin
Eg2. digitalWrite(13, HIGH);
Our first Arduino Sketch/Program
/*
* Arduinos ketch to toggle the LED connected to pin-13 with a rate/delay of 1sec
*/
void setup()
{
// put your setup code here, to run once: -->I*
pinMode(13, OUTPUT); //pin-13 configures as o/p -->II
}
void loop()
{
// put your main code here, to run repeatedly: -->1*
digitalWrite(13, HIGH); //HIGH Value or Binary-1 send to pin-13 -->2
//delay(x); //x-ms second(s) delay -->3*
//delayMicroseconds(y); //y-us second(s) delay -->4*
delay(1000); //1000-milliseconds=1second delay -->5
digitalWrite(13, LOW); //LOW Value or Binary-1 send to pin-13 -->6
delay(1000); //1000-milliseconds=1second delay -->7
//Toggling rate of led connected to pin-13 is of 1second -->8*
}
Uploading and Running the blink sketch
In Arduino, open up:
File → Examples → 01.Basics → Blink
Opens up a
Serial
Terminal
Window
Digital Input
• Connect digital input to your Arduino using Pins # 0 –
13 (Although pins # 0 & 1 are also used for
programming)
• Digital Input needs a pinMode command: pinMode
(pinNumber, INPUT); Make sure to use ALL
CAPS for INPUT
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for a pushbutton)
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
}
Proteus design (with external resister)
Using a key/push button with external
resistor
▪ The digitalRead function monitors the voltage on the input pin
(inputPin), and it returns a value of HIGH if the voltage is high (5
volts) and LOW if the voltage is low (0 volts).
▪ Actually, any voltage that is greater than 2.5 volts (half of the voltage
powering the chip) is considered HIGH and less than this is treated
as LOW.
▪ If the pin is left unconnected (known as floating) the value returned
from digitalRead is indeterminate (it may be HIGH or LOW, and it
cannot be reliably used).
▪ The resistor shown in Figure shown in previous slide ensures that
the voltage on the pin will be low when the switch is not pressed,
because the resistor “pulls down” the voltage to ground.
▪ When the switch is pushed, a connection is made between the pin
and +5 volts, so the value on the pin interpreted by digital Read
changes from LOW to HIGH.
Proteus design (with out external resister)
▪ Digital inputs
must have a
resistor to hold
the pin to a
known value
when the switch
is not pressed.
▪ Arduino has
internal pull-up
resistors that
can be enabled
by writing a
HIGH value to a
pin that is in
/* Pullup sketch: a switch connected to pin 2 lights the LED on pin 13 */
const int ledPin = 13; // output pin for the LED
const int inputPin = 2; // input pin for the switch
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
digitalWrite(inputPin,HIGH); // turn on internal pull-up on the inputPin(i,.e. at
pin2)
}
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == LOW) // check if the input is LOW or switch ON
{
digitalWrite(ledPin, HIGH); // turn LED ON if switch is pressed
}
else if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, LOW); // turn LED OFF
}
/* array sketch an array of switches controls an array of LEDs */
int inputPins[] = {2,3,4,5}; // create an array of pins for switch inputs
int ledPins[] = {10,11,12,13}; // create array of output pins for LEDs
void setup()
{
for(int index = 0; index < 4; index++)
{
pinMode(ledPins[index], OUTPUT); // declare LED as output
pinMode(inputPins[index], INPUT); // declare pushbutton as input
digitalWrite(inputPins[index],HIGH); // enable pull-up resistors
}
}
void loop()
{
for(int index = 0; index < 4; index++)
{
int val = digitalRead(inputPins[index]); // read input value
if (val == LOW) // check if the switch is pressed
{
digitalWrite(ledPins[index], HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPins[index], LOW); // turn LED off
}
}
}
Proteus design
Seven Segment Display
(SSD) Interfacing with Arduino
Agenda
• Introduction to Seven Segment Display
• Types of 7-Segment Display
• Common Cathode
• Common Anode
• Displaying Digital Digits
• Driving 7-Segment Display
• Interfacing 7-Segment Display to Arduino
• Ports and Registers of ATmega328
Introduction
• The 7-segment display, also written as “seven
segment display”, consists of seven LEDs arranged in
a rectangular fashion as shown in the Figure.
• Each of the seven LEDs is called a segment.
Introduction
• Each one of the seven LEDs in the display is given a positional segment which
is controlled by one pin.
• These LED pins are labeled a, b, c, d, e, f, and g representing each individual
LED.
• The other LED pins are connected together and wired to form a common pin.
• By forward biasing the appropriate pins of the LED segments, some segments
will be light and others will be dark allowing the desired character pattern of
the number to be generated on the display.
• This allows us to display each of the ten decimal digits 0 through to 9 or hexa
decimal numbers 0 through to F , on the same 7-segment display.
• An additional 8th LED is sometimes used within the same package thus
allowing the indication of a decimal point, (DP) when two or more 7-segment
displays are connected together to display numbers greater than ten.
Types of 7-Segment Displays
• The displays common pin is generally used to identify
which type of 7-segment display it is.
• As each LED has two connecting pins, one called the
“Anode” and the other called the “Cathode”.
• Therefore , there are two types of LED 7-segment
display:
– 1. Common Cathode (CC)
– 2. Common Anode (CA)
Common Cathode (CC)
• In the common cathode display,
– all the cathode connections of the LED segments are joined
together to logic “0″ or ground.
– The individual segments are illuminated by application of a
“HIGH”, or logic “1″ signal via a current limiting resistor to
forward bias the individual Anode terminals (a-g).
Common Anode (CA)
• In the common anode display,
– all the anode connections of the LED segments are joined
together to “HIGH”, or logic “1″.
– The individual segments are illuminated by application of a
logic “0″ or ground signal via a suitable current limiting
resistor to the Cathode of the particular segment (a-g).
Displaying Digital Digits
• Depending upon the decimal digit to be
displayed, the particular set of LEDs is forward
biased.
• The various digits from 0 through 9 can be
displayed using a 7-segment display as shown.
Displaying Digital Digits (Cont..)
3 1 0 0 1 1 1 1 F G B
4 1 1 0 0 1 1 0 E D C
5 1 1 0 1 1 0 1
6 1 1 1 1 1 0 1
7 0 0 0 0 1 1 1
8 1 1 1 1 1 1 1
9 1 1 0 1 1 1 1
Driving a 7-Segment Display
• Although a 7-segment display can be thought of as a single
display, it is still seven individual LEDs within a single package
and as such these LEDs need protection from over-current.
• LEDs produce light only when it is forward biased with the
amount of light emitted being proportional to the forward
current.
• This means that an LEDs light intensity increases in an
approximately linear manner with an increasing current.
• So this forward current must be controlled and limited to a safe
value by an external resistor to prevent damaging the LED
segments.
Driving a 7-Segment Display (Cont..)
• The forward voltage drop across a red LED segment is very low
at about 2-to-2.2 volts.
• To illuminate correctly, the LED segments should be connected
to a voltage source in excess of this forward voltage value with a
series resistance used to limit the forward current to a
desirable value.
• Typically for a standard red colored 7-segment display, each LED
segment can draw about 15mA to illuminated correctly, so on a
5 volt digital logic circuit,
– the value of the current limiting resistor would be about
200Ω (5v – 2v)/15mA, or
– 220Ω to the nearest higher preferred value.
Driving a 7-Segment Display (Cont..)
• In this example,
– the segments of a common anode display are illuminated
using the switches.
– If switch a is closed, current will flow through the “a” segment
of the LED to the current limiting resistor connected to pin a
and to 0 volts, making the circuit.
– Then only segment a will be illuminated.
– If we want the decimal number “4″ to
illuminate on the display, then switches
b, c, f and g would be closed to light the
corresponding LED segments.
Interfacing 7-Segment Display to Arduino
//int
CommonAnode[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,
0x48,0x03,0x46,0x21,0x06,0x0E};
int
CommonCathode[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6
F, 0x77,0x7C,0x39,0x5E,0x79,0x71};
void setup()
{
DDRD=B11111111; //pins 7 to 0, set port D as output i.e. pin-0
to A-led
//DDRD=0xFF; //pins 7 to 0 (port D), configured/set as output
port
}
void loop()
{
for(int j=0;j<10;j++)
{
Proteus Design (CommonCathode)
•
LCD Interfacing with Arduino
Agenda
• Introduction to LCD
• LCD Interfacing (8-bit mode)
• LCD Interfacing (4-bit mode)
• LCD Library Commands
• Still message display on LCD
• Moving message display on LCD
• Custom message display on LCD
Introduction
• Liquid crystal displays (LCDs) offer a convenient and
inexpensive way to provide a user interface for a
project.
LCD pin configuration:
Introduction (Cont..)
LCD Interfacing (8-bit mode)
int commandvalues[]={0x38,0x0E,0x0F,0x06,0x01,0xC5};
int displaymessage[]={‘L’,’O’,’V’,’E’,’L’,’Y’}; void setup()
void command(unsigned int x) {
{ DDRD=B11111111; //set port D as output
PORTD=commandvalues[x]; pinMode(10,OUTPUT);
digitalWrite(10,LOW); pinMode(9,OUTPUT);
digitalWrite(9,LOW); pinMode(8,OUTPUT);
digitalWrite(8,HIGH); }
delay(250); void loop()
digitalWrite(8,LOW); {
} for(int x=0;x<=5;x++)
void lcddata1(unsigned int y) {
{ command(x);
PORTD=displaymessage[y]; delay(250);
digitalWrite(10,HIGH); }
digitalWrite(9,LOW); for(int y=0;y<=5;y++)
digitalWrite(8,HIGH); {
delay(250); lcddata1(y);
digitalWrite(8,LOW); delay(250);
} }
}
LCD Interfacing (4-bit mode)
LCD Interfacing (4-bit mode)
// Arduino sketch to display: **Hello Students** in Line-0 and **Study Well** in Line-1 of LCD
#include <LiquidCrystal.h> // include the library code
//constants for the number of rows and columns in the LCD
const int numRows = 2; //2 is a decimal number
const int numCols = 16; //16 is a decimal number
void setup()
{
lcd.begin(numCols, numRows); // lcd.begin(16, 2); //set up the LCD's number of columns and rows
}
void loop()
{
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print(" **Hello Students** "); //(note: line 0 is the 1st row, since counting begins with 0)
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(" **Study Well** "); //(note: line 1 is the 2nd row, since counting begins with 0)
}
LCD Library Commands
begin()
Description
Initializes the interface to the LCD screen, and specifies the dimensions (width
and height) of the display. begin()needs to be called before any other LCD
library commands.
Syntax
lcd.begin(cols, rows)
Parameters
lcd: a variable of type LiquidCrystal
cols: the number of columns that the display has
rows: the number of rows that the display has
LCD Library Commands (Cont..)
setCursor()
Description
Position the LCD cursor; that is, set the location at which subsequent text
written to the LCD will be displayed.
Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiquidCrystal
col: the column at which to position the cursor (with 0 being the first column)
row: the row at which to position the cursor (with 0 being the first row)
LCD Library Commands (Cont..)
write()
Description
Write a character to the LCD.
Syntax
lcd.write(data)
Parameters
lcd: a variable of type LiquidCrystal
data: the character to write to the display
LCD Library Commands (Cont..)
print()
Description
Prints text to the LCD.
Syntax
lcd.print(data)
lcd.print(data, BASE)
Parameters
lcd: a variable of type LiquidCrystal
LCD Library Commands (Cont..)
cursor() noCursor()
Description: Description
Display the LCD cursor Hides the LCD cursor.
an underscore (line) at
the position to which the
next character will be
written. Syntax
lcd.noCursor()
Syntax
lcd.cursor()
LCD Library Commands (Cont..)
noDisplay() display()
Description Description
Turns off the LCD display, without Turns on the LCD display, after it's
losing the text currently shown on it. been turned off with noDisplay(). This
will restore the text (and cursor) that
was on the display.
Syntax
Syntax
lcd.display()
lcd.noDisplay()
LCD Library Commands (Cont..)
setCursor()
Description
Position the LCD cursor; that is, set the location at which subsequent text written to
the LCD will be displayed.
Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiquidCrystal
col: the column at which to position the cursor (with 0 being the first column)
row: the row at which to position the cursor (with 0 being the first row)
LCD Library Commands (Cont..)
scrollDisplayLeft() scrollDisplayRight()
Description Description
Scrolls the contents of Scrolls the contents of
the display (text and the display (text and
cursor) one space to cursor) one space to
the left. the right.
Syntax Syntax
lcd.scrollDisplayLeft() lcd.scrollDisplayRight()
LCD Library Commands (Cont..)
leftToRight() rightToLeft()
Description Description
Set the direction for text written to
Set the direction for text written to
the LCD to right-to-left (the default is
the LCD to left-to-right, the default. left-to-right). This means that
This means that subsequent subsequent characters written to the
characters written to the display will display will go from right to left, but
go from left to right, but does not does not affect previously-output text.
affect previously-output text.
Syntax
Syntax lcd.rightToLeft()
lcd.leftToRight()
LCD Library Commands (Cont..)
clear()
Description
Clears the LCD screen and positions the cursor in the
upper-left corner.
Syntax
lcd.clear()
Parameters
lcd: a variable of type LiquidCrystal
Eg1. Still Message Display On LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop()
{
lcd.setCursor(0,1);
delay(250);
}
Eg2. Moving Message Display On LCD
#include <LiquidCrystal.h>
char message[]={"Lovely Professional University"};
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void loop()
void setup() {
{ lcd.setCursor(0, 0);
lcd.begin(16, 2);
} for (int i = 0; i <= 30; i++)
{
lcd.print(message[i]);
delay(200);
if(i>16)
{
lcd.autoscroll();
}
}
}
Eg3. LCD Custom Message Display
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
// create new custom characters
lcd.createChar(0, customChar);
lcd.createChar(1, customChar1);