/* Includes ------------------------------------------------------------------*/ #include "main.h" * Private variables ---------------------------------------------------------*/ ADC_HandleTypeDef hadc1; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ uint16_t readValue; uint8_t MSG[100]; float sensitivity = 0.185; //0.185 for 5A module => datasheet float actual_voltage; //0A -->Vcc*0.5 = 5*0.5 = 2.5V => datasheet float current ; float Input_votage_range =3.3; //3.3v stm32 int ADC_range = 4095; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_ADC1_Init(void); static void MX_USART1_UART_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ if(HAL_ADC_Start(&hadc1)!=HAL_OK) { Error_Handler(); } /* USER CODE END 2 */ while (1) { while(HAL_ADC_PollForConversion(&hadc1, 1000)!=HAL_OK) { Error_Handler(); } readValue= HAL_ADC_GetValue(&hadc1); //if acual_voltage is not 2.5 V => multiply by factor in my case it is 17.54 actual_voltage= (float) ((readValue* Input_votage_range)/ADC_range)*17.54; current=(actual_voltage-2.5)/sensitivity; sprintf(MSG,"readValue = %d || actual_voltage = %.2f || current value = %.2f\n\r", readValue,actual_voltage,current); HAL_UART_Transmit(&huart1, MSG, sizeof(MSG), 200); HAL_Delay(1000); } }