Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
There  are so many examples of GPIO control available I just wanted to do a very simple example. I wanted to use a simple relay to trigger a device either manually or by scheduling using cron.

One thing I Iearned along the way was that not all the GPIO pins behave the same when rebooted. For example GPIO2 is set to high on boot, which meant that if my Raspberry Pi rebooted the relay I had set up would be triggered until I ran the script to switch it on/off.

GPIO18 on the other hand is set to low by default, so this is what I have used for this example.

This is just another copy and paste job that I have pinched from someone else's blog but with step by step instructions for people like me that know nothing.

Create a Python script  to activate a relay on GPIO18 on a Raspberry Pi

Open a terminal and type : sudo nano /home/pi/

This will open an editor for you to paste the code below in.

import RPi.GPIO as GPIO
import time
print "Relay Energised"
print "Relay De-energised"

Press CTRL O followed by ENTER to save your file. CTRL X will exit the editor and you're now ready to test.

In the terminal type python and hit ENTER - you should now have a terminal that displays "Relay Energised" for 2 seconds followed by Relay De-energised for a further 5 seconds before the terminal automatically closes. Adjust time.sleep(X) to suit your needs.

To create a desktop shortcut for this please read my previous post on how to set that up. Replace the file and path in that example to show