Decreasing Arduino Analog Input Current  - Page 1
Forum section: electronics / beginners

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 13, 2022, 02:49:32 pm
content: Hello,I try to measure 6V lithium battery. I made a voltage diverder with 10k but It doesn't show right values after sometimes. Therefore, I wanted to use lower resistor value like 7.5ohm and I made a voltage divider. However, if I do this I can burn analog input and then I put one series 10k resistor to analog input of arduino. Does that work? How can I measure this battery voltage truely?I measure 5.4V when I just make a voltage divider with 10kohm resistors but when I measure CR-P2 battery voltage with 10ohm resistor I measure around 4V.Could you help me, please?

author: MikeK
author_rank: Super Contributor
timestamp: October 13, 2022, 03:18:35 pm
content: Draw the exact circuit that you are using. A voltage divider of two equal resistors will give you a voltage of 1/2 the battery voltage. 7.5 ohms is too low a value...If those are typical 1/2 watt resistors you're going to burn them up very quickly.EDIT: I meant "typical 1/4 watt resistors"

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 13, 2022, 03:31:13 pm
content: That what MikeK wrote, and show the sketch you use to display the voltage you measure.Search for the specifications of the MCU you are using and look at what the ADC needs at its input like min and max voltages, what the reference voltage is and how many bits it converts the analog signal into. Based on this information you can calculate the measured voltage.Give us more information to work with then what you wrote

author: ledtester
author_rank: Super Contributor
timestamp: October 13, 2022, 04:22:56 pm
content: Quote from: elefurtronik on October 13, 2022, 02:49:32 pmIn general you do not want to attach a series resistor to the ADC input pin. Try a small capacitor across the bottom resistor instead.From this discussion "voltage divider making adc nonlinear":https://www.eevblog.com/forum/projects/voltage-divider-making-adc-nonlinear/msg988174/#msg988174Quote from: georges80 on July 21, 2016, 09:15:21 pm

author: Faranight
author_rank: Unknown
timestamp: October 14, 2022, 08:10:24 am
content: I've read that Arduino's (atmega328) analog input pins are meant for sources with maximum impedance of 10kohm. Any more than that may skew the results.This is why you would normally buffer the output from a resistor divider network with an opamp in a voltage-follower configuration.

author: golden_labels
author_rank: Super Contributor
timestamp: October 14, 2022, 01:32:30 pm
content: I have a feeling that it’s not the divider that’s the problem. Not only that 5kΩ divider is within specs, but failing to fit the requirements affects the bandwidth, not DC accuracy. Bandwidth would not be an issue for battery voltage measurement. For a moment I thought that maybe internal resistance of the battery affects the measurement, but it’s within hundreds of ohms, so the effect shouldn’t be that great. At this point I suspect the problem is in the description or… there is no problem at all and OP is misinterpreting the situation.elefurtronik:I read your message carefully a few times and with each pass it makes less and less sense. I am now even doubting if, what you call “voltage divider”, is actually a voltage divider. The way you refer to it and the way you describe its possible effects do not fit a voltage divider at all.Please provide an exact and clear schematic of the circuit, which you are now using. With all four(?) elements present,namedand with all values given. Then, slowly and carefully,step by stepdescribe what you are doing in that circuit, referring to elements by the names given. Also tell us which Arduino board you are using.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 14, 2022, 08:22:25 pm
content: Hello,I am using a 5w Resistor. It is getting hot but I still can't get the right measurements from the battery. In the beginning, values are right but sometimes values are wrong. The system measures 5.3V but it is actually 4.8V.Quote from: ledtester on October 13, 2022, 04:22:56 pmI didn't get why I shouldn't put series resistor to ADC?Quote from: Faranight on October 14, 2022, 08:10:24 amDoes that mean what I do is right?I use 7.5ohm/7.5 ohm 5W voltage divider and then series 10kohm to analog input.Quote from: golden_labels on October 14, 2022, 01:32:30 pmI attached the circuit. Please check.

