N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Automated light controlled chicken coop door (Self Locking), GPIO motors and limit switches

Sun Jan 21, 2018 6:57 pm

Hey everyone,
I have a project i started to open and close the door to my chicken coop. I have the raspberry pi with a LDR (light sensor) module and 2 magnetic switches attached. I have the design finalized and am now fumbling my way through learning python to control the setup.

This is the design of the door:
Image *https://drive.google.com/file/d/1JQRUI_ ... sp=sharing*
This is my attempt at writing the code:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)
GPIO.setup(11,GPIO.IN)
GPIO.setup(12,GPIO.IN)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setwarnings(False)

try:
        while GPIO.input(7) == False:
                if GPIO.input(11) == False:
                        print ("Day")
                        print ("Open door")
                        GPIO.output(15,True)
                elif GPIO.input(11) == True:
                        GPIO.output(15,False)
                        print ("STOP")
        time.sleep(5)
                        
        while GPIO.input(7) == True:
                if GPIO.input(12) == False:
                        print ("Night")
                        print ("Close door")
                        GPIO.output(13,True)                   
                elif GPIO.input(12) == True:
                        GPIO.output(13,False)
                        print ("STOP")
                time.sleep(5)
        time.sleep(10) 
finally: 
        GPIO.cleanup()
Thank you in advance for any help that can be provided and apologies for any noob mistakes.
Last edited by N_Aguiar on Thu Oct 25, 2018 1:57 am, edited 3 times in total.

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 8:19 am

so basically the goal is to have the pi check to see if it is light or dark out
if its light, check to see it either of the limit switches are activated
if its light and the bottom switch is activated - run motor in open direction until the top switch is activated.

and vise versa if it is dark

Check every hour ( the code has 5 second pauses at an attempt to test the theory)

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 12:17 pm

So you have told use what you want but not what problems you are having.

One problem I can see is with your motor drive , you seem to be using 2 gpio outputs to drive your motor but you have not said what these are connected to to actually drive you motor, as you can't drive a motor directly from the gpio and you need some way of reversing the connections on the motor to change its direction.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 12:50 pm

Duh, sorry about that, the problem seems to be at last check the code spits out a stop no matter what switch is activated

Thanks for the reply

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 1:02 pm

And the gpio outputs are driving 3v relays attaches to a 12v bat and a 12v stepper motor

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 1:05 pm

well for a start you seem to have your night and day reversed.

according to drawing gpio 7 true = day, false = night

but in your code

Code: Select all

        while GPIO.input(7) == False:
                if GPIO.input(11) == False:
                        print ("Day")
                        
        while GPIO.input(7) == True:
                if GPIO.input(12) == False:
                        print ("Night")             
                        
     
gpio 7 = false = day , gpio 7 = true = night

so which is correct
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 1:16 pm

Sorry again. The code has been changed to reflect what physically happens when a light is shined on the ldr gpio 7. I must not have changed the drawing

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 1:23 pm

Also i have not physically connected the relays to the gpio output pins yet and am going only by the print statement at the moment

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 2:03 pm

ok another question for your switch inputs how are you pulling them low are you using external resistors ? as you have no pull down commands in your gpio set up.

So simulated your set up and tested your code seems to be operating correctly you just needed some delays in your code to slow things down a bit and make the program loop so that it will open and close the door , as you had it after one cycle your program stopped.

So this is the code after I made a few changes.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7,GPIO.IN)   #ldr
GPIO.setup(11,GPIO.IN)  #top switch
GPIO.setup(12,GPIO.IN)  # bottom switch
GPIO.setup(15,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)

delay1 = 1  # time between checks when door moving for switch operation may need to be shorter 
delay2 = 5  # time between checks of LDR

while True:
        while GPIO.input(7) == False:
                if GPIO.input(11) == False:
                        print ("Day")
                        print ("Open door")
                        GPIO.output(15,True)
                        print "drive motor"

                elif GPIO.input(11) == True:
                        GPIO.output(15,False)
                        print ("STOP door open")
                time.sleep(delay1)        
        time.sleep(delay2)
                        
                        
        while GPIO.input(7) == True:
                if GPIO.input(12) == False:
                        print ("Night")
                        print ("Close door")
                        GPIO.output(13,True) 
                        print "drive motor"

                        
                elif GPIO.input(12) == True:
                        GPIO.output(13,False)
                        print ("STOP door closed")
                time.sleep(delay1)       
        time.sleep(delay2)
        
