#include "esp_adc_cal.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// --- Configuracion TFT ILI9341 ---
#define tft_SLCK 18
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
// Instancia del TFT (Hardware SPI usa pines por defecto MOSI, SCK, MISO)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// --- Configuracion Sensor PT1000 ---
#define PT1000_Pin 35 // Pin ADC (GPIO 35)
int PT1000_Raw = 0;
float PT1000_TempC = 0.0;
float Voltage = 0.0;
// --- Prototipo de funcion de calibracion ---
uint32_t readADC_Cal(int ADC_Raw);
void setup() {
Serial.begin(57600); // Velocidad segun ejemplo
// setCpuFrequencyMhz(240); // Opcional
// 1. Inicializar Pantalla TFT
tft.begin();
tft.setRotation(1); // Paisaje (Landscape)
tft.fillScreen(ILI9341_BLACK);
// Texto de bienvenida
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Sistema PT1000");
tft.println("Iniciando...");
delay(1000);
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
// 2. Leer pin ADC del sensor
PT1000_Raw = analogRead(PT1000_Pin);
// 3. Calibrar y obtener Voltaje (en mV)
Voltage = readADC_Cal(PT1000_Raw);
// 4. Calcular Temperatura
// Formula deducida: Temp = Voltage(mV) / 8
PT1000_TempC = Voltage / 8.0;
// 5. Imprimir en Monitor Serial
Serial.print("Voltaje: ");
Serial.print(Voltage);
Serial.print(" mV -> Temp: ");
Serial.print(PT1000_TempC);
Serial.println(" C");
// 6. Mostrar en Pantalla TFT
// Nota: Para evitar parpadeo se recomienda borrar solo el area de texto
// o usar colores de fondo en el texto, aqui usamos fillScreen simple.
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setTextSize(2);
tft.setTextColor(ILI9341_CYAN);
tft.println("Sensor PT1000");
// Mostrar Temperatura Grande
tft.setCursor(10, 40);
tft.setTextSize(3);
tft.setTextColor(ILI9341_YELLOW);
tft.print("T: ");
tft.print(PT1000_TempC, 1); // 1 decimal
tft.println(" C");
// Mostrar Voltaje
tft.setCursor(10, 100);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print("V: ");
tft.print(Voltage, 0);
tft.println(" mV");
delay(3000);
}
// --- Funcion Auxiliar de Calibracion ---
uint32_t readADC_Cal(int ADC_Raw)
{
esp_adc_cal_characteristics_t adc_chars;
// Caracterizar ADC1 a 11dB (rango completo 3.3V aprox)
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11,
ADC_WIDTH_BIT_12, 1100, &adc_chars);
// Convertir lectura RAW a milivoltios calibrados
return(esp_adc_cal_raw_to_voltage(ADC_Raw, &adc_chars));
}