miguel5612 / MQSensorsLib

We present a unified library for MQ sensors, this library allows to read MQ signals easily from Arduino, Genuino, ESP8266, ESP-32 boards whose references are MQ2, MQ3, MQ4, MQ5, MQ6, MQ7, MQ8, MQ9, MQ131, MQ135, MQ136, MQ303A, MQ309A.
MIT License
169 stars 64 forks source link

"Equation_V_ADC" is wrong #59

Closed voblaunsane closed 1 year ago

voblaunsane commented 1 year ago

in the debug "Equation_V_ADC" displayed as

v = ADC * A0_max_voltage / 1023

but it should be

v = ADC * A0_max_voltage / 1024

Im using an ESP8266 dev board. The possible analogue pin values range from 0..1023. this amounts to a total of 1024 values!

miguel5612 commented 1 year ago

Hello,

Your observation is correct in the sense that there are 1024 possible values the ADC can take, ranging from 0 to 1023 (inclusive). However, the formula you mentioned:

v = ADC * A0_max_voltage / 1023

Is correct and is widely used when converting ADC readings to voltages. The reason why it's divided by 1023 instead of 1024 is because you're looking for a range that goes from 0 to A0_max_voltage. So, when the ADC reads 1023 (its maximum value), you want that to translate into the maximum voltage. If you divided by 1024, an ADC reading of 1023 would be slightly less than the maximum voltage, which wouldn't be correct.

As for your question about whether a different value can be set on the ADC, it will depend on the board and development platform you're using. Some microcontrollers allow you to configure the ADC resolution, which would change the range of values it can take. In the case of the ESP8266, the ADC resolution is fixed and cannot be changed. The ESP8266's ADC has a 10-bit resolution, which means it outputs 1024 (2^10) different values.

Can you provide me with an example of ADC whose minimum value is not zero to consider the change to 1024 in the library?

voblaunsane commented 1 year ago

you are right!