Timed actuator issues, help please?

Hello peeps,

I have written my first Arduino project!
The setup is a basic dual timer for opening and closing the door on my chicken coop. So they chooks can be closed in safe at night (22:00/01 and let out in the morning (07:00/01)

Works 'perfectly' (ran for a week) on my bench, without the relay linked up to the actuator but when I link it up to the actuator it seems to get stuck either open or closed. :sob:

I did have lots of serial printing when I was testing, to be sure everything was working on my bench but I took it out for running outside as it is powered from a car battery not a USB linked to a computer that can read it.

Using an Uno, DS3231, 2 Relay module and two override push to make mini buttons.

2-Relay module is used to switch between neg/pos feed for the actuator, to change direction from Open to Close.
The relay module jumper is set to VCC/VCC (not GND) which works on the bench.

In my eyes it is very VERY simple code. (Could be better, with more global vars etc but I wrote it pretty quickly)
Still setup on a breadboard.

#include <Wire.h>
#include <DS3231.h>

#define DS3231_I2C_ADDRESS 0x68
DS3231  rtc(SDA, SCL);

/*
 * GND - GND
 * VCC - 5v
 * SDA - SDA
 * SCL - SCL
 */

#define RELAY1  2
#define RELAY2  3

  byte bHour;      //current hour
  byte bMin;      //current min
  byte bSecond;   //current second
  byte bMonth;   //current Month
  byte bDate;   //current Date
  byte bDay;   //current Date
  byte bYear;   //current Year

int dOpen = 1;
int dClose = 0;

#define BUTTONOp        8  // Button
#define BUTTONCl        9  // Button


void setup()
{
  // Initialize the rtc object
  rtc.begin();
    
  readTime(&bSecond, &bMin, &bHour, &bDay, &bDate, &bMonth,  &bYear);

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  digitalWrite(RELAY1,HIGH);
  digitalWrite(RELAY2,HIGH);

  pinMode(BUTTONOp, INPUT);  digitalWrite(BUTTONOp, HIGH);
  pinMode(BUTTONCl, INPUT);  digitalWrite(BUTTONCl, HIGH);

/*  
  // The following lines can be uncommented to set the date and time
  rtc.setDOW(WEDNESDAY);     // Set Day-of-Week
  rtc.setTime(15, 23, 0);     // Set the time (24hr format)
  rtc.setDate(10, 7, 2016);   // Set the date
*/
}

void loop()
{
  readTime(&bSecond, &bMin, &bHour, &bDay, &bDate, &bMonth,  &bYear);

  if (digitalRead(BUTTONOp) == 0)
  {
      //Open Door button
      digitalWrite(RELAY1,LOW);
      digitalWrite(RELAY2,HIGH);
  }
  else if (digitalRead(BUTTONCl) == 0)
  {
      //Close Door button
      digitalWrite(RELAY1,HIGH);
      digitalWrite(RELAY2,LOW);
  }
  else if (bHour == 7 && (bMin == 0 || bMin == 1))
  {
      //Open Door timer
      digitalWrite(RELAY1,LOW);
      digitalWrite(RELAY2,HIGH);
  }
  else if (bHour == 22 && (bMin == 0 || bMin == 1))
  {
      //Close door timer
      digitalWrite(RELAY1,HIGH);
      digitalWrite(RELAY2,LOW);
  }
  else
  {
    //Stop
    digitalWrite(RELAY1,HIGH);
    digitalWrite(RELAY2,HIGH);
  }
}

void readTime(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}

Can anyone tell me if they can see anything in the code that would cause this ... and/or recommend a better/different way for me to write the code?

Looking forward to your comments. :grin:

I know it's messy but I am only breadboarding at the moment....

The Open relay is ON, the code dictates it should be all off (in the "else" of the if statement)

To recap, the buttons work, as does the timer.
Sometimes, when using the buttons, the relay stays on. Either open or closed.
And MOST times the same thing happens when the timer triggers an open or close.

Reset the arduino and it works perfectly.
Run it on the bench, from a USB, without the actuator attached to the relay, it works perfectly.

Really struggling to work out what this is... can anyone help?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Not a fritzy diagram please.
Also a link to specs/data of your actuator.

Thanks.. Tom... :slight_smile:

Looks like the chickens did the wiring :slight_smile:

Draw a simple wiring diagram with pencil and paper and post a photo of it. Comments based on photos of a wiring nest are not reliable.

...R

HA! I think the chooks may have done a BETTER job wiring :smiley:

Will do a wiring diagram now. Hold on. :slight_smile:

OK, so I can't draw and I am useless with paintshop. :frowning:

Here it is.... and no, the chickens didn't do this either, they would probably have done a better job! :confused:

Think the wiring for the relays & Actuator is right.

IN1 IN2 DIR
OFF OFF Stop
ON OFF Open
OFF ON Close

N.B. Code should never allow both to be ON at the same time.

oh and here is the Linear Actuator

Specification:
Product Name: Linear Actuator
Stroke Length(inch): 12"
Stroke Length(mm): 300
Retracted Length (mm): 420
Extended Length (mm): 720
Condition: 100% new
Input Voltage: 12V
Max Push Load: 1500N/330lbs
Max Pull Load: 1000N/264lbs
Max load: 150KG
Speed: 5.7mm/s(±0.1mm/s)
Duty cycle: 20%
Operating frequency: 20%
Material: Aluminum alloy
Color: Silver grey
Operation temperature: -26 ° C ~ +65 ° C
Protection Class:IP65
No-load current 0.8A
Max load current: 3A
Certification: CE/SGS
Warranty: Standard 1 year warranty
Maintenance: Free-maintenance

Features:

  1. Reliable performance
  2. Stable running
  3. The same characteristics of push and pull
  4. Good environment adaptability
  5. No-load current<1A
  6. Max load current=3A
  7. IP65 rated(suitable for outdoor use)
  8. Certification=CE

It has internal limit switches, and takes about 70 seconds from open to close (and vice versa)
So I went with 2 mins, just to be sure.

slimjim:
Works 'perfectly' (ran for a week) on my bench, without the relay linked up to the actuator

Just to be clear do you mean that the relay was connected to the Arduino but not to the actuator?

Is the 12v battery powering the Arduino?
If so, is the battery big enough to supply the actuator without the voltage dropping?

Your diagram about the relay contacts is confusing. Are you sure there is no short circuit?

...R

Yes, power is coming from a 12v leisure van battery for both the actuator and the Arduino.
I did some brief calculations and it would last about two weeks, without me linking it up to the solar trickle charger, which I will be doing when it works.

The Relay is connected to the Arduino on the control side, with pins from the Digital 2 & 3 pins to turn it on and off, with VCC and GND also from the Arduino, all on the "pin" side.

The Relay is connected to the Actuator and the battery on the "power" side, using screws terminals.
When one relay is on, the actuator gets power on one cable and earth to the other. Then when the other relay is one, the actuator gets power where it DID have earth and earth where is DID have power, to switch the polarity and change the direction of the actuator.
If both are off, the actuator gets negative down both cables, so it does nothing.
If both are on (this should never happen with the code) then both cables get power, so it does nothing.

I have never drawn anything like this before, so not sure how it should look.
Added some 'arcs' to show where cables are not connected and altered the colours of some of the cables to make it easier to understand.

Any better? I'd really like input on this. :slight_smile:
Driving me mad!!!

Hi,
You do not seem to have any bypassing capacitors or suppression of any sort on the actuator motor.

Put 0.1uF capacitor across the input of the actuator and also across the 5V to gnd on the protoboard.
A 10uF cap across the 5V and gnd as well.

It could be switching noise from the actuator as you drive then short the input to the motor.

Thanks.. Tom.... :slight_smile:

It is highly likely this is an EMC problem caused by voltage spikes from the motor coupling back into the I2C bus.

Unfortunately extra clock pulses (spikes) cause havoc on an I2C bus and the stock Arduino library causes the Arduino to go into an infinite loop (locks up) waiting for an Ack. There are some other I2C libraries that do have a time-out.

Power the RTC direct from the Arduino with short wires (<5cm). Keep the I2C lines short too. Keep power feeds all together and away from the I2C lines and avoid loops. Ideally twist power feeds with the corresponding return wire (twisted pairs cancel the magnetic fields).

When you have got it working, add a watchdog timer so the Arduino reboots and re-initialises the I2C bus if it locks up. A watchdog is not a cure for the frequent problems you are getting at the moment as the relays will be reset and your chooks may need to spend the night out :frowning: or stay in bed all day :).

