Compiled RAK811 ADC RUI example and downloaded successfully
Firmware Upgrade Tool used v1.4
Test #1
Wired the pin 2 to the center point of the battery series. 2xAA Alkaline batteries. Total voltage measured with voltmeter 3.064. Each battery measured with voltmeter 1.532 and 1.533
Result of the output log via serial
The voltage at pin 2 is 1650 mV.
The voltage at pin 2 is 1650 mV.
The voltage at pin 2 is 1650 mV.
The voltage at pin 2 is 1650 mV.
The voltage at pin 2 is 1625 mV.
Test #2
Wired the pin 2 to the center point of the battery series. 2xAA NiMh rechargeable batteries. Total voltage measured with voltmeter 2.699. Each battery measured with voltmeter 1.347 and 1.351
The voltage at pin 2 is 1645 mV.
The voltage at pin 2 is 1645 mV.
The voltage at pin 2 is 1645 mV.
The voltage at pin 2 is 1645 mV.
The voltage at pin 2 is 1645 mV.
From the results i can conclude that the ADC code is assuming VREF=VCC=3.3 V. So the code needs clarification that it will only produce usable results when powered with a battery with a voltage superior to 3.3v and a voltage regulator is used.
Conclusion:
The ADC RUI code can’t be used on RAK811 powered directly from batteries.
FWIW typically the way you make an MCU ADC give you the right answer is to measure your input voltage relative to the unknown power supply, then measure the chip’s bandgap reference also relative to that supply, and then do the proportion.
The bandgap reference is typically one of the ADC mux inputs, and often has to be explicitly enabled as it consumes some power - you’d turn it off again afterwards.
Implicit in there is that you can measure your supply voltage, too.
It would be great if RUI supports this, if not you can probably manipulate the necessary registers to make it happen directly.
Feedback from RUI development team (same answer as on double posted issue on Discord)
In RUI V2 you cannot access to HAL to get the current Vref value. You would need Vref to calculate the measured voltage. Extract from STM32L151 manual:
No battery voltage is steady over discharge life - and even at a given point on the curve, the voltage will vary under load.
One can directly manipulate the existing hardware of the MCU ADC and Vref even though RUI doesn’t support it; of course the non-extensible design of RUI makes that a lot harder than it should be, in needing to go look up register values rather than simply call the underlying ST libraries one normally could. A scheme more like MBED’s where one can build online, or export everything needed to build offline is more flexible than fixing issues one at a time, and more able to meet the needs of professional customers who aren’t allowed to depend on an online compiler.
Accessing and using the calibration value of the internal bangap reference is a further refinement beyond the basic step of measuring the internal bandgap reference against the supply. Either way, once you have a “good enough” value for the current VDDA from that, then you can correct the measurement of the unknown voltage against it.
Totally agree. Yo gave me the answer: RUI isn’t the tool that fill my needs. Switching to STM SDK(or maybe MBED?), more pain, but much much more control