author: Kleinstein
author_rank: Super Contributor
timestamp: October 14, 2022, 08:53:33 pm
content: A series resistor alone is of limited use, as the input impedance of the ADC is not that well defined. It can vary quite a bit. So it usually needs 2 resistors for a divider.The 7.5 ohms resistors draw way too much current and drain the battery quite fast. A voltage divider is the right way, but with larger resistors. The ADC in the AVRs are normally ok with up to some 20 K of source impedance. So 2x39 K should still be OK and give 1/2 the battery voltage to the ADC. The 20 K is not a hard limit and depends a bit on the ADC clock (a bit lower with a fast clock) and the problem with too high resistance is increasingly more error, not a sudden failure above a hard limit.One can use a higher impedance (e.g. 2x220 K) if there is a parallel capacitor (e.g. 100 nF) to buffer the current peaks.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 14, 2022, 09:06:23 pm
content: I was using 10k-10k resistor before this setup and realized that my measurements are wrong. I think this problem is caused by Zener diode into AREF pin. As I measure lower than 5.1V, when the battery is below 5.1V zener is out of the game and my referance voltage is wrong. I will change my zener diode with 3.3V one and measure again. I hope it will work.

author: fourfathom
author_rank: Super Contributor
timestamp: October 14, 2022, 10:16:43 pm
content: Yeah, that 7.5 Ohm resistor is about 1000 times too low.Which Arduino? I recall that some of the "Zero" types (which use a SAM D21 or similar chip) had a problem with the Aref input -- you couldn't actually use it for the analog reference. If so, I suggest that you use one of the internal references, they ought to be at least as good as a generic resistor/Zener reference. And a 100K + 100K divider with a 0.1u cap at the tap should work well. Or a 10K + 10K. With these values you don't need a series resistor (which is usually used for protection against voltage spikes).When you say "doesn't show the right values", what values are you actually getting?

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 14, 2022, 11:32:51 pm
content: Hi,I am usingArduino Mega 2560.I also attached my schematic in the prior mail than previous one.I am using the AREF pin as a reference and reading the Analog value from the A0 pin and calculating it with basic ohm law. Eventually, I am calculating the CR-P2 battery voltage. However, when I make the test initially Arduino sends to the Serial monitor, the right values and after some battery consumption it is still showing some voltage values but when I measure the battery, I see the value in the serial monitor is wrong.

author: fourfathom
author_rank: Super Contributor
timestamp: October 15, 2022, 12:16:04 am
content: Quote from: elefurtronik on October 14, 2022, 11:32:51 pmWhat does this mean? Your A/D measurement will be a number equal to (full-scale value) * (input voltage) / (reference voltage). Input voltage is determined by whatever voltage you apply to the resistive divider and the divide ratio. What number are you getting? What do you expect?

author: sleemanj
author_rank: Super Contributor
timestamp: October 15, 2022, 01:49:03 am
content: Lose the 10kIncrease both 7.5 to something more sensible, 1k minimumPost code, results, and expected results

author: golden_labels
author_rank: Super Contributor
timestamp: October 15, 2022, 02:01:44 am
content: elefurtronik:I asked for multiple things. From that I only see the schematic and, some posts later, the name of the board. Where is the description I asked for?To expect other people to spend their free time on helping, one first needs to show some effort. Being a beginner is absolutely fine — everybody here once was. Not being able to focus for a minute to write a proper post and instead throwing random, confusing scraps that make little sense — that is not fine.I would remove the entire AREF segment. ATmega2560 has two built-in voltage references (1.1V and 2.56V). They offer better accuracy and precision than a plain zener diode, itself driven by the measured voltage,(1)and missing filtering. That is unlikely to be the source of any described issues, which still must be addressed even if you can no longer observe the problem after switching to internal references. But that will remove one factor from the equation.The 10kΩ resistor in series, as already mentioned above, is pointless. But you would still see the value at least converging to the expected one, not simply being lower.(1)The dependency is strongly non-linear, but it’s still a poor design choice.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 15, 2022, 06:28:21 am
content: Quote from: golden_labels on October 15, 2022, 02:01:44 amWith these internal references it is important to understand that the input signal must not be higher then the chosen reference, otherwise you just get the maximum reading.So it is necessary to adjust the voltage divider to match the wanted range, and use the division factor in your calculations to get the actual measured voltage.If you choose the 2.56V reference and you need to measure 6V at max you can't use a 10K/10K divider, because the maximum voltage is then 3V. A 10K/6K8 is then better. Max voltage at the input is then ~2.43V. In your calculations you then need to multiply with 2.47 to get the battery voltage.

