Blackpill's adc read error when trying to read an NTC

30 Views Asked by At

Devboard: Blackpill WeAct v3.0
MCU: STM32F401CEU6
IDE: Platformio
Core: STM32duino

I'm trying to rean an NTC using a simple voltage divider with a capacitor for filtering as shown in the circuit diagram:

the circuit diagram

The code flows as:
1- Set temp_outpin as HIGH
2- Read the adc value in temp_inpin
3- Process and save the adc value
4- Set temp_outpin as LOW

  digitalWrite(_outPin, HIGH);
  float accumulated = 0;
  for (int i = 0; i < iterations; i++)
  {
    int adc = analogRead(_inPin);
    float rt = ((adc * rs) / ((pow(2, resolution)) - adc));
    rt = rt / r0;
    rt = log(rt);
    float temp = beta / (rt + beta / t0);
    temp = temp - 273.5;
    accumulated += temp;
  }
  digitalWrite(_outPin, LOW);
  temperature = accumulated / iterations+ offset;

The code works perfectly if I let the _outpin as HIGH forever. Just adding the line "digitalWrite(_outPin, LOW);" makes the reading erroneous. I tried putting delays (from 1 to 100 miliseconds) after setting as HIGH the pin and before and after setting it LOW.
I could let it HIGH continously, but that comes with an energy cost I prefer not to take.
Thanks for the help beforehand.

1

There are 1 best solutions below

3
Harvid On

Well, it was all solved with a delay. The error came from the charging time of the capacitor. In this case the RC constant is 100ms, so I put a delay bigger than 100 after setting HIGH the pin, and it worked perfectly.