User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Sensing when Garage Door is Open

Fri Jun 29, 2012 7:37 pm

I'm doing 2 follow up projects that are and are not related to this :-P Here's the links

Part 2 - Opening the Garage Door with the Pi
Part 3 - Irrigation Controller

Okay so as part of my grand master scheme for a Raspberry Pi controller I was hoping to get some input on this basic circuit I've drawn up.

Just a disclaimer, I'm not a master electrician, so if I use the wrong symbol (which I'm sure I have) be nice about it ;-)

Image

The left side of the circuit is the detection part. I send 3v3 out to a normally open magnetic reed relay, when the garage door opens, the relay closes. Which completes the circuit to my GPIO which I'll set up as an input. I've set up the circuit, I think, to prevent floating inputs. I've also tentatively put a capacitor in the mix for debounce, but I don't know what kind to get. The pi will probably be 10-20 feet away from the magnetic reed relay, so I'm also hoping the capacitor will provide some RF filtration.

The right side of the circuit is the alerting part. When the relay closes, I'm going to light up a single, super bright, red LED. I don't think the GPIO can put out enough current to light the LED to my satisfaction, so I'm going to have it trigger a relay that will use the 5V line instead. I've again configured it to be a pull down circuit, but now that I'm thinking about it, I should have the pull down on the GPIO itself, rather than the 5V line, oops. That also means the resistor right before the LED should be a full 220 Ohm resistor, rather than a 120 Ohm, according to it's packaging.

Should I put a capacitor on the right side of the circuit for RF filtration as well? It will travel probably... 40 ft to the LED and then back again to the pi.

Finally, I'm thinking I should put a diode after the 10K resistor attached to the 3v3 line as a buffer between it and the 5v line.

Thoughts?
Dear forum: Play nice ;-)

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:02 pm

You don't really need a relay for a super bright LED, in fact it is harder to drive a relay than it is the bright LED as you have emf feedback with a relay coil that will require a clamping diode. Also you will not find a relay that will energise at 3.3V and less than 10mA so you would require a transistor.

Basically all you need is a transistor to drive your high powered LED.

I don't know how quickly you plan on doing this but the 3rd part if the In Control article in the MagPi magazine will be covering transistors, relays, triacs etc.

scorp
Posts: 96
Joined: Tue May 29, 2012 6:21 pm

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:15 pm

Why do you need to use GPIO(out)? Just use transistor for LED. Left side is a bit confusing. I would do like this:
Image

Uploaded with ImageShack.us

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:19 pm

scorp wrote:Why do you need to use GPIO(out)? Just use transistor for LED. Left side is a bit confusing. I would do like this:
Image

Uploaded with ImageShack.us
Oh! Not a bad idea at all! Now I notice that you're using a transistor in this layout as well. Now that you got me thinking about it, is there any reason not to put the LED in line with the relay?

Side note: If I'm going to do it like this, I don't really need to use *any* GPIO at all and can just connect it straight to ground.


Edit: Sorry if you're in the middle of a reply, but now that you got my on this track, I think I'll just power the whole thing from the 5V rail and skip using the GPIO. I was only planning on using the GPIO when I was going to use my original circuit. If I'm not doing that, there are other things I want to use my GPIO pins for.
Dear forum: Play nice ;-)

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:31 pm

You'll need to use a GPIO if you want the Pi to know when the garage is open.

scorp
Posts: 96
Joined: Tue May 29, 2012 6:21 pm

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:38 pm

domesday wrote:You'll need to use a GPIO if you want the Pi to know when the garage is open.
+1, ... and regarding transistor, depends on LED current(by the way, I forgot resistor in series with LED) and rate of your relay contacts you may connect LED via relay.

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:41 pm

domesday wrote:You'll need to use a GPIO if you want the Pi to know when the garage is open.
Sorry, domesday, I made this response, but I did so by clicking "edit" on your post rather than "quote" :oops:

True true, but only if I want to know when the garage is open on the Pi. I haven't decided about this one yet. My main concern was being able to see if the garage door was up when I am climbing into bed, which the LED does. I haven't come to a strong conclusion on if I want to know programatically when the garage door is up or not. I might do it just so if one day in the future I want that feature I've already configured things for it.... I'll let y'all know which way I go.

One last question though. What about the capacitor? I know that using one is a good idea for debounce at the very least, but what about line filtration? And what size capacitor should I get for such a low powered application?
Dear forum: Play nice ;-)

scorp
Posts: 96
Joined: Tue May 29, 2012 6:21 pm

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:46 pm

