Hardware Hump Day: Earth Day Hacking

Make an easy project for energy conservation!

Favorited Favorite 0

This Saturday is Earth Day, and all of us at SparkFun are super proud and excited to share how we celebrate Earth Day every day. SparkFun is deeply committed to engaging in environmentally conscious practices and behaviors. From our aggressive recycling habits to our solar panel-covered roof and bike-to-work program, we are doing everything we can to have a minimal carbon footprint.

Today I want to share a simple way to bring the green spirit into your own home with an energy conservation hack. With this project, you will be able to connect any of your appliances to a timer so that they will automatically turn on and off based on your schedule and habits. To make this I used only three parts:

Arduino Uno - R3 SMD

SparkFun DeadOn RTC Breakout - DS3234

PowerSwitch Tail II


The below diagram illustrates the circuit.

alt text

Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

You will notice that there are two open leads going to + and -. These represent the terminals in the PowerSwitch Tail. You will also need to plug one end of the PowerSwitch Tail into the wall and then plug your home appliance into its other end as shown below.

alt text

Once your circuit is completed, it should look like this.

alt text

Now it’s time to upload your program using the Arduino IDE. I relied on the SparkFun DS3234 Arduino Library. This program will turn your appliance on at 7 a.m. and turn it off at 7 p.m. I have added comments in the code for you to follow along and easily customize the timer based on your own schedule.

// Earth Day Conservation Hack by Melissa Felderman for SparkFun Electronics
#include <SPI.h> //include SPI library
#include <SparkFunDS3234RTC.h> //include Sparkfun DS3234 RTC library

//RTC pin definition
#define DS13074_CS_PIN 10 

//Power switch tail Pin 
int pin = 7; 

