element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Documents PiFace Home Timeswitch - turning a light on and off
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: PEarle
  • Date Created: 23 May 2013 8:29 PM Date Created
  • Last Updated Last Updated: 8 Oct 2021 5:29 AM
  • Views 4559 views
  • Likes 1 like
  • Comments 46 comments
Related
Recommended

PiFace Home Timeswitch - turning a light on and off

** Update**

 

I've now implemented this using an Energie Pi Controlled mains Socket - see my blog at Home Automation using ENERGENIE - ENER002-2PI

 

For details on the Energie socket see ENER002-2PI - ENERGENIE - RF CONTROLLED MAINS SOCKETS, FOR | CPC


 

 

One of the things I wanted to do with my Pi from the outset was to be able to use it as a timer, to turn a light on and off at night when I was out, as a simple form of home security. Being something of a newbie when it comes to electronics, my biggest problem was how to wire it up to be able to turn the power on and off.

 

The Pi relays are rated at a maximum of 20v and 5amps - therefore I cannot connect the Pi directly to the 'mains powered' light !!!

 

What I am planning on doing is investigating using a to a commercial relay which can cope with the current. When I've done that I shall update this blog again with details of the how the Pi can safely control a mains powered device.

 

However, the rest of the blog is still valid as it explains how the relay on the PiFace is operated and will be used for the Timer application, when completed.

 

I've written a java program to control the light switching on and off which does the following;

 

  • Calculate a random wait time between two values that I have supplied (2 and 20 minutes at the moment)
  • Wait for that period of time to elapse
  • At the end of the wait time, toggle the relay - switch it on if it was already off, and vice-versa.
  • repeat the above ad infinitum

 

The result is that the light will keep turning on and off in a random pattern making it look as if someone is using the room even when I'm not there !

 

 

Java Code

I've already written a document about using Java on the Pi - see http://www.element14.com/community/docs/DOC-54112/l/java-on-the-raspberry-pi. The code that I used for this project is attached to this document (e.g. timeswitch.zip)

 

 

Running Automatically

My intention is really to have this as a 'black box' function so that the TimeSwitch application will run as soon as the Pi is switched on. This is accomplished quite easily - all I need to do is write a script to run the java application, and then add it to the initialisation script.

 

The script to run the java application is as follows ;

cd /opt/pi4j/examples
java -classpath .:classes:/opt/pi4j/lib/'*' com.phe.datetime.TimeSwitch

To run at start up I have edited  /tec/rc.local and added the following line

 

/opt/pi4j/examples/timeswitch.sh > /opt/pi4j/examples/timeswitch.log

 

This will execute the java app at start up and also pipe any log messages to timeswitch.log.

Attachments:
timeswitch.zip
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply

Top Comments

  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago in reply to Problemchild +2
    Maybe a DANGER rating from Bunny minion through to Skeleton minion
  • mcb1
    mcb1 over 11 years ago in reply to michaelkellett +1
    If you look at the RCSwitch there is a RPi solution. Frederick also used these to interface as an I2C device. http://www.element14.com/community/community/design-challenges/forget-me-not/blog/2014/08/11…
  • mcb1
    mcb1 over 11 years ago in reply to eatojg +1
    This adds a piece for the RPI and while it suits the UK, there are many others using a similar 433 or 315 MHz RF control. The code in the Watts Clever have a 24 bit data stream, but in reality it only…
