Basically what I am trying to archive is to get an average of the analog readings from A0 and convert it into millivolts value, what I get from this code is in 0.20V, what I would like to see is 200mV. I don't now if the code is right or not ( new to programming)and also some part of the code are for a PPG signal reading. looking for a stable analog voltage reading in mV. thank you
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
void setup() {
Serial.begin(9600); // initialize serial communication with computer
for (int thisReading = 0; thisReading < numReadings; thisReading++) { // initialize all the readings to 0:
readings[thisReading] = 0;
}
}
void loop() {
total = total - readings[readIndex]; // subtract the last reading
readings[readIndex] = analogRead(inputPin); // read from the sensor
total = total + readings[readIndex]; // add the reading to the total
readIndex = readIndex + 1; // advance to the next position in the array
if (readIndex >= numReadings) { // if we're at the end of the array...
readIndex = 0; // ...wrap around to the beginning
}
average = total / numReadings; // calculate the average:
Serial.print(average); // send it to the computer as ASCII digits
float voltage = readings[numReadings] * (5.0 / 1023.0);
Serial.print(" ");
Serial.println(voltage);
delay(10); // delay in between reads for stability
}
thank you for that, the issue now is that the values are fluctuating, ideally I have to pick one analog voltage representative of that persons finger. is that a way for that?
Your average (or total) value will keep increasing with the posted code. To reduce fluctuations in the readings, you would either use an average of multiple readings or you would "clamp" the readings into steps (eg. 5, 10, 15 etc.).
EDIT: I missed the first line in loop, average is good (thanks to UKHeliBob). You are just not using it to calculate the voltage, instead you are using a single reading.
analogRead() has the ability to detect 1/1023 of the reference voltage. So if the reference is 5v then the smallest reading will be about 5 millivolts. If the reference is 1.1v then the smallest reading will be about 1.1mV.
If you are trying to detect smaller voltages then you need to amplify the voltage that is passed to analogRead()
Robin2:
analogRead() has the ability to detect 1/1023 of the reference voltage. So if the reference is 5v then the smallest reading will be about 5 millivolts. If the reference is 1.1v then the smallest reading will be about 1.1mV.
If you are trying to detect smaller voltages then you need to amplify the voltage that is passed to analogRead()
...R
Correct. And if the reference is 6V, you create a very small silicon brick.
thanks for the replies, the problem now is because of the fluctuation of the analog voltage readings I don't know for to get one voltage reading for one person from the light passing through the finger coming in the diode.
In this code fragment, you say you subtract the last reading (first comment):
total = total - readings[readIndex]; // subtract the last reading
readings[readIndex] = analogRead(inputPin); // read from the sensor
total = total + readings[readIndex]; // add the reading to the total
readIndex = readIndex + 1; // advance to the next position in the array
Is that what's really happening? In loop(), if we enter and readIndex= 0, but at the bottom of the code fragment above readIndex is incremented to 1. It seems this means that the last reading is not subtracted. Indeed, if the current readingIndex is 2 and its incremented to 3, it is readings[0] that gets subtracted, not the last reading (readings[2]).
Also, you could write the code as:
total -= readings[readIndex]; // subtract the last reading
readings[readIndex] = analogRead(inputPin); // read from the sensor
total += readings[readIndex]; // add the reading to the total
readIndex++; // advance to the next position in the array
which is a common style for such code. However, yours is easier to read (document) and the compiler probably generates the same code, so there's no performance penalty.