I am doing a small project which consists of turning LEDs on and off using the GPIO library. I am able to successfully turn the LED's on and off using a browser on my laptop. But now I want the LEDs to turn on automatically at a specified time for a short duration.

Example: If I set the LED's to turn on after 12:00 for 2 hours, then the Raspberry Pi would automatically turn them on at 12:00 for 2 hours.

Is this possible, and if so, how?

  • 3
    Yes, that is perfectly possible. What have you tried and why didn't it work?
    – joan
    Commented Oct 31, 2015 at 22:28
  • I havent tried it actually.But I googled for it but didnt find any suitable links. Commented Oct 31, 2015 at 22:30
  • So, after thinking about it, I think you can do this from X and from the command line, are you accessing your pi through ssh? Do you need to be root to turn the leds on or off?
    – eri0o
    Commented Nov 1, 2015 at 10:37

2 Answers 2


Let's break this answer in two parts: scheduling a task and turning a led on and off, than wrap together. You can go to the end and just get the script if you prefer!

Scheduling a task

So I would suggest you use the atd for one time scheduling in Raspbian. If you need something repetitive, use cron - I will cover here only how to use the at command. More details here: https://debian-handbook.info/browse/stable/sect.task-scheduling-cron-atd.html

First, you need to install it!

sudo apt-get install at

Now for using it, see the date right now on your raspbian - I'm assuming you are connected to it using ssh or is accessing through the terminal.


Let's make a blank folder on your home for testing purpose

mkdir ~/test
cd ~/test

Let's schedule a job!!

echo "echo \"teste\" > ~/test/test.log " | at 10:52

Let's wait it to be executed!!!

watch ls

The watch command will execute the command after it repeatedly and will output it's output with a clock, so we can confirm it works! (an archive named test.log be created at the scheduled time.

Turning a LED on and off

Now you need to make a script to turn on and off a LED - I don't know how you are doing this and schedule it with using the at command. We will use the gpio utility from wiringpi. Let's learn about it.

sudo apt-get install wiringpi

I will suppose you want to turn on and off led 5, but do whatever you want. Let's test if it works. Try:

gpio -g mode 5 out
gpio -g write 5 1

You can turn the led off using:

gpio -g write 5 0

Let's do both!

Ok, now we need to wrap all of this in a bash script for ease of use.

Let's create ledsched.sh:



endtime=`date +%H:%M -d " $timeat today + $duration"`

gpio -g mode $ledpin out
echo "gpio -g write $ledpin 1" | at $timeat
echo "gpio -g write $ledpin 0" | at $endtime

And call it using (remember to use first chmod +x ledsched.sh ):

./ledsched.sh 12:00 2hours 5

I'm scheduling to turn pin 5 on at 12:00 and off after 2 hours. If your led is at pin 5 it should work!

Oh, if you do have access to the visual interface, you can use the alarm-clock-applet. Install it from the command line with sudo apt-get install alarm-clock-applet .


Making the script

To start, you need to make sure that your Raspberry Pi is connected to the internet. This is needed to track time. Then type in this code:

import RPi.GPIO as GPIO
import datetime
import time

pin = your_output_pin_here
GPIO.setup(pin, GPIO.OUT)

while True:
    time = datetime.datetime.now().strftime("%H:%M")
    if time == "12:00":
        GPIO.output(pin, True)
        GPIO.output(pin, False)

Replace things like 12:00 and your_output_pin_here with your own values. Every 30 milliseconds, it checks whether it is 12:00. If it is, it turns on the LED. It then waits a number of seconds, then turns it back off.

Editing the Crontab

If you want this program to be run whenever you start the Raspberry Pi, go into LXTerminal and type this:

sudo crontab -e

It will open a text document. Use the arrow keys to navigate to the bottom, then type this:

@reboot sudo python /path/to/your/python/file.py &

Replace "/path/to/your/python/file.py" with the path to your python file (like /home/pi/timed_led.py). Do CTRL+O and RETURN to save. Then to exit, do CTRL-X.

When you start up your Pi, it will take a while to boot up, then it will run the program. If you ever want to stop it doing this, then do sudo crontab -e again and put a hash (SHIFT+3) at the front of the line.

I really hope this helps!

  • This was a really good question, I have been puzzled by getting python time for years: I only learnt how the day before yesterday! But recently, I have found huge uses for it in all circumstances. And just remember, StackExchange is here to help; I'm always happy to give my skills to others! Commented Nov 1, 2015 at 13:33
  • Thanks a ton !!! :). This solved both of my time related and scheduling problem. Btw is there any way through which i can obtain the time from raspberry pi's without any internet connection ? Commented Nov 1, 2015 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.