Rounding down measured value in Arduino

In summary: But if the voltage drops very low, the Arduino might turn off the relay, as it assumes that the appliance is not operational anymore. In summary, the problem is that the Arduino keeps switching on and off the relay, even when the voltage fluctuates between 206V to 207V. This is because the Arduino only understands integer values, and RoundoffError causes the voltage to be interpreted incorrectly. In order to prevent this problem, you could use a timer to delay the relay turn-on for a certain amount of time, after which it will turn off automatically. Alternatively, you could use hysteresis, which will keep the relay on as long as the voltage is above a certain value, and turn it off
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
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
  • #2
Have you tried #include <math.h>? Did it work?
 
  • #3
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 Asymptotic and anorlunda
  • #4
Borek said:
Have you tried #include <math.h>? Did it work?
That worked. Thanks.
 
  • #5
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.
 
  • #6
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.
 

1. What does "rounding down" mean in the context of measuring values in Arduino?

Rounding down is a mathematical process of reducing a number to its nearest whole number or specified decimal place by dropping any digits after that decimal place. In the context of measuring values in Arduino, rounding down means that the measured value will be rounded to a smaller number, ignoring any decimal values.

2. Why is rounding down necessary in Arduino?

Rounding down is necessary in Arduino because the analog-to-digital converter (ADC) used to measure values in the board can only provide a certain level of precision. Rounding down ensures that the measured values will fall within the range of the ADC and can be accurately represented in the code.

3. How does Arduino round down measured values?

Arduino uses a function called floor() to round down measured values. This function takes a floating-point number as input and returns the largest integer that is less than or equal to that number. It essentially truncates any decimal values and returns the nearest whole number.

4. Can the rounding down process affect the accuracy of measured values?

Yes, rounding down can affect the accuracy of measured values. By rounding down, any decimal values are essentially ignored, which can result in a loss of precision. This means that the measured value may not be as accurate as the actual value, but it will fall within an acceptable range of accuracy determined by the ADC.

5. Are there any alternatives to rounding down measured values in Arduino?

Yes, there are alternatives to rounding down measured values in Arduino. One option is to use the round() function, which rounds the measured value to the nearest integer. Another option is to use the map() function, which allows you to scale a measured value to a desired range without rounding it down. However, these alternatives may not be suitable for all applications, so it is important to understand the specific needs and limitations of your project before deciding on a rounding method.

Similar threads

  • Electrical Engineering
2
Replies
38
Views
3K
  • Electrical Engineering
Replies
25
Views
2K
Replies
31
Views
2K
  • Electrical Engineering
Replies
5
Views
3K
Replies
20
Views
2K
  • Electrical Engineering
Replies
10
Views
862
Replies
16
Views
1K
Replies
12
Views
2K
  • Electrical Engineering
Replies
18
Views
2K
  • Electrical Engineering
Replies
18
Views
2K
Back
Top