author: Brumby
author_rank: Unknown
timestamp: October 15, 2022, 12:06:46 pm
content: Quote from: sleemanj on October 15, 2022, 01:49:03 amI'd have said something along these lines as well.I also agree with not using the zener idea. Ditch that and use an internal reference.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 15, 2022, 10:40:21 pm
content: Hello,I canceled the Zener idea but still, I can't read the value that I get from Fluke 179 True RMS Multimeter. Not even close. I measure the CR-P2 battery and those 32,85k / 5.65k combinations which I created can't load the battery. Therefore, I can't read the right voltage measurements in the serial monitor.I expect to read 11.2ohm loaded and read multimeter results in my serial monitor.My program can't read the right voltage values of LiMnO2 batteries(CR-P2).Example:The serial monitor says:3.73V and the real voltage under 11.2ohm is 2.5VOther battery serial monitor 4.76V and under load 4,4VOne another battery, serial monitor 5.5V, and under load 5.7VLast battery, serial monitor 3,80V and under load 0.7VCode:int voltage_input_pin =A0;float input_voltage_data = 0;float v_step = 1.1 / 1023.0 ;float voltage_array;int voltage_measurement_amount=1201;float voltage_sum;float av_voltage;void setup() {Serial.begin(115200);pinMode(voltage_input_pin, INPUT);analogReference(INTERNAL1V1);}void loop() {input_voltage_data = analogRead(voltage_input_pin)*v_step;for(int i=0;i<voltage_measurement_amount;i+=1){if (input_voltage_data<0.06){voltage_array=input_voltage_data;}else{voltage_array=input_voltage_data + ((input_voltage_data / 6580) * 32850);}voltage_sum+=voltage_array;}av_voltage=voltage_sum/voltage_measurement_amount;voltage_sum=0;}

author: hubi
author_rank: Regular Contributor
timestamp: October 15, 2022, 11:12:20 pm
content: The code seems to only take one voltage reading outside of the for loop, so the averaging does not really do anything. I also don't see a scale factor to reflect the voltage divider.Not exactly sure what you mean by "I expect to read 11.2ohm loaded and read multimeter results in my serial monitor.".

author: ledtester
author_rank: Super Contributor
timestamp: October 15, 2022, 11:19:33 pm
content: How about trying a simpler program like this. Make sure R_UPPER and R_LOWER reflect the values used in your voltage divider.Code:const float R_UPPER = 32.85;const float R_LOWER = 5.65;const float scale = 1.1*(1.0+(R_UPPER/R_LOWER))/1024.0;void setup() {Serial.begin(115200);pinMode(A0, INPUT);analogReference(INTERNAL1V1);}void loop() {int adc = analogRead(A0);Serial.print("ADC reading: ");Serial.print(adc);Serial.print(" volts: ");Serial.println(adc*scale);delay(3000);}

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 09:53:12 am
content: Quote from: hubi on October 15, 2022, 11:12:20 pmI am a beginner but there is really one basic thing about measuring a lithium battery. If you don't load the battery, even though you take the measurement with multimeter, it won't show you the right voltage value of the battery.Quote from: ledtester on October 15, 2022, 11:19:33 pmShows a fully empty battery full. I think first we need to be clear about how to measure LiMnO2 batteries. That's the reason why I am trying to make my voltage divider with lower resistors. If you don't load the battery with lower resistors and pull a high current, it will show you the wrong voltage value. Measuring LiMnO2 batteries are not like measuring AA battery.Your program measures 5.2V but the battery is actually 0.8V. If I load this battery with this measurement, my device will not work.

author: ledtester
author_rank: Super Contributor
timestamp: October 16, 2022, 01:48:22 pm
content: You can load the battery with an additional resistor like this:In this configuration the ADC will reflect the voltage of the battery under the 11 ohm load.

