This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Reading battery voltage

Hello Guys,

I am using NucleoF070RB board to read battery voltage using voltage sensor module. I read ADC can be started by three ways Poll Conversion, Interrupts, DMA. I found many examples regarding the Arduino but could not find about STM32 Nucleo boards. 

I do not have coding experience. I did the following.

int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t raw;
char msg[10];

/* USER CODE END 1 */

while (1)
{
// Test: Set GPIO pin high
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);

// Get ADC value
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
raw= HAL_ADC_GetValue(&hadc);

// Test GPIO pin low
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);

// Convert string and print
sprintf(msg, "%hu\r\n", raw);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);

My confusion is now how to intialise the condition for ADC resolution. 

Voltage divider Vin= Vout* R2/(R1+R2)  where R1 = 30K, R2 = 7.5K

ADC Resolution= 12 bit ADC = 4096 bits (0 to 4095)

Reference voltage = 3.3V (Microcontroller)

Vout= (Analog read x Reference voltage)/ 4096

I want to write the above formula in the code. But I am confusing how to write it. Can soome one help me

Thanks and Regards

Banala