Hello;
I measure temperature with NTC and show it on common cathode 7 segment display. I measure the temperature correctly and I showed the measured value on the display, but the screen flickers.
Dual 7 Segment Display Datasheet
I shared the datasheet of the display I used above. The code I wrote as follows;
#include <Arduino.h>
#define THERMISTOR_PIN A0
#define LIMIT_SWITCH_PIN A3
#define SEV_SEG_A 2
#define SEV_SEG_B 3
#define SEV_SEG_C 4
#define SEV_SEG_D 5
#define SEV_SEG_E 6
#define SEV_SEG_F 7
#define SEV_SEG_G 8
#define DIG_1_RED_PIN 11
#define DIG_1_GREEN_PIN A2
#define DIG_1_BLUE_PIN 9
#define DIG_2_RED_PIN 12
#define DIG_2_GREEN_PIN A1
#define DIG_2_BLUE_PIN 10
float thermistorResistance, tempKelvin, tempCelsius;
const float beta = 3977.0;
const float tempRef = 298.15;
const float R0 = 10000.0;
const float R1 = 10000.0;
const int sampleNumber = 10;
float thermistorVolt[sampleNumber];
int digit_1;
int digit_2;
float averageThermistor;
float tempCelsiusForDisplay;
void Temperature();
void SEV_SEG_DISPLAY();
void DIGIT_1(int x);
void DIGIT_2(int y);
void converter(int number);
void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();
void setup()
{
analogReference(EXTERNAL);
pinMode(LIMIT_SWITCH_PIN, INPUT);
pinMode(SEV_SEG_A, OUTPUT);
pinMode(SEV_SEG_B, OUTPUT);
pinMode(SEV_SEG_C, OUTPUT);
pinMode(SEV_SEG_D, OUTPUT);
pinMode(SEV_SEG_E, OUTPUT);
pinMode(SEV_SEG_F, OUTPUT);
pinMode(SEV_SEG_G, OUTPUT);
pinMode(DIG_1_RED_PIN, OUTPUT);
pinMode(DIG_1_GREEN_PIN, OUTPUT);
pinMode(DIG_1_BLUE_PIN, OUTPUT);
pinMode(DIG_2_RED_PIN, OUTPUT);
pinMode(DIG_2_GREEN_PIN, OUTPUT);
pinMode(DIG_2_BLUE_PIN, OUTPUT);
digitalWrite(DIG_1_RED_PIN, HIGH);
digitalWrite(DIG_1_GREEN_PIN, HIGH);
digitalWrite(DIG_1_BLUE_PIN, HIGH);
digitalWrite(DIG_2_RED_PIN, HIGH);
digitalWrite(DIG_2_GREEN_PIN, HIGH);
digitalWrite(DIG_2_BLUE_PIN, HIGH);
Serial.begin(9600);
}
void loop()
{
Temperature();
SEV_SEG_DISPLAY();
}
void Temperature()
{
for (uint8_t i = 0; i < sampleNumber; i++)
{
thermistorVolt[i] = analogRead(THERMISTOR_PIN);
delay(10);
}
averageThermistor = 0;
for (uint8_t j = 0; j < sampleNumber; j++)
{
averageThermistor += thermistorVolt[j];
}
averageThermistor = averageThermistor / sampleNumber;
thermistorResistance = R1 / (1023.0 / averageThermistor - 1.0);
tempKelvin = (1.0 / tempRef) + (log(thermistorResistance / R0) / beta);
tempKelvin = 1.0 / tempKelvin;
tempCelsius = tempKelvin - 273.15;
if (abs(tempCelsius - tempCelsiusForDisplay) >= 1.0)
{
tempCelsiusForDisplay = tempCelsius;
}
}
void SEV_SEG_DISPLAY()
{
digit_1 = (int)tempCelsiusForDisplay / 10;
digit_2 = (int)tempCelsiusForDisplay % 10;
for (int i = 0; i < 30; i++)
{
DIGIT_2(digit_2);
delay(10);
DIGIT_1(digit_1);
delay(10);
}
}
void zero()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 1);
digitalWrite(SEV_SEG_F, 1);
digitalWrite(SEV_SEG_G, 0);
}
void one()
{
digitalWrite(SEV_SEG_A, 0);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 0);
digitalWrite(SEV_SEG_E, 0);
digitalWrite(SEV_SEG_F, 0);
digitalWrite(SEV_SEG_G, 0);
}
void two()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 0);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 1);
digitalWrite(SEV_SEG_F, 0);
digitalWrite(SEV_SEG_G, 1);
}
void three()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 0);
digitalWrite(SEV_SEG_F, 0);
digitalWrite(SEV_SEG_G, 1);
}
void four()
{
digitalWrite(SEV_SEG_A, 0);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 0);
digitalWrite(SEV_SEG_E, 0);
digitalWrite(SEV_SEG_F, 1);
digitalWrite(SEV_SEG_G, 1);
}
void five()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 0);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 0);
digitalWrite(SEV_SEG_F, 1);
digitalWrite(SEV_SEG_G, 1);
}
void six()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 0);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 1);
digitalWrite(SEV_SEG_F, 1);
digitalWrite(SEV_SEG_G, 1);
}
void seven()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 0);
digitalWrite(SEV_SEG_E, 0);
digitalWrite(SEV_SEG_F, 0);
digitalWrite(SEV_SEG_G, 0);
}
void eight()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 1);
digitalWrite(SEV_SEG_F, 1);
digitalWrite(SEV_SEG_G, 1);
}
void nine()
{
digitalWrite(SEV_SEG_A, 1);
digitalWrite(SEV_SEG_B, 1);
digitalWrite(SEV_SEG_C, 1);
digitalWrite(SEV_SEG_D, 1);
digitalWrite(SEV_SEG_E, 0);
digitalWrite(SEV_SEG_F, 1);
digitalWrite(SEV_SEG_G, 1);
}
void DIGIT_1(int x)
{
analogWrite(DIG_1_BLUE_PIN, 0);
analogWrite(DIG_1_RED_PIN, 0);
analogWrite(DIG_1_GREEN_PIN, 255);
analogWrite(DIG_2_BLUE_PIN, 255);
analogWrite(DIG_2_RED_PIN, 255);
analogWrite(DIG_2_GREEN_PIN, 255);
switch (x)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
}
}
void DIGIT_2(int y)
{
analogWrite(DIG_2_BLUE_PIN, 0);
analogWrite(DIG_2_RED_PIN, 0);
analogWrite(DIG_2_GREEN_PIN, 255);
analogWrite(DIG_1_BLUE_PIN, 255);
analogWrite(DIG_1_RED_PIN, 255);
analogWrite(DIG_1_GREEN_PIN, 255);
switch (y)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
}
}
I checked the screen connections. There is nothing wrong. In the for loop inside the void SEV_SEG_DISPLAY() function, if I print digit 1 first, it flickers, if I print digit2, the 2nd digit starts flickering.
for (int i = 0; i < 30; i++)
{
DIGIT_2(digit_2);
delay(10);
DIGIT_1(digit_1);
delay(10);
}
I am constantly updating the screen. Is the problem solved in the update only when the temperature value changes?
I shared the video of the system above. As you can see in the video, while digit 2 is flickering, digit 1 is lit. If I write digit1 first in the for loop in the software, this time digit 1 starts to flicker. By the way, Digit 1 is the tenth, Digit 2 is the units.