MellBell Electronics
Published © CC BY-NC-SA

Digital Hourglass Using PICO

This is an hourglass alarm that uses PICO, LEDs, and a buzzer to notify you when your time ends.

BeginnerFull instructions provided1 hour865
Digital Hourglass Using PICO

Things used in this project

Hardware components

PICO
×1
LED (generic)
LED (generic)
×5
Buzzer
Buzzer
×1
Resistor 1k ohm
Resistor 1k ohm
×6
Jumper wires (generic)
Jumper wires (generic)
×10
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Breadboard (generic)
Breadboard (generic)
×1
9V battery (generic)
9V battery (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

LEDs and Timer Code

C/C++
This is the code for the timer and the LEDs only
unsigned long previousTime = 0;                           //variable which holds the time an LED was last changed.
int led;                                                  //variable that holds the led pin number, this variable will update in real-time running.

const long interval  60000                                   //the time interval 60000(1 min). Change this if you want to change the time period 

void setup() {

  for (led = 0 ; led <= 4 ; led++) {                       //set the 5 LEDs as an OUTPUTs, and turn them OFF using the for loop.
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);
  }

  led = 0;                                                 //update the led variable value to be 0.
}

void loop() {

  unsigned long currentTime = millis();                   //the amount of time the Arduino has been running with millis() and store it in currentTime variable.

  if ( (currentTime - previousTime) >= interval)         //if the time interval between to two events is greater than or equal 60000(1 min.) turn a single LED ON
  {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;                                                //increment the led variable to be ready to turn ON the next LED.
  }

}

LEDs and Timer Code + Buzzer

C/C++
This is the same code as earlier. We just added the buzzer code to it.
unsigned long previousTime = 0;                           //variable which holds the time an LED was last changed.
int led;                                                  //variable holds the led pin number, this variable will update in real-time running.

#define interval  60000                                   //the time interval 60000(1 min). Change this if you want to change the time period 
#define buzzer A1                                         //the pin which connected to the buzzer.

void setup() {

  for (led = 0 ; led <= 4 ; led++) {                       //set the 5 LEDs as an OUTPUTs, and turn them OFF using the for loop.
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);
  }
  
  led = 0;                                                 //update the led variable value to be 0.
  
  pinMode(buzzer, OUTPUT);                                 //set the buzzer as an OUTPUT Pin.
}

void loop() {

  unsigned long currentTime = millis();                   //the amount of time the Arduino has been running with millis() and store it in currentTime variable.

  if ( (currentTime - previousTime) >= interval)         //if the time interval between to two events is greater than or equal 60000(1 min.) turn the a single LED ON
  {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;                                                //increment the led variable to be ready to turn ON the next LED.

    if (led == 5)                                         //if the fifth LED is ON turn ON the noisy buzzer.
      tone(buzzer, 500);
  }

}

The Final Code

C/C++
This is the project's final code.
unsigned long previousTime = 0;                           //variable which holds the time an LED was last changed.
int led;                                                  //variable holds the led pin number, this variable will update in real-time running.

#define interval  60000                                   //the time interval 60000(1 min). Change this if you want to change the time period 
#define switchPin  A0                                     //the pin which connected to the pushbutton.
#define buzzer A1                                         //the pin which connected to the buzzer.

void setup() {

  for (led = 0 ; led <= 4 ; led++) {                       //set the 5 LEDs as an OUTPUTs, and turn them OFF using the for loop.
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);
  }
  
  led = 0;                                                 //update the led variable value to be 0.
  
  pinMode(switchPin, INPUT);                               //set the switchPin as an INPUT Pin.
  pinMode(buzzer, OUTPUT);                                 //set the buzzer as an OUTPUT Pin.
}

void loop() {

  unsigned long currentTime = millis();                   //the amount of time the Arduino has been running with millis() and store it in currentTime variable.


  if ( (currentTime - previousTime) >= interval)         //if the time interval between to two events is greater than or equal 60000(1 min.) turn the a single LED ON
  {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;                                                //increment the led variable to be ready to turn ON the next LED.

    if (led == 5)                                         //if the fifth LED is ON turn ON the noisy buzzer.
      tone(buzzer, 500);
  }

  int switchRead = digitalRead(switchPin);                //read the switch state and save the reading inside the switchRead variable.
  
  if (switchRead == HIGH)                                 //if the pushbutton is pressed turn OFF all the LEDs, turn OFF the buzzer, and reset the Timer.
  {
    for (int i = 0 ; i <= 4 ; i++)
    {
      digitalWrite(i, LOW);                               //turn OFF the LEDs.
    }
    noTone(buzzer);                                       //turn OFF the buzzer.
    led = 0;                                              //update the led value to start from point zero.
    previousTime = currentTime;
  }
}

Credits

MellBell Electronics

MellBell Electronics

2 projects • 2 followers
We want to provide makers with great content, along with our products, to give them the ability to build anything they want.

Comments