Vous êtes sur la page 1sur 25

7 Segment Display On Arduino

Step 1: Making Connections

Connect the pins described below:

Arduino Pin 2 to Pin 7.


Arduino Pin 3 to Pin 6.
Arduino Pin 4 to Pin 4.
Arduino Pin 5 to Pin 2.
Arduino Pin 6 to Pin 1.
Arduino Pin 8 to Pin 9.
Arduino Pin 9 to Pin 10.

GND to Pin 3 and Pin 8 each connected with 220 ohm resistors.

Code
Seven segment displays are of two types: common anode and common cathode. The Internal
structure of both types is nearly the same. The difference is the polarity of the LEDs and
common terminal. In a common cathode seven-segment display (the one we used in the
experiments), all seven LEDs plus a dot LED have the cathodes connected to pins 3 and pin 8.
To use this display, we need to connect GROUND to pin 3 and pin 8 and, and connect +5V to
the other pins to make the individual segments light up. The following diagram shows the
internal structure of common-cathode seven-segment display:

Common Cathode The


common anode display is the exact opposite. In a common-anode display, the
positive terminal of all the eight LEDs are connected together and then connected
to pin 3 and pin 8. To turn on an individual segment, you ground one of the pins.
The following diagram shows the internal structure of the common-anode seven-

segment display.
Common anode

The seven segment are labelled a-g, with the dot being "dp," as shown in the figure below:

SSD Configuration

To display a particular number, you turn on the individual segments as shown in the table below:

Digit gfedcba abcdefg a b c d e f g

0 03F 07E on on on on on on off

1 006 030 off on on off off off off

2 05B 06D on on off on on off on


3 04F 079 on on on on off off on

4 066 033 off on on off off on on

5 06D 05B on off on on off on on

6 07D 05F on off on on on on on

7 007 070 on on on off off off off

8 07F 07F on on on on on on on

9 06F 07B on on on on off on on

A 077 077 on on on off on on on

B 07C 01F off off on on on on on

C 039 04E on off off on on on off

D 05E 03D off on on on on off on

E 079 04F on off off on on on on

F 071 047 on off off off on on on

Experiment 1

In this experiment, we will simply turn on and turn off the LEDs to get familiar with how a
seven-segment display works.

Hardware Required
1 x seven segment display (common cathode)
1 x Arduino MEGA 2560

1 x breadboard

jumper wires
Wiring Diagram

In this circuit, the pins of seven-segment display are connected to Arduino pins 2-9, as shown in
the table below. Common pins (pin 3 and pin 8) are connected to GND and dp is left
unconnected, because it is not used in this experiment

Seven Arduino Wire


segment pins pins Color

1(e) 6 orange

2(d) 5 white

3,8(COM) GND n/a

c 4 yellow

5(dp) -

6(b) 3 red

7(a) 2 blue

9(f) 7 cyan

10(g) 8 green
Code
void setup()
{
// define pin modes
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);

void loop()
{
// loop to turn leds od seven seg ON

for(int i=2;i<9;i++)
{
digitalWrite(i,HIGH);
delay(600);
}

// loop to turn leds od seven seg OFF


for(int i=2;i<9;i++)
{
digitalWrite(i,LOW);
delay(600);
}

delay(1000);

Experiment 2

Description

In this tutorial, we will be interfacing a seven segment display with Arduino mega and learn to
display a count down from nine with a delay of a second, on seven segment display.
Hardware Required
The hardware required for this experiment is the same as for Experiment 1.

Wiring Diagram

The wiring diagram for this experiment is the same as the circuit for Experiment 1.

Code
// make an array to save Sev Seg pin configuration of
numbers

int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 0


{ 0,1,1,0,0,0,0 }, // 1
{ 1,1,0,1,1,0,1 }, // 2
{ 1,1,1,1,0,0,1 }, // 3
{ 0,1,1,0,0,1,1 }, // 4
{ 1,0,1,1,0,1,1 }, // 5
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7
{ 1,1,1,1,1,1,1 }, // 8
{ 1,1,1,0,0,1,1 }}; // 9

//function header
void Num_Write(int);

void setup()
{
// set pin modes
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);

void loop()
{

//counter loop

for (int counter = 10; counter > 0; --counter)


{
delay(1000);
Num_Write(counter-1);
}
delay(3000);
}

// this functions writes values to the sev seg pins


