Dtynin
Dtynin
DIIDevHeads IoT Integration Server
Created by Dtynin on 6/27/2024 in #middleware-and-os
working on a project that involves monitoring temperature using an LM35 sensor with an STM32F4
// Function to read temperature sensor using ADC library function
uint16_t read_temperature_sensor() {
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0; // Assuming the sensor is connected to ADC channel 0
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
// Channel configuration error
printf("ADC Channel Configuration Error\n");
return 0;
}

if (HAL_ADC_Start(&hadc1) != HAL_OK) {
// ADC start error
printf("ADC Start Error\n");
return 0;
}

if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) != HAL_OK) {
// ADC conversion error
printf("ADC PollForConversion Error\n");
return 0;
}

uint16_t raw_adc_data = HAL_ADC_GetValue(&hadc1);

if (HAL_ADC_Stop(&hadc1) != HAL_OK) {
// ADC stop error
printf("ADC Stop Error\n");
}

return raw_adc_data;
}
// Function to read temperature sensor using ADC library function
uint16_t read_temperature_sensor() {
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0; // Assuming the sensor is connected to ADC channel 0
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
// Channel configuration error
printf("ADC Channel Configuration Error\n");
return 0;
}

if (HAL_ADC_Start(&hadc1) != HAL_OK) {
// ADC start error
printf("ADC Start Error\n");
return 0;
}

if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) != HAL_OK) {
// ADC conversion error
printf("ADC PollForConversion Error\n");
return 0;
}

uint16_t raw_adc_data = HAL_ADC_GetValue(&hadc1);

if (HAL_ADC_Stop(&hadc1) != HAL_OK) {
// ADC stop error
printf("ADC Stop Error\n");
}

return raw_adc_data;
}
12 replies