Geekzone: technology news, blogs, forums
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.

1344 posts

Uber Geek

# 260059 8-Nov-2019 14:39
Send private message quote this post

Any kind Python experts there who could produce some code to turn one GPIO pin on and off multiple times per day, 7 days a week? I need to specify the start and end times ie On at 8am off at 8:02 am for probably 6 time slots per day.


My knowledge of Python is pretty basic but if someone could produce the basic script I could then modify it as required.

Create new topic
3305 posts

Uber Geek


  # 2350311 8-Nov-2019 17:45
Send private message quote this post

  • What's your timeframe?
  • Does it need to be doing anything else at the same time (i.e. what OS does it need running in the background, assuming Raspbian)?
  • How do you imagine specifying the times (text config file)?

I don't know how to do this, but it doesn't sound that hard so I might be willing to have a crack if no one else can help you out but I don't know when I'd be able to get to it.

2150 posts

Uber Geek

Lifetime subscriber

  # 2350321 8-Nov-2019 18:07
2 people support this post
Send private message quote this post

Look at the raspberry pi foundation education documents on how to use the gpio connectors. There are tutorials from memory which teach you how to light a led so if you could start looking there.

There are lots of basic educational resources out there.

My programming skills ended with bbc basic in 1980s and I was able to write a python script converting entered text into Morse code beeps with beeper connected to gpio and a bread board.

You might surprise yourself in how easy it could be.


31 posts


  # 2350393 8-Nov-2019 18:50
Send private message quote this post

Check this out

1698 posts

Uber Geek


  # 2350505 8-Nov-2019 23:48
One person supports this post
Send private message quote this post

First, make sure you test with a voltage meter.  Also make sure your assumptions on GPIO channels match the Pi version you are attempting.  Different versions have different locations and you may be looking at the wrong map!  Make sure you are using an always off channel.


Your requirement for time frames isnt too clear, but would it not be easier to have two python scripts, one that turns the GPIO on and then one that turns off and have them timed on your cron job?


On (08:00, 10:00, 12:00)


0 8,10,12 * * * /usr/bin/python >/dev/null 2>&1


Off at (08:02,10:02,12:02)


2 8,10,12 * * * /usr/bin/python >/dev/null 2>&1


I dont code in Python, however I believe something along the lines of below would work as a python script (untested).  I have used similar in a relay I played with.


Turn on


import RPi.GPIO as GPIO
import time


channel = 21


# GPIO setup
GPIO.setup(channel, GPIO.OUT)


#Turns GPIO on
def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn on


if __name__ == '__main__':
    except KeyboardInterrupt:





Turn Off


import RPi.GPIO as GPIO
import time


channel = 21


# GPIO setup
GPIO.setup(channel, GPIO.OUT)


#Turns GPIO off
def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn off


if __name__ == '__main__':
    except KeyboardInterrupt:






1344 posts

Uber Geek

  # 2350544 9-Nov-2019 07:27
Send private message quote this post

Thanks for the ideas, will see where they lead.

4235 posts

Uber Geek

  # 2350565 9-Nov-2019 10:10
Send private message quote this post

Use Node-Red?

The BigTimer module is really good.

378 posts

Ultimate Geek

  # 2350646 9-Nov-2019 10:50
Send private message quote this post

Cron is good and simple way to do this if the Pi remains on the network and sync's time. It has no real-time clock so it may get confused time-wise.


You can always buy a real-time clock add-on.

Create new topic

Twitter and LinkedIn »

Follow us to receive Twitter updates when new discussions are posted in our forums:

Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:

Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:

News »

Microsoft launches first Experience Center worldwide for Asia Pacific in Singapore
Posted 13-Nov-2019 13:08

Disney+ comes to LG Smart TVs
Posted 13-Nov-2019 12:55

Spark launches new wireless broadband "Unplan Metro"
Posted 11-Nov-2019 08:19

Malwarebytes overhauls flagship product with new UI, faster engine and lighter footprint
Posted 6-Nov-2019 11:48

CarbonClick launches into Digital Marketplaces
Posted 6-Nov-2019 11:42

Kordia offers Microsoft Azure Peering Service
Posted 6-Nov-2019 11:41

Spark 5G live on Auckland Harbour for Emirates Team New Zealand
Posted 4-Nov-2019 17:30

BNZ and Vodafone partner to boost NZ Tech for SME
Posted 31-Oct-2019 17:14

Nokia 7.2 available in New Zealand
Posted 31-Oct-2019 16:24

2talk launches Microsoft Teams Direct Routing product
Posted 29-Oct-2019 10:35

New Breast Cancer Foundation app puts power in Kiwi women's hands
Posted 25-Oct-2019 16:13

OPPO Reno2 Series lands, alongside hybrid noise-cancelling Wireless Headphones
Posted 24-Oct-2019 15:32

Waikato Data Scientists awarded $13 million from the Government
Posted 24-Oct-2019 15:27

D-Link launches Wave 2 Unified Access Points
Posted 24-Oct-2019 15:07

LG Electronics begins distributing the G8X THINQ
Posted 24-Oct-2019 10:58

Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.

Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron

Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.