-
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?
-
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"
-
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 :)
-
... and then I put one series 10k resistor to analog input of arduino. Does that work? ...
In 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/#msg988174 (https://www.eevblog.com/forum/projects/voltage-divider-making-adc-nonlinear/msg988174/#msg988174)
I've used a resistor divider (365K/48.7k) with a 0.1uF across the bottom 48.7k resistor into AVR tiny devices and that works just fine from 24 - 0V input range. AVR running at 3V and using the VCC as the reference (3V).
-
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.
-
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, named and with all values given. Then, slowly and carefully, step by step describe what you are doing in that circuit, referring to elements by the names given. Also tell us which Arduino board you are using.
-
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.
... and then I put one series 10k resistor to analog input of arduino. Does that work? ...
In 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/#msg988174 (https://www.eevblog.com/forum/projects/voltage-divider-making-adc-nonlinear/msg988174/#msg988174)
I've used a resistor divider (365K/48.7k) with a 0.1uF across the bottom 48.7k resistor into AVR tiny devices and that works just fine from 24 - 0V input range. AVR running at 3V and using the VCC as the reference (3V).
I didn't get why I shouldn't put series resistor to ADC?
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.
Does that mean what I do is right?
I use 7.5ohm/7.5 ohm 5W voltage divider and then series 10kohm to analog input.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, named and with all values given. Then, slowly and carefully, step by step describe what you are doing in that circuit, referring to elements by the names given. Also tell us which Arduino board you are using.
I attached the circuit. Please check.
-
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.
-
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.
-
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?
-
Hi,
I am using Arduino 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.
-
calculating it with basic ohm law.
What 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?
-
Lose the 10k
Increase both 7.5 to something more sensible, 1k minimum
Post code, results, and expected results
-
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.
-
---- 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, ....
With 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.
-
Lose the 10k
Increase both 7.5 to something more sensible, 1k minimum
Post code, results, and expected results
I'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.
-
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.5V
Other battery serial monitor 4.76V and under load 4,4V
One another battery, serial monitor 5.5V, and under load 5.7V
Last battery, serial monitor 3,80V and under load 0.7V
int voltage_input_pin =A0;
float input_voltage_data = 0;
float v_step = 1.1 / 1023.0 ;
float voltage_array[1200];
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[i]=input_voltage_data;
}
else{
voltage_array[i]=input_voltage_data + ((input_voltage_data / 6580) * 32850);
}
voltage_sum+=voltage_array[i];
}
av_voltage=voltage_sum/voltage_measurement_amount;
voltage_sum=0;
}
-
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.".
-
How about trying a simpler program like this. Make sure R_UPPER and R_LOWER reflect the values used in your voltage divider.
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);
}
-
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.".
I 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.
How about trying a simpler program like this. Make sure R_UPPER and R_LOWER reflect the values used in your voltage divider.
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);
}
Shows 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.
-
You can load the battery with an additional resistor like this:
(https://www.eevblog.com/forum/beginners/decreasing-arduino-analog-input-current/?action=dlattach;attach=1615894)
In this configuration the ADC will reflect the voltage of the battery under the 11 ohm load.
-
I 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.
That 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 voltage under 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.)
Shows 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.
It’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.htm (https://www.powerstream.com/AA-tests.htm)
And if you look at alkaline under a constant-power load (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).
-
You can load the battery with an additional resistor like this:
(https://www.eevblog.com/forum/beginners/decreasing-arduino-analog-input-current/?action=dlattach;attach=1615894)
In this configuration the ADC will reflect the voltage of the battery under the 11 ohm load.
Here, 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?
I 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.
That 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 voltage under 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.)
Shows 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.
It’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.htm (https://www.powerstream.com/AA-tests.htm)
And if you look at alkaline under a constant-power load (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).
I 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.
-
Here, 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?
Are 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.
[attachimg=1]
-
Here, 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?
…
I 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.
What 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.
-
Here, 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?
Are 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.
(Attachment Link)
Yes, this is what I want. And one more question. Is MOSFET a low-power solution as I am driving the system with a battery?
Here, 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?
…
I 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.
What 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.
Hi 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?
-
Yes, this is what I want. And one more question. Is MOSFET a low-power solution as I am driving the system with a battery?
Yes it is.
-
Hi Ledtester,
I just don't want to change the topic but why not with a BJT?
-
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.
-
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?
This 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.
-
Yes, this is what I want. And one more question. Is MOSFET a low-power solution as I am driving the system with a battery?
You 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. :-//
-
What 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.
Hi 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?
It’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 share in detail your 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.
-
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:
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[1200];
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[i]=input_voltage_data;
}
else{
voltage_array[i]=input_voltage_data + ((input_voltage_data / 6580) * 32850);
}
voltage_sum+=voltage_array[i];
}
av_voltage=voltage_sum/voltage_measurement_amount;
voltage_sum=0;
}
-
Code is also very basic.
Full 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[1200];
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[i]=input_voltage_data;
}
else{
voltage_array[i]=input_voltage_data + ((input_voltage_data / 6580) * 32850);
}
voltage_sum+=voltage_array[i];
}
av_voltage=voltage_sum/voltage_measurement_amount;
voltage_sum=0;
}
I 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).
-
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?
-
In your 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?
-
In your 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?
I was trying PWM and I forgot to change it while posting the code here. Sorry for this.
-
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?
I can't tell without you posting the modified code that did not work.
-
... Arduino Mega 2560 ...
How 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.
-
... Arduino Mega 2560 ...
How 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.
Hi,
I am powering my Arduino from my laptop's USB. Therefore, this shouldn't be a problem.
-
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?
-
Hi,
I am powering my Arduino from my laptop's USB. Therefore, this shouldn't be a problem.
You 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.
-
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?
I can't tell without you posting the modified code that did not work.
Hi Hubi,
I am trying to adjust the code and I will fix the recent schematic.
Update:
Code is below.
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;
}
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?
It will be always connected to my computer or I will provide 12V from the power input of Arduino Mega.
-
In your code:
voltage_sum+=input_voltage_data + ((input_voltage_data / 6580) * 32850);
You 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.