author: tooki
author_rank: Super Contributor
timestamp: October 16, 2022, 02:17:20 pm
content: Quote from: elefurtronik on October 16, 2022, 09:53:12 amThat is true for every battery, not just lithium.Also let’s correct your wording: there’s no such thing as the “right” voltage of a battery. There’s only a voltageunder a given load. What the multimeter measures, since it applies essentially no load, is called the open-circuit voltage (OCV). What the OCV doesn’t tell you is what voltage you will get with a given load applied. The voltage at one load will be different than at another one. (More on this later.)Quote from: elefurtronik on October 16, 2022, 09:53:12 amIt’s much more alike than you realize. Every battery needs to be tested under load, and every battery’s voltage will be different at different load currents. Yes, lithium batteries have a sharp knee in their discharge curve. But the voltage does nonetheless taper down linearly for the bulk of the discharge curve. It’s just a flatter slope until the knee comes.Note that most alkaline battery datasheets truncate the discharge curve at 0.8V because most loads can’t operate at such low voltages. What’s very interesting is that at lower discharge currents, the curve of alkaline cells also shows a very clear knee, it’s just usually cut off. Take a look:https://www.powerstream.com/AA-tests.htmAnd if you look at alkaline under a constant-powerload (as opposed to constant-current), as is the case in modern devices using DC-DC converters, the knee appears much more readily. See the attached AA datasheet.Here’s the thing: you have to determine (read: measure) that discharge curve for your application in order to create an accurate state-of-charge indication. But once you characterize it, it should be reasonably reproducible. Then use a lookup table to map a voltage to a %. What doesn’t make sense is to measure at a different load than your real-world load. At least, if you do, you’ll have to do some much more complex testing, so that you can correlate the percentages at both combinations of load and measured voltage.For example, look at the CR-P2 datasheet attached. If we take the inflection point of the knee as “empty”, then at 50mA, you get around 30h of service, during which the voltage drops linearly from 5.9V to 5.5V, then accelerates downward. At 15h (50% service life), it’s at about 5.6V. At 250mA, total life is about 5h, falling linearly from 5.5V down to 5.0V, with the halfway point at about 5.1V.You could then map the data so that you can test at a different current.Also, this is why state of charge measurement chips, called “fuel gauges”, for rechargeable devices are so complex, and don’t bother relying on voltage measurement alone: they actually count coulombs flowing in and out, and then correlate that to a huge set of parameters that accurately characterize both the battery and the load (parameters collected during product development and then programmed into the fuel gauge IC).

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 04:04:03 pm
content: Quote from: ledtester on October 16, 2022, 01:48:22 pmHere, my doubt is while I am measuring the voltage as parallel, my battery will also drain faster. I am also powering motor and valve in parallel. Is there any other way not to drain the battery but that measurement?Quote from: tooki on October 16, 2022, 02:17:20 pmI have a valve connected but still, when I do an experiment, I see that the measurement is not correct. What can be the reason? Why I can't get the right measurement even though my valve load is connected? I measure the voltage divider, and it is correct, normally valve should work on this voltage but it doesn't. Besides that when I measure with an 11ohm resistor, I see that the voltage is under the spec voltage of the dc motor.

author: ledtester
author_rank: Super Contributor
timestamp: October 16, 2022, 04:30:43 pm
content: Quote from: elefurtronik on October 16, 2022, 04:04:03 pmAre you asking how to connect the 11 ohm resistor only when you want to make a measurement?Here's a possibility using a MOSFET. Set the GPIO line to HIGH when you want to connect in the resistor. Set it to LOW to disconnect.

author: tooki
author_rank: Super Contributor
timestamp: October 16, 2022, 05:39:16 pm
content: Quote from: elefurtronik on October 16, 2022, 04:04:03 pmWhat do you think the “right” voltage is, and how do you arrive at that value?I think you need to take a step back and explain from the beginning what you’re trying to accomplish, how you’ve arrived at your targets, what assumptions you’ve made, and what you’ve tried so far. Because right now, it seems to me that you are rather confused, and struggling to explain it to us.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 06:13:31 pm
content: Quote from: ledtester on October 16, 2022, 04:30:43 pmYes, this is what I want. And one more question. Is MOSFET a low-power solution as I am driving the system with a battery?Quote from: tooki on October 16, 2022, 05:39:16 pmHi Tooki,I was a little bit confused, to be honest. I was not imagining, there would be huge measurement differences. I made an experiment and saw the real situation.At this point, I had a question about my load's resistance value. I had a parallel connected motor and valve, I took the multimeter and measured their resistance. Is this the right way to measure the resistance of those two connected parts?

author: ledtester
author_rank: Super Contributor
timestamp: October 16, 2022, 06:28:13 pm
content: Quote from: elefurtronik on October 16, 2022, 06:13:31 pmYes it is.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 06:37:53 pm
content: Hi Ledtester,I just don't want to change the topic but why not with a BJT?

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 16, 2022, 06:40:16 pm
content: Are you powering the MCU that does the measurement from the same battery?Provide a schematic of the complete system you are trying to make, so we can understand better what it is you are trying to do.If it is to monitor the battery power during operation then the battery is already loaded with the valve and the motor you are writing about, and then there would be no need to put on an extra load.Or is it a separate system to just measure different batteries to see what their available power is?Paint the complete picture as asked already by others in this thread.