abishur wrote:
domesday wrote:You'll need to use a GPIO if you want the Pi to know when the garage is open.
Sorry, domesday, I made this response, but I did so by clicking "edit" on your post rather than "quote" :oops:

True true, but only if I want to know when the garage is open on the Pi. I haven't decided about this one yet. My main concern was being able to see if the garage door was up when I am climbing into bed, which the LED does. I haven't come to a strong conclusion on if I want to know programatically when the garage door is up or not. I might do it just so if one day in the future I want that feature I've already configured things for it.... I'll let y'all know which way I go.

One last question though. What about the capacitor? I know that using one is a good idea for debounce at the very least, but what about line filtration? And what size capacitor should I get for such a low powered application?
The cap is not really for debounce. Debounce better to filter by simple software reading twice in short delay(10-20ms). Cap is more to filter noise if wires are long. You can omit it. I would put 10-100nF

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:49 pm

scorp wrote:
abishur wrote:
domesday wrote:You'll need to use a GPIO if you want the Pi to know when the garage is open.
Sorry, domesday, I made this response, but I did so by clicking "edit" on your post rather than "quote" :oops:

True true, but only if I want to know when the garage is open on the Pi. I haven't decided about this one yet. My main concern was being able to see if the garage door was up when I am climbing into bed, which the LED does. I haven't come to a strong conclusion on if I want to know programatically when the garage door is up or not. I might do it just so if one day in the future I want that feature I've already configured things for it.... I'll let y'all know which way I go.

One last question though. What about the capacitor? I know that using one is a good idea for debounce at the very least, but what about line filtration? And what size capacitor should I get for such a low powered application?
The cap is not really for debounce. Debounce better to filter by simple software reading twice in short delay(10-20ms). Cap is more to filter noise if wires are long. You can omit it. I would put 10-100nF
The wires will be a little on the longer side, it's less than a buck to add it in seems worth while ;-)
Dear forum: Play nice ;-)

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 8:51 pm

I think perhaps I'm not following something somewhere but ...

If all you want is an LED to light up in your bedroom to tell you when the garage door is open without any monitoring by the Pi then all you need is a reed switch that will be normally closed, so that when the door is down and the magnet in-line with the reed switch the circuit will be broken, but then if you open the garage door the magnet will move away and the circuit will close to light the LED. Literally you will need power supply, reed switch, LED & current limiting resistor (value dependent on voltage used). No capacitors or transistors will be required.

If you intend to monitor the switch status with the Pi GPIO then you will need a de-bounce capacitor, no need to worry about RF, unlikely to induce a high enough Voltage to trigger a high state.

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 9:08 pm

domesday wrote:I think perhaps I'm not following something somewhere but ...

If all you want is an LED to light up in your bedroom to tell you when the garage door is open without any monitoring by the Pi then all you need is a reed switch that will be normally closed, so that when the door is down and the magnet in-line with the reed switch the circuit will be broken, but then if you open the garage door the magnet will move away and the circuit will close to light the LED. Literally you will need power supply, reed switch, LED & current limiting resistor (value dependent on voltage used). No capacitors or transistors will be required.

If you intend to monitor the switch status with the Pi GPIO then you will need a de-bounce capacitor, no need to worry about RF, unlikely to induce a high enough Voltage to trigger a high state.
Yeah the confusion is because originally I had planned to do this with 2 GPIO pins, but when I saw I could do it with one, that made me wonder if I really needed a GPIO at all or, if like you say, I could do it with just a PSU, reed switch, and LED. I'll be using the PI simply because I also plan to do other things which will need the pi, but as the thread's been going I've been talking out loud, leading to the confusion. :P

Give me a few, I'll come to a decision and post revision 2 of my circuit idea.
Dear forum: Play nice ;-)

scorp
Posts: 96
Joined: Tue May 29, 2012 6:21 pm

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 9:26 pm

Garage door can be part of your future house security system. Alarm via sms...

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 10:13 pm

Okay here's revision 2

Image

I have two questions about this revision

1) Will it actually work? I based this off scorp's suggestion, but will it send power to both my GPIO pin and the Base pin of the transistor?

2) Where should I place the capacitor?
Dear forum: Play nice ;-)

User avatar
[email protected]
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Sensing when Garage Door is Open

Fri Jun 29, 2012 10:59 pm

So you want a visual indication of when the garage door is open, and optionally you want a signal into the Pi...

From the KISS department:

Code: Select all

   ----+----- 3.3v
       |
       R
       R 270 ohm
       R
       |
      ---
      \ / LED
      ---
       |
       +------> GPIO
       |
       +
     |    Reed
       +
       |
    ---+--- 0v
