arduino:divide-by-1023-or-1024
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
arduino:divide-by-1023-or-1024 [2025/06/20 12:48] – [Conclusion] Ilias Iliopoulos | arduino:divide-by-1023-or-1024 [2025/06/21 11:56] (current) – [Finally, to get the voltage from the reading of analogRead(), do we divide by 1023 or 1024? What do AI engines have to say?] Ilias Iliopoulos | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Finally, to get the voltage | + | ===== Finally, to get the voltage |
While I was working lately on a project to build a joystick using salvaged parts from an old RC transmitter https:// | While I was working lately on a project to build a joystick using salvaged parts from an old RC transmitter https:// | ||
Line 7: | Line 7: | ||
Discussion forums hosted several opinions, some purely theoretical, | Discussion forums hosted several opinions, some purely theoretical, | ||
- | As expected, AI machines are trained by the information they find online. One was in favor of 1024, explaining that this is the correct value, because it is consistent to the theory of the operation of Analog to Digital Converters. It also considered 1024 as the value that provides the highest precision, making this approach more applicable to operations that require high accuracy. Yet, asking the machine why some people are in favor of 1023, it took an neutral position, stating that the results do not differ so much. | + | As expected, AI machines are trained by the information they find online. One was in favor of 1024, explaining that this is the correct value, because it is consistent to the theory of the operation of Analog to Digital Converters. It also considered 1024 as the value that provides the highest precision, making this approach more applicable to operations that require high accuracy. Yet, asking the machine why some people are in favor of 1023, it took an neutral position, stating that the results do not differ so much and that although 1024 is " |
- | Another AI engine was absolutely and without any doubt in favor of 1023. After I insisted that 1024 would be a better option, it explained that the difference is subtle and provided a more detailed explanation. | + | Another AI engine was absolutely and without any doubt in favor of 1023. After I insisted that 1024 would be a better option, it explained that the difference is subtle and provided a more detailed explanation. |
So, if our fellow colleagues of the future would like to be informed on our final verdict, would they consider that it is just a matter of personal opinion? At the time that scientists are arguing on quantum mechanics, how can we leave such a trivial issue without a clear and precise answer? | So, if our fellow colleagues of the future would like to be informed on our final verdict, would they consider that it is just a matter of personal opinion? At the time that scientists are arguing on quantum mechanics, how can we leave such a trivial issue without a clear and precise answer? | ||
Line 16: | Line 16: | ||
Several schools of thought crossed their swords: | Several schools of thought crossed their swords: | ||
- | * The purists: //" | + | * The purists: //" |
* The believers: //"The manual says `A*Vref/ | * The believers: //"The manual says `A*Vref/ | ||
* The practical thinkers: //"The difference is so small that we should not bother"// | * The practical thinkers: //"The difference is so small that we should not bother"// | ||
Line 46: | Line 46: | ||
0x3FF is decimal 1023, so the very designers of the ADC inform us that the maximum digital value produced by the ADC is not expected to reach the reference voltage. Considering also that the +- 2 LSB accuracy, there is a whole range of voltages that may produce 1023. This proves that the very concept of considering 1023 as equal to the reference voltage is invalid. | 0x3FF is decimal 1023, so the very designers of the ADC inform us that the maximum digital value produced by the ADC is not expected to reach the reference voltage. Considering also that the +- 2 LSB accuracy, there is a whole range of voltages that may produce 1023. This proves that the very concept of considering 1023 as equal to the reference voltage is invalid. | ||
- | It is strange that lots of people, as well as their trained AIs have the wrong belief that the maximum ADC value should be equal to the voltage reference. Consider for example the most simple ADC with one bit. This is actually a comparator circuit. Assume that the threshold has been set to half of the reference voltage, such as 2.5 V for a Vref of 5 V. To make things simple, assume also that there is no hysteresis over the threshold voltage, In that case, digital 0 is the output of the ADC for every voltage between 0 V and 2.5 V. Digital 1 would be the ADC output for every voltage higher than 2.5 V. How is it possible that somebody would even think that 1 should be exactly 5V? For a 10-bit ADC, the voltage range is of course much, much smaller | + | It is strange that lots of people, as well as their trained AIs have the wrong belief that the maximum ADC value should be equal to the voltage reference. Consider for example the most simple ADC with one bit. This is actually a comparator circuit. Assume that the threshold has been set to half of the reference voltage, such as 2.5 V for a Vref of 5 V. To keep things simple, assume also that there is no hysteresis over the threshold voltage. In that case, digital 0 is the output of the ADC for every voltage between 0 V and 2.5 V. **Digital 1 would be the ADC output for every voltage higher than 2.5 V**. How is it possible that somebody would even think that 1 should be exactly 5V? A 10-bit ADC such as the one of ATMega328 implements multiple such comparison thresholds and the voltage range between two successive thresholds |
==== So, why some people are in favor of 1023? ==== | ==== So, why some people are in favor of 1023? ==== | ||
- | Because 1023 is correct, but to a different question. | + | Because 1023 would be the correct |
> // | > // | ||
Line 58: | Line 58: | ||
> // | > // | ||
- | This would be a totally valid question to someone who is building for example a digital display to somehow present the input voltage, or use it for other calculations. For most projects where the designer has already selected an ATMega328 as the appropriate and adequate analog to digital converter, a precision of 0.02 Volts and a resolution of two decimal digits may suffice. It is always the responsibility of the designer to estimate the maximum allowed error. | + | This would be a totally valid question to someone who is building for example a digital display to somehow present the input voltage. For most projects where the designer has already selected an ATMega328 as the appropriate and adequate analog to digital converter, a precision of 0.02 Volts and a resolution of two decimal digits may suffice. It is always the responsibility of the designer to estimate the maximum allowed error. |
- | Therefore, the question for which 1023 is the correct answer, has to do with the mapping of parameters and not with an absolute voltage level. The mapping of the analog to digital converter output | + | Therefore, the question for which 1023 is the correct answer, has to do with the **mapping** of parameters |
+ | |||
+ | **A most common application would be to convert 0-1023 to percentages from 0% to 100%**. After this conversion, you can manipulate the values any way you like. But, please, do not refer to abolute input voltages, unless you accompany your data with the proper documentation and validation. | ||
As a proof of thought, consider the '' | As a proof of thought, consider the '' | ||
Line 76: | Line 78: | ||
- | This shows where 1023 comes from. And also shows that there is no need to explicitly set a voltage unit (V) near the 5, for those who considered the coconut analogy a simple | + | This shows where 1023 comes from. And also shows that there is no need to explicitly set a dimension, such as a voltage unit (V) after the 5. Might as well be a (C) for coconut, for those who considered the coconut analogy |
==== Any comments on the various arguments in the online discussions? | ==== Any comments on the various arguments in the online discussions? | ||
- | It seems that arguments | + | Arguments |
- | Also, for the " | + | Yes, of course there are cases where we take decisions |
- | Induced errors should always be taken into account. | + | Induced errors should always be taken into account. |
==== Is it wrong to use 1023? ==== | ==== Is it wrong to use 1023? ==== | ||
- | It always depends on what exactly you want to do. If for example, your intention is to present your data to your user as a slider that goes from 0% to 100%, then it is a mapping problem and the proper value to use is 1023. But if you care about absolute values, why use 1023 when you know that the correct value is 1024? It is true that the difference between the results obtained using 1023 or 1024 is quite small. Actually, the practical thinkers would be happy to know that the value obtained using 1023 falls within the 1 LSB range between two successive ADC readings, so that actual difference is lower than the +-2 LSB absolute accuracy that the ADC provides. Yet, the small difference is not an excuse to knowingly do something wrong. | + | It always depends on what exactly you want to do. If for example, your intention is to present your data to your user as a slider that goes from 0% to 100%, then it is a mapping problem and the proper value to use is 1023 and not 1024. But if you care about absolute values, why use 1023 when you know that the correct value is 1024? It is true that the difference between the results obtained using 1023 or 1024 is quite small. Actually, the practical thinkers would be happy to know that the value obtained using 1023 falls within the 1 LSB range between two successive ADC readings, so that actual difference is lower than the +-2 LSB absolute accuracy that the ADC provides. |
In conclusion, you need to analyze your problem and most important, to define your acceptable margins of error. If your display presents a value that is not stable at the second decimal point, this does not mean that your ADC is crap. It is your responsibility to inform the users on what exactly they are observing. | In conclusion, you need to analyze your problem and most important, to define your acceptable margins of error. If your display presents a value that is not stable at the second decimal point, this does not mean that your ADC is crap. It is your responsibility to inform the users on what exactly they are observing. | ||
Line 94: | Line 96: | ||
Going back to the Arduino '' | Going back to the Arduino '' | ||
- | I would also like to point out that there are other mapping functions that you can use in your application, If you do not care about going to the extremes of 0% and 100% of the scale, you may consider the '' | + | I would also like to point out that there are other mapping functions that you can use in your application. If you do not care about going to the extremes of 0% and 100% of the scale, you may consider the '' |
==== So, is AI both right and wrong? ==== | ==== So, is AI both right and wrong? ==== | ||
Line 100: | Line 102: | ||
I believe that it has been proven beyond doubt at the discussion above, that you get correct answers only if you ask the right questions. | I believe that it has been proven beyond doubt at the discussion above, that you get correct answers only if you ask the right questions. | ||
- | > __"Go back to the description of the problem" | + | > __"If you cannot find the solution |
- | If you obtain a better understanding of the nature of the problem and even re-phrase it in a way that the new expression exposes the parameters that govern the problem, you will have many more chances to solve it. | + | If you obtain a better understanding of the nature of the problem and even re-phrase it in a way that the new expression exposes the parameters that govern |
Same with AI. I purposely did not present here the questions I submitted to the AI engines and their exact answers, since the phrasing of the question produced a totally different result. | Same with AI. I purposely did not present here the questions I submitted to the AI engines and their exact answers, since the phrasing of the question produced a totally different result. | ||
~~DISQUS~~ | ~~DISQUS~~ |
arduino/divide-by-1023-or-1024.1750412914.txt.gz · Last modified: 2025/06/20 12:48 by Ilias Iliopoulos