void Num_Write(int number)
{
int pin= 2;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array[number][j]);
pin++;
}
}
Arduino Lesson 11. LCD Displays - Part 1

Overview
by Simon Monk

In this lesson, you will learn how to wire up and use an alphanumeric LCD display.
The display has an LED backlight and can display two rows with up to 16 characters on each
row. You can see the rectangles for each character on the display and the pixels that make up
each character. The display is just white on blue and is intended for showing text.
In this lesson, we will run the Arduino example program for the LCD library, but in the next
lesson, we will get our display to show the temperature and light level, using sensors.

There are quite a few connections to be made. Lining up the display with the top of the
breadboard helps to identify its pins without too much counting, especially if the breadboard has
its rows numbered with row 1 as the top row of the board. Do not forget, the long yellow lead
that links the slider of the pot to pin 3 of the display. The 'pot' is used to control the contrast of
the display.

You may find that your display is supplied without header pins attached to it. If so, follow the
instructions in the next section.

Arduino Code
by Simon Monk

The Arduino IDE includes an example of using the LCD library which we will use. You can find
this on the File menu under Examples Liquid Crystal HelloWorld.
This example uses different pins to the ones we use, so find the line of code below:

Copy Code

1. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

and change it to be:

Copy Code

1. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

Upload the code to your Arduino board and you should see the message 'hello, world' displayed,
followed by a number that counts up from zero.

The first thing of note in the sketch is the line:

Copy Code

1. #include <LiquidCrystal.h>

This tells Arduino that we wish to use the Liquid Crystal library.

Next we have the line that we had to modify. This defines which pins of the Arduino are to be
connected to which pins of the display.

Copy Code

1. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

The arguments to this are as follows:

Display Pin Name Display Pin Number Arduino Pin (in this example) RS 4 7
E68 D4 11 9 D5 12 10 D6 13 11 D7 14 12

After uploading this code, make sure the backlight is lit up, and adjust the
potentiometer all the way around until you see the text message

In the 'setup' function, we have two commands:

Copy Code

1. lcd.begin(16, 2);
2. lcd.print("hello, world!");

The first tells the Liquid Crystal library how many columns and rows the display has. The second
line displays the message that we see on the first line of the screen.
In the 'loop' function, we aso have two commands:

Copy Code

1. lcd.setCursor(0, 1);
2. lcd.print(millis()/1000);

The first sets the cursor position (where the next text will appear) to column 0 & row 1. Both
column and row numbers start at 0 rather than 1.

The second line displays the number of milliseconds since the Arduino was reset.

Learn Arduino, Lesson 12. LCD Displays - Part 2

Overview
by Simon Monk

In this lesson, you will build on what we have learnt in lesson 11 and use a LCD display to show
the temperature and light intensity.
Light intensity is measured using the same photocell that you used in lesson 9.
To measure the temperature, you will use a temperature measurement chip. This device has just
three leads two for 5V and GND and the third lead is connected directly to an analog input on the
Arduino.

Breadboard Layout
by Simon Monk
The breadboard layout is based on the layout from lesson 11, so if you still have this on the
breadboard it will simplify things greatly.

There are a few jumper wires that have been moved slightly on this layout. In particular, those
near the pot.

The photocell, 1 k resistor and TMP36 are all new additions to the board. The TMP36 has its
curved face towards the display.

Arduino Code
by Simon Monk

The sketch for this is based on that of lesson 11. Load it up onto your Arduino and you should
find that warming the temperature sensor by putting your finger on it will increase the
temperature reading.

Also if you wave your hand over the photocell blocking out some of the light, the reading will
decrease.

Copy Code

1. /*
2. Adafruit Arduino - Lesson 12. Light and Temperature
3. */
4.
5. #include <LiquidCrystal.h>
6.
7. int tempPin = 0;
8. int lightPin = 1;
9.
10. // BS E D4 D5 D6 D7
11. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
12.
13. void setup()
14. {
15. lcd.begin(16, 2);
16. }
17.
18. void loop()
19. {
20. // Display Temperature in C
21. int tempReading = analogRead(tempPin);
22. float tempVolts = tempReading * 5.0 / 1024.0;
23. float tempC = (tempVolts - 0.5) * 100.0;
24. float tempF = tempC * 9.0 / 5.0 + 32.0;
25. // ----------------
26. lcd.print("Temp F ");
27. lcd.setCursor(6, 0);
28. lcd.print(tempF);
29.
30. // Display Light on second row
31. int lightReading = analogRead(lightPin);
32. lcd.setCursor(0, 1);
33. // ----------------
34. lcd.print("Light ");
35. lcd.setCursor(6, 1);
36. lcd.print(lightReading);
37. delay(500);
38. }

