Help with clock project

Hi
I'm a newbie to Arduino, and have taken a projekt with a lot of hardware and programming issues. I'm trying to make an old clock a new heart. It's a big external clock with an internal gear and motor system. My plan is to use Arduino as a base, with RTC addon and a encoder for correct time. I have set the encoder up on one of the axles of the machinery. Rotation is 2minutes on 360 degree turn. This is a simple clock function with only minute hand moving and hour following. So every minute give a pulse to a relay, and move minute hand to get accurate time with the encoder. I apprechiate all the help I can get.

You said you can control the clock with a relay, which suggests that the clock is driven by an electric motor. Clocks driven by AC synchronous motors are very accurate, so if it runs on an AC motor, then why isn't that good enough to keep the clock accurate without going to all this extra trouble?

Is it a church type clock ?

You probably need to give a better description of the clock mechanism and what parts you are replacing with your new development and give links to the parts you plan to or have already purchased (e.g. encoder)

Setting an arduino plus RTC to deliver accurate time pulses to a new or existing relay or motor control should no problem. Reading an encoder should also be straightforward.

Is the clock drive motor a stepper motor? At one revolution every 2 minutes, that would be half the speed of a clock's second hand

It's a very old Church type clock. And it is driven by a 12VDC motor that is very inaccurate. It has its own external 12VDC supply, thats why the relay. Thought of installing a stepper motor, but it is not an easy fix to replace the motor. It was previously two limit switches that controlled the clock on the axle that I have installed the encoder. It was a simple pulse from a timer that started the motor. But after a little while the clock has to be corrected because of delay on motor and limit switches. And also powerloss will be trouble. My plan is for the encoder to always know what time it should be. With daylight savings and all.

How does it handle daylight saving time ? I guess by "quickly" advancing 1 or 11 hours.

Once the arduino application is in sync with the clock, then it can correct any deviations between the calculated time and the measured shaft position. You want this to be as smooth as possible to keep the minute hand rotating at a consistent rate.
Correcting for daylight saving time simply means performing 30 or 330 additional rotations of the shaft if the assumption above is correct

I suspected the clock had a DC motor when I asked if it had a AC motor, but I had to eliminate that possibility first to get more information. I'm sure you know the problem with a DC motor is overshoot. That's because a DC motor will coast to a stop when power is removed, unless some type of brake mechanism is added. The coast-down can be compensated for by shortening the DC power pulse, but that leads to inaccuracies over time, because the coast-down varies as the mechanism ages and even changes in the ambient temperature will affect coast-down. I know you don't want to hear this, but you really need to remove the DC motor and find some way to attach a stepper motor to the motor shaft. A stepper motor has high torque and if you give it the right number of steps and/or micro-steps, then it will move the clock hands exactly one minute on each cycle from the Arduino with no overshoot or undershoot. Plus, this will make the Arduino sketch very simple. With a stepper motor, you won't need the encoder, because the clock movements will be very precise, so this will also simplify the Arduino code. I'd also ditch the real time clock and replace it with two simple push buttons, one button to generate enough steps to advance the clock by one hour and a second button to reverse direction and back up one hour. How much trouble is it to take one second to press a button once every six months when daylight savings time rolls around. And, eliminating the RTC will further simplify the Arduino code. Finally, if you use a smart stepper motor controller, then the Arduino code will be so simple you can do it yourself, because all you'll need to do is send the controller the number of steps/micro-steps, then send it a command to step once every minute. The hard-core software guys on here may not like these suggestions because it eliminates 95 percent of the code, but this will make the Arduino sketch so simple you can do it yourself with just a little help from folks on here.

I disagree about dispensing with the Real Time Clock. (1) Most Arduinos have a cheap resonator instead of a crystal which do not allow good timekeeping. Even the ones with a crystal are not very good. (2) An Arduino will need to have the time reset after a power failure whereas an RTC will have a backup battery. A DS3231 for example even has a crystal temperature and age compensation mechanism.

