You are on page 1of 8

University of Engineering and Technology, Lahore

Spring 2016

ARDUINO 1: SEVEN SEGMENT DISPLAYS AND PWM


Name
Regd-No

:
:

Date :

OBJECTIVES:

Extend I/O programming to handle multiple pins at the same time.


Learn how to use analogWrite() function to create PWM.

SUGGESTED READING:

Lab Lecture 3: I/O Programming


http://arduino.cc/en/Guide/HomePage
http://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html
http://www.newtechandme.com/arduino/arduino-tutorial-lesson-4-2-multiplexing-2-digit7-segment-display/
http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds?view=all
http://playground.arduino.cc/uploads/Documentation/Arduino_programming_notebook.p
df
Please read through all the suggested reading before you come to lab.

EQUIPMENT AND COMPONENTS:


Arduino UNO or similar Board
Standard A-B type USB cable
7-segment display
3-digit Mux. 7-segment display
LED matrix display
Jumper wires
Push Button
---------------------------------------------------------------------------------------------------------------------

INTRODUCTION:
SEVEN SEGMENT DISPLAY

A seven-segment display (abbreviation: "7-seg(ment)


display"), , is a form of electronic display device for
displaying decimal and hexadecimal numbers. Sevensegment displays are widely used in digital clocks,
electronic meters, and other electronic devices for
displaying numerical information. A seven segment
display, as its name indicates, is composed of seven
elements. Individually on or off, they can be combined to
MCT-223: EMBEDDED SYSTEMS

Fig.1.1: Seven-Segment Display

1
ARD 1

University of Engineering and Technology, Lahore

Spring 2016

produce simplified representations of the numbers. The segments of a 7-segment


display are referred to by the letters A to G, as shown to the right, where the
optional DP decimal point (an "eighth segment") is used for the display of noninteger numbers. The seven elements present in a display are basically seven
LEDs having cathode and anode
terminal. And individual led gets on when
a positive terminal of dc (3-5 Volts with
resistance of R > 100 ohms in series with
led to limit the current) is applied to
anode and negative to cathode.
Seven segments are available in
two different schemes i-e
1) Common cathode
2) Common anode
In a common cathode the cathode terminal Fig.1.2: Internal Structure of a 7-seg Display
of all LEDs are common and a separate
anode terminal of all LEDs are available whereas its vice versa for common
anode. Fig.1.2 shows the internal structure and pin diagram of a seven-segment
display made from LEDs.
ARDUINO AND SEVEN SEGMENT DISPLAY:

The seven segment display LEDs can be connected to the I/O pins of
Arduino. Here is the pin mappings of the display, note that common anode displays
will have reversed wiring:

Fig.1.3: Pin mappings for a common cathode


7Segment display

Use your solder-less breadboard to make the


connections between the seven segment LED and
your Arduino board.

MCT-223: EMBEDDED SYSTEMS

2
ARD 1

University of Engineering and Technology, Lahore

Spring 2016

Example code:
This Arduino sketch counts down from 9 to 0. This is a nice, compact version that
uses a 2 dimensional array to hold the LED bit patterns, and "for" loops to get
things
done.
(Sample
code
and
diagrams
taken
http://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html)

from:

// Define the LED digit patters, from 0 - 9


// Note that these patterns are for common cathode displays
// For common anode displays, change the 1's to 0's and 0's to 1's
// 1 = LED on, 0 = LED off, in this order:
// Arduino pins: a,b,c,d,e,f,g =pins 2,3,4,5,6,7,8
byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 },
// = 0
seven_seg_digits is a 2 dimensional array
{ 0,1,1,0,0,0,0 },
// = 1
that contains information about the led
{ 1,1,0,1,1,0,1 },
// = 2
combination to show a particular digit
{ 1,1,1,1,0,0,1 },
// = 3
[10][7] means this array has 10 rows, and
{ 0,1,1,0,0,1,1 },
// = 4
7 columns.
{ 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
};
void writeDot(byte dot)
{
// the writeDot function writes the logic 0 or 1 to the dot pin
digitalWrite(9, dot);
}
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
writeDot(0);
}
void sevenSegWrite(byte digit)
{
byte pin = 2;
for (byte segCount = 0; segCount < 7; ++segCount)
{
digitalWrite(pin, seven_seg_digits[digit][segCount]);
++pin;
}
}

