An electronic counter tracks events like button presses, pulses, or sensor activations, displaying the count. The microcontroller processes inputs and manages the counting, making counters useful in applications ranging from event tracking to industrial automation.
Objective
- Understand the relevance of counter systems in daily life.
- Build a basic electronic counter using an Arduino microcontroller.
This project provides practical experience in creating a simple yet versatile counting system, demonstrating its importance in various fields.
Materials to be used
Circuit diagram
Figure: Schematic diagram for LCD display connection
Figure: Components used to make connection on the board
Arduino Codes
Code knowledge #
The variable x counts the number of turns or events. input refers to the analog input pin A0, used to read a sensor or input signal. state tracks the current state of the input.
This function runs once when the Arduino is powered on or reset.
- lcd.setCursor(0, 0): This sets the cursor to the first row (row 0) and first column (column 0) of the LCD.
- lcd.print(“Turn counter”): This displays the text “Turn counter” on the first line of the LCD.
- lcd.setCursor(0, 1): Moves the cursor to the second row (row 1) and the first column (column 0).
- lcd.print(x): Displays the value of x (which starts at 0).
- lcd.print(” =turn”): Displays the text ” =turn” after the x value.
The loop() function runs continuously after the setup() function.
int counter = digitalRead(A0): This reads the digital state of pin A0 (either HIGH or LOW) and stores the result in the counter variable. Although A0 is an analog pin, you can use it for digital input. It reads HIGH (1) or LOW (0) depending on the input connected to it (e.g., a button, switch, or sensor).
if (state == 0): This checks if the state is 0 (meaning the system is waiting for input. The input in this context is when a person enters the place to be counted).
switch (counter): This is a switch statement that acts based on the value of counter:
- case 1: If counter is 1 (HIGH), it changes the state to 1, increments x by 1, and updates the LCD to display the new value of x.
- case 0: If counter is 0 (LOW), the state remains 0 (waiting for input).
if (counter == LOW): This condition ensures that when the counter reads LOW, the state is reset to 0, meaning it’s waiting for the next input or turn.
Results
The logic increments a counter (x) whenever the input signal changes.
The LCD displays:
- Top Row (Row 0): ” Turn counter “
- Bottom Row (Row 1): “0 =turn ” This shows the initial counter value, x = 0.
On each increment, the LCD updates the bottom row to reflect the new value of x.
For example:
- After the first HIGH signal: “1 =turn “
- After the second HIGH signal: “2 =turn “, and so on.
Visible Output:
- Initial State:
Turn counter
0 =turn
- After Incrementing (e.g., 3 turns):
Turn counter
3 =turn The display updates correctly with the new counter value every time the signal on A0 transitions from LOW to HIGH.