Glenn from Sweden here. I was wondering if some kind soul could help me out here.

My partner bought a cheap chinese knock-off automatic chicken coop door that operates based on sunlight. Link to the actual chicken coop door In the morning the door opens and at the evening the door closes. It does this (from my humble understanding) with a LDR, a 5v dc motor and a small circuit board. The main problem is that the door opens way to early (about 07:00 am) with the risk that predators may snatch the chickens and there is no way to regulate the opening/closing times.

What I would like is the following: I would like the door to open at 10:00am and close at 21:00pm every day of the week using the existing 5v motor. I do have some 28BYJ-48 step motors and A3967 driver boards I bought 10+ years ago, but then I would have to start modify physical things to get it to fit. I rather not do that if it’s possible to use the original parts.

Now, coding is way over my head. Some people can draw beautiful things with paints and brushes, others can not. I can get some LED's to flash but I just can’t wrap my head around ”advanced” coding like this project. That is why I, out of desperation, tried to get Bing A.I. to write the code for me. I can compile the code without any errors, but it doesn’t work IRL. The motor does not start at the specified time, and sometimes the motor starts to spin out of the blue but doesn’t stop spinning. I have installed ”Time (1.6.1) by Michael Margolis” in the Arduino Library Manager to get the timer function to work. Is that the problem?

I must mention here that the three leads from the SN754410 originally was routed to pins 7,8,9 on the Arduino, but since it didn't work I tried switching them to 3,5,6 in case it had something to do with the analog output?

With the original electronics the door opens and closes mechanical fine, but I don’t understand how the ”brain” knows when to stop the motor so it doesn’t start to crunch the gear on the motor. There are no microswitches at the end points or anything…

I added the red and green LED's (got Bing to include them in the code) just to be able to see when the door opens/closes if dark outside).

I understand a Fritzing layout is preferable so I did my best to create one. Its my first time so please bear that in mind.

ritzing circuit layout

Chicken coop door

    // Include the Time library
#include <TimeLib.h>

// Define the pins for the motor controller
#define ENA 6
#define IN1 5
#define IN2 3

// Define the pins for the LEDs
#define GREEN_LED 10
#define RED_LED 11

// Define the times for the motor actions
#define START_TIME 1825 // 10:25 AM in 24-hour format
#define END_TIME 1925 // 21:25 PM in 24-hour format
#define DURATION 20 // 20 seconds

// Define a variable to store the current time
int currentTime;

// Define a variable to store the motor state
int motorState;

// Define the motor states
#define STOPPED 0
#define CLOCKWISE 1

