Wednesday, December 28, 2011

Power Off Delay Circuit

Recently I have been kicking myself for forgetting to turn off the motor that pumps water from a sump to an overhead tank at home. Every time I turn on the motor, I try to remember to turn it off in 30 minutes (about the time it takes to fill up the overhead tank from empty to full), but I keep forgetting to turn it off most of the time, wasting a lot of water before I realize it. Then I got an idea. Why not just make a simple, cost effective timer circuit that will turn off the motor after say 30 minutes?

So I started thinking about it. All I need is a 555 timer and the correct resistors and capacitors that will delay the timer for the exact time I want the motor to stay on. Next I need a relay to control the motor.

First the timer has to run in monostable mode. We just need a one-shot pulse. The pulse should be long enough to keep running the motor. Next we use t = 1.1RC to get the approximate time (in seconds) the output of 555 timer will be high based on the R (in ohms) and C (in Farads) values. I wanted the motor to be on for 30 minutes. And since the time is so long I would need a huge capacitor. The biggest I could quickly find was a 1000uF capacitor. Plugging in those values:
t = RC
30 * 60 = R *  1000 x 10^-6
R = 30 * 60 / 1000 MOhms = 1.8 MOhms

However, the best I could find was a 2M4 resistor. Which would mean the motor will be on for more than 30 minutes. I could mix a bunch of resistors to get 1M8 resistance, but I decided to keep it simple. So put 2 2M4 resistors in parallel and let the time come down to 22 minutes. That is fine for my purpose. Next I proceeded with the circuit diagram.


Pretty straight forward circuit for running the timer in monostable mode. The trigger pin (pin 2) has to be low (the voltage on the pin should be below 1/3 the supply voltage) for a small period of time. Normally a 100K resistor in series with 1uF capacitor is used. I was too lazy to find those and just used what I had (1K in series with 100uF). Once the trigger goes high the relay is ON for 22 minutes.

Next I made the circuit on a breadboard to make sure it works and then proceeded to built the circuit board. Been a long time since I used the soldering iron. However, I did not plan on a proper design and so the wires crisscrossed each other.


All done. Now to actually test it. The plan is to connect the motor to the NO (normally open) of the relay. I have not yet tested the circuit, but hopefully I will be able to do it this weekend.

Thursday, November 10, 2011

InduinoX and wireless relays: Part II

In my last post, I played a bit with the wireless relays to turn on a light. In this post I will show how to use LDR to detect when to turn on the light depending on the ambient light.

Lets dive into the code quickly

#define TRUE  1
#define FALSE 0

int LIGHT_PIN = 7;
int SLEEP_TIME = 100;
int LDR_PIN = 3;
int LDR_THRESHOLD_FOR_DARK = 350;
int MAX_COUNT_BEFORE_TOGGLE = 10;

int isLightOn = FALSE;
int toggleCount = 0;

void setup() {
  turnOffDevice(LIGHT_PIN);
  digitalWrite(LIGHT_PIN, LOW);

  Serial.begin(115200);
}

void turnOffDevice(int pin) {
  pinMode(pin, INPUT);
}

void turnOnDevice(int pin) {
  pinMode(pin, OUTPUT);
}

void toggleLight() {
  if (isLightOn) {
    turnOffDevice(LIGHT_PIN);
    isLightOn = FALSE;
  } else {
    turnOnDevice(LIGHT_PIN);
    isLightOn = TRUE;
  }
}

void toggleLightIfNecessary(int input, int threshold) {
  if (input > threshold) {
    toggleCount++;
    if (toggleCount > MAX_COUNT_BEFORE_TOGGLE) {
      toggleLight();
      toggleCount = 0;
    }
  } else {
    toggleCount = 0;
  }
}

void loop()
{
  delay(SLEEP_TIME);
  int ldrReading = 1023 - analogRead(LDR_PIN);
  Serial.println(ldrReading);

  Serial.println(toggleCount);
  if (isLightOn) {
    toggleLightIfNecessary(ldrReading, LDR_THRESHOLD_FOR_DARK);
  } else {
    toggleLightIfNecessary(1023 - ldrReading,
                           1023 - LDR_THRESHOLD_FOR_DARK);
  }
}

So in the loop(), I keep reading the data from the light sensor, invert it and check to see if the light is already turned on. If it is already on and there is enough ambient light, then I turn in off. If the light is off, then it is turned on if the ambient light is not sufficient.

In both cases, I make sure the sensor data is consistently above or below the threshold. Otherwise the light might turn on when some one's shadow causes the light sensor to report a low light etc.

Some things to note:
  1. In this experiment, I made sure that the light sensor is pointed away from the light source and towards a window from where sun light comes into the room.
  2. The threshold value is empirical, based on conditions in my room and my "feeling" of darkness.

Tuesday, October 25, 2011

InduinoX and wireless relays: Part I

It has been a while since I received my wireless relay and I finally got some time this weekend to put them to good use.

The connections were really simple. I connected a 5V DC power supply (check polarity, the center pin should be positive) to the wireless transmitter and a 12V DC power supply to the relay board (which also has the receiver).

To control the relays using InduinoX, I connected the wireless transmitter board ground to the InduinoX board's ground. Connected the relay 0 pin on the board to digital pin 7 on InduinoX.


Finally connecte the light bulb to mains neutral, the live from mains to the common pin on the relay and "normally closed" (NC) pin on the relay to the light bulb.


Generally you would connect the "Normally open" (NO) pin to the light bulb instead of NC, but I will explain in a minute why I had to connect NC. Now to the coding part. The specification for the relay tells us that when the relay pin is open (high impedance), the relay is active. And when the relay pin is connected to the ground, the relay is inactive.

On the InduinoX board, when a digital pin is set to input mode, the input impedance is very high causing it to act like an open switch. So setting the pin 7 to input mode will cause the relay to be active. Now the InduinoX board always starts with all the digital pins set to input mode which means the relay will start off being active. Which is why I connected the NC pin to the light bulb otherwise with NO connected to the light bulb, the light will be on when the InduinoX starts up.

To turn off the relay I have to connect the relay pin to ground. The way it is done with InduinoX is by setting the pin to output mode and then send low signal. Here is the code for all this logic.

#define TRUE  1
#define FALSE 0

int LIGHT_PIN = 7;
int SLEEP_TIME = 1000;

int isLightOn = FALSE;

void setup() {
  turnOffDevice(LIGHT_PIN);
  digitalWrite(LIGHT_PIN, LOW);
}

void turnOffDevice(int pin) {
  pinMode(pin, INPUT);
}

void turnOnDevice(int pin) {
  pinMode(pin, OUTPUT);
}

void loop()
{
  delay(SLEEP_TIME);
  if (isLightOn) {
    turnOffDevice(LIGHT_PIN);
    isLightOn = FALSE;
  } else {
    turnOnDevice(LIGHT_PIN);
    isLightOn = TRUE;
  }
}

A more exciting project with the light bulb and LDR in my next post.

When light bulb is off

When light bulb is on