INTRODUCTION
For this project, the goal is to create a very simple Volt-Meter, with at least good accuracy and to understand how Analog-to-Digital Converter (ADC) works. This uses an RT-Spark (Spark-1) Development Board from the RT-Thread company.
ANALOG-TO-DIGITAL CONVERTER (ADC)
ADC is type of peripheral device, meaning, a type of module/sub-system which has its own purpose, and is typically connected/integrated to the bigger system with the goal of converting an analog signal to digital signal, hence the name ADC.
For this scenario, the ADC is an integrated peripheral device of the RT-Spark (Spark-1) Development Board, which uses the STM32F407ZGT6 Microcontroller.
SETTING UP THE ADC
To start off, it is an advantage to know that the RT-Spark (Spark-1) has 4 dedicated converters, 3 technically the same with different channels, and one as Digital-to-Analog Convert (DAC). For this scenario, 1st ADC will be used for the sake of simplicity. The ADC digital value range is 0-4095, and you need to map the values to Voltage by code.
First, create a new STM32 Project, choose the STM32F407ZGT6 MCU, and let the default settings work by itself.
Then, setup pin PA2 to use ADC Module 1 on channel 2, and generate the CubeMX code. Note, you do not need to touch the GPIO Configuration of pin PA2; simply, keep it on default settings.
INPUT CIRCUIT DIAGRAM
For this section, the idea of voltage-divider will be used for a few reasons. One is to prevent loading voltages and currents beyond the maximum/acceptable values of the RT-Spark's pins or else it'll break/blow up the board by halving what the input gets. 2nd is to reduce the amount of power usage from the battery when checking the voltage by lowering the current passing through.
Once done, update the code, then build and run. After that, access debugging mode instead of running the code to check the value of the rawValue variable.
ENHANCED VOLTAGE READING ACCURACY (OPTIONAL)
To further improve the voltage reading, simply measure the actual resistance of R1 and R2 using a multimeter that can achieve a resistance reading of 1MΩ, and use the following formula






_circuit_diagram_JuT3oNykw7.png)


Comments