Project showcase
4-Digit 7-Segment Counter with LiPo Backup

4-Digit 7-Segment Counter with LiPo Backup

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

  • 74 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

I needed a counter at work and currently have a habit of making things with bright LEDs.

So I came up with this.

It uses a large 4-digit 7-segment display, an Adafruit PowerBoost 500C for LiPo battery backup and an Arduino Uno. A relay turns off the display when on battery, there is an option to override this (using black button).

The PCB is just a prototype board, I dont have time for etching, it's housed in a perspex case.

The controls are to increment the count by +1, +10 and +100, decrement by -1, reset to 0, turn of the display and LEDs to power save when on battery and turn off the unit.

Modified Arduino code from https://simple-circuit.com/arduino-7-segment-display-4-digit/

Code

CodeArduino
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.

Schematics

Fritzing
Counter bb 6g0y4dw3g7

Comments

Similar projects you might like

Programming 4 Digit 7 Segment LED Display

by

  • 109,792 views
  • 31 comments
  • 90 respects

Automatic Arduino Goal Counter for Table Football/Soccer

Project showcase by OSZ Täuffelen

  • 3,714 views
  • 1 comment
  • 4 respects

Meeting Cost Counter

Project showcase by Robert Korn

  • 1,970 views
  • 1 comment
  • 8 respects

Fidget Spinner RPM Counter

by Andriy Baranov

  • 46,390 views
  • 30 comments
  • 108 respects
Add projectSign up / Login