Another Arduino timer

Another little project, mostly made to learn how to use TIP102 transistors.

The setup is controller an 250 kg electromagnet (500mA@12V), and using only one TIP102 to open and close, and one small 1A 12V switch mode power supply. The power supply is connected the the Arduino’s connector for an external power source, and the magnet is then driven through the VIN pin.

I also added a small piezo from a computer to make it able to beep when the timer is started and stopped.

Everything is mounted on a protoshield 4 and looks very clean (both because there aren’t many components, plus no extra supply wires for the load).

I also added a “Start timer” function, where the analog pin 5 is used to determine when the timer should start. The reason for the analog pin is so it can be used for the pressure pads too, and start when enough pressure is reached. The pin is pulled up by an 10k ohm resistor, and will then be pulled towards gnd by the switch, variable resistor, pressure pad, or what else it could be.

Version 1.0

This is with the TIP102 to switch the 12V output on and off, the piezo to give some audio signal, and a blue LED to show that the TIP102 is activated.

BLAH! Hit the blue wire with my soldering iron, but didn’t burn through the isolation.

Version 1.1

This got the same as version 1.0, but here I added screw terminals for the wires, and also feeding the Arduino through the VIN port, as the on-board diode on the Arduino gave up when it had to both pull the relay and the little motor I added to the TIP120. The TIP120 is connected to the PIN6 (pwm) on the Arduino.

//Define the timer (Up to 49 days)
unsigned long seconds     = 0;
unsigned long minutes     = 15;
unsigned long hours       = 0;
unsigned long days        = 0;

int relDelay    = 1000;   //How long delay there will be between the beep and the release of the magnet

//Digital pins
int piezoPin    = 12;    //Where the piezo is connected to
int ledPin      = 13;    //Using the on board LED for visualisation
int relayPin    = 9;     //Where the TIP102 is connected to
int switchPin   = 5;     //The pin with the switch that starts the timer

unsigned long waitTime = 0;
unsigned long startTime = 0;
unsigned long lastBlink = 0;

boolean activated = false;

void beep(int freq, int dur)
{
  digitalWrite(ledPin, HIGH);

  tone(piezoPin, freq, dur);
  delay(dur);

  digitalWrite(ledPin, LOW);
}

void setup()
{
  //Set pin modes
  pinMode(piezoPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(relayPin, OUTPUT);

  //Calculate the waiting time
  waitTime = seconds*1000;
  waitTime += (minutes*1000)*60;
  waitTime += ((hours*1000)*60)*60;
  waitTime += (((days*1000)*60)*60)*24;

  if (waitTime >= 4294967295)
  {
    waitTime = 4294967294;
    beep(150, 1000); //Warn with one long beep about it shortened down
    delay(500);
  }
}

void loop()
{
  if (!activated)
  {
    if (analogRead(switchPin) < 1)
    {
      activated = true;

      for(int i=0; i < 2; i++) //Two beeps to tell the timer has started
      {
        beep(2500, 100);
        delay(80);
      }

      digitalWrite(relayPin, HIGH);
      startTime = millis();
      lastBlink = millis();
    }
  }
  else
  {
    if ((millis() - startTime) + relDelay >= waitTime)
    {
      if (waitTime != 4294967295)
      {
        waitTime = 4294967295; //Yay! You made it, lets set it to max so it won't keep beeping

        for(int i=0; i < 4; i++)
        {
          beep(1000, 100);
          delay(80);
        }

        delay(relDelay); //Release delay to warn about the magnet is about to be released

        //Switch magnet off
        digitalWrite(relayPin, LOW);
      }
    }
    else
    {
      if ((millis() - lastBlink) >= 60000 && waitTime != 4294967295)
      {
        beep(500, 50);
        lastBlink = millis();
      }
    }
  }
}

2 comments

  • I just got your code -Thanks for that .
    I am trying to make an off peak pool timer which checks if power is availabl then switches a relay on for 3 hours in each day . problem being the power goes on and off randomly so I have to accumulate the time on until it gets all it needs each day then wait for the next day.

    Hav’nt found any suitable examples so if you know of one please put it here .Meanwhile I will play with your code and see what I can make.

    • Sorry, I haven’t used Arduino’s for quite a while, and I am not sure how to store the time as you need it.

      One bad solution would be to store it in the eeprom, but it can’t be written an unlimited amount of times, so it would end up breaking.

      Maybe you could add a RTC module, and SD card. Then check the RTC module for the date, and store current date and amount of minutes on. In setup() you could then read the file on the SD card, and check date from the file against date from the RTC module, if date isn’t a match, zero the time stored on the SD card. If date is the same, put the amount on into an integer, then add to that, and store to the SD card every minute.

      Storing the data once every minute will give you up to 59 seconds not registered though, but saving once ever second sounds a bit excessive too.

Leave a Reply