Rounding down measured value in Arduino

  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Arduino Value
Click For Summary

Discussion Overview

The discussion revolves around controlling a solid state relay using an Arduino based on fluctuating AC voltage readings. Participants explore methods to handle voltage variations and prevent rapid switching of the relay, which could potentially damage connected appliances.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant describes their project involving an Arduino that reads AC voltage and controls a relay based on specific voltage thresholds (>=205V or <=230V).
  • Another participant suggests using the floor() function from the math.h library to round down the voltage readings, questioning its effectiveness.
  • Concerns are raised about the effectiveness of rounding in preventing relay switching when voltage fluctuates around threshold values, suggesting that rounding may not resolve the underlying issue.
  • Hysteresis is proposed as a potential solution, where the relay remains on until the voltage drops below a certain lower threshold, rather than switching off immediately.
  • A timer is also suggested to prevent rapid switching after a relay activation, although this could lead to appliances being left on during low voltage conditions.
  • One participant reflects on the complexity of the problem, noting that voltage fluctuations at the limits (e.g., 204V to 205V) cause the most issues, and discusses potential circuit modifications or additional components like a Schmitt trigger to address this.
  • A later post indicates that the original problem has been resolved, with a promise to share the solution in a separate thread.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of rounding and the necessity of hysteresis or timers, indicating that the discussion remains unresolved on the best approach to handle voltage fluctuations.

Contextual Notes

The discussion highlights limitations in the current circuit design and the need for additional components or coding strategies to manage voltage fluctuations effectively. Specific assumptions about voltage behavior and the impact of timing on relay control are also noted but not fully resolved.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,186
Reaction score
2,694
I am making a circuit with arduino that will read AC voltage supplied to the mains, and based on that value, control a solid state relay. As per my theory till date, if the AC is >=205 or <= 230V, the relay will be on, otherwise it will be off.

The circuits have been borrowed from here:
  1. AC voltmeter project with arduino
  2. SSR control with arduino (but without the fan)
I am not giving the code here, because I shall post a separate thread with the whole circuit in the DIY section.

Now, I have been measuring AC supplied to our mains for some time, and it seems that I have some trouble. (In India, we get AC RMS at 220V to 230V, 50Hz, but goes down to around 200V when all the air-conditioners of the neighbouring houses are working). The voltage does remain strictly at some value; it is continuously varying by ##\pm##1V or 2V in a very short period, say 1s. And that is the difficulty. For example, if the voltage varies between 204V and 206V continuously, then the circuit will keep switching on and off the relay, and that will create some trouble for the appliance being controlled by the relay.

What I want to do is to simply round off the calculated voltage to the nearest lowest integer. In that way, I can easily get around with this problem.

Since I am not conversant in C programming, can anyone tell whether the floor() function will work in Arduino? What libraries do I need and how do I download them?
 
Engineering news on Phys.org
Have you tried #include <math.h>? Did it work?
 
Wrichik Basu said:
What I want to do is to simply round off the calculated voltage to the nearest lowest integer. In that way, I can easily get around with this problem.
Why? If the voltage varies between 205.5 and 204.5 V you can still get switching processes. In fact you still get the same range for the voltages as before. Rounding shouldn't make any difference for a >= comparison (and it just shifts the range by 1 V for <= comparisons).
You could work with integers in C, then you have automatic rounding. But I don't think that is what you want.

What you probably want is hysteresis: If the device switches on when the voltage goes above 205 V, it shouldn't switch off if the voltage drops to e.g. 204.9 V. Keep it on unless the voltage drops a bit more (1-3 V? Whatever works for you).
Alternatively, add a timer: After a switching process there cannot be additional switches within x seconds. While this makes sure there are not too many switching processes it can leave your device connected with a very low/high voltage if the voltage changes rapidly.
 
  • Like
Likes   Reactions: Asymptotic and anorlunda
Borek said:
Have you tried #include <math.h>? Did it work?
That worked. Thanks.
 
mfb said:
Why? If the voltage varies between 205.5 and 204.5 V you can still get switching processes. In fact you still get the same range for the voltages as before. Rounding shouldn't make any difference for a >= comparison (and it just shifts the range by 1 V for <= comparisons).
You could work with integers in C, then you have automatic rounding. But I don't think that is what you want.

What you probably want is hysteresis: If the device switches on when the voltage goes above 205 V, it shouldn't switch off if the voltage drops to e.g. 204.9 V. Keep it on unless the voltage drops a bit more (1-3 V? Whatever works for you).
Alternatively, add a timer: After a switching process there cannot be additional switches within x seconds. While this makes sure there are not too many switching processes it can leave your device connected with a very low/high voltage if the voltage changes rapidly.
I just realized that the problem is not only about decimals, but it is a bit complex. I will explain briefly. Let's forget decimals for a moment to keep things simple. What I am finding is that, the voltage shifts from 204V to 205V in about 1s, and again comes back to 204V, and this goes on for a long time. This is where the actual problem lies. If I write my code such that it will switch the relay on in a range >= 205V, and provide a delay of 1s or so, even then it will keep switching the relay on and off continuously, as the voltage fluctuates. The problem doesn't arise when voltage fluctuates between 206V to 207V, but occurs at the limit points. There is a chance that the Arduino will continually switch on and off the relay, thereby damaging the appliance (an air-conditioner).

Now comes the bottleneck: If I provide a long time delay of about 5s or 10s, there is a chance that the voltage will drop to a very low value and the appliance will still work, as the Arduino will be sleeping then.

I need to do something such that the fluctuations at the edge can be removed. Either by coding, or by changing circuit, as my current circuit doesn't take this into account. I cannot use voltage regulators, as they'll provide me with constant DC and I don't want that. Perhaps some IC, but I have to research a bit. I might need something like a Schimitt trigger, but I have to see.

Anyways, these topics were not asked for in the thread, and thanks for your input.
 
Wanted to mention that the above problem has been solved. I will post the solution soon in a separate thread.

Thanks to everyone for their input.
 

Similar threads

  • · Replies 38 ·
2
Replies
38
Views
5K
  • · Replies 25 ·
Replies
25
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
20
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
16
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K