Tuesday, February 25, 2014

Arduino based motion activated light and gas sensor for the kitchen using attiny85


I've been wanting to get my hands on arduino after seeing how powerful it is. Luckily, I was able to find a clone here in the Philippines, which is good enough to tinker with. This board is named Aceduino and is very similar to the Uno.

It wasn't hard to find what I'm going to use it for (endless possibilities!). The immediate application around the house is automation. It's not like I have a big house that flicking a switch is a big effort. There are just some things you want done automatically especially when it is going to make things safer.

For this project, I wanted it to automatically turn the kitchen counter light on and off so we don't have to touch the switch with wet hands. I shopped for a few components to go with the arduino:

  • PIR sensor - to detect motion near the counter or sink
  • 5V Relay - to turn the light on/off
  • Some diodes, resistors and a small switch
I don't intend this to be a tutorial so unless there are people interested on the build itself, I'll skip to the good part. 

Using a few guides on the net, I was able to make a working prototype. That's when I realized this board would be a huge waste if all I'm going to use is just a couple of I/O ports.  This is where the attiny85 comes in.

Aceduino and Attiny85 
 The attiny85 has only 5 I/O ports that I can use but even then, there will be a few more unused ports if all it will do is turn on a light. I thought about other ways to make use of the extra ports and found something appropriate for the kitchen - a gas sensor.

I went to buy the additional parts:
  • MQ5 Gas sensor
  • Buzzer

So here are the features of the final build:
  • motion activated light
  • detects a gas leak and sounds an alarm when it does
  • manual switch - if you want to keep the light on even when no motion is detected or if you want to suppress the alarm
It could use a better case but until I get a 3D printer, this will do.

And here it is in action:



Edit:
Here's the sketch for the attiny85 chip:
const int inPin1 = 1;   // switch connected to digital pin 1
const int ssrPin = 2;   // relay
const int pirPin = 4;   // PIR sensor
const int buzPin = 0;   // buzzer
int motionDetect= 0;
int manualSwitch = 0;
int motionSwitch = 0;
int gas = 0;
int buz = 0;

unsigned long timer;
unsigned long timerGas;
unsigned long timerPIR;
unsigned long timerBuz;
unsigned long timerBuzMin;

void setup() {
  pinMode(ssrPin, OUTPUT);
  pinMode(pirPin, INPUT);
  pinMode(inPin1, INPUT);
  digitalWrite(ssrPin, LOW);
  timerPIR = 0 - 70000;
  timerBuzMin = 0 - 6000;
  //Serial.begin (9600);
}

void loop()
{

  timer = millis();

  motionSwitch = digitalRead (inPin1); 
  if (motionSwitch == HIGH) // Motion Mode
  {
  gas = analogRead(A3); // read gas sensor
  if (gas >= 500) timerBuzMin = timer;  // range is from 0-1024
  if (timer - timerBuzMin <= 5000)
    {
      if (timer - timerBuz >= 300)
      {
        tone (buzPin, 2000, 200);
        timerBuz = timer; 
      }
    }

    //Serial.println("motionDetect");
    motionDetect = digitalRead(pirPin);
    //Serial.println(motionDetect);
    if (motionDetect == HIGH)
    {
      digitalWrite(ssrPin, HIGH);
      timerPIR = timer;
    }
    else
    {
      if (timer - timerPIR >= 60000) // turn on relay for at least a minute
      {
        digitalWrite(ssrPin, LOW);   
      }
    }
  }
  else digitalWrite(ssrPin, HIGH); // Manual on
}

8 comments:

  1. You know what's funny? I had to adjust the sensitivity of the MQ5 gas sensor in the arduino sketch because the alarm goes off whenever someone near it passes some gas :D LOL!

    ReplyDelete
  2. Do you know where i can find a tutorial on building a motion sensor using the ATtiny? I've looked and only found your example. I just need to drive a small LED strip light to make a 'night light' for late night trips to the bathroom. I don't need a full power light with a relay, just a small LED strip light to come on at a low power level when the motion is detected and turn off after a set amount of time. I do like your idea for the kitchen and will probably do something like that soon.

    ReplyDelete
  3. Hi there. I believe that's exactly what I've accomplished here in my other post. If you can find a similar PIR sensor, you won't even need the attiny chip for the simple task of turning on and off a few LEDs. You have to mind the maximum current it can supply. Mine still works up until now. =) If you really must use the attiny chip, let me know. I might be able to give you a hand.

    ReplyDelete
    Replies
    1. I found that shortly after I posted this... I think I have a different PIR because it works night and day no matter what the "sensitivity" POT is set at; it must be for distance not light. I'm looking for a new PIR. I was thinking i could power it with an old 5V phone charger... Thanks for the post, it helped a lot; and i might take you up on the ATtiny help offer a little later. Thanks again.

      Delete
  4. Sure. I'm just traveling for a day. I will post them as soon as I can.

    ReplyDelete
  5. Hi there,

    Could you pls send to my email the circuit diagram?

    thanks in advance,

    Diego

    ReplyDelete
  6. Hi there,

    Could you pls send to my email the circuit diagram?

    thanks in advance,

    Diego

    ReplyDelete