Ratti3
Published

4-Digit 7-Segment Counter with LiPo Backup

A large 4-digit 7-segment counter with LiPo backup, in case USB power is cut.

BeginnerShowcase (no instructions)10,878
4-Digit 7-Segment Counter with LiPo Backup

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
I used 2N3904
×4
Resistor 100 ohm
Resistor 100 ohm
×7
Resistor 10k ohm
Resistor 10k ohm
×4
One Channel Relay Module Board Shield
relay
×1
Adafruit 4 Digit 7 Segment LED Display, 1.2" Yellow, Common Cathode
Just a standard relay from Amazon\Ebay, +5V signal with open connection when there is no signal
×1
Adafruit Powerboost 500C
×1
Battery, 3.7 V
Battery, 3.7 V
LiPo Battery 18650, 3000mAH
×1

Story

Read more

Schematics

Fritzing

Code

Code

Arduino
You may need to reorder the PINs depending on how you wire it to the display.
// counter button definition
#define button       A0 // +1
#define button_minus A1 // -1
#define button_10    A2 // +10
#define button_100   A3 // +100
#define button_reset A4 // reset

// common pins of the four digits definitions
#define Dig1    2
#define Dig2    3
#define Dig3    4
#define Dig4    5
// segment pin definitions
#define SegA    6
#define SegB    7
#define SegC    8
#define SegD    9
#define SegE    10
#define SegF    11
#define SegG    12
// A=10 : 

// variable declarations
byte current_digit;
int  count = 0;
 
void setup()
{
  pinMode(button, INPUT_PULLUP);
  pinMode(button_minus, INPUT_PULLUP);
  pinMode(button_10, INPUT_PULLUP);
  pinMode(button_100, INPUT_PULLUP);
  pinMode(button_reset, INPUT_PULLUP);
  pinMode(SegA, OUTPUT);
  pinMode(SegB, OUTPUT);
  pinMode(SegC, OUTPUT);
  pinMode(SegD, OUTPUT);
  pinMode(SegE, OUTPUT);
  pinMode(SegF, OUTPUT);
  pinMode(SegG, OUTPUT);
  pinMode(Dig1, OUTPUT);
  pinMode(Dig2, OUTPUT);
  pinMode(Dig3, OUTPUT);
  pinMode(Dig4, OUTPUT);

  disp_off();  // turn off the display

  // Timer1 module overflow interrupt configuration
  TCCR1A = 0;
  TCCR1B = 1;  // enable Timer1 with prescaler = 1 ( 16 ticks each 1 µs)
  TCNT1  = 0;  // set Timer1 preload value to 0 (reset)
  TIMSK1 = 1;  // enable Timer1 overflow interrupt

}

ISR(TIMER1_OVF_vect)   // Timer1 interrupt service routine (ISR)
{
  disp_off();  // turn off the display

  switch (current_digit)
  {
    case 1:
    if (count >= 1000)
    {
      disp(count / 1000);   // prepare to display digit 1 (most left)
      digitalWrite(Dig1, HIGH);  // turn on digit 1
    }
      break;

    case 2:
    if (count >= 100)
    {
      disp( (count / 100) % 10);   // prepare to display digit 2
      digitalWrite(Dig2, HIGH);     // turn on digit 2
    }
      break;

    case 3:
    if (count >= 10)
    {
      disp( (count / 10) % 10);   // prepare to display digit 3
      digitalWrite(Dig3, HIGH);    // turn on digit 3
    }
      break;

    case 4:
      disp(count % 10);   // prepare to display digit 4 (most right)
      digitalWrite(Dig4, HIGH);  // turn on digit 4
  }

  current_digit = (current_digit % 4) + 1;
}

// main loop
void loop()
{
  if(digitalRead(button) == 0)
  {
    count++;  // increment 'count' by 1
    if(count > 9999)
      count = 0;
    delay(200);  // wait 200 milliseconds
  }
  else if(digitalRead(button_minus) == 0)
  {
    count--;  // decrement 'count' by 1
    if(count < 0)
      count = 9999;
    delay(200);  // wait 200 milliseconds
  }
  else if(digitalRead(button_10) == 0)
  {
    count=count+10;  // increment by 10
    if(count > 9999)
      count = 0;
    delay(200);  // wait 200 milliseconds
  }
  else if(digitalRead(button_100) == 0)
  {
    count=count+100;  // increment by 100
    if(count > 9999)
      count = 0;
    delay(200);  // wait 200 milliseconds
  }
  else if(digitalRead(button_reset) == 0)
  {
    count=0;  // reset
    delay(200);  // wait 200 milliseconds
  }
}

void disp(byte number)
{
  switch (number)
  {
    case 0:  // print 0
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, LOW);
      break;

    case 1:  // print 1
      digitalWrite(SegA, LOW);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, LOW);
      break;

    case 2:  // print 2
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, LOW);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, HIGH);
      break;

    case 3:  // print 3
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, HIGH);
      break;

    case 4:  // print 4
      digitalWrite(SegA, LOW);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;

    case 5:  // print 5
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, LOW);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;

    case 6:  // print 6
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, LOW);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;

    case 7:  // print 7
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, LOW);
      break;

    case 8:  // print 8
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;

    case 9:  // print 9
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
  }
}

void disp_off()
{
   digitalWrite(Dig1, LOW);
   digitalWrite(Dig2, LOW);
   digitalWrite(Dig3, LOW);
   digitalWrite(Dig4, LOW);
}

// end of code.

Credits

Ratti3
3 projects • 12 followers
IT Engineer with an interest in electronics.

Comments