Vet ikke om dette hjelper deg dersom noen av dine includes ikke er supportert for ESP32, men esp32 er 12-bits ADC, Arduino er 10-bits. ESP32 vil returnere verdier fra 0 til 4095. ikke 0 til 1023:
R2 = R1 * (4095.0 / (float)Vo - 1.0);
en kjapp rewrite med openai:
Kode
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <driver/adc.h>
#include <esp_now.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Use an ADC1 channel connected GPIO pin for the thermistor
// Example: GPIO34 on ESP32 is ADC1_CHANNEL_6
#define ThermistorPin 34 // Update this to the actual GPIO pin number you're using
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
void setup(){
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // OLED address
display.clearDisplay();
Serial.begin(9600);
// Configure ADC resolution to 12 bits
analogReadResolution(12);
// Configure attenuation (if necessary)
// For example, to measure up to the maximum range (approx. 3.3V), use 11dB attenuation
analogSetPinAttenuation(ThermistorPin, ADC_ATTEN_DB_11);
}
void loop(){
// For ESP32-C3, consider using analogReadMilliVolts for direct millivolt reading
// This simplifies the calculation by providing a voltage reading directly
Vo = analogRead(ThermistorPin);
// Convert Vo to resistance (Ohm's Law) - adjust the calculation for 12-bit ADC
R2 = R1 * (4095.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15; // Convert Kelvin to Celsius
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(70, 0);
display.println("C");
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(3, 0);
display.println(Tc, 2); // Print with 2 decimal places
display.display();
delay(1000); // Adjust delay as needed
}