Also with automatic daylight saving time compensation, whereas a manual solution would be easy to implement, someone has to be there at maybe 02:00 or 03:00 (depending on the timezone) to operate it.

quadradic. I agree with you on the DC vs Stepper motor. That was my inital plan to replace. But when i disassembled the mecanism of the clock, and removed the DC motor, there is an issue. My hope was that it was a pinion on the motorshaft. But the shaft itself is the pinion. That is the main problem, and it would simplify everything a lot to replace. But not very easy to fabricate. As you say about the DC motor, it overshoots, because of no brakesystem. And as you say, it's not that big of a deal to make a manual push button for daylight savings.

Janexx, I sympathize with your mechanical predicament now that you've explained it. Given the situation, you'll probably need to solve the clock problem the way you first described, but I do have a few practical suggestions: First, add a reverse diode "snubber" across the motor (if one is not already there) to minimize "overshoot" by shunting the reverse motor current when power to the motor turns off. The coasting motor becomes a generator, producing counter EMF, but shunting the generated current will stop the motor faster and also reduce arcing of the relay contacts. If the motor is large, then you may need a resistor in series with the diode to limit the counter current. If you don't know how to calculate the diode and resistor values, then you can buy a commercial DC motor snubber with specifications that match you motor's current and voltage ratings. They aren't expensive. Also, if you're currently using a mechanical relay to switch the motor current, then I'd suggest replacing it with a solid state relay, because mechanical relay contacts will degrade over time due to oxidation and arcing. But, be sure to buy a solid state relay that's made specifically to control DC current, because most solid state relays are made to control AC current. And, be sure the solid state relay's current rating exceeds the maximum current rating of your DC motor. Finally, you can turn a solid state relay on and off directly with the 5 volt digital output of an Arduino; whereas, you'll need a relay driver circuit between the Arduino and the existing relay, assuming the existing relay has a traditional 12 volt coil. So, why not keep it simple and just replace the old mechanical relay with a superior solid state DC relay module.

That is good advice! Thanks! There is no snubber on it at the moment. And I only have a 5v triggered standard relay at the moment. But as you say a SSR would be a lot better. Plenty on Ebay. The motor is 12VDC, 2000rpm, 6,5w, 1,55A. It's a really old motor. Not possible to get a new one. So if it fails, then I will dismantle it and Frankenstein a new stepper motor.

The motor specs in your last post were enlightening. Does the DC motor have built-in gear reduction that reduces the output shaft RPM, or is the motor shaft actually turning the clock shaft at 2000 RPM? The reason why I'm asking is-- if the motor is actually spinning the clock shaft at 2000 RPM, then the gearing in the clock is designed for a higher input RPM than a common stepper motor is designed for. So, if the clock requires a motor with a 2000 RPM shaft speed and the existing DC motor dies (forcing you to attach a different motor), then forget about my previous advice to replace it with a stepper motor. You'll have to replace it with another DC motor, because I imagined that the clock was driven by a slower, geared down motor with an internal gearbox. You haven't said much about what provides the timing signal that currently closes the motor's power relay, but in your last post you said the relay only needs 5 volts to close. That's surprising, because generally speaking a mechanical relay that will drive a motor of that size has a 12 volt coil. If it's really a "standard" relay, then it would normally have a 12 volt coil, unless it happens to be more than just a simple relay. By any chance, is this is a timed delay relay, the type that is triggered and then runs for a specific amount of time and then shuts off automatically? In that case, the relay could contain active components and be triggered by only 5 volts. The more I learn about your clock, the more I realize how little I understand how it works.

Sorry for the confusion. The 5v relay is some of the parts for the Arduino project that I have bought, and as you said, should be replaced with a solid state relay. Originally the clock had a 12VDC relay system controlling it. But milliseconds of delay on limit switches, and relay made it inaccurate in a short period of time. The motor is direct 2000rpm to the gearbox clock system. But I think there might be a possibility to connect a high torque stepper to the secondary control axle. But would like to use it as intended with the original motor. There has been several attempts to fix the clock by previous people, but no luck. But my hope was to make this analogue clock a little more digital, to compensate for the delays in the clockwork.