void loop()
{
for (byte count = 10; count > 0; --count)
{
delay(1000);

// declaring the used pins as output pins


// (low impedance state) because we want
// to write a digital value (logic 1 or 0) to it

// start with the "dot" off

// sevenSegWrite writes a particular combination to the


// pins connected to the seven segment display

// loop to call elements from the 2D array


// delay function for 1000ms delay

MCT-223: EMBEDDED SYSTEMS

3
ARD 1

University of Engineering and Technology, Lahore


sevenSegWrite(count - 1);
}
delay(4000);

Spring 2016

// calling the write function to update the I/O pin status

TASK 1_1:

Attach a push button to pin13 using a PULL DOWN resistor of 10k.


Sketch change:
o Modify the code such that the value on the seven segment display
decrements once when the button is pressed.
o Modify the code again to make an up counter (the value increments on
each button press)

MULTIPLEXED SEVEN SEGMENT DISPLAYS:

A multiplexed seven segment display has two or more seven segment


displays connected together in one package. The control requires fast switching
(around 10ms intervals) between elements to display all the digits together.

Fig.1.4: Multiplexed 3-digit seven segment display

Fig.1.4 shows the pin description for a common cathode multiplexed 3 digit seven
segment display. Only eight pins are required to send the control signals, and three
additional pins to switch between the digits. The switching must be carried out
using transistors to make sure the Arduino pins dont get damaged by excessive
current draw.

MCT-223: EMBEDDED SYSTEMS

4
ARD 1

University of Engineering and Technology, Lahore

Spring 2016

Fig.1.5: Schematic for a 4 digit MUX 7-segment display

Fig.1.6: MUX seven segment display


using Arduino

The seven segment displays come in Mux packages of four digits as well. (Fig.4.6
for schematic view)

TASK 1_2:

Attach the 3-digit seven seg. display to Arduino.


Sketch change:
o Modify the code to make an up-counter that counts from 0~999.

MCT-223: EMBEDDED SYSTEMS

5
ARD 1

University of Engineering and Technology, Lahore

Spring 2016

PWM and analogWrite() Function:

The analogWrite() function is used to generate a PWM signal at the


dedicated pin of Arduino. On most Arduino boards (those with the ATmega168 or
ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino
Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only
support analogWrite() on pins 9, 10, and 11.
Syntax:
analogWrite(pin,value);
Parameters:
pin: the pin to write to
value: the duty cycle: between 0 (always OFF) and 255 (always ON)
Returns:
Nothing
The desired pin must be set as an OUTPUT pin before analogWrite() function can
be used. Setting a duty cycle of 255 sets the related pin to a constant +5V, while a
duty cycle of 0 sets the related pin to a constant 0V. As is, this function will suffice
for most applications, except for those which require a specific base frequency. In
anaogWrite(), the frequency of the PWM signal is approximately 490 Hz.
Pulse Width Modulation, or PWM, is a technique for getting analog results with
digital means. Digital control is used to create a square wave, a signal switched
between on and off. This on-off pattern can simulate voltages in between full on (5
Volts) and off (0 Volts) by changing the portion of the time the signal spends on
versus the time that the signal spends off. The duration of "on time" is called the
pulse width. To get varying analog values, you change, or modulate, that pulse
width. If you repeat this on-off pattern fast enough with an LED for example, the
result is as if the signal is a steady voltage between 0 and 5v controlling the
brightness of the LED.
In the graphic below, the green lines represent a regular time period. This
duration or period is the inverse of the PWM frequency. In other words, with
Arduino's PWM frequency at about 500Hz, the green lines would measure 2
milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that
analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127)
is a 50% duty cycle (on half the time) for example.(Fig.1.7)

MCT-223: EMBEDDED SYSTEMS

6
ARD 1

University of Engineering and Technology, Lahore

Spring 2016

Example:
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/
int led = 9;
int brightness = 0;
int fadeAmount = 5;

// the pin that the LED is attached to


// how bright the LED is
// how many points to fade the LED by

void setup()
{
pinMode(led, OUTPUT);

// declare pin 9 to be an output:

void loop()
// the loop routine runs over and over again forever
{
analogWrite(led, brightness);
// set the brightness of pin 9:
brightness = brightness + fadeAmount;

if (brightness == 0 || brightness == 255)

Fig.1.7: Duty cycle and analogWrite()

// change the brightness for next time through the loop:

// reverse the direction of the fading at the ends of the fade:

{
fadeAmount = -fadeAmount ;
}
delay(30);

// wait for 30 milliseconds to see the dimming effect

TASK 1_3:

Attach a push button to pin13 using a PULL UP resistor of 10k.


Sketch change:
o Modify the code such that the brightness of the LED changes by 10%
on each button press.

MCT-223: EMBEDDED SYSTEMS

7
ARD 1

University of Engineering and Technology, Lahore

Spring 2016

RGB LEDs:

RGB LEDs consist of three LEDs in a single


package (Fig.1.8), and a common terminal
(common anode or common cathode).
To create a specific color, the ratio of power to
each color must be controlled using the
analogWrite() function.
Fig.1.8: RGB LED

TASK 1_4:

Attach a push button to pin2 using a PULL


UP resistor of 10k.
Sketch change:
o Create a sketch to make the LED
cycle through at least 9 distinct colors.
Use pins 9, 10, and 11 for
analogWrite() (Fig.1.9 shows a
common cathode)
TIP: you can use three different color LEDs if
RGB led is not available. Use one terminal as
common to create a common anode or a common
cathode version.

Fig.1.9: RGB LED control and color mixing

Bonus Task:[+2]
Modify the sketch such that the LED cycles through all the rainbow colors
smoothly (diffusing one color into another).

MCT-223: EMBEDDED SYSTEMS

8
ARD 1

You might also like