Dual Seven Segment Flicker Problem

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?

Flicker Video

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.

Because this delay causes a 100 ms pause... which means only the last digit updated is displayed without the flicker. Remove it and see what happens.

Ideally you shouldn't have any delay statements in your program.

It's hard to say for certain without a schematic, but I think this circuit may damage your Nano.

What value are those resistors for the display? 220R?

Thank you for your comment I try it asap.

Yes, I use 220ohm resistors. I think this is enough but what do you suggest I can change it. I try to prepare schematic and share with you guys.

Bro you best I deleted to delay and it is work. Thank you so much.

Are they connected to anodes or cathodes of the display?

I connect to the cathode side. As seen in the datasheet, I connected a 220 ohm resistor to the pins 4,7,8,10,11 and 14 of the display and then connected them to the pwm pins of the nano.

Ok then, I think this will not damage the Nano.

I can see from your code that the multiplexing is done by digit (digit 1's segments are lit, followed by digit 2's segments, then back to digit 1 etc).

Connecting the series resistors to the digit common pins and multiplexing by digit will have an unwanted side effect. When the digit shows a "1" it will be much brighter than when a digit shows "8" because the same current will be shared between the segments that are lit.

For example if the series resistor allows 14 milliamps to flow, then when "1" is displayed, each of the 2 lit segments will have 14/2=7 milliamps flowing, but when "8" is displayed, each lit segment will have only 14/7=2 milliamps flowing.