Thanks for the clarification about the relay voltage. My hat is off to you for undertaking such an ambitious project. I honestly believed I could help you when I thought a simple stepper motor could be used to drive the clock, but I understand now that a stepper motor it is not the answer for several reasons. Your proposed solution with an encoder and a real-time clock is brilliant, but it's beyond my ability, so this will be my last comment. I wish you the best of luck and I hope there is someone on here with the programming skill to help you write the code.

Interesting project. It does not sound that complex to me, at least not at this point!

Can you post some pictures? The PSU, motor, encoder etc. in particular.

Also a link to details of the encoder. Did you say you get a signal from this encoder for every 2 minutes of movement of the minute hand? If so, the code will have to "guess" the length of the pulse for every other minute. This won't lead to long-term inaccuracy.

Solid state relays are for AC. They won't work with DC. All you need to control that motor is a MOSFET. An N-channel one with a logic-level gate, to be precise. For example irl540 or stp16nf06l. Put a current limiting resistor between its gate and an Arduino output (100R to 510R would be fine) and a pull-down (e.g. 10K) on the Arduino pin to prevent the motor running accidentally while the Arduino is starting up. You will need that snubber on the motor. And a cap to suppress interference, if it doesn't already have one.

There are many ways to get time time. An RTC such as the ds3231 suggested above is simple and easy. Or you can pick up the time wirelessly from the internet or long-wave radio. Is there WiFi reception where the clock mechanism is?

But the biggest problem I can foresee is how to recover from power loss. The RTC module will have a backup battery, so that will be fine. The problem is the Arduino won't know what time the clock hands are showing. The encoder, I am assuming, will tell you when the hands have moved two minutes, but it won't tell you the absolute position of the hands.

One approach to solving this would be to use non-volatile memory of some kind to record the known position of the hands. This would need to be updated every minute when the hands are moved. The obvious answer is to use the Arduino's built-in EEPROM memory. But that only has a lifetime of 100,000 write cycles. Updating it once per minute means it could wear out in around 10 weeks. An external EEPROM chip might give 10 times that life, but that's still only a couple of years.

Some RTC modules have some battery-backed RAM, but I don't think the ds3231 has any to spare. ds1307 does, but it is nowhere near as accurate and stable as ds3231.

EDIT: OK, here's an idea. The ds3231 has built-in alarm times. These alarm settings won't be lost during a power outage, they are protected by the battery. The Arduino could use one of these alarms to trigger it to move the clock each minute. Then it would update the alarm time for the next minute's update. This would have the side-effect of providing a means of recording the known position of the hands. If there is a power loss, the Arduino can read the last recorded alarm time and compare this to the current time. Then it can move the clock hands to the correct time.

I'm also curious about this project. I had a quick look in Google but could not find anything which matches the description given here. Most of the church type clocks with an electric mechanism, usually retrofitted, simply manage the winding mechanism to haul the weights up. These have no impact on the time keeping mechanism.

Storing the state of the clock to maintain synchronisation even after a power failure could also be done using an FRAM which is the equivalent of an EEPROM, but with a long lifetime.

Janexx:
So every minute give a pulse to a relay, and move minute hand to get accurate time with the encoder. I apprechiate all the help I can get.

Well, this isn't very difficult. You can drive it off the minutes output of the rtc. You use i2c to get one byte from the rtc - the byte holding the minutes value.

const byte rtc_address = /* the i2c adress of the clock */ ;
const int minutes_location = /* the offset of the minutes buye in the clock data block */ ;
const byte relayPin = /* where your delay is */ ;

const unsigned long relay_pulse_ms = 100; // tenth of a second
const unsigned long sample_rate_ms = 250; // check the clock 4 times a second


byte minute;



void setup() {
  pinMode(relay_pin, OUTPUT);
}

