Output (Relais) oscillates when a certain value (read from ADC) is exceeded

Hi guys,

I have a simple program which monitors an analog voltage (through voltage divider) and turns off a relais, when a certain range (11V - 15V) is undershoot or overshoot.

The strange thing is that if overshoot the upper limit by small bit the relais starts oscillating. If the voltage is well above the limit (16V), it turns off, as expected.

On the lower limit, everything works as expected. The relais turns off when the voltage drops below 11V.

Here is the code: http://pastebin.com/FKDGp1Ag

Here is a short video demonstrating the problem: - YouTube

Anyone has an idea?

Any help appreciated!

Thanks,
Tobias

Hi Tobias

Any electrical noise will cause the reading from the ADC to vary up and down by a few counts. When you are close to the threshold value, this will cause the relay to oscillate.

You can add hysteresis into your code.

Take the example of testing against the nominal 15V threshold. If the relay is on, use a threshold of 15.1V to turn the relay off. If the relay is off, use a threshold of 14.9V to turn the relay on. You would use a similar pair of thresholds for the lower limit, say 10.9V and 11.1V.

This does mean that you need a variable to keep track of whether the relay is already on or off. Depending on this, you can compare with the appropriate thresholds.

Regards

Ray

Welcome to the forum.
Thank you so much for the whole sketch and even a video :stuck_out_tongue:

There is some power-current-voltage-grounding issue that backfires to the Arduino.
Perhaps the relays need current from the 5V, which lowers the voltage for the Arduino, causing the Arduino to turn the relay off.
An easy fix is to use the average of a few samples.
But to tell what it is, we have to know more.

You have an Arduino Uno. How is it powered.
What kind of shield is on top of the Uno ?
What kind of relay board is it ? Do the relays need 5V or 12V, and where is that 5V coming from ?

I did an analysis on the video. The clicks of the relays are 60ms spaced, and a very small click in between. I’m guessing that turning the relay ‘on’ makes a click sound, and turning them off is almost silent. That matches the 30ms in your sketch.

Can you make a sketch that calculates the voltage at the Arduino pin (using float), and after that calculate the measured voltage.
Your test would look like this: if (voltage < 11.0 || voltage > 15.0)
You will appreciate it later when you make a sketch with the real units.

Here are the clicks, 60ms in between, but if you look better it might be 30ms in between.

clickrelay.png

Thanks for your suggestions! Indeed, it was a power problem. I powered the Arduino Uno through USB. Despite that the USB ports of my Macbook and my USB Hub were 2000mA, it appears that the power surge, while enabling the relais, caused a small drop in voltage, messing with the ADC.

For the sake of completeness:
Relais are 5V (a chinese PCB with 4 relais, incl. driver transistor & LED)

Powering the Arduino through it’s voltage plug solved the problem. Now the system behaves as expected.

Thanks again for your help!

Nice.
Nevertheless, adding hysteresis as Hackscribble wrote and do some averaging are still good ideas.

Thanks Peter_n & Hackscribble for your help. I have now also included hysteresis and averaging. Now it even works with just the USB connection powering the Arduino + Relais.

Very cool 8)