author: fourfathom
author_rank: Super Contributor
timestamp: October 16, 2022, 06:42:45 pm
content: Quote from: elefurtronik on October 16, 2022, 06:13:31 pmThis is OK for measuring the valve coil resistance, but not for a spinning motor. When you measure the motor with a multimeter you are measuring the winding resistance, and this will let you calculate the "stall current". But when the motor is spinning the effective resistance will be significantly higher, and will depend on the load (torque) on the motor.There are other factors when dealing with solenoid coils and motors, but let's start with these.

author: Vovk_Z
author_rank: Super Contributor
timestamp: October 16, 2022, 06:59:29 pm
content: Quote from: elefurtronik on October 16, 2022, 06:13:31 pmYou are a bit strange man. You want to load a battery, but you don't want to load a battery.The more I read this topic the more I guess TS is a troll.

author: tooki
author_rank: Super Contributor
timestamp: October 16, 2022, 07:45:03 pm
content: Quote from: elefurtronik on October 16, 2022, 06:13:31 pmIt’s ok to be confused — nobody here is expecting you to be an expert! But you’re still confused, and what’s preventing us from untangling your confusion (and clearly frustrating many people in this thread) is your continued reluctance to share information.I’ve asked you numerous specific questions, and so have others, and you don’t answer them. You have been asked repeatedly to sharein detailyour complete setup, goals, etc. and you just throw out bits and pieces.And I and others have given extensive information that should have elicited responses and specific follow up questions. But you largely ignored it, making me think you either didn’t understand (and don’t want to say so) or don’t care.If you want help, you need to put in the work, too. But you are wasting our time by ignoring everything we ask for. We are happy to help, but we need to see you making use of the time we give you.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 08:14:16 pm
content: Hi,I was thinking in my schematic I showed almost all I have. When I learn more, I share more because, in the beginning, I thought something will not be an issue, but it seems can be an issue. This is the reason why I am sharing partially.Previously some fellows asked what chip I use and so on but I have already shown it in the schematic. In the previous posts, I also mentioned my aim. However, as it is bit by bit maybe now I can try to draw a clear picture of what I have and what I want.What do I want?I want to see the battery voltage under my load condition(while DC motor and valve working). Because I am using a battery and I want to know what is the current voltage value as I have a cut-off voltage of around 4.9V, I would like to see my battery's condition and then when the voltage is 4.9V, I will shut the system off.Code is also very basic.Full Code:Code:int voltage_input_pin =A0;float input_voltage_data = 0;float v_step = 1.1 / 1023.0 ;int A1A = 10;int A1B = 8;float voltage_array;int voltage_measurement_amount=1201;float voltage_sum;float av_voltage;void setup() {pinMode(A1A, OUTPUT);pinMode(A1B, OUTPUT);Serial.begin(115200);pinMode(voltage_input_pin, INPUT);analogReference(INTERNAL1V1);}void loop() {input_voltage_data = analogRead(voltage_input_pin)*v_step;digitalWrite(A1B,HIGH);analogWrite(A1A,LOW);delay(500);analogWrite(A1B,LOW);analogWrite(A1A,LOW);delay(500);for(int i=0;i<voltage_measurement_amount;i+=1){if (input_voltage_data<0.06){voltage_array=input_voltage_data;}else{voltage_array=input_voltage_data + ((input_voltage_data / 6580) * 32850);}voltage_sum+=voltage_array;}av_voltage=voltage_sum/voltage_measurement_amount;voltage_sum=0;}

author: hubi
author_rank: Regular Contributor
timestamp: October 16, 2022, 08:37:22 pm
content: Quote from: elefurtronik on October 16, 2022, 08:14:16 pmI mentioned this earlier, you only read the analog voltage at the very beginning of loop() and the averaging loop does nothing. Instead of using a delay(500) between enabling and disabling the motor, you should read and average the voltage for the desired time. You can implement that by saving the start time using start_millis=millis() and then exiting the measurement loop when (millis()-start_millis>500).

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 09:27:41 pm
content: Hubi,Now, I understood what you mean. Yes, I also realized what you say now. However, I couldn't make this in the code. When I take the measurement between low and high. My motor system doesn't work at all.How can I adjust my code with your way?

