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

Arduino1

The document provides an introduction to Arduino, detailing its components, programming environment, and various applications including interfacing with sensors and motors. It explains the functionalities of microcontrollers, the types of Arduino boards, and the process of setting up the Arduino IDE. Additionally, it covers serial communication, digital inputs, and interfacing with a seven-segment display.

Uploaded by

Alan John
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)
6 views

Arduino1

The document provides an introduction to Arduino, detailing its components, programming environment, and various applications including interfacing with sensors and motors. It explains the functionalities of microcontrollers, the types of Arduino boards, and the process of setting up the Arduino IDE. Additionally, it covers serial communication, digital inputs, and interfacing with a seven-segment display.

Uploaded by

Alan John
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/ 99

Introduction and

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.

• It is an open source hardware, any one can get the details


of its design & modify it or make his own one himself.
What can it do?
• Sensors ( to sense stuff )

– Push buttons, touch pads, tilt switches.


– Variable resistors (eg. volume knob / sliders)
– Photoresistors (sensing light levels)
– Thermistors (temperature)
– Ultrasound (proximity range finder)
• Actuators ( to do stuff )

– 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

Arduino Arduino Arduino


BT Nano Mini
Arduino & Arduino compatible boards:
Arduino Uno Board
Digital output
~: PWM.
0,1: Serial
port.
Arduino Uno Board Description
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
INPUT v/s OUTPUT
Referenced from the perspective of the microcontroller (electrical board).

Inputs is a signal / information Output is any signal exiting the


going into the Arduino board. Arduino board.

Examples: Buttons Switches, Examples: LEDs, DC motor,


Light Sensors, Flex Sensors, servo motor, a piezo buzzer,
Humidity Sensors, Temperature relay, an RGB LED
Sensors…

https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
Getting Started (Installing Arduino IDE and Setting up Arduino Board)

Check out: http://arduino.cc/en/Reference/HomePage ,


http://arduino.cc/en/Guide/HomePage .
1. Download & install the Arduino environment (IDE)
https://www.arduino.cc/en/Main/Software
2. Connect the board to your computer via the USB cable
3. If needed, install the drivers
4. Launch the Arduino IDE
5. Select your board
6. Select your serial port
7. Open the blink example
8. Upload the program
Basic Procedure

• Design the circuit:


– What are electrical requirements of the sensors or actuators?
– Identify inputs (analog inputs)
– Identify digital outputs

• Write the code:


– Build incrementally
• Get the simplest piece to work first
• Add complexity and test at each stage
• Save and Backup frequently
– Use variables, not constants
– Comment liberally
Writing and Uploading Code into Arduino
Running Code
• Running Code While Tethered

• Running Code Stand-Alone


Arduino IDE

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

Settings: Tools → Serial Port

Your computer
communicates to the
Arduino microcontroller via a
serial port → through a USB-
Serial adapter.

Check to make sure that the


drivers are properly installed.
Arduino IDE (Cont..)

Settings: Tools → Board


Next, double-check that the proper board is selected under the
Tools→Board menu.
Breadboard
Arduino Digital I/0
pinMode(pin, mode);
Sets pin to either INPUT or OUTPUT
Eg1. pinMode(13, OUTPUT);

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

const int ledPin = 13; // LED connected


to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the
LED on
delay(2000); // wait for two seconds
digitalWrite(ledPin, LOW); // set the LED
off
delay(2000); // wait for two seconds
}
Uploading and Running the blink sketch (Cont..)

• Write your sketch/program


• Press compile button
(to check for errors)
• Press upload button to program
Arduino board with your sketch
• Try it out with the “Blink” sketch!

Arduino programs can be divided in three


main parts: Values(Variables & Constants),
Structure, and functions.
Proteus design
Serial Communication
Using Serial Communication

• Method used to transfer data between two devices.

Data passes between the computer and Arduino


through the USB cable. Data is transmitted as zeros
(‘0’) and ones (‘1’) sequentially.

Arduino dedicates: Digital I/O pin # 0


to receiving(Rx) and Digital I/O pin # 1 to
transmit(Tx).
Using Serial Communication (Cont..)

• Serial communications provide an easy and flexible way for


your Arduino board to interact with your computer and other
devices. This chapter explains how to send and receive
information using this capability.
• You can also send data from the Serial Monitor to Arduino by
entering text in the text box to the left of the Send button.
• Baud rate is selected using the drop-down box on the bottom
right. You can use the drop down labeled “No line ending” to
automatically send a carriage return or a combination of a
carriage return and a line at the end of each message sent when
clicking the Send button.
• Your Arduino sketch can use the serial port to indirectly access
(usually via a proxy program written in a language like
Processing) all the resources (memory, screen, keyboard, mouse,
network connectivity, etc.) that your computer has. Your
computer can also use the serial link to interact with sensors or
other devices connected to Arduino.
Serial Monitor & analogRead()

Initializes the Serial


Communication

