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
    About the element14 Community
  • 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 4969 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
  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago

    I agree 100% with mark on this one.

     

    Also, just because the relay can handle the 250V at 10A, does not mean that the board it is on can or should, it is all about how the board is designed, built and protected from other things, when dealing with Mains voltages there are very clear guidelines regarding PCB trace spacing. creapage allowances etc that MUST be followed for a module to be certified safe. This includes how it is boxed

     

    in the case of a PIFace, it would never get a CSA or CA or UA etc approval sitting on top of the primary RPI board with nothing more than the connector preventing it from contacting the main board or a finger getting to the high voltage connectors

     

    It is a low voltage design through and through. If there are no warnings on the board then perhaps there should be but there are warnings in the documentation

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

    THERE IS.

     

    This was the whole point of this comment made at the time an element14 staff member did this DANGEROUS project.

     

    I see that despite the promises this has not been updated (Peter did remove the 230v references) and because it has been created by a staffer, you can't report it to the moderator.

     

    Samuel

    If I can offer this advice from many years experience.....

    It always pays to check and research the subject if you are going to offer advice/comment.

    The one thing you have during your career/life is your reputation and credibility.

     

    Both of these are under your control, so treat them well since it's the one thing others can't take away from you.

    They may question it, but if the evidence is on your side then its their credibility that gets questioned.

     

     

    The operation of a light using a relay that is well underrated and used moderately is unlikely to create "    another point of failure.    "

    In this particular application it is essential for safety.

     

    Mark

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

    The black catches out so many people here.

    Green or Green/yellow is earth and but any other colour is allowed to be phase.

     

    • Not sure about your plugs, but here the earth wire should be the longest, so it remains intact after the rest are pulled.
    • The active and neutral wires should not cross over.
    • Strain relief is correctly tensioned and not damaging the cable.
    • Correct tension on the screw
    • Correctly filling the terminal by twisting and then doubling back the wire
    • No stray conductors hanging out

     

    So there are 6 things to ensure a plug is correctly terminated ... and then you have the fuse.

     

    there is a 110V on each terminal centered around gnd

    Is it simply floating or can you actually pull current

    Mark

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

    Earth is longest simply because it is the furthest from where the lead enters the plug. In my school science class we spent more than an hour being taught how to wire a plug and select a fuse. This not including the time spent on the theory about why all this is done.

    • 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 mcb1

    15Amps either side, used often to power cookers and dryers so effectively 220V at 15A vs 110V @15A, centered around GND so nothing is ever actually much above 110V from gnd

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

    Earth is longest simply because it is the furthest from where the lead enters the plug

    Same here, but it doesn't make it last to be pulled out.

    It's great you have had instructions, they don't seem to worry here in NZ, which leads to some frightening discoveries. image

     

    Peter

    That seems a strange system and I'd heard it was floating but obviously not like an isolation transformer floats the 230v.

    Can you actually run an appliance at 110v.? Do they use the ground as the return in that situation ??

     

    Mark

    • 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 mcb1

    yup, there is a wiring technique where if you need 2 * 110V but at full current and uses one twin socket. The ones  here can be split by removing a jumper and then you connect one phase to the lower and one to the upper giving 110V on each but there actually separate phases centered around neutral

    imageimage

    on some socket types you can split the bridge between the upper and lower socket

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

    Peter, it would be best not to post such WRONG images. When the voltage is "120V AC" the RMS value of the sine wave is 120V. That means that the top of the sine wave is at around 170V. Pictures like yours perpetuate the falsehood that the peaks are at the rated voltage.

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

    There seems to be lots of "incorrect" images out there.

     

    Its also interesting that it shows the phases as 180 degrees apart.

    Here our phases are 120 deg (meaning all three equal 360).

     

    Obviously the load in the neutral (which is often a screen around the phase cores) should be zero, but at worst only carries the current of one phase.

     

    Mark

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

    Yes, the USA has 180 degree out-of-phase system (and 2 phases), while europe has a 120 degreen phase shift and three phases. I don't know what the rest of the world does.

     

    If your wires can carry say 10A, at 200V(*), you can can carry 2kW using two wires. Upgrade to four wires and you can transport 4kW, right? Well, for USA and their system that's true. But with the 120 degree phase shift, you can transport 6kW with those same 4 wires!

     

    That's the advantage of the three-phase system.

     

     

    (*) I'm using nice round numbers, to ease manual calculations. No relationship with actual real-world voltages or currents implied.

    • 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 rew

    Not my image , I borrowed it and without looking close enough (I saw what I was looking for, not what was correct) but now you mention it, yes I totally agree, when I get home I will create an image of my own and replace this one

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

    Rodger

    Very interesting ... thanks

    NZ and Australia both use 120 deg and you're right about the power capability.

     

    Most houses here are 60A/phase but are usually single phase at 230v.

    Neutral and earth are the same potential (they derive the neutral from the earth at the transformer)

     

    cheers

    Mark

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

    Rodger

    Very interesting ... thanks

    NZ and Australia both use 120 deg and you're right about the power capability.

     

    Most houses here are 60A/phase but are usually single phase at 230v.

    Neutral and earth are the same potential (they derive the neutral from the earth at the transformer)

     

    cheers

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
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