Arduino 3
Arduino 3
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
F G B
3 1 0 0 1 1 1 1
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,0x6F,
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++)
{
//PORTD= CommonAnode[j];
PORTD= CommonCathode[j]; // PORTD= CommonCathode[0]; => 0x3F -->’0’
delay(1000);
}
}
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.
Introduction (Cont..)
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)
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
Syntax lcd.noCursor()
lcd.cursor()
LCD Library Commands (Cont..)
noDisplay() display()
Description Description
Turns off the LCD display, without Turns on the LCD display, after it's been
losing the text currently shown on it. 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 the cursor) one space to the
left. right.
Syntax Syntax
lcd.scrollDisplayLeft() lcd.scrollDisplayRight()
LCD Library Commands (Cont..)
leftToRight() rightToLeft()
Description Description
Set the direction for text written to the
Set the direction for text written to the
LCD to right-to-left (the default is left-
LCD to left-to-right, the default. This to-right). This means that subsequent
means that subsequent characters characters written to the display will
written to the display will go from left go from right to left, but does not
to right, but does not affect affect previously-output text.
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);
• We will connect the Arduino to IN1 (pin 5), IN2 (pin 7),
and Enable1 (pin 6) of the L293d IC. Pins 5 and 7 are
digital.,
• i.e. ON or OFF inputs, while pin 6 needs a pulse-width
modulated (PWM) signal to control the motor speed.
Arduino Sketch
the DC motor
void setup()
{
pinMode(pwm,OUTPUT) ; //we have to set PWM pin as output
pinMode(in_2,OUTPUT) ;
}
Arduino Sketch (Cont..)
void loop()
{
//For Clock wise motion , in_1 = High , in_2 = Low
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ; //2^8= 256 (0-255)
delay(3000) ; //3sec.
/*setting pwm of the motor to 255 we can change the speed of rotaion by chaning pwm input but we are only using
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000); //1sec
//For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(3000);
//For brake
//3sec
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,LOW) ;
delay(1000) ; //1sec
}
Servo Motor Interfacing With
Arduino
Agenda
• Introduction
• Circuit connections
• Arduino sketch
Introduction
• Inside the micro servo, you will find the pieces like
they are showing in the image below.
• The top cover hosts the plastic gears while the middle
cover hosts a DC motor, a controller, and the
potentiometer.
Introduction (Cont..)
• The servo motor has three leads, with one more than
a DC motor.
• Ultra-low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion
(while requesting data)
• Good for 20-80% humidity readings with 5%
accuracy
• Good for 0-50°C temperature readings ±2°C
accuracy
DHT22 Specifications
• Low cost
• 3 to 5V power and I/O
• 2.5mA max current use during conversion
(while requesting data)
• Good for 0-100% humidity readings with 2-5%
accuracy
• Good for -40 to 125°C temperature readings
±0.5°C accuracy
• Before you can use the DHT11 on the Arduino, you’ll need to install the
DHTLib library.
• https://github.com/adafruit/DHT-sensor-library
• It has all the functions needed to get the humidity and temperature
readings from the sensor.
• It’s easy to install, just download the DHTLib.zip and open up the Arduino
IDE.