If you value your chooks and don't check every operation of the door yourself then I would make a similar completely separately powered system (no good if the door-driving battery is flat you see) that checks 10 minutes later if the door is in the right state and sounds an alarm if it is not!

In a good system the watchdog should never trigger, but I have had a nearby lightning strike cause a running Arduino to reboot on the watchdog! It made me jump out of my skin, so I guess my brain rebooted too. :slight_smile:

Just noticed your relay module is getting power from the RTC breadboard. That is not helping as the relay generates a kickback voltage that puts a transient spike on the +5V line and that passes right through the wiring to the RTC. Ideally use a seperate 5V supply for the relay as indicated here, or add say 470 uF capacitor at the relay module on the 5V feed (note these capacitors are typically electrolytic and polarized, wire them the wrong way around and they go bang).

Remove the JD-VCC jumper and run a wire from separate 5V + to JD-VCC, run a wire from separate 5v - to the GND terminal near JD-VCC, remove the GND wire from Arduino, like this:

relaybrd.JPG

NOTE: Power to JD-VCC must be 5V, NOT 12V.

Some SUPERB info there guys... thank you so so much!!!! 8)

Kind of makes sense to me now.
I think I will do two things to start with.

1. Shorten all the wires.
I only made them that long so it was easier for me to see what was going on, with it being my first project. Had no idea they could/would effect the way it works.
So now I know how it works, I can shorten things up.