So the GPIO pin will read high (enable the internal pull-up) when the reed switch is open. When the reed closes (ie. when the door is open) then the LED will light up (Standard superbright 5mm LED is fine) and the GPIO input will read zero.

Job done with no more fancy faffing about with more resistors, capacitors, transistors, GPIO outputs, relays, etc.

However, FWIW: The GPIO is more than capable of illuminating a high brightness 5MM LED with a 330 ohm resistor - I got some accidentally and almost blinded myself when I turned them on...

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 2:10 am

[email protected] wrote:So you want a visual indication of when the garage door is open, and optionally you want a signal into the Pi...

From the KISS department:

Code: Select all

   ----+----- 3.3v
       |
       R
       R 270 ohm
       R
       |
      ---
      \ / LED
      ---
       |
       +------> GPIO
       |
       +
     |    Reed
       +
       |
    ---+--- 0v
So the GPIO pin will read high (enable the internal pull-up) when the reed switch is open. When the reed closes (ie. when the door is open) then the LED will light up (Standard superbright 5mm LED is fine) and the GPIO input will read zero.

Job done with no more fancy faffing about with more resistors, capacitors, transistors, GPIO outputs, relays, etc.

However, FWIW: The GPIO is more than capable of illuminating a high brightness 5MM LED with a 330 ohm resistor - I got some accidentally and almost blinded myself when I turned them on...

-Gordon
hmmm... I suppose it doesn't matter if it's a 1 or a 0 on the GPIO as long as I program it correctly.

One thing I'm missing though. If the GPIO is an input, then why wouldn't the LED light up all the time? That's a path to ground, a complete circuit, right :? .
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 2:51 am

I still don't understand why the LED wouldn't always be lit up, but if I put it on the other side like so

Code: Select all

----+----- 3.3v
           |
           +------> GPIO
           |
           R
           R 270 ohm
           R
           |
          ---
          \ / LED
          ---
           |
           +
         |    Reed
           +
           |
        ---+--- 0v
Then that should work too right? I enable the internal resistor (making it a pull up resistor) so when the relay is open the GPIO will read high. When the relay is closed, it will go the path of least resistance through the LED. Right?
Dear forum: Play nice ;-)

User avatar
jojopi
Posts: 3862
Joined: Tue Oct 11, 2011 8:38 pm

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 3:20 am

abishur wrote:One thing I'm missing though. If the GPIO is an input, then why wouldn't the LED light up all the time? That's a path to ground, a complete circuit, right :? .
If the GPIO is an input, then it presents a very high resistance. It measures the voltage at the pin but does not significantly influence the voltage or allow significant current to flow.

If the GPIO is an output 0, then the LED will be on all the time. Also, if the GPIO is accidentally an output 1, then the reed switch will short it to ground. You should probably add a series resistor of at least 1KΩ to protect against damage from any such software error.
abishur wrote:Then that should work too right? I enable the internal resistor (making it a pull up resistor) so when the relay is open the GPIO will read high. When the relay is closed, it will go the path of least resistance through the LED. Right?
That GPIO is tied directly to 3.3V. Ergo it will always read high.

Incidentally, path of least resistance is a total myth. Consider two parallel resistors X and Y, with X < Y. The current does not flow all through X, but through both X and Y in ratio (1/X):(1/Y) (or Y:X). The effective resistance is not X but (X*Y)/(X+Y).

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 3:39 am

jojopi wrote:
abishur wrote:One thing I'm missing though. If the GPIO is an input, then why wouldn't the LED light up all the time? That's a path to ground, a complete circuit, right :? .
If the GPIO is an input, then it presents a very high resistance. It measures the voltage at the pin but does not significantly influence the voltage or allow significant current to flow.
Okay, I'm just going to have to trust you on that one ;-)
abishur wrote:Then that should work too right? I enable the internal resistor (making it a pull up resistor) so when the relay is open the GPIO will read high. When the relay is closed, it will go the path of least resistance through the LED. Right?
That GPIO is tied directly to 3.3V. Ergo it will always read high.
Wait, I don't see how this would be different from what you just said about the LED except reversed a little. When the relay is closed, then by that formula you listed the majority of the current should go through the led and relay and there shouldn't be enough left over to allow current to flow?

But like I said, I'm not a master electrician so it might be something I just don't get until I learn more about this stuff. I suppose I could always set up my bread board and do some real life applications to see it working in action, now couldn't I? ;-)
Dear forum: Play nice ;-)

User avatar
[email protected]
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 7:39 am

abishur wrote:I still don't understand why the LED wouldn't always be lit up, but if I put it on the other side like so

Code: Select all

