# How to measure rate of change in voltage with UNO

Hi,

Im currently working on this project in which i need to measure dV/dt at the terminals of a battery. Is there a function i can use? I am now able to get the voltage by using the analogRead(), but how can i get the rate of change in that voltage?

Thanks
Shirley

Measure it and check the time. Store the values.
Measure it and check the time again. Store the values someplace else.
Repeat a few times to get more data to work with.

Use millis to get the time.

Shpaget:
Measure it and check the time. Store the values.
Measure it and check the time again. Store the values someplace else.
Repeat a few times to get more data to work with.

Use millis to get the time.

I modified a piece of code from the tuorial. Here's what i did:
/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
float voltage;
float LastVoltage;
float rate;
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
LastVoltage = voltage;
Serial.print("Last voltage: ");
Serial.println(LastVoltage);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
// Calculate the rate of change in voltage, in V/s
rate = (voltage-LastVoltage);
Serial.print("dV: ");
Serial.println(rate);
}

However the LastVoltage is always 0, do you know why?

Thanks
Shirley

Hi, look at this code fragment:

``````float voltage;
float LastVoltage;
float rate;
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
LastVoltage = voltage;
Serial.print("Last voltage: ");
Serial.println(LastVoltage);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):

float voltage = sensorValue * (5.0 / 1023.0); // voltage is redeclared here, masking the global variable
``````

Also note that you should divide by 1024. And you are not calculating a rate since there is no reference to millis() or micros().
Besides, you don't really need to convert to voltage: an int value ranging from 0 to 1023 is as good to detect change as a float value ranging from 0 to 5, but the conversion from int to float is computationally expensive and you'll get less samples.
Finally, don't forget to put your code in code tags.

Read the how to use this forum sticky, go back and put code tags in that post.

``````float voltage = sensorValue * (5.0 / 1023.0);
``````

Creates a new variable called voltage that is not the same as the global one you created outside the loop. Therefore when you do

``````LastVoltage = voltage;
``````

voltage is always zero because it is never being set to anything.

It is assigned 0 as voltage did not get its value yet.
This is caused by the "float voltage" in the loop which create a second variable inside the loop called voltage .

slightly modified your code

``````/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

float voltage = 0;
float LastVoltage = 0;
float rate = 0;
unsigned long lastTime = 0;

unsigned long dt = 100; // dt in milliseconds

void setup()
{
Serial.begin(9600);
}

void loop()
{
if (millis() - lastTime  >= dt)   // wait for dt milliseconds
{
lastTime = millis();
int sensorValue = analogRead(A0);
voltage = sensorValue * (5.0 / 1023.0); // this line changed !!

Serial.print("SensorValue: ");
Serial.println(sensorValue);
Serial.print("Last voltage: ");
Serial.println(LastVoltage, 4);
Serial.print("Current voltage: ");
Serial.println(voltage, 4 );

rate = (voltage-LastVoltage);
Serial.print("dV/dt: ");
Serial.println(1000*rate/dt, 4);
Serial.println();

LastVoltage = voltage;
}
}
``````