#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "math.h"
#include "driver/adc.h" //Required for ADC functions.
void app_main()
{
adc1_config_width(ADC_WIDTH_BIT_12); // Set 12-bit ADC resolution (0–4095)
// GPIO4 is ADC1 channel 3, input voltage up to ~3.3V
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
//Declaring variables and assigning values to them.
float VRT = 0.0f;
float T1 = 25 + 273.15;
float T2;
float R = 10000.0f;
float R2 = 10000.0f;
float B = 3950.0f;
float RT;
float celsius;
while (1) {
int adc_value_12bit = adc1_get_raw(ADC1_CHANNEL_3); // Read raw 12-bit value and store it into a variable.
VRT = adc_value_12bit * 3.3f / 4095; // Vref = 3.3V
RT = R2*VRT/(3.3-VRT);
T2 = 1 / ((1/T1) + (logf(RT/R)/B));
celsius = T2 - 273.15; //Calculation to get the value in celsius.
vTaskDelay(pdMS_TO_TICKS(500));
printf("\nADC Value: %d | VRT: %.2f | Resistance: %.2fΩ | Current Temperature: %.2f°\n", adc_value_12bit, VRT, RT, celsius);
}
}