----+----- 3.3v
           |
           +------> GPIO
           |
           R
           R 270 ohm
           R
           |
          ---
          \ / LED
          ---
           |
           +
         |    Reed
           +
           |
        ---+--- 0v
Then that should work too right? I enable the internal resistor (making it a pull up resistor) so when the relay is open the GPIO will read high. When the relay is closed, it will go the path of least resistance through the LED. Right?
That won't work - for a few reasons. Mostly because the GPIO input is tied directly to +3.3v, so even with the reed switch closed, it will still be connected directly to 3.3v and therefore read "high" to the GPIO.

So when in input mode, a GPIO will not sink current, it will sense voltage. Think of a volt meter - when you connect it over a component, it doesn't take any current from the component, but it's able to tell the voltage (actually it does take some current, but it's in the order of microAmps - usually not enough for most circuits to notice) - the same is happening on the GPIO pin - it "senses" the voltage but doesn' sink current.

The internal resistors are quite high value I think I read that the BCM chip has the equivalent of 100K ohm resistors on the pull-up side which in most cases in't going to affect anything.

I'll build this up later today and post a video demo.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
[email protected]
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 10:02 am

[email protected] wrote: I'll build this up later today and post a video demo.

-Gordon
Built it up quickly - photo here:

Image

and a video demo here:

http://youtu.be/SNHY1Tv7p1w

It got cut off slightly, but you'll get the idea (my old camera can only do 1 minute of video!)

The program is:

Code: Select all

#!/bin/bash

    red=0
  green=1


# setup:
#       Program the GPIO correctly and initialise the lamps
#######################################################################

setup ()
{
  echo Setup
  gpio mode $red in
  gpio mode $red up
  gpio mode $green out
  gpio write green 1
}

#######################################################################
# The main program
#######################################################################

setup
while true; do

  if [ `gpio read $red` = 1 ]; then
    gpio write $green 1
  else
    gpio write $green 0
  fi
  sleep 0.1

done
Enjoy!

-Gordon
--
Gordons projects: https://projects.drogon.net/

scorp
Posts: 96
Joined: Tue May 29, 2012 6:21 pm

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 11:22 am

Just a reminder, I don't know what kind of reed relay is going to be used but some of them are very low current and may be stuck if higher current used. Also type of LED is unknown, the current consumption. It is obvious for those who know it and do it every day.

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 12:08 pm

Thanks for the vid Gordan, I may have had a touch of insomnia last night caused by working this out continually in my mind until I got up and build the circuit with my bredboard too :-P

About your program (finally an area I'm good in! :lol: ), I'm guessing you've changed the GPIO interface provided and assigned the names red and green to two of the pins. Also, I don't need to use 2 GPIO, right? You were just doing that for my visual benefit.

A major thanks to *EVERYONE* whose helped me out on this thread. I'm a little disappoint *none* of my original circuit idea made it in the end, but I'm glad that at least it was due to being overcomplicated rather than flat out wrong! :P It is encouraging that I'm slowly but surly getting a handle on this stuff.

Thanks again :D
Dear forum: Play nice ;-)

User avatar
[email protected]
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 3:30 pm

abishur wrote:Thanks for the vid Gordan, I may have had a touch of insomnia last night caused by working this out continually in my mind until I got up and build the circuit with my bredboard too :-P

About your program (finally an area I'm good in! :lol: ), I'm guessing you've changed the GPIO interface provided and assigned the names red and green to two of the pins. Also, I don't need to use 2 GPIO, right? You were just doing that for my visual benefit.
The program is a bash script - not something I'd recommend for "production" use, but something that can be used to quickly prototype "stuff". So red=0 and green=1 sets the variables red and green to 0 and 1 respectively. And this is when confusion sets in - I wrote my wiringPi library and the gpio program to have a simplified view of the pins to make it familiar to people coming to the Pi from Arduino land, so in wiringPi, pin 0 refers to BCM_GPIO 17 and pin 1 to BCM_GPIO 18, so the input 'red' is GPIO 17 and the output green is GPIO 18.

The gpio program is part of wiringPi - it allows you to control the GPIO pins from the command-line - again, it's not really good for production, but makes it easy to test things out. If you want to see a bigger example, then look at the TuxX (Tux Crossing) program I wrote last week:

http://project-downloads.drogon.net/fil ... es/tuxx.sh

The main article for that is:

https://projects.drogon.net/raspberry-p ... -crossing/

And yes, I put the Green LED there for a visual demo - it was easier to put the LED there than to move that RPi in-front of a screen where I'd just be printing "open" or "closed" :-)

I did change my wiringPi recently to recognise "proper" GPIO numbering too, so if you replaced every call to 'gpio' with 'gpio -g' and changed the assignments to red=17 ; green=18 then it would work just the same.
abishur wrote:A major thanks to *EVERYONE* whose helped me out on this thread. I'm a little disappoint *none* of my original circuit idea made it in the end, but I'm glad that at least it was due to being overcomplicated rather than flat out wrong! :P It is encouraging that I'm slowly but surly getting a handle on this stuff.

Thanks again :D
One thing I've learned over the years is to not be afraid of throwing away code for something better and I've done it a lot, however I am a bit of an efficiency nut :) So when I see things that aren't strictly neccessary I tent to not use them - so e.g. in my circuit, if I were to accidentally set GPIO 17 (signal from the red LED/switch) to an output and write 1 to it, and you pushed the button then you're short-circuiting that pin to ground which may burn out the pin driver, and possibly something else on the SoC... But I acknowledge this and take the risk, as the designers of the Pi did when the wired up the SD card present pin. I (or you!) could put in a 100 ohm series resistor with the signal from the Red LED to the GPIO which would more or less eliminate any real issues while still maintaining working conditions ... but apply that to the Pi and the SD present sensor input - if that resistor and the additional manufacturing costs amount to an extra 1p per board, then when they make 1,000,000 boards, they've cost £10,000 more ... And in the world of tiny tiny margins, that's a lot!

So... A light and a switch doesn't need a computer, but if you arrange it so that adding a computer in to sense the switch is easy, then it's also becomes fun!

Your homework is making the Pi drive a motor to automatically close the garage doors ;-)

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Sat Jun 30, 2012 5:34 pm