Now as per the question at the start of this post you do need pull downs on your switches or when you remove the 3.3v high they will just float and things will not work as expected. ( for testing I just moved input form 3.3v pin to ground pin )
Last edited by pcmanbob on Mon Jan 22, 2018 7:02 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

boyoh
Posts: 1491
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 4:40 pm

Be fair and explicit with what you want to know from the forum. How can they help you with the programing
when you keep shifting the goal post's Advice draw a compleat circuit diagram of your design electronics
for your project, Then you might get to know about your programing

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 4:53 pm

Thank you a Million times... I was tearing my hair out...

Here is how the switches are wired:
https://drive.google.com/file/d/193qzGE ... sp=sharing

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 5:05 pm

Thank you to all.. i will be sure to include any and all information in future project posts. One more quick question, may be a dumb one but can you run more than one script at a time? I would eventually like to hook up another relay with a heater, monitored by a temp sensor to keep water above freezing. This however would only need to be run in the winter months. Even further down the line i would like to stream a video feed from the same pi, but thats way off in the future...

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 7:01 pm

The switches are fine you are using external pull down resistors, which I always prefer.

Yes you can run more that one program , I have pi's running 3 programs 24/7 without any problems.

You door program and a heater program will not push the pi , adding the live video streaming will be a bit more processor/network intensive but you should still not have a problem.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 7:09 pm

Great, thanks again...

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 8:54 pm

Ok last question lol... if i went with the following magnetic sensor modules i could do away with the resistors, true?

https://rover.ebay.com/rover/0/0/0?mpre ... 2235567517

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Mon Jan 22, 2018 9:27 pm

May be if you are using the digital output and it will work at 3.3V , if it will only work on 5V then you will have to use resistors any way to reduce the voltage for input to the gpio.

Before you spend money why not try your setup using internal pull downs
Removed the resistors and change you input set up like this

Code: Select all


GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
And see if that will work for you.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 2:39 pm

The setup is fine now, i have already ordered those switches i just didn't want to put resisters on it since it already has a digital output and not knowing enough about electronics didn't want it to stop it from working

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 2:44 pm

N_Aguiar wrote:
Tue Jan 23, 2018 2:39 pm
The setup is fine now, i have already ordered those switches i just didn't want to put resisters on it since it already has a digital output and not knowing enough about electronics didn't want it to stop it from working
1. I thought you said you were using resistors already, you posted a diagram showing how they ere connected.

2. check the output of these boards before you connect them to your pi to be sure they output 3.3v max or you will damage your pi ( note switch board operating range 3.3 - 5V )
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 3:43 pm

The switches i am using right now are just push button switches to test, those 3v magnet sensor switches will be on final assembly. I was planing to use regular old window magnet switches from an old home security system that were pretty much just a magnet operated momentary switch, thats why i had the resistors

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 3:50 pm

That type of switch will work well especially in you chicken coop environment, you can mount the resistors on a small piece of strip board in the same case you intend to use for your pi, and just run the 2 wires to each switch.

The sensor boards will require there own cases and at least 3 wires to each to make them work reliably, and protect them from the chickens.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 3:54 pm

Ok, i will just keep those for a future project, thank you

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 5:14 pm

You might have issues with the door closing on cloudy days. People at BC that use light to control the door have had issues with false positives.
I use a pi in my coop but only for my water heater and lights.

I think a better option would be to calculate sunset,sunrise and have the door open and close that way.

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 5:51 pm

That is a good point. Maybe i can write in a key press over ride to start the open/close cycle

pcmanbob
Posts: 9773
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 8:55 pm

You could set you LDR so that it's got to be totally dark or near as before the door will close.
I would experiment with the LDR out doors and see how you get on, before putting it in to operation.

Have you also though about what happens if the door closes while a chicken is sat in the door way !
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

N_Aguiar
Posts: 49
Joined: Sat Jan 20, 2018 7:25 pm

Re: Automated light controlled door, motors and limit switches

Tue Jan 23, 2018 11:54 pm

I have kind of thought about that and the door is basically a thin piece of wood that's not all that heavy. When it is built i will have to stick my arm in there and see how heavy it really is and how the whole thing would react. If it turns out to be heavy i would have to figure out some kind of ir setup such as a garage door would have.

Do you have anything in mind to address that? All input is greatly appreciated here.

Return to “Beginners”