The derivative of a volt graph (Should be easy)

Hi, i am an arduino beginner and have no coding experience. I am making a seismograph for a school project by putting a moveable magnet in a coil that will give voltage when te magnet vibrates. I am using the arduino uno r3. To live plot the voltage i am using the following code:

float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;
 
void setup() {
  Serial.begin(9600);
   
  Serial.println("--------------------");
  Serial.println("DC VOLTMETER");
  Serial.print("Maximum Voltage: ");
  Serial.print((int)(vPow / (r2 / (r1 + r2))));
  Serial.println("V");
  Serial.println("--------------------");
  Serial.println("");
   
  delay(2000);
  
}

void loop() {
  float v = (analogRead(0) * vPow) / 1024.0;
  float v2 = v / (r2 / (r1 + r2));
   
  Serial.println(v2);

}

My question is how would i plot the derivative of the voltage,time graph using the arduino serial plotter?

not sure you need the derivative, but the derivative is simple the change between the last 2 values. so keep track of the last value (e.g. vLst) and report the difference, v - vLst)

gcjr:
not sure you need the derivative, but the derivative is simple the change between the last 2 values. so keep track of the last value (e.g. vLst) and report the difference, v - vLst)

Thank you for your answer! I need the derivative to get the "induction voltage" (not sure if i translated that right). How do i keep track of the previous voltage value?

gcjr:
not sure you need the derivative, but the derivative is simple the change between the last 2 values. so keep track of the last value (e.g. vLst) and report the difference, v - vLst)

The derivative is the rate of change of one variable with respect to another. Presumably, OP wants rate of change of voltage with respect to time. That can be approximated as delta(v) / delta(t).

So, you'd take the difference between two voltage readings and divide it by the time difference between when those readings were taken. The millis or micro functions can be used to provide the timestamps.

emielj:
How do i keep track of the previous voltage value?

define another variable, vLst. set add vLst = v; near the end of your code

Watch out for the derivative with sampled data. It is not at all the same as the mathematical function which approaches a value to a limit. The math derivative applies to an infinitely small region of the x number line, and only for well defined functions. That means, for example, the difference between the last and present value might consist more of input noise, than the actual derivative.

There is also a problem with resolution, as the difference between last and current values, is usually much smaller than the maximum or nominal input value range. So the quantization that happens in your A/D conversion, is greatly amplified, thus producing very "chunky" output.

The way you "keep track" of something in C++ is to assign the value to a variable.

gcjr:
define another variable, vLst. set add vLst = v; near the end of your code

Hi, based on your suggestion i made the following code:

float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;


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

}

void loop() {
  float v = (analogRead(0) * vPow) / 1024.0;
  float v2 = v / (r2 / (r1 + r2));
  float vLst = v2;
  float v3 = v2 - vLst;

  Serial.println(v3);
}

The plot stays 0 because vLst = v2 means x =x and x - x = 0. I cant seem to find a way to get it working. What am i doing wrong?

vLst needs to be persistent between calls to loop() so that its' value is available the next time loop() executes.. either declared as a global outside of loop() or as "static float" inside loop()

gcjr:
vLst needs to be persistent between calls to loop() so that its' value is available the next time loop() executes.. either declared as a global outside of loop() or as "static float" inside loop()

Hi, thank you in advance for bearing with a beginner to this. I changed "float vLst = v2" to "static float vLst = v2" This gave me a voltage output but not the derivative. My coding knowledge is not very big so apologies for these constant questions.

float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;
void setup() {
  Serial.begin(9600);
   

   
}
 
void loop() {
  float v = (analogRead(0) * vPow) / 1024.0;
  float v2 = v / (r2 / (r1 + r2));
  static float vLst = v2;
  float v3 = v2 - vLst;
   
  Serial.println(v3);

This gave me a voltage output but not the derivative.

How do you know it's not the derivative? What are you expecting? What did you get?

can you post your results: v, v3, v2 and vLst

aarg:
How do you know it's not the derivative? What are you expecting? What did you get?

I currently have no coil to test with so i am using two batteries paralell to test. When the voltage is constant like on a battery the derivative should be 0. The plot displayed a constant voltage of 1.36v. I do not know where to find my v /v 2/ v3 / vLst results

Serial.print ("v ");
Serial.print (v);
Serial.print ("v2 ");
Serial.print (v2);
Serial.print ("v3 ");
Serial.print (v3);
Serial.print ("vLst ");
Serial.println (vLst);

The voltage produced by the coil will have both positive and negative swings, but the Arduino analog input cannot accept or measure negative voltages.

So, you need to "bias" the input to Vcc/2 or 2.5V for a 5V Arduino.

This is how it is usually done. In the schematic below, the "C.T. output" is your coil. C1 could be 10 uF, but the actual value is not critical.

Results:

Capture arduino resl.PNG

Capture arduino resl.PNG

Why do you post 11kB of picture, when you could post a few hundred bytes of text?

TheMemberFormerlyKnownAsAWOL:
Why do you post 11kB of picture, when you could post a few hundred bytes of text?

I blame the dumb serial monitor. It's hard to do a cut and paste while it's receiving. I often write in a "stop" if I know I'll want to capture the displayed data.

TheMemberFormerlyKnownAsAWOL:
Why do you post 11kB of picture, when you could post a few hundred bytes of text?

I got an error and all the data is the same anyway. I am currently testing with a battery with a constant voltage

Results:

...of what? Several revisions of code have been posted, maybe some you didn't post...

I am currently testing with a battery with a constant voltage

You can't test the derivative that way. It's always zero.

sorry. vLst needs to be set last, not before it is used in the calculation