Lesson 13 7-segment Display

3650

Overview

In this lesson, we will program the Arduino to achieve the controlling of a segment display.


Components

- 1 * Arduino UNO

- 1 * USB Cable

- 1 * 220Ω Resistor

- 1 * 7-segment Display

- 1 * Breadboard

- Several jumper wires



Principle

The seven-segment display is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays.

Seven-segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.

The seven-segment display is an 8-shaped LED display device composed of eight LEDs (including a decimal point). The segments respectively named a, b, c, d, e, f, g, and dp.

The segment display can be divided into two types: common anode and common cathode segment displays, by internal connections.

1.png


For a common-anode LED, the common anode should be connected to the power supply (VCC); for a common-cathode LED, the common cathode should be connected to the ground (GND).

Each segment of a segment display is composed of an LED, so a resistor is needed for protecting the LED.

A 7-segment display has seven segments for displaying a figure andone more for displaying a decimal point. For example, if you want to display a number '1', you should only light the segment b and c, as shown below.

2.png


Procedures

Step 1: Build the circuit

3.png



Step 2: Program

_13_7segment.ino

/***********************************************************
File name: 13_7segment.ino
Description:Segment display Numbers 0 to 9, each digital display 
            1 second.After the cycle show
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2015/05/02 
***********************************************************/
int a=7; //definition digital 7  pins as pin to control 'a' section(segment)
int b=6; //definition digital 6  pins as pin to control 'b' section(segment)
int c=4; //definition digital 4  pins as pin to control 'c' section(segment)
int d=11;//definition digital 11 pins as pin to control 'd' section(segment)
int e=10;//definition digital 10 pins as pin to control 'e' section(segment)
int f=8; //definition digital 8  pins as pin to control 'f' section(segment)
int g=9; //definition digital 9  pins as pin to control 'g' section(segment)
int dp=5;//definition digital 5  pins as pin to control 'dp' section(segment)
void digital_0(void) //Segment display digital 0
{
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,HIGH);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void digital_1(void) //Segment display digital 1
{
  digitalWrite(a,LOW);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,LOW);
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,LOW);
  digitalWrite(dp,LOW);
}
void digital_2(void) //Segment display digital 2
{
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,LOW);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,LOW);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void digital_3(void) //Segment display digital 3
{
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void digital_4(void) //Segment display digital 4
{
  digitalWrite(a,LOW);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,LOW);
  digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void digital_5(void) //Segment display digital 5
{
  digitalWrite(a,HIGH);
  digitalWrite(b,LOW);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void digital_6(void) //Segment display digital 6
{
  digitalWrite(a,HIGH);
  digitalWrite(b,LOW);  
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void digital_7(void) //Segment display digital 7
{
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);  
  digitalWrite(c,HIGH);  
  digitalWrite(d,LOW); 
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,LOW);
  digitalWrite(dp,LOW);
}
void digital_8(void) //Segment display digital 8
{
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void digital_9(void) //Segment display digital 9
{
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
  digitalWrite(dp,LOW);
}
void setup()
{
  int i;             //Define variables
  for(i=4;i<=11;i++)
  pinMode(i,OUTPUT); //Set up 4 ~ 11 pins to output mode
}
void loop()
{
  digital_0(); //Segment display digital 0
  delay(1000); //Delay 1s
  digital_1(); //Segment display digital 1
  delay(1000); //Delay 1s
  digital_2(); //Segment display digital 2
  delay(1000); //Delay 1s
  digital_3(); //Segment display digital 3
  delay(1000); //Delay 1s
  digital_4(); //Segment display digital 4
  delay(1000); //Delay 1s
  digital_5(); //Segment display digital 5
  delay(1000); //Delay 1s
  digital_6(); //Segment display digital 6
  delay(1000); //Delay 1s
  digital_7(); //Segment display digital 7
  delay(1000); //Delay 1s
  digital_8(); //Segment display digital 8
  delay(1000); //Delay 1s
  digital_9(); //Segment display digital 8
  delay(1000); //Delay 1s
}

Step 3: Compile the program and upload to Arduino UNO board

Now, you should see the number 0~9 and characters A~F displayed in turn on the segment display.

4.jpg


Summary

Through this lesson, you should have learned the principle and programming of the segment display. You can use what you've learned in the previous lessons to modify the code provided in this lesson to make cooler works.