First analogRead pin always reads full power each loop iteration.

haddow777
Posts: 14
Joined: Tue Jul 13, 2021 3:25 pm

First analogRead pin always reads full power each loop iteration.

Postby haddow777 » Tue Aug 10, 2021 10:29 am

Hello. I have just run into a very weird problem. I have found a very poor solve for it, but I would rather not cheat a solve and understand why things are working the way they are.

I am writing a program that includes reading the values from 7 thermistors. I am using a library called SmoothThermistor, which reads each sensor 10 times over a period of time to smooth out the jitter the analog inputs have. Basically, all it does is analogRead for the same pin 10 times with a small delay between each interval.

Now, I have my main program loop that includes this bit of functionality. This is the loop the issue arrises in. I have 7 SmoothThermistor objects stored in an array. Once per main loop iteration, I call a method called readTemps. This method starts a loop that iterates through the array of SmoothThermistors.

The problem I had was, every main loop iteration, the first sensor was always reading -273 degrees C. Upon investigation, I found that the analogRead was returning 4095 every read for that first pin. As soon as my array loop moved onto the next loop, it would read properly. I thought that maybe I had things hooked up improperly. I changed the sensor, the wires, the resistor, and capacitor (used to further smooth the value). Nothing worked. I changed the pin, didn't matter.

I eventually realized that it was always the first pin read per main loop iteration. Also, it didn't matter how many times that pin was read, it always returned that max value. So, my cheater fix is to initially start the method with a analogRead to an unused pin. Then, all my thermistors return the correct values. How is this possible?

I should also add, that I tested running the program with nothing connected to the first read pin. In these cases, it seemed to return random values between 0 and 4095. I would assume that it would return 0, but I can't fully remember if nothing being connected affected the analogRead function.

Any help or insights would be greatly appreciated. Also, I am writing this here as I'm using analogRead, which is an Arduino method as far as I know. Let me know if this sounds more like a general discussion topic. Thank you.

haddow777
Posts: 14
Joined: Tue Jul 13, 2021 3:25 pm

Re: First analogRead pin always reads full power each loop iteration.

Postby haddow777 » Tue Aug 10, 2021 10:31 am

Also, I should note that I am using a NodeMCU Esp32 dev board. I'm also using arduino-esp32 2.0.0-rc1 in PlatformIO. Also, I tested the code on two different Esp32's just to be sure it wasn't some sort of hardware issue.

Who is online

Users browsing this forum: No registered users and 80 guests