Ard1 - Using 7-Segment and PWM
Ard1 - Using 7-Segment and PWM
Please read through all the suggested reading before you come to lab.
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. Seven-
segment 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 Fig.1.1: Seven-Segment Display
void setup()
{
pinMode(2, OUTPUT); // declaring the used pins as output pins
pinMode(3, OUTPUT); // (low impedance state) because we want
pinMode(4, OUTPUT); // to write a digital value (logic 1 or 0) to it
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
writeDot(0); // start with the "dot" off
}
void loop()
{
for (byte count = 10; count > 0; --count) // loop to call elements from the 2D array
{
delay(1000); // delay function for 1000ms delay
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)
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 don’t get damaged by excessive
current draw.
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.
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)
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.
*/
void setup()
{
pinMode(led, OUTPUT); // declare pin 9 to be an output:
void loop() // the loop routine runs over and over again forever Fig.1.7: Duty cycle and analogWrite()
{
analogWrite(led, brightness); // set the brightness of pin 9:
brightness = brightness + fadeAmount; // change the brightness for next time through the loop:
if (brightness == 0 || brightness == 255) // 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.
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)
Bonus Task:[+2]
Modify the sketch such that the LED cycles through all the rainbow colors
smoothly (diffusing one color into another).