void setup() {
  // Set the motor pins as outputs
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  // Set the LED pins as outputs
  pinMode(RED_LED, OUTPUT);

  // Stop the motor and turn off the LEDs initially

  // Set the time to sync with the computer's time
  setTime(17,27,10,7,10,2023); // hour,minute,second,day,month,year

void loop() {
  // Get the current time in HHMM format
  currentTime = hour() * 100 + minute();

  // Check if the current time matches the start time or the end time
  if (currentTime == START_TIME || currentTime == END_TIME) {
    // Check the motor state and switch it accordingly
    switch (motorState) {
      case STOPPED:
        // Start the motor clockwise and turn on the green LED
      case CLOCKWISE:
        // Stop the motor and start it counterclockwise after DURATION seconds and switch the LEDs accordingly
        delay(DURATION * 1000);
        // Stop the motor and start it clockwise after DURATION seconds and switch the LEDs accordingly
        delay(DURATION * 1000);

// A function to stop the motor
void stopMotor() {
  // Set the enable pin to low
  digitalWrite(ENA, LOW);

  // Set the motor state to stopped
  motorState = STOPPED;

// A function to start the motor in a given direction
void startMotor(int direction) {
  // Set the enable pin to high
  digitalWrite(ENA, HIGH);

  // Set the input pins according to the direction
  if (direction == CLOCKWISE) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    motorState = CLOCKWISE;
  else if (direction == COUNTERCLOCKWISE) {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    motorState = COUNTERCLOCKWISE;

// A function to turn on a given LED pin
void turnOnLED(int ledPin) {
  // Set the LED pin to high
  digitalWrite(ledPin, HIGH);

// A function to turn off a given LED pin
void turnOffLED(int ledPin) {
  // Set the LED pin to low
  digitalWrite(ledPin, LOW);

// A function to turn off both LEDs
void turnOffLEDs() {
  // Turn off both LED pins
  // A function that gets the time from the computer
time_t getTime() {
  // Check if there is serial data available
  if (Serial.available()) {
    // Read the serial data and convert it to a time_t value
    time_t t = Serial.parseInt();
    // Return the time value if it is valid
    if (t >= 946684800) { // Check if the value is after 1 Jan 2000
      return t;
  // Return a zero value if there is no valid time
  return 0;

  • The code in the switch makes no sense to me. It instantly starts and stops the motor with no time for the motor to do anything.
    – user10489
    Oct 8, 2023 at 13:51
  • there is no such time as 21:00 PM ... it's either 21:00 or 9 PM
    – jsotola
    Oct 8, 2023 at 15:48
  • 1
    start by writing simple code that runs the motor for 500 ms, stops the motor and quits ... expand to alternating directions
    – jsotola
    Oct 8, 2023 at 15:53
  • it may be simpler to cover the LDR and then shorting it with a relay
    – jsotola
    Oct 8, 2023 at 15:56
  • 1
    @Glenn LOL, no. What I'm getting at is the "this forum" thing. It's a Q&A site. It works a little differently from a forum. There's at least one subreddit for Arduino, discord server, IRC channel, and as you mentioned the official forum. All of them designed for something like dialogue, which can help if you're a bit lost. If you fall in with a good crowd, people just help each other out with stuff. It doesn't matter quite so much if things evolve as you go.
    – timemage
    Oct 9, 2023 at 19:15

1 Answer 1


Using generative AI is only really helpful, if you have enough knowledge to work further with the generated code. Current LLMs are not as plug and play as many believe them to be.

Timemages idea of covering the LDR with an LED in your control seems very reasonable. Then you don't need to hassle with the motors. Try taping one if your LEDs to the sensor and use a blink code to test that. You might need a brighter LED, depending on the LDR. Just make sure to use a transistor in low side switch configuration, if the needed current for the LED is greater than 20mA, which is the maximum continous current for a digital output pin (small bipolar transistors are very cheap; you will find much information online when searching for something like arduino transistor low side switch).

Then about the time keeping: The Arduino itself is not good at keeping time accurately over a longer span. Due to manufacturing differences and temperature clock drift your time will be very much off after some days. For this you need a RTC (Real Time Clock). It has a very precise internal clock, so it can keep time accurately over long timestamps. You can buy ready to use RTC modules which also include a battery backup, so that the clock doesn't loose time on powerloss. Often these modules are build with the DS3231. There are also libraries for this RTC, for example this one. Look at the provided alarm example, because that is what you want (code triggering on specific times of the day). Use that example as starting point and make the LED turn on and off at these alarms. That are only small changes to the existing example code.

  • I'd never thought to use a LED with the LDR as I don't understand how this will work. Do you mean the LED would act like the sun and therefore could be controlled precisely with on/off times? As I'm writing this I guess that is what you mean. Hey, that's pretty clever and yet so simple! As I'm new to electronics, what is the most power efficient way I can achieve this? Do I use a Arduino Nano, or is there an even easier way?
    – Glenn
    Oct 9, 2023 at 8:36
  • (I can no longer edit my last comment) So I tried timeage's solution to use an LED to simulate the sun and it worked like a charm! "HIGH" for one minute, then "LOW" for one minute and so on just to try it out. Now, for the door to remain open between 10:00 to 21:00 the LED would have to be set to "HIGH" for 9 hours. How do I do this practically? Is there a better solution than using let's say an Arduino Nano? I mean, I would have to power the Nano which draws extra power from the batteries. Is there a small chip or something that can be programmed to control the LED instead?
    – Glenn
    Oct 9, 2023 at 9:31
  • LDR = Light dependent Resistor (Eller ljus beroende resistor). A Arduino (independent on model) has a clock that will drift and needs to be adjusted. You could use a more precise clock module, a RTC (Real Time Clock) of if you have WiFi/internet and uses an ESP6266 or ESP32 then you can use NTP. And lastly something is a Arduino,.
    – MatsK
    Oct 9, 2023 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.