author: ledtester
author_rank: Super Contributor
timestamp: October 16, 2022, 09:36:19 pm
content: In your code:Code:...digitalWrite(A1B,HIGH);analogWrite(A1A,LOW);delay(500);analogWrite(A1B,LOW);analogWrite(A1A,LOW);...Questions:- Why are you using both digitalWrite and analogWrite with A1B?- Why do you only analogWrite(A1A, LOW) and never with any other value?What are you really trying to do with that code?

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 16, 2022, 09:42:07 pm
content: Quote from: ledtester on October 16, 2022, 09:36:19 pmI was trying PWM and I forgot to change it while posting the code here. Sorry for this.

author: hubi
author_rank: Regular Contributor
timestamp: October 16, 2022, 10:35:51 pm
content: Quote from: elefurtronik on October 16, 2022, 09:27:41 pmI can't tell without you posting the modified code that did not work.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 17, 2022, 05:43:03 am
content: Quote from: elefurtronik on October 14, 2022, 11:32:51 pmHow are you powering this board from your 6V battery?The standard Arduino Mega 2560 uses a 5V processor and has a voltage regulator on the board that needs an input voltage of 7-12V.It is possible to bypass the voltage regulator, but only with a 5V supply, not 6V from your battery.Detecting the battery dropping to 4.9V and running the MCU from the same battery might not work due to voltage drop across a voltage regulator.You might want to look into a 3V3 Arduino Nano to use in your project.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 17, 2022, 06:03:44 pm
content: Quote from: pcprogrammer on October 17, 2022, 05:43:03 amHi,I am powering my Arduino from my laptop's USB. Therefore, this shouldn't be a problem.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 17, 2022, 07:12:07 pm
content: Not while you are testing, but when the system is running on its own, how will you have it work then? Or is it always going to be connected to a computer?

author: hubi
author_rank: Regular Contributor
timestamp: October 17, 2022, 08:00:31 pm
content: Quote from: elefurtronik on October 17, 2022, 06:03:44 pmYou should really post a complete schematic and show how everything is connected, as well as the complete arduino sketch. This should not be too difficult to figure out once all the details are available.

author: elefurtronik
author_rank: Regular Contributor
timestamp: October 17, 2022, 09:22:07 pm
content: Quote from: hubi on October 16, 2022, 10:35:51 pmHi Hubi,I am trying to adjust the code and I will fix the recent schematic.Update:Code is below.Code:void loop() {digitalWrite(A1B,HIGH);digitalWrite(A1A,LOW);start_time = millis();i=0;while(millis()-start_time<=500){input_voltage_data = analogRead(voltage_input_pin)*v_step;if (input_voltage_data<0.4){voltage_sum+=0;}else{voltage_sum+=input_voltage_data + ((input_voltage_data / 6580) * 32850);}i+=1;}av_voltage=voltage_sum/i;digitalWrite(A1B,LOW);digitalWrite(A1A,LOW);delay(500);String cycleStr=String(cycle);Serial.println(cycleStr);Serial.print(av_voltage);Serial.print(",");voltage_sum=0;av_voltage=0;cycle+=1;}Quote from: pcprogrammer on October 17, 2022, 07:12:07 pmIt will be always connected to my computer or I will provide 12V from the power input of Arduino Mega.

author: fourfathom
author_rank: Super Contributor
timestamp: October 17, 2022, 11:06:21 pm
content: In your code:QuoteYou are essentially adding (input_voltage_data * 5.992xxx) to voltage_sum every time you take a measurement. After 500 ms you then divide voltage_sum by the number of measurements (this is a reasonable averaging method).Question: The math in that quoted bit above looks like it has to be a mistake. There are simpler (and obvious) ways to do scaling, so I doubt that you actually want to do what you have written. What is your intent?Question: What type of variable is "voltage_sum"? Int32? Int64? Floating point? You probably won't overflow even a 32-bit variable here, but you should be aware of how big the value can grow.Observation: There's probably no good reason to do any scaling of your data before you add it to the voltage_sum accumulator. It's probably best to scale it after you read the sum. You're not getting any improved resolution by pre-scaling it, and it's less (software) efficient.