9600 baud data rate prints data


to serial bus
Serial Monitor & analogRead() Cont..

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

• To get a digital reading:


int buttonState = digitalRead (pinNumber);

• Digital Input values are only HIGH (On) or LOW (Off)


Digital Input (Cont..)
// Pushbutton sketch: a switch is connected to pin 2 lights the LED on pin 13
// (or) Control an LED connected to pin-13 w.r.to. a switch connected to pin-2,
with the help of external resister

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

• Table 1: Display decimal digits using the 7-segments (Common Cathode)


Decimal Digit G F E D C B A
0 0 1 1 1 1 1 1
1 0 0 0 0 1 1 0
2 1 0 1 1 0 1 1 A

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

• So to understand how the segments of the display are connected


to a 220Ω current limiting resistor consider the circuit below.
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

• The 7-Segment Display (shown in Figure below) can be interfaced to the


Arduino Uno Board as shown in the next two slides.
• Power supply of 5V DC is provided, either through Vcc1 pin or Vcc2 pin.,
with respect to a resister connected across.

Pin configuration of the 7-Segment


display with common anode
G F Vcc1 A B
CommonAnode(CA):
Com: Vcc=5V (through a Resistor)
A – G : Vcc=5V i.e. bibary ‘1’ => LEDs (A – G) will be OFF
A – G : Gnd=0V i.e. bibary ‘0’ => LEDs (A – G) will be ON
CommonCathode(CC):
Com: Gnd=0v (through a Resistor)
A – G : Vcc=5V i.e. bibary ‘1’ => LEDs (A – G) will be ON
A – G : Gnd=0V i.e. bibary ‘0’ => LEDs (A – G) will be OFF
E D Vcc2 C DP
Glowing the Led’s of 7-segment display
//Arduino sketch to test common anode 7 segment//
//Arduino pins from 0-7 are connected with a-g pins of common anode 7 segment display//
void setup() void loop()
{
{
// loop to turn leds of seven seg ON
// define pin modes for(int i=6;i<13;i++) //for_loop-1
{
pinMode(6,OUTPUT); //pin6-a digitalWrite(i, LOW); //
led digitalWrite(6-12,LOW);
pinMode(7,OUTPUT); //pin7-b delay(500); //500ms
led }
//delay(1000); //1sec delay
pinMode(8,OUTPUT); //pin8-c // loop to turn leds of seven seg OFF
led for(int i=6;i<13;i++) //for_loop-2
pinMode(9,OUTPUT); //pin9-d {
led digitalWrite(i, HIGH); //
digitalWrite(6-12,HIGH);
pinMode(10,OUTPUT); //pin10- delay(500); //0.5 seconds
e led }
Glowing the Led’s of 7-segment display(Cont..)
//Arduino sketch to test common anode 7 segment//
//Arduino pins from 0-7 are connected with a-g pins of common anode 7 segment display//
void setup() void loop()
{
{
// loop to turn leds of seven seg ON
// define pin modes for(int i=0;i<7;i++) //for_loop-1
{
pinMode(0,OUTPUT); //pin0-a led digitalWrite(i, LOW); //
pinMode(1,OUTPUT); //pin1-b leddigitalWrite(0-6,LOW);
//delay(500); //500ms
pinMode(2,OUTPUT); //pin2-c led }
delay(1000); //1sec delay
pinMode(3,OUTPUT); //pin3-d led
// loop to turn leds of seven seg OFF
pinMode(4,OUTPUT); //pin4-e led for(int i=0;i<7;i++) //for_loop-2
{
pinMode(5,OUTPUT); //pin5-f led digitalWrite(i, HIGH); //
pinMode(6,OUTPUT); //pin6-g leddigitalWrite(0-6,HIGH);
//delay(500); //500ms
} }
Display 0 and 1 on 7-segment display(Cont..)
//Arduino sketch to test common anode 7 segment//
//Arduino pins from 0-7 are connected with a-g pins of common anode 7 segment display//
void setup() void loop()
{
{ digitalWrite(0, LOW); // a-led ON
digitalWrite(1, LOW); // b-led ON
pinMode(0,OUTPUT); //pin0-a led digitalWrite(2, LOW); // c-led ON
digitalWrite(3, LOW); // d-led ON
pinMode(1,OUTPUT); //pin1-b led digitalWrite(4, LOW); // e-led ON
digitalWrite(5, LOW); // f-led ON
digitalWrite(6, HIGH); // g-led OFF
pinMode(2,OUTPUT); //pin2-c led digitalWrite(7, LOW); // dp-led ON
delay(1000); //1sec delay
pinMode(3,OUTPUT); //pin3-d led digitalWrite(0, HIGH); // a-led OFF
digitalWrite(1, LOW); // b-led ON
pinMode(4,OUTPUT); //pin4-e led digitalWrite(2, LOW); // c-led ON
digitalWrite(3, HIGH); // d-led OFF
pinMode(5,OUTPUT); //pin5-f led digitalWrite(4, HIGH); // e-led OFF
digitalWrite(5, HIGH); // f-led OFF
pinMode(6,OUTPUT); //pin6-g led digitalWrite(6, HIGH); // g-led OFF
digitalWrite(7, LOW); // dp-led ON
pinMode(7,OUTPUT); //pin7-dp delay(1000); //1sec delay
led }
Proteus Design (CommonAnode)

Display 0 and 9 on 7-segment display(Cont..)
//Arduino sketch to test common anode 7 segment//
//Arduino pins from 0-7 are connected with a-g pins of common anode 7 segment display//
void setup()
{
DDRD=B11111111; //pins 7 to 0 (port D), configured/set as output port //or
//DDRD=0xFF; // in hex. notation //pins 7 to 0 (port D), configured/set as output port
}
void loop()
{
//PORTD= B01000000; // to display-0 on 7-seg. display //or
PORTD= 0x40; // in hex. notation
delay(1000);
PORTD= 0x79;
delay(1000);
PORTD= 0x24;
delay(1000);
PORTD= 0x30;
delay(1000);
PORTD= 0x19;
delay(1000);
PORTD= 0x12;
delay(1000);
PORTD= 0x02;
delay(1000);
PORTD= 0x78;
delay(1000);
PORTD= 0x00;
delay(1000);
PORTD= 0x10;
delay(1000);
}
Registers of ATmega328
• All the work of MCU happens through registers
(special memory locations)
– Registers on the Atmega328 are 8-bits wide
• The data direction register (DDRx) handles the data
directions for pins in PORTx
Registers of ATmega328 (Cont..)

Data Direction Register:


• If the bit is zero -> pin will be an input
– Making a bit to be zero == ‘clearing the bit’
• If the bit is one -> pin will be an output
– Making a bit to be one == ‘setting the bit’
• To change the data direction for a set of pins belonging
to PORTx at the same time:
1. Determine which bits need to be set and cleared in DDRx
2. Store the binary number or its equivalent (in an alternate
base, such as hex) into DDRx
Registers of ATmega328 (Cont..)

• For digital IO, the important registers are:


– DDRx
• Data Direction bit in DDRx register: i/p or o/p (read/write)
– PORTx
• PORTx data register: controls wether the pin is HIGH or LOW (read/
write)
– PINx
• PINx register: to read the status of INPUT pins (read only)
Ports of ATmega328
PORTD maps to Arduino digital pins 0 to 7

DDRD - The Port D Data Direction Register - read/write


PORTD - The Port D Data Register - read/write
PIND - The Port D Input Pins Register - read only
PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to

the crystal pins and are not usable

DDRB - The Port B Data Direction Register - read/write


PORTB - The Port B Data Register - read/write
PINB - The Port B Input Pins Register - read only
PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on

the Arduino Mini

DDRC - The Port C Data Direction Register - read/write


PORTC - The Port C Data Register - read/write
PINC - The Port C Input Pins Register - read only
Displaying Hexa-Decimal Values: 0 to F

//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

//initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LiquidCrystal lcd(Rs,En,D4,D5,D6,D7)
//LiquidCrystal LPU(12, 11, 5, 4, 3, 2); // LiquidCrystal lcd(Rs,En,D4,D5,D6,D7)

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

• The LCD display has two lines of characters, 16


characters per line.
• Each character is composed of matrix of pixels size 5x8.
Eg3. LCD Custom Message Display (Cont..)

DDRAM Memory (Display Data RAM)


• Display data RAM (DDRAM) stores the information we
send to LCD in ASCII Code.
• For each letter there is a special code that represents it.
• For example, the letter A in ASCII code, “receives” a value
of 65 in base 10 or 01000001 in binary base, or 41 in the
base 16.
• The memory can contain up to 80 letters.
Eg3. LCD Custom Message Display (Cont..)

CGRAM Memory (Character Generator RAM)


• Using CGRAM memory the user can “build” and store their
own letters.
• For 5x8 dots, eight character patterns can be written,
• And for 5x10 dots, four character patterns can be written.
• The difference between the memories is that the DDRAM
memory displays on the screen the “ready” characters in
accordance with the ASCII code, while the CGRAM
memory displays the special characters that the user has
created.
Eg3. LCD Custom Message Display (Cont..)

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte customChar[8] = { byte customChar1[8] = {


0b11111, 0b11111,
0b00100, 0b00101,
0b00100, 0b01001,
0b11100, 0b10111,
0b10111, 0b10101,
0b11101, 0b10111,
0b00101, 0b01001,
0b00101 0b00101
}; };
Eg3. LCD Custom Message Display (Cont..)

void setup()
{
// create new custom characters
lcd.createChar(0, customChar);
lcd.createChar(1, customChar1);

// set up number of columns and rows


lcd.begin(16, 2);
lcd.write((byte)0);
lcd.write((byte)1);
}
void loop()
{
}

You might also like