void loop() {
  byte previousMinute = minute;

  Wire.begin( rtc_address);
  Wire.requestFrom(minutes_location, 1);
  while(!Wire.available()) 
    /* do nothing */ ;
  minute = wire.read();
  if(minute != previousMinute) {
    digitalWrite(relayPin, HIGH);
    delay(relay_pulse_ms); // tenth of a second
    digitalWrite(relayPin, LOW);
  }

  delay(sample_rate_ms); 
}

You can do things like putting the arduino to sleep, or using the square-wave output of the clock to drive an interrupt, but this will work ok. Presumably you set the time by hand, so there's no need for any of that - just move the clock hands to where they belong, and the arduino will click that relay once a minute.

PaulRB was incorrect when he said: "Solid state relays are for AC. They won't work with DC." While most solid state relays are made to control AC, a DC version has been available for many decades. I was using them more than forty years ago. You can find solid state DC relays with a wide variety of current and voltage specifications if you google "solid state DC relay". The reason why I suggested a solid state DC relay to Janexx (instead of suggesting a MOSFET), was because I don't know how good Janexx is at soldering individual components, so I decided to play it safe and suggest a solid state DC relay, because it's built into a rugged block with screw terminals, so no soldering is required. Also, the solid state DC relay has built-in optical isolation, making it less likely for a person with a limited knowledge of component electronics to short something out, or run into ground loop problems. If Janexx wanted to use a MOSFET and have the total DC isolation that's already built into a solid state relay, then it would be necessary to add an opto-isolator chip and that would require Janexx to build the circuit on a board. Solid state DC relays are readily available and relatively inexpensive, so (in my opinion), the extra cost over a simple MOSFET driver is offset by the convenience in many cases.

Next, regarding encoders: There are two types of encoders, the absolute encoder and the incremental encoder. An absolute encoder reports the shaft angle within a 360 degree range and it does not lose its position after a power-down, so it can be used to read the last position of the hour hand instantly on power-up without requiring a home cycle, a shaft rotation, or a battery back-up. Based on statements made by Janexx, it appears that an incremental encoder is currently being used, but the ideal encoder for this clock project would be an absolute encoder turned by the shaft of the hour hand. This would enable the microcontroller to know the time showing on the clock face to an accuracy greater than one minute and make it possible to build a self-correcting clock that would always be accurate to within a minute of the real time clock module. All the microcontroller would need to do is read the time indicated on the clock face (from the encoder), compare it with the RTC, and adjust the time shown on the clock until it agreed with the RTC. This would also solve the daylight savings time problem, assuming Janexx has an RTC that automatically corrects for DST. If it turns out to be possible to use an absolute encoder turned by the hour hand shaft, then the discussion about solid state DC relays verses MOSFETS is moot, because it will be better to use an H-BRIDGE, so the microcontroller can run the drive motor both forward and backward to correct for the clock being either fast or slow. Unfortunately, based on previous comments made by Janexx (about difficulties associated with changing the drive motor), connecting an absolute encoder to the hour hand shaft is probably too difficult and connecting an absolute encoder anywhere else would defeat the purpose.

While most solid state relays are made to control AC, a DC version has been available for many decades.

My apologies, I've only ever seen the AC kind. If janexx can find a suitable one, it would save a little soldering. But soldering up a mosfet and a couple of resistors and some screw terminals is no big deal if you can't find one.

I've not heard of an rtc that has automatic daylight savings correction either. But it's not that hard to get the Arduino to perform this logic, I have suitable code that I have used before, and I'm sure I'm not the only one who does!

Just for the record, the Intersil ISL12020M RTC has built-in automatic daylight savings time correction that's accurate until the year 2099. It's available from Mouser in single unit quantities for $6.31, but it may be cheaper elsewhere. Just to be clear, I'm not saying this is a better solution than using microcontroller code for DST correction. Also, I haven't searched for this chip on a module, so it may be necessary to build a board to hold the back-up battery and a few other components, which could be an issue if Janexx doesn't like to solder. All I'm saying is this type of RTC is available and it's one of several ways to solve the DST problem.

Well, feels like we have the design sewn up. Pity the OP has not made a reappearance.