This tutorial covers the most widely and commonly used 7-segment display. A 7-segment is used to display digits or even alphabets. They are typically used for timers, but today we will use it to display voltage output of a light dependent resistor (LDR) or simply a photoresistor. This practical application will provide us with a better understanding of how a 7-segment works. So, let's get started.
7-Segment Displays:The 7-segment display, also written as “seven-segment display," consists of seven LEDs (hence its name) arranged in a rectangular fashion as shown. Each of the seven LEDs is called a segment because when illuminated the segment forms part of a numerical digit (both Decimal and Hex) to be displayed. An additional 8th LED () is sometimes used within the same package, thus allowing the indication of a decimal point, (DP) when two or more 7-segment displays are connected together to display numbers greater than ten.
STEP 1: Set Up Arduino IDE for SurilliMake sure you have selected the right port, board and processor for the Surilli as shown in the picture below and it is programmable (compile and upload “Blink” from File>Examples>Digital>Blink onto your Surilli to check if everything is working fine).
STEP 2: The CircuitryThe circuitry is a little tricky, so we will go through it first. Complete your circuit connections and then move on to the next step.
NOTE: LDRs are sold by different manufacturers so their pinouts often vary. Consult the datasheet of your specific part to find out correct pinouts.
STEP 3: Upload and Burn Code Onto Surilli- If you do not have the AnalogInput library, download it from the source given below. Unzip the downloaded.rar file and paste in into This PC > Documents > Arduino > libraries.
- Now you have completed setting up your hardware and Arduino IDE. Copy and paste the Arduino sketch given below into your Arduino IDE and hit upload.
- After it is uploaded, the 7-segment will start to show output voltage of the LDR sensor as shown in the GIF below.
Change the lightning around the LDR by placing your hand over it or flashing it with a torch light. You will see how voltage varies on the 7-segment display as you do so.
#include <AnalogInput.h>
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
void setup()
{
// define pin modes
pinMode(A5, INPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
AnalogInput analoginput (A5, 13);//AnalogInput analoginput (InputPin, OutputPin):
char pin [] = {A1, A2, A3, A4, 10, 11, 12};
void loop()
{
//counter loop
int s = analoginput.exec();
int v = (s*5)/1023;
Num_Write(v);
SerialUSB.println(s);
SerialUSB.println(v);
delay(1000);
}
// this functions writes values to the sev seg pins
void Num_Write(int number)
{
for (int j = 0; j < 7; j++) {
digitalWrite(pin[j], num_array[number][j]);
}
}
That’s all for now. If you have any queries, visit surilli.io or contact our support. Stay connected with Surilli family for more amazing stuff! :-)
Comments