Need help with thermistor temp reading on: ESP32C3 xiao

hi. I have made an thermistor temp reading that shos temp on a oled, it works great on arduino uno, only 5 wires in total, but then i bought an ESP32C3, and now it dosent work, i have been reading and asking people for days now, no one is good enough in esp to help :frowning:
only thing i get on the display is: -273 blinking like hell...

here is my code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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);

int ThermistorPin = 0;
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);
}

void loop(){
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
Tc = T - 273.15;

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);
display.display();
delay(10);
}

1 Like

Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Topic ahs been moved.

Please edit your post, select all code and and apply so-called code tags by clicking the <CODE/> button. Next save your post.
This makes it easier to read, easier to copy and the forum software will maintain the formatting.

The esps have a 12 bit ADC so dividing by 1023 is wrong.

Change
Vo = analogRead(ThermistorPin);
To
uint32_t Vo = analogReadMillivolts(ThermistorPin);

Change
R2 = R1 * (1023.0 / (float)Vo - 1.0);
To
R2 = R1 * (((3.3*1000.0)/ float(Vo))-1.0);

1 Like

thanks for reply, it worked on arduino uno? so shouldn't it work on esp?
When i change the sentence: uint32_t Vo = analogReadMillivolts(ThermistorPin);
I get error: Compilation error: 'analogReadMillivolts' was not declared in this scope; did you mean 'analogReadMilliVolts'?

Which board do you select when you compile and upload the sketch?

xiao_esp32c3

Try this sketch

void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  
  //set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
}

void loop() {
  // read the analog 
  int analogValue = analogRead(A0);
  int analogVolts = analogReadMilliVolts(A0);
  
  // print out the values you read:
  Serial.printf("ADC analog value = %d\n",analogValue);
  Serial.printf("ADC millivolts value = %d\n",analogVolts);
  
  delay(500);  // delay in between reads for clear read from serial
}

Use A0 for the pin number

like this:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <driver/adc.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);

int ThermistorPin = 2;
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() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  
  //set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
}

void loop() {
  // read the analog 
  int analogValue = analogRead(0);
  int analogVolts = analogReadMilliVolts(0);
  
  // print out the values you read:
  Serial.printf("ADC analog value = %d\n",analogValue);
  Serial.printf("ADC millivolts value = %d\n",analogVolts);
  
  delay(500);  // delay in between reads for clear read from serial
}

Now the display is only solid showing: "nan c"

I trying to find out why it does not like analogReadMillivolts.
Use the sketch exactly as I show in post #9
Check the readings on the serial monitor

yes it's strange :S, now i lost the screen..

it's like such an easy thing to do in arduino, made it in like 10-15 min, but switch over to the xiao_esp32c3 and everything is messed up xD

There is no screen in the sketch.
So it did compile with analogReadMillis(A0)
What does the serial monitor show?

ADC analog value = 3565

ADC millivolts value = 2456

ADC analog value = 3550

ADC millivolts value = 2456

ADC analog value = 3551

ADC millivolts value = 2457

So the analogReadMillivolts is working.
You must have made a typo when you first tried it.
So your original sketch with the modifications I suggested should compile and work
Try it again

this is what i have now:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <driver/adc.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);

int ThermistorPin = A0;
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(115200);
  
  //set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
}

void loop(){
  int analogValue = analogRead(A0);
  int analogVolts = analogReadMilliVolts(A0);
  
  // print out the values you read:
  Serial.printf("ADC analog value = %d\n",analogValue);
  Serial.printf("ADC millivolts value = %d\n",analogVolts);
  
  delay(500);  // delay in between reads for clear read from serial
  R2 = R1 * (((3.3*1000.0)/ float(Vo))-1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;

  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);
  display.display();
  delay(10);
}

Now it's just showing "-273,15" on display :S

I copied and pasted the line: uint32_t Vo = analogReadMillivolts(ThermistorPin);

so I couldnt made an typo

the word volts was with a smal "v" xD but now i could upload this code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <driver/adc.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);

int ThermistorPin = A0;
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(115200);
  uint32_t Vo = analogReadMilliVolts(ThermistorPin);
  //set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
}

void loop(){
  int analogValue = analogRead(A0);
  int analogVolts = analogReadMilliVolts(A0);
  
  // print out the values you read:
  Serial.printf("ADC analog value = %d\n",analogValue);
  Serial.printf("ADC millivolts value = %d\n",analogVolts);
  
  delay(500);  // delay in between reads for clear read from serial
  R2 = R1 * (((3.3*1000.0)/ float(Vo))-1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;

  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);
  display.display();
  delay(10);
}

But still: just blinking -273

Try this code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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);

int ThermistorPin = A0;
int Vo;
float R1 = 10000.0;
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);
}

void loop() {
  Vo = analogReadMilliVolts(2);
  R2 = R1 * (((3.3*1000.0)/ float(Vo))-1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
  Tc = T - 273.15;

  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);
  display.display();
  delay(10);

now i got: Compilation error: 'c2logR2' was not declared in this scope; did you mean 'logR2'?