
Time-Based Output Control with Raspberry Pi Using Python
Welcome to the world of time-based output control with Raspberry Pi using Python! This guide empowers you to automate tasks by harnessing the power of GPIOs and crafting precise schedules. Learn how to program your Raspberry Pi to execute actions at specific times, opening a realm of possibilities for tailored, efficient automation. Whether you’re a novice or an experienced Raspberry Pi enthusiast, embark on this journey to enhance your projects with scheduled precision.
Introduction: Mastering Time-Based Output Control with Raspberry Pi and Python
Unlock the potential of your Raspberry Pi with our latest guide: ‘Time-Based Output Control with Raspberry Pi Using Python.’ Delve into the fascinating world of programming as we explore how to dynamically control outputs based on the time of the day. Discover the power of Python scripting and enhance your Raspberry Pi projects with automated actions. Whether you’re a seasoned enthusiast or just starting, this article is your gateway to mastering time-driven functionalities on the Raspberry Pi platform. Let’s harness the capabilities of Python and transform the way your projects respond to the clock.
Discover in this tutorial how to effortlessly manage Raspberry Pi GPIO outputs based on the time of day using Python and the gpiozero library. Learn to create a program that automates output activation and deactivation, with LEDs as a practical example. Extend this approach to control various digital outputs to suit your project needs.
Prerequisites
Before proceeding with this tutorial, ensure you meet the following prerequisites:
- Familiarity with the Raspberry Pi board; if not, refer to our Raspberry Pi Getting Started Guide.
- Proficiency in running and creating Python files on your Raspberry Pi. We recommend programming via SSH using VS Code; find guidance in our tutorial: Programming Raspberry Pi Remotely using VS Code (Remote-SSH).
- Understanding of Raspberry Pi GPIOs for proper circuit wiring. Review our tutorial: Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs.
Wiring the Circuit
We will manage the activation of two LEDs based on the current time. Feel free to adapt this approach to control various digital outputs to suit the needs of your project.
We’ll link one LED to GPIO 17 and another to GPIO 27, though you can choose alternative pins from the Raspberry Pi pinout. Gather the following components:
- Raspberry Pi Board (check out the Best Raspberry Pi Starter Kits)
- 2x LEDs
- 2x 220 Ohm resistors (or equivalent)
- Breadboard
Feel free to explore price options for these components.
Explore the provided links or visit MakerAdvisor.com/tools for convenient access to all project components at competitive prices!

image source from Maker Advisor
Manage Raspberry Pi Outputs According to Time of Day
The gpiozero library offers a range of interfaces for common components such as LEDs, buttons, potentiometers, sensors, and others. If you’re new to controlling Raspberry Pi GPIOs with the gpiozero library, we suggest starting with this tutorial.
If you’re using Raspberry Pi OS, the gpiozero library should be pre-installed. If not, you can install it by running the following command:
python3 -m pip install gpiozero
The TimeOfDay Interface: Customized Control of Raspberry Pi Outputs
The gpiozero library includes a useful interface called TimeOfDay, enabling the creation of a device active during specific time ranges. In the example below, we’ll explore how to implement this feature in a Python file named ‘outputs_timeofday.py’ on your Raspberry Pi.
# Complete Project Details: https://RandomNerdTutorials.com/raspberry-pi-control-outputs-based-on-time/
from gpiozero import LED, TimeOfDay
from datetime import time
from signal import pause
led1 = LED(17)
led2 = LED(27)
tod1 = TimeOfDay(time(16,30), time(16,45), utc=False)
tod2 = TimeOfDay(time(16,45), time(16,50), utc=False)
tod1.when_activated = led1.on
tod1.when_deactivated = led1.off
tod2.when_activated = led2.on
tod2.when_deactivated = led2.off
pause()
Understanding the Code
Keep reading to understand the functioning of the code.
Library Imports
“Importing Libraries”
To start, import the LED component from the gpiozero library for GPIO control of the LED, and TimeOfDay to establish a time interval device. Additionally, import the time function from the datetime module to create time objects. Lastly, include the pause() function from the signal module to maintain program execution for event detection.
from gpiozero import LED, TimeOfDay
from datetime import time
from signal import pause
Declaring the LEDs
“Creating LED Objects”
Following that, generate two LED objects, named led1 and led2, pointing to GPIO 17 and GPIO 27, respectively.
led1 = LED(17)
led2 = LED(27)
Generating TimeOfDay Objects
“Generating TimeOfDay Instances
Following that, we instantiate two objects of the TimeOfDay type. These objects function similarly to pushbuttons, but all processes occur internally. They remain active within a predetermined time frame. For instance, tod1 is active from 16:30 to 16:45. Ensure to include the parameter ‘utc=False’ to use your local time.
tod1 = TimeOfDay(time(16,30), time(16,45), utc=False)
You have the option to provide additional useful parameters to the TimeOfDay class:
classgpiozero.TimeOfDay(start_time, end_time, *, utc=True, event_delay=10.0, pin_factory=None)
Here’s the breakdown of these parameters:
start_time(time): The time when the device becomes active.end_time(time): The time when the device becomes inactive.utc(bool): If set to True (default), it uses a naive UTC time for comparison; otherwise, it uses local time. I recommend setting it to False for local time usage.event_delay(float): The number of seconds between file reads, with a default of 10 seconds.pin_factory: This is an advanced feature that is typically not needed for regular use.
We create an additional TimeOfDay object set to be active from 16:45 to 16:50. Adjust these values as needed to avoid unnecessary wait times during testing.