#include <stdio.h>
#include <math.h> // for log()
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
float readTemperature(adc1_channel_t channel, float nominalRes, float B);
void app_main()
{
adc1_config_width(ADC_WIDTH_BIT_12); // pass appropriate argument
adc1_config_channel_atten (ADC1_CHANNEL_3, ADC_ATTEN_DB_11); // pass appropriate argument
while (1) {
float tempC = readTemperature(ADC1_CHANNEL_3, 10000, 3950); // pass appropriate arguments
printf("Current temperature is:%.2f°C\n",tempC); // complete this statement correctly
vTaskDelay(pdMS_TO_TICKS(500));
}
}
float readTemperature(adc1_channel_t channel, float nominalRes, float B) {
// setting it into celcius format
float T1 = 25 + 273.15;
// total resistance
float RT;
// Voltage
float VRT;
// current temerature
float T2;
float R2 = 10000;
//getting the raw data and setting it to adc_value
int adc_value = adc1_get_raw(channel);
int adc_10bit = adc_value >> 2;
//setting the voltage to be equal to the raw times 3.3 divided by max adc value for 10bit resolution
VRT = ( adc_10bit * 3.3f) / 1023;
//total resistance
RT = (R2 * VRT) / (3.3 - VRT);
//temperature
T2 = 1 / ( (1 / T1) + (logf(RT / nominalRes) / B));
//setting it to celcius
float celcius = T2 - 273.15;
vTaskDelay(1000 / portTICK_PERIOD_MS);
return celcius;
}