[email protected] wrote:Your homework is making the Pi drive a motor to automatically close the garage doors ;-)

-Gordon
:lol:

That's phase two actually! Open the garage door from an encrypted web page. You click a button it writes a 1 to an optocoupler for half a second, which in turn allows the 120V AC electricity through a triac completing the circuit and openning the garage door (it will be wired in parallel with the regular push button for the garage door).

But like I said, that's phase 2 and better left for another post ;-)
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4501
Joined: Thu Jul 28, 2011 4:10 am
Location: USA

Re: Sensing when Garage Door is Open

Sun Jul 01, 2012 3:56 am

Just to follow up, here's the circuit in action!

I wired it to be a normally open relay, so here's the magnet removed, closing the circuit and the LED lights up (edit: or rather you would see the LED lit up if I had actually gotten the LED in the shot! :oops: ).

Image

I didn't take my pic to showcase the circuit as Gordon has already done a masterful job there! I'm just showing that it worked :-P

Here's the magnet in place, making the circuit open and the LED turns off. (Again, the LED is out of the frame, but it was being controlled by the magnetic reed relay and that's all I cared about!)

Image

And here's my computer screen full of zeros and ones (which just to make things confusing here, when the pin is zero, the circuit is closed and the LED is on, but when it's one, the circuit is open and the LED is off. I understand why it does it, but it still makes my brain grind and scream for me to use the clutch next time ;-))

Image

Finally, here's the code I used to observe it happening. I used Gordon's library for interfacing the GPIO pins in C since I actually know that language, and I'm lazy so I based it off Gordon's test1 program :-P

Code: Select all

/*
 * garageSense.c:
 *      Tells when Garage door is not fully shut
 */

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main (void)
{
  int oldValue = 2 ; // Set to 2 so that on first run through I'd see the default value for the pin, when the relay is closed (garage door is open) value on input pin will be zero.
  int newValue ; // Will be used for change of state detection
  int pin = 1; // used to keep while loop going
  int garageStatus = 8; // gave GPIO pin a logical name rather than remembering the number, Pin 8 SDA0 - Has on-board 2k2 pull-up resistor

  if (wiringPiSetup () == -1) // Makes sure GPIO was initialized correctly, if it doesn't program ends
    exit (1) ;

  pinMode (garageStatus, INPUT) ;  // assigned my pin to be an input

  while (pin != 2) // pin never equals 2, must use ctrl+c to end program, used for testing purposes
  {
     newValue = digitalRead (garageStatus); // get's current value of input pin
         if (newValue != oldValue) // Change of state detection, used for testing purposes, prints the current value of the input pin every time it goes from a zero to a one and vise versa
                printf ("%d\n", newValue) ;
        oldValue = newValue ;
        delay(10); // debounce
  }


  return 0 ;
}

Again, thanks to everyone for helping out! It will be a while before I do anything with this as I need a second pi, but it was fun to finally get to play with the hardware of the pi!
Dear forum: Play nice ;-)

Return to “Automation, sensing and robotics”