void setup() {


   //start RTC clock

  //Pin mode for power switch tail
  pinMode(pin, OUTPUT);


void loop() {
//serial print commands for debugging

  static int8_t lastSecond = -1;


   if (rtc.second() != lastSecond) // If the second has changed
    printTime(); // Print the new time

    lastSecond = rtc.second(); // Update lastSecond value

//line of code to control appliance with the RTC. 
//To change the times at which to turn on and off the appliance, change the values in the IF statement. 
  if(rtc.hour() > 7 && rtc.hour() < 19 ){
    digitalWrite(pin, HIGH);
  }else {
    digitalWrite(pin, LOW);


//function for serial 
void printTime()
  Serial.print(String(rtc.hour()) + ":"); // Print hour
  if (rtc.minute() < 10)
    Serial.print('0'); // Print leading '0' for minute
  Serial.print(String(rtc.minute()) + ":"); // Print minute
  if (rtc.second() < 10)
    Serial.print('0'); // Print leading '0' for second
  Serial.print(String(rtc.second())); // Print second

  if (rtc.is12Hour()) // If we're in 12-hour mode
    // Use rtc.pm() to read the AM/PM state of the hour
    if (rtc.pm()) Serial.print(" PM"); // Returns true if PM
    else Serial.print(" AM");

  Serial.print(" | ");

  // Few options for printing the day, pick one:
  Serial.print(rtc.dayStr()); // Print day string
  //Serial.print(rtc.dayC()); // Print day character
  //Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat)
  Serial.print(" - ");
  Serial.print(String(rtc.month()) + "/" +   // Print month
                 String(rtc.date()) + "/");  // Print date
  Serial.print(String(rtc.date()) + "/" +    // (or) print date
                 String(rtc.month()) + "/"); // Print month
  Serial.println(String(rtc.year()));        // Print year

It’s as easy as that! Look at this light automatically turn on for me!

alt text

The best part about this project is the PowerSwitch Tail. With this part, you can easily control any appliances with your sensors and microcontroller. It eliminates the trouble of working with a relay switch and will cut your project build time dramatically!

It’s not pretty, but it’s a quick and easy to way to save energy. If you’re constructing a house from the ground up, you can look into some energy-saving features like the automatic lights and gray outlets at SparkFun’s headquarters. The goal of the outlets is to shut off at night, cutting phantom power draw from any electronics plugged into them. Just like the gray outlets, some of our lights are on a timer as well as motion sensors. One of the really cool features about the lights that are close to the windows is that they have light sensors attached to them. If it is super sunny out, the lights will shut off, and the natural light will flood into the building.

Learn more about SparkFun’s sustainability efforts on Facebook and Twitter (#EarthWeek) and share your Earth Day projects with us there and in the comments below!

Comments 10 comments

  • Ahem… I think you have a couple of typos in your code…

      if(rtc.hour() > 7 || rtc.hour > 19 ){
        digitalWrite(pin, HIGH);
      }else {
        digitalWrite(pin, LOW);

    First, you have “rtc.hour()” (with parenthesis) in one place and “rtc.hour” (without parenthesis) in another. The compiler is going to complain about it…

    Second, you have a logic problem… you are making the output high if the hour is greater than 7 OR it’s greater than 19. From what I learned in school (admittedly nearly 50 years ago, so things might have changed) if something is greater than 19, it’s also greater than 7. What I THINK you mean is to ask an “AND” and make it LESS THAN 19. So it should be

      if(rtc.hour() > 7 && rtc.hour() < 19 ){

    and I would generally add another couple of pairs of parenthesis, and maybe a few more spaces, just to make it a bit clearer and easier to read:

      if ( (rtc.hour() > 7) && (rtc.hour() < 19)  ){
    • Oh, yes: you also should include a “wall-wart” in your parts list, unless you are planning on leaving your computer on and attached to the Arduino 24/7.

      I sincerely hope that your actual program is going to be quite a bit more sophisticated than simply “on at 7:00 AM and off at 7:00 PM”. If not, a simple timer, available for less than $15 at places like Target, WalMart, CVS, Walgreens, Home Depot, ad nauseum, is a “more appropriate technology”, as well as being a bit “greener”. Although I haven’t measured the “modern” electronic timers, I have measured one of the slightly older “mechanical” ones, and it only uses about 2W.

      • Nope - this program works perfectly for the purposes of this project. It’s a fun and easy earth day hack that I wanted to share. If you make your own, I would love to see how you would do it. This is my version and I like it this way.

        • My disagreement is with your invoking Earth Day for this. Earth Day is all about saving resources by using them efficiently. If you were to add, say, code that picked the on and off times based on the day of the week, that’s something that the simple mechanical timer can’t do. (A more advanced version, that would do things the electronic timers can’t, would be to have a list of holidays and adjust for those.) Another idea might be to add a motion sensor to only turn on the light when there’s someone to actually see it.

          Along the lines of “conserving resources”, one of the resources that is important to conserve is $. I’d suggest that the Teensy 3.5 might be a better choice than the Uno and a separate RTCC, at less cost than the Uno alone. (Yes, the RTCC you selected has very high accuracy, but it seems to me that this is the proverbial swatting flies with sledgehammers.)

    • Oh no! It was an old version of the program. I have updated it with the more recent version. Thanks for catching!

      • You still need to change the “||” to “&&”. The way it is now, it’s ALWAYS ON.

        • Thanks for correcting that. FWIW, I’d estimate that probably 20% of the bugs I’ve worked on in 35+ years as a Software (and more) Engineer have been caused by “minor” logic errors like this. Imagine trying to find one buried in 20,000 lines of code… ;-)

          • Happened to think of another reason for including the extra parentheses that I mentioned earlier: If you have them there, and accidentally put in a single ampersand, it would still work (thanks to the fact that in reality, C represents “true” as 1), but without the parentheses, you would get different results.

  • great, simple build. I would run the PowerSwitchTail before the power strip to cut everything at the entertainment center to cut all the devices that draw power while off and then add an override button for that late night “Stranger Things” binge.

Related Posts

Recent Posts


All Tags