#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
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);
int adc_value_12bit = 0;
int adc_value_11bit = 0;
int adc_value_10bit = 0;
int adc_value_9bit = 0;
float voltage = 0;
while (1) {
adc_value_12bit = adc1_get_raw(ADC1_CHANNEL_3); // Read raw 12-bit value
voltage = adc_value_12bit * 3.3 / 4095; // Vref = 3.3V
// Simulate lower resolutions by right shifting
adc_value_11bit = adc_value_12bit >> 1; // Divided by 2 for 11 bits
adc_value_10bit = adc_value_11bit >> 1;// Divided by 4 for 10 bits
adc_value_9bit = adc_value_10bit >> 1;// Divided by 8 for 9 bits
printf("ADC 12-bit: %4d | 11-bit: %4d | 10-bit: %4d | 9-bit: %4d | Voltage: %.2f V\n",
adc_value_12bit, adc_value_11bit, adc_value_10bit, adc_value_9bit, voltage);
vTaskDelay(pdMS_TO_TICKS(500));
}
}