Digital Hourglass Using Arduino

by | Dec 31, 2022 | Arduino, Basic Coding, Coding

Home » Coding » Digital Hourglass Using Arduino

Introduction

A digital hourglass using an Arduino microcontroller is a project that involves creating a device that can keep track of a certain amount of time and display it to the user. This can be useful for various applications, such as timing tasks, timing games, or simply as a decorative piece. So In this Arduino project tutorial, we will teach you how to create a Digital Hourglass using Arduino.

We can set a time interval after which each LED will glow till a particular time. After that, we can also reset the time interval by tilting it, and the process will restart just like an hourglass.

 

Supplies

To make a Digital Hourglass using Arduino, we will require the following components:

Components

Circuit Diagram

Steps To Make A Digital Hourglass Using Arduino

Step 1: The first thing to do is to assemble components on Digital Board or Physical Table.

Step 2: Plug the Tilt sensor and LEDs into the Breadboard.

Tilt Sensor:

Step 3: Connect the first terminal of the Tilt sensor to the Ground pin of the Arduino using a black-colored wire through a resistor.

Step 4: Connect the second terminal of the Tilt sensor to the 6-number pin of the Arduino on the digital side.

Step 5: Connect the third terminal of the Tilt sensor to the 5V power supply pin of the Arduino Uno.

LEDs:

Step 6: All the cathode terminals of the LEDs to resistors and then to the GND pin of the Arduino, as shown in the figure.

Step 7: Connect the Anode Terminal of all the LEDs to the pin numbers 1, 2, 3, 4, & 5 of the Arduino, respectively, as shown in the figure.

Source Code

const int sensorPin = 6;
unsigned long previousTime = 0;
int state = 0;
int prevState = 0;
int led = 0;
long interval = 300;
void setup()
{
for(int m = 1; m <6; m++)
{
pinMode(m, OUTPUT);
}


pinMode(sensorPin, INPUT);
}
void loop()
{
unsigned long currentTime = millis();
if(currentTime - previousTime > interval)
{
previousTime = currentTime;
digitalWrite(led, HIGH);
led++;
}
state = digitalRead(sensorPin);
if( state != prevState)
{
for( int m = 1; m<6; m++)
{
digitalWrite(m, LOW);
}
led = 1;
previousTime = currentTime;
}
prevState = state;
}

Explanation of the Code

1. Initially, we initialized some variables that will be used in the code.

2. In the setup function, we use a for loop to produce output in LEDs connected from 1 to 5 pin numbers of Arduino.

3. Next, we are taking input from the sensor pin, which is connected to pin number 6 of the Arduino, to reset the hourglass.

4. In the loop function, we are taking the time in milliseconds. Then, we use an if statement; if the difference in current and previous time is greater than the interval, we change the previous time to the current time and make one LED glow. Also, increasing the LED pin number by 1.

5. After that, we are reading the sensor state and store it in the state variable.

6. If the state is not equal to the previous state, we are making each LED low and initializing the led to pin number 1 so that when it starts again, it starts from LED number 1.

7. Then, we change the previous time to the current time and the previous state to the state of the sensor.

Output

Digital Hourglass Using Arduino

As per our coded time interval, the LEDs will glow one after the other.

To reset it, simply tilt the sensor, and LEDs will again start glowing after the fixed time interval.

 

 

You May Also Like To Create…