Pages: [1]   Go Down
 Author Topic: How to measure rate of change in voltage with UNO  (Read 159 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 4
 « on: March 17, 2013, 12:59:36 pm » Bigger Smaller Reset

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
 Logged

Croatia
Offline
Full Member
Karma: 9
Posts: 240
 « Reply #1 on: March 17, 2013, 01:07:00 pm » Bigger Smaller Reset

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.
 Logged

Offline
Newbie
Karma: 0
Posts: 4
 « Reply #2 on: March 17, 2013, 02:01:36 pm » Bigger Smaller Reset

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:
/*
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:
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
 Logged

Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
 « Reply #3 on: March 17, 2013, 02:23:30 pm » Bigger Smaller Reset

Hi, look at this code fragment:

Code:
float voltage;
float LastVoltage;
float rate;
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
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.
 Logged

Manchester (England England)
Offline
Brattain Member
Karma: 271
Posts: 25422
Solder is electric glue
 « Reply #4 on: March 17, 2013, 02:24:33 pm » Bigger Smaller Reset

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

Code:
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
Code:
LastVoltage = voltage;
voltage is always zero because it is never being set to anything.
 Logged

Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #5 on: March 17, 2013, 02:34:22 pm » Bigger Smaller Reset

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 .

Code:
/*
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();
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;
}
}
 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

 Pages: [1]   Go Up