2. Sort a separate 5v supply for the relay, to keep the spikes going back to the Arduino.
I don't understand exactly how all the spikes and kickbacks work but I now at least know that it happens and needs to be avoided.

If that doesn't solve the issue, I will start to look into capacitors to help, as suggested.
(Will probably look into this as well anyway!)

A final question....

This "separate" power supply. I will use a 12v->5v converter, rather than using the 5v supplied by the Arduino. but it will be linked up to the same battery for source.
I DO have the ability to add another battery for a truly different supply of power. Adds more complexity to the charging process but it is something I COULD do. Is that going too far?

To clarify, here is the new setup.

Which also includes me shortening all the wires and keeping the power/earth and data cables as separate as possible.
Including making sure the power for the relay & actuator doesn't go anywhere near the Adruino.

(Ordered a cheap 12v->5v converter for the relay power, till it arrives I am actually using a spare Arduino as a temp measure!)

You have your relay contacts drawn like this

RelayA.png

They should be like this (with apologies for the crude art work)

RelayB.png

...R

Cheers Robin...

Like I said, I have never drawn anything like this before, so I just went with what was on the Relay.
Seems like you understood it though... :wink:
I have altered them to match your diagram. :sunglasses:

What do you think of my new diagram and do you think I need to have a DIFFERENT battery for the relay control or will a different 12v->5v converter to the job?

Thanks.

The 12v to 5v converter should be fine.

I can't see, from your diagrams, why you are having a problem.

I presume the actuator does work properly when you connect it to a battery?

Please post a link to the datasheet for the relay - I don't see it among your earlier Posts.

...R

I posted a link to the relay, bought from eBay here.
Can't see any datasheets for it.

Yes, the actuator works perfectly, when linked to the battery.
It even works perfectly from the buttons I added for the manual over ride. Both open and close (MOST of the time)
But once it does it automatically itself (or infrequently when you use the buttons) it "locks".
Hit reset and it works perfectly again.

I even added an auto reset at 15 and 45 past each hour, in case it was some kind of memory leak.
(This was before I heard about the 'watchdog' which seems like it would solve my problem but not really what I want to do)

When it is on the bench NOT linked up to the actuator, it worked for a full week with no issues.

Really appreciate your input on this.
Was convinced it was the code screwing things up, which would have been VERY embarrassing as I am a programmer by trade! :-[

Don't understand the whole Electronic field issues and/or kickbacks/feedback but I am glad there is something screwing it up and I can do something about it. AND it's not the code :smiley:

slimjim:
It even works perfectly from the buttons I added for the manual over ride. Both open and close (MOST of the time)

Are the relays used in this case?

But once it does it automatically itself (or infrequently when you use the buttons) it "locks".

I don't understand what you mean by "locks"
OR
what you mean by "automatically"

It may be useful to write the simplest possible Arduino program that does nothing but extend and retract the actuator once a minute (or whatever seems reasonable to you) and see if that works. Then piece by piece add additional features to the program ensuring that everything works at each stge before moving on.

...R

Robin2:
Are the relays used in this case?

Yes, relays are always used.

Robin2:
I don't understand what you mean by "locks"
OR
what you mean by "automatically"

I think I misspoke with the "locks"
When I mean automatically, I mean with the timer.

Robin2:
It may be useful to write the simplest possible Arduino program that does nothing but extend and retract the actuator once a minute (or whatever seems reasonable to you) and see if that works. Then piece by piece add additional features to the program ensuring that everything works at each stge before moving on.

...R

Yup, I will do that... IF this doesn't work.... which it does!

So far, it has closed last night, opened this morning... and the relay has reset from this morning and looking good.
I am really hoping that the alternative power supply has solved the problem. :slight_smile:

If it works tonight and the rest of the weekend, I think it may be problem solved!!! 8)

REALLY appreciate all the info and help guys.