#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_log.h"
#define TAG "LM35"
// ADC channel for GPIO32
#define LM35_ADC_CHANNEL ADC_CHANNEL_4 // GPIO32
#define ADC_UNIT_USED ADC_UNIT_1
void app_main(void)
{
adc_oneshot_unit_handle_t adc_handle;
// ADC unit configuration
adc_oneshot_unit_init_cfg_t init_config = {
.unit_id = ADC_UNIT_USED,
.ulp_mode = ADC_ULP_MODE_DISABLE
};
adc_oneshot_new_unit(&init_config, &adc_handle);
// ADC channel configuration
adc_oneshot_chan_cfg_t channel_config = {
.bitwidth = ADC_BITWIDTH_12,
.atten = ADC_ATTEN_DB_11 // allows ~3.3V range
};
adc_oneshot_config_channel(adc_handle, LM35_ADC_CHANNEL, &channel_config);
while (1) {
int adc_raw = 0;
adc_oneshot_read(adc_handle, LM35_ADC_CHANNEL, &adc_raw);
// Convert ADC value to voltage
float voltage = (adc_raw / 4095.0) * 3.3;
// LM35 conversion (10mV per °C)
float temperature = voltage * 100.0;
ESP_LOGI(TAG, "ADC Raw: %d | Voltage: %.3f V | Temp: %.2f °C",
adc_raw, voltage, temperature);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}