Parents
  • michaelkellett
    michaelkellett over 12 years ago

    I'm adding to Mark's warning  - this is a totally unsafe use of UK mains power and this blog should be taken down immediately. It should be obvious to Farnell/Elememt14 that a great many complete novices are interested in RPi applications and know no better than to copy whay they see. If they copy this some oneis likely to be seriously injured or killed.

     

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • PEarle
    PEarle over 12 years ago in reply to michaelkellett

    Thanks Michael - Ive changed the blog and removed the phot of the PiFace connection that I made.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to PEarle

    Peter

    It was me who started the comment ...

    Your post is still inaccurate.

    Luckily, the PiFace comes with a couple of relays built in which can switch mains (204v) voltage up to 10 amps, which is more than enough for my needs.

     

    Rather than include any words regarding 230v, I suggest you use this.

     

    "The Piface is designed for switching no more than 20v and 5A, so I will connect it to a sperate relay to switch the mains voltage for my application."

     

    People read what they want, and this removes the opportunity for them to read the WRONG voltage.

     

    I have done a home minder using Arduino, and you could very easily use small desk lamp that uses a 12v lamp to achieve the same result.

    Its surprising how the room appears illuminated when its shone onto a wall, or similar light coloured surface.

     

    Thanks

    Mark

     

    PS apparently I can edit it the document ... not sure thats quite right.

     

     

    edit 04/09/13 rectified the issue.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to mcb1

    Peter

     

    One thing I mentioned in the article for "Home Minder" I did was to ensure the device couldn't fall or shift while you were away.

    As we found in Christchurch with the earthquakes, some things tended to move, and arriving home to find the charred remains of your house is not the best coming home present.

     

    So electrical safety isn't the only consideration when you leave appliances on while away.

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • PEarle
    PEarle over 12 years ago in reply to mcb1

    Hi Mark - thanks for your comments, and also your advice above, which I will be incorporating into the project. The points about making sure the device is contained within an appropriately robust container are very useful.

     

    Ive been away on vacation for a week, without Wifi acccess - I just had time to make a quick edit before I left. I've now changed the content to remove any mention of 240 volts!!

     

    Once again, thanks for your help.

     

    Regards,

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to PEarle

    Much better Peter.

    I've edited the comments.

     

    Just one thought about random lights.

     

    If you were standing outside looking at a house you think might be unoccupied.....

    Would a light randomly switching ON and OFF at 2am, 3am, or even 4am not seem a little wierd.?

     

    The idea is to emulate occupancy, so a quick note of any 'normal' behaviour might be in order.

    Adding some time ranges to the random along with a set number of cycles within that period could be an advantage.

    Adding a sequence to simulate kitchen then bathroom might be also a bonus.

    If you could incorporate a RTC, then simulating normal weekday activity could be a benefit, ie lounge off at 10pm, bedroom off 10:30pm, etc.

     

    Please don't get me wrong, this has potential, and is not dis-similar to an Arduino based version I did earlier.

     

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to PEarle

    Peter,

     

    The points about making sure the device is contained within an appropriately robust container are very useful.

     

    There seems to be somewhat of a conflict between the need for a robust container,

    and the Safety Data Sheet's warning that the device should not be covered.

    http://www.element14.com/community/docs/DOC-44828/l/raspberry-pi-safety-data-sheet

     

     

    One of the things I wanted to do with my Pi from the outset was to be able to use it as a timer, to turn a light on and off at night when I was out, as a simple form of home security.

     

    The Safety Data Sheet also warns that the device is not Class B compliant, which seems

    to be in conflict with its proposed use as a simple form of home security.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Problemchild
    Problemchild over 11 years ago in reply to mcb1

    Mark this raises my point made to E14 that their should be a mark on such a project to raise caution for any one reading it or that Certain project can "vote" a project "dangerous" or "safety critical" so that readers can use their common sense and realise what they are doing can be dangerous. Mind you if you need to be told it's dangerous then the project probably isnt for you any way !

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Problemchild
    Problemchild over 11 years ago in reply to mcb1

    Mark this raises my point made to E14 that their should be a mark on such a project to raise caution for any one reading it or that Certain project can "vote" a project "dangerous" or "safety critical" so that readers can use their common sense and realise what they are doing can be dangerous. Mind you if you need to be told it's dangerous then the project probably isnt for you any way !

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • kidiccurus
    kidiccurus over 11 years ago in reply to Problemchild

    Good idea. Its more likely to get noticed than something in the comments.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago in reply to Problemchild

    Maybe a DANGER rating image from Bunny minion through to Skeleton minion

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • kidiccurus
    kidiccurus over 11 years ago in reply to Robert Peter Oakes

    Yes, and don't strip to much insulation. I like the idea of a rating system. Perhaps implement it for other things too. Time, cost, difficulty. Someone needs to implement this. Also, what is the weird obsession with minions on this site?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago in reply to kidiccurus

    As you will hear in "Dr Who" all the time.... Spoilers image, if you get enough points and show enough experience, you may one day discover

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 11 years ago in reply to Problemchild

    As I said up further I couldn't report it as it was generated by a staff member.

    Trying to implement something similar is probably going to run into the same problems.

     

    Besides we all know that the 'ignorant' don't read the warnings ...image

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube