Chicken Coop door hardware not working

Hi There,

I am building a automatic chicken coop door. I am basically just following instructions.

Its not working so I need your guidance.

I have checked circuits and hardware and all seems OK.

I have substituted the reed switches for 125V-250V Micro Roller Lever Arm Limit Switch

  • Is this workable and if yes how should the switch be wired - I have it positive to C and negative to NC ?

I have a 24 volt battery powering the Dual H Bridge Stepper motor L298N and the motor to be driven is 12.0V 45rpm 15.5mm Gear Motor.

All advice is appreciated.

Thanks
Tony

I have it positive to C and negative to NC ?

The video shows the switches wired (as far as I can tell) C to Vcc, NO to digital input, 470 ohm resistor from digital input to ground. I assume NO because reed switches are usually NO.

All advice is appreciated.

What other questions do you have?

Thanks for that just to clarify - Positive to NO and negative to NC ?

Positive to NO and negative to NC ?

Sorry, but that means nothing to me. I don't know what, precisely, that you mean by positive and negative. The way that I see the wiring in the video is +5V to the switch common (C) terminal, normally open (NO) terminal of switch to a digital input, and a 470 ohm resistor from the digital input (and NO terminal) to ground (pulldown resistor).

It is not a good idea to use a 12V motor with a 24V battery.

OK I have the wiring of the switches done but still no go. Any other suggestions ?

The reason I am using a 24 Volt power source is that my understanding is the at the Dual H Bridge Stepper motor L298N. If I were to use a 12 volt power source it would not be sufficient to drive H Bridge and the 12 volt motor.?

Am I correct?

Thanks
Tony

Would it help if i inserted some Serial print statements into the code?
If yes where do you suggest I insert them ?

Sorry for these basic questions but I am brand new to arduino.

Any suggestions ?

The reason I am using a 24 Volt power source is that my understanding is the at the Dual H Bridge Stepper motor L298N. If I were to use a 12 volt power source it would not be sufficient to drive H Bridge and the 12 volt motor.?

The data sheet for the L298 shows a voltage drop of around 2V (4V max) at 2 Amps. That leaves 20 - 22 volts across the motor. Not healthy for the motor, I would think. Can you connect the motor directly to a 12V source and make sure it still runs?

OK I have the wiring of the switches done but still no go. Any other suggestions ?

What does "no go" mean?

To troubleshoot a problem like this we isolate the parts and test each one. You have tested the motor so now test the Arduino. Disconnect all the wiring. Go into the examples in the IDE. Load the blink example. Does the onboard LED blink?
If the Arduino is OK, we test one switch. Connect the switch Common to Vcc (+5), NO to pin 2 and the resistor from pin 2 to ground. Load and run the following code.

const byte switchPin = 2;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(digitalRead(switchPin));
  delay(500);
}

Open serial monitor, confirm 9600 baud. There should be zeros running down the screen. Push the switch (close). The zeros should change to ones and, when released, back to zero if the switch is working properly. Repeat for the other switch but connect to pin 4 and change to switchPin = 4; in the sketch.

Post results and we can go from there.

To test the Light Dependent Resistor (LDR) connect one leg to Vcc (5V) and the other leg to A0. Put a 10K resistor between A0 and ground. Load and run this code.

const byte ldrPin = A0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(analogRead(ldrPin));
  delay(500);
}

Open the serial monitor and observe the values. Shine a light on the LDR. Do the values increase? Block the light. Do the values decrease? What are the maximum and minimum values?

Hi There,

Still not working -
I did the above and tested the arduino - motor - pins 2 & 4 and the LDR
and all was working.
I then rewired the project.

I have uploaded a diagram of the wring of the dual H stepper motor.

Please have a look at the diagram and see if the wiring is correct.

Thanks, if the wiring is correct not sure what the next steps are to be to get this working?

Cheers
Tony

I did the above and tested the arduino - motor - pins 2 & 4 and the LDR
and all was working.

OK, that is good. The simple stuff is done.

I found this Instructable that uses a board that looks like yours. Try going through it to test the motor with the driver by themselves (no switches or LDR to effect the test).

What exactly is your 12V supply? What is the rated output current of the supply? Can you tell me the stall current of your motor? If you have a DMM (Digital Multi Meter), measure the winding resistance of the motor. Do it several times rotating the motor a bit before each measurement. Record the lowest reading. The stall current will be motor supply voltage divided by the resistance reading (Istall = Vm/Rm).

Also Please post the code that you tried last. Have you read the "how to use the forum" stickies to see how to format and post code?

Hi There,

Please remember I am a beginner so a lot of the above is beyond me.

So what I can do is list all the components I used and the code (see below).

Then if you would be so kind as to look it over and see if there may is an issue
with the configuration and make any possible suggestion to get it working.

At present I have a chicken coop ready and waiting!

Components -

Arduino Uno - Clone

Dual H Bridge Stepper Motor Drive Controller Board Module For Arduino L298N - http://www.ebay.com.au/itm/252465630958?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

DC 12.0V 45rpm 15.5mm Gear Motor Chihaimotor Silver - http://www.ebay.com.au/itm/132057391891?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

125V-250V Micro Roller Lever Arm Limit Switch - http://www.ebay.com.au/itm/222270162986?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

Power source for testing

Thanks
Tony

//Locking, Automatic Chicken Coop Door 
//By Seth Johnson  Land To House llc 2016