I find it useful to put a comment line above the 'lcd' command.

Copy Code

1. // BS E D4 D5 D6 D7
2. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

This makes things easier if you decide to change which pins you use.

In the 'loop' function there are now two interesting things going on. Firstly we have to convert
the analog from the temperature sensor into an actual temperature, and secondly we have to work
out how to display them.

First of all, let's look at calculating the temperature.

Copy Code

1. int tempReading = analogRead(tempPin);


2. float tempVolts = tempReading * 5.0 / 1024.0;
3. float tempC = (tempVolts - 0.5) * 100.0;
4. float tempF = tempC * 9.0 / 5.0 + 32.0;

The raw reading from the temperature sensor is first multiplied by 5 and then divided by 1024 to
give us the voltage (between 0 and 5) at the 'tempPin' analog input.

To convert the voltage coming from the TMP36 into a temperature in degrees C, you have to
subtract 0.5V from the measurement and then multiply by 100.

To convert this into a temperature in Fahrenheit, you then have to multiply it by 9/5 and then add
32.

Displaying changing readings on an LCD display can be tricky. The main problem is that the
reading may not always be the same number of digits. So, if the temperature changed from
101.50 to 99.00 then the extra digit from the old reading is in danger of being left on the display.

To avoid this, write the whole line of the LCD each time around the loop.

Copy Code
1. // ----------------
2. lcd.print("Temp F ");
3. lcd.setCursor(6, 0);
4. lcd.print(tempF);

The rather strange comment serves to remind you of the 16 columns of the display. You can then
print a string of that length with spaces where the actual reading will go.

To fill in the blanks, set the cursor position for where the reading should appear and then print it.

Exactly the same approach is used for displaying the light level. There are no units for the light
level, we just display the raw reading from the analog read.

Program for Scroll LCD

/*
LiquidCrystal Library - scrollDisplayLeft() and scrollDisplayRight()

Demonstrates the use a 16x2 LCD display. The LiquidCrystal


library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.

This sketch prints "Hello World!" to the LCD and uses the
scrollDisplayLeft() and scrollDisplayRight() methods to scroll
the text.

The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008


by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll

*/

// include the library code:


#include <LiquidCrystal.h>

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


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
delay(1000);
}

void loop() {
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}

// scroll 29 positions (string length + display length) to the right


// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
// wait a bit:
delay(150);
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}

// delay at the end of the full loop:


delay(1000);

Arduino LCD scrolling text

#include <LiquidCrystal.h>

#include <string.h>

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

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

char message[] = "This is some long message that will end up scrolling";

int previous = 0;

int pos = 0;
void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

//lcd.print(message);

void printLine(int refreshSeconds){

//Check if the current second since restart is a mod of refresh seconds ,

//if it is then update the display , it must also not equal the previously

//stored value to prevent duplicate refreshes

if((millis()/1000) % refreshSeconds == 0 && previous != (millis()/1000)){

previous = (millis()/1000);//Store the current time we entered for comparison on the next cycle

lcd.setCursor(0, 1);//Set our draw position , set second param to 0 to use the top line

char lcdTop[16];//Create a char array to store the text for the line

int copySize = 16; // What is the size of our screen , this could probably be moved outside the
loop but its more dynamic like this

if(strlen(message) < 16)

//if the message is bigger than the current buffer use its length instead;

copySize = strlen(message);
}

//Store the current position temporarily and invert its sign if its negative since we are going in
reverse

int tempPos = pos;

if(tempPos < 0)

tempPos = -(tempPos);

//Build the lcd text by copying the required text out of our template message variable

memcpy(&lcdTop[0],&message[tempPos],copySize);

lcd.print(lcdTop);//Print it from position 0

//Increase the current position and check if the position + 16 (screen size) would be larger than
the message length , if it is go in reverse by inverting the sign.

pos += 1;

if(pos +16 >= strlen(message))

pos = -(pos);

void loop() {
printLine(1);

Vous aimerez peut-être aussi