Anshuman Banik
Published © Apache-2.0

Arduino implementation of a 5-bit Synchronous dual Counter

In this Project, I have tried to demonstrate the way in which a 5-bit Synchronous counter circuit behaves with respect to a Clock Signal.

BeginnerFull instructions provided2 hours138
Arduino implementation of a 5-bit Synchronous dual Counter

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
5 mm LED: Red
5 mm LED: Red
×11
Resistor 330 ohm
Resistor 330 ohm
×11
Jumper wires (generic)
Jumper wires (generic)
Red wires for all positive connections and, Black wires for all sorts of GND connections.
×30
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires

Story

Read more

Schematics

Schematic diagram for the Project

Code

Arduino IDE code to implement the dual counter

Arduino
int upCounter = 0;
int downCounter = 31;
int clockPin = 13; // Pin for the clock LED
unsigned long previousMillis = 0;
const long interval = 2000; // 2-second interval

void setup() {
  // Define digital pins as outputs for the up counter
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  // Define digital pins as outputs for the down counter
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

  // Define clockPin as output
  pinMode(clockPin, OUTPUT);

  // Initialize down counter to 31
  downCounter = 31;

  // Display the initial value on the down counter pins
  digitalWrite(8, (downCounter >> 4) & 0b00001);
  digitalWrite(9, (downCounter >> 3) & 0b00001);
  digitalWrite(10, (downCounter >> 2) & 0b00001);
  digitalWrite(11, (downCounter >> 1) & 0b00001);
  digitalWrite(12, downCounter & 0b00001);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // Save the last time the clock LED state changed
    previousMillis = currentMillis;

    // Toggle the clock LED state
    if (digitalRead(clockPin) == LOW) {
      digitalWrite(clockPin, HIGH);

      // Increment the up counter
      upCounter++;

      if (upCounter == 32) {
        upCounter = 0;
      }

      // Convert and display the up counter
      digitalWrite(2, upCounter & 0b00001);
      digitalWrite(3, (upCounter >> 1) & 0b00001);
      digitalWrite(4, (upCounter >> 2) & 0b00001);
      digitalWrite(5, (upCounter >> 3) & 0b00001);
      digitalWrite(6, (upCounter >> 4) & 0b00001);

      // Decrement the down counter
      downCounter--;

      if (downCounter < 0) {
        downCounter = 31;
      }

      // Convert and display the down counter
      digitalWrite(8, (downCounter >> 4) & 0b00001);
      digitalWrite(9, (downCounter >> 3) & 0b00001);
      digitalWrite(10, (downCounter >> 2) & 0b00001);
      digitalWrite(11, (downCounter >> 1) & 0b00001);
      digitalWrite(12, downCounter & 0b00001);
    } else {
      digitalWrite(clockPin, LOW);
    }
  }
}

Credits

Anshuman Banik

Anshuman Banik

4 projects • 1 follower
Hi, myself Anshuman Banik. I am a sophomore in electronics engineering. My interests include Analog Circuits, DSP, and Control Theory.

Comments