//This section is for initializing things:

// Initialize the motor values to the pins 8, 9, 10. 
//Pins 8 and 9 send high or low to the motor controller.
//pin 10 enables motor one on the motor controller. 
int enA = 10;
int in1 = 9;
int in2 = 8;


//Initialize "lightSensor" as the value from pin A0 and read in the value. This is the photoresistor.
int lightSensor = analogRead(A0);
//The lightVal will hold the value of lightsensor in this variable
int lightVal = 0;

//These are the pins for the reed switches
// reed1Pin is the lower switch on the door. This is digital pin 2
int reed1Pin = 2;
//reed2Pin is the top switch on the door. This is digital pin 4
int reed2Pin = 4;
//These are the variables that hold the state of the reed switches
int switchState1 = 0;
int switchState2 = 0;

//This only runs once.
void setup()
{
  // set the motor control pins as outputs. This means pins 8, 9, 10 are outputs to the l298n motor controller.
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
//this is important to make sure that the door starts up when the program gains power.
//if switchState2 is not high then go to the while statement
if (switchState2 != HIGH)
{
// this function will run the motor down as long as switch 1 has not been triggered
  while (switchState2 != HIGH) 
{
  // turn on motor and set speed to 255 
  analogWrite(enA, 255);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  //read the state of the switch again to make sure that it has not been triggered
  switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);   
}
  // once switchstate2 has been triggered turn off the motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
}

//this runs over and over
void loop() 
{
  //read the light sensor and place it in lightval
  lightVal = analogRead(lightSensor);
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
  //read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
  //the lightSensor is read and placed into lightVal. if its less than 200 and switchState2 is 
  //equal to high then go to the motor down code. But if the light is greater than 200 and the switchstate1
  //is equal to high then call motor up code
 if (switchState2 = HIGH && lightVal < 200) 
{
 delay(2000);
    motordown();
} 
 else if (switchState1 = HIGH && lightVal > 200)
{
  delay(2000);
   motorup();
}
}

void motordown()
{
  //Read the state of switch 1 (the Bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);

  //If switchState2 is high and the light is dark then continue
  if (switchState2 = HIGH && lightVal < 200)
 //wait 2 seconds
 delay(2000);
   //read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
  
  while (switchState1 != HIGH) {
  // turn on motor and set speed to 255 
  analogWrite(enA, 255);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW); 
 //read the state of switch 2 (the top one) and place it in switchState2
  switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);  
  }
  //wait 1 second before turning off the motor to let the locks engage at the bottom
  delay(1000);
  // now turn off motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

void motorup()
{
  //read the state of switch 1 (the bottom one) and place it in switchState2
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);

  //if switchState1 is high and the light is bright then continue
  if (switchState1 = HIGH && lightVal > 200)
 {

    //read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
  //delay 2 seconds
  delay(2000);
  //while switchState2 is not high turn on the motor up
  while (switchState2 != HIGH) 
 {
  // this function will run the motor as long as switch 2 has not been triggered
  // turn on motor and set speed to 255 
  analogWrite(enA, 255);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  //read the state of switch 1 (the bottom one) and place it in switchState2
  switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);   
  }
  // now turn off motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
}

bondibiz:
Thanks for that just to clarify - Positive to NO and negative to NC ?

NO is for Normally Open

NC is for Normally Closed

If I read correctly, positive (+V) goes to C and you either hook ground(negative) to NO or NC.

NO will read high when the switch is open and NC will read HIGH when the switch is closed.
That way you can hook up either one to suit whatever logic you need.

Hi GoForSmoke,

Thats how it is wired at present for both switches.

Anyone else see a problem with the hardware used and the code?

Thanks

The wiring diagram shown in reply #11 is incomplete. It does not show a ground connection to the Arduino or the actual connections to the input pins.

You need pullup or pulldown resistors for the switches to work, depending on the rest of the wiring.

You need to be able to demonstrate motor control with the Arduino.

The reason you are having so much trouble with this project is that you skipped all the introductory steps, like learning to write a program to read a switch, control a motor, read an LDR, etc.

I recommend that you start with the simple examples that come with the Arduino programming environment and learn how to do those things.

Hi jremington,

Just to let you know, all I have done is copy an existing project and I am trying to ascertain why it's not working. Have done a far bit of seeing if individual components work.

You can read through the above to see what has been done

I have uploaded the hardware and code I have used.

Would like someone to maybe spot why this configuration is not work.

Regards
Tony

Hi There,

Can some one please recommend other Arduino Forums where I can get help.

Thanks
Tony

void loop()
{
  lightVal = analogRead(lightSensor);
  bottomSwitchState = digitalRead(bottomReedPin);
  topSwitchState = digitalRead(topReedPin);

  if (topSwitchState = HIGH && lightVal < 200)  // door is all the way open and getting dark
  {
    delay(2000);
    motordown();
  }
  else if (bottomSwitchState = HIGH && lightVal > 200)  // door is all the way closed and getting light
  {
    delay(2000);
    motorup();
  }  
// needs an else to handle other possible cases
}

This is the loop() function. Notice that for anything to happen the door must start all the way open or (else if) all the way closed. There is no code to handle the case where the door is between the switches. The door should be closed (from setup) but if its not, nothing will happen. Does the motor run at setup()? What happens when you run the code?

Maybe add:

else
{
  motorup()  // or motordown()
}