subtracting analogue inputs

Very new to arduino programming and having difficulties creating a sketch that works for me.

I have an adruino uno in which I have placed two analogue inputs with varying voltages from 0-5 volts.

Using the analogRead code I have been able to read these voltages using the serial monitor.

If possible I would like to take both of my analogue inputs and subtract one from the other at certain intervals, I would then like to take this difference in voltage and display it.

Was hoping someone could help me out or tell if at all this operation is possible


Not sure if this is what you want, but why not try something like this:

Serial.println(analogRead(0) - analogRead(1));

Hi, welcome to this forum.

It is a job that the Arduino is good at. But if you ask 10 Arduino users, they will give 10 different ways to do that ;) Please show your sketch between code tags. It helps us a lot. Did you calculate the voltage from the output of analogRead() ? With a float number ? You can add delay(1000); at the end of the loop() function. It will wait for 1 second.

float v1 = (float) analogRead(A0) / 1024.0 * 5.0;
float v2 = (float) analogRead(A1) / 1024.0 * 5.0;
float result = v2 - v1;

Thank you for your response

The code works perfectly now, and yes I used a float voltage to get the sensor to covert into volts for the serial monitor.

As analog Reads affect each other you might need a double read per pin. Furthermore you can reduce the floating point math a bit (as it is slow)

Finally if you want the difference you should take the abs() of the 2 measurements.

analogRead(A0); // dummy
int val0 = analogRead(A0);
analogRead(A1); // dummy
int val1 = analogRead(A1);

float result = abs(val1 - val0) * (5.0 / 1023.0);
Serial.println(result, 4);  // 4 decimals iso 2