Loading...
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 Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Full Member
***
Karma: 9
Posts: 240
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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:
/*
  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
Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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:
  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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 271
Posts: 25422
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Tesla Member
***
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 .

slightly modified your code  
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;
  }
}
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Print
 
Jump to: