September 2019 Update

New! We're moving to the new forum!

ALL new Blog posts will be on the forum.

Please join us there.

This Blog will no longer be updated...

Friday, August 9, 2019

Sensor detection conclusion?


Here is my code for testing a sensor...



It will detect a small difference when the sensor is not connected, however, that number can fluctuate depending on the value returned from a floating pin. This makes it difficult to pick a magic number to use as a cutoff... Certainly if the number is 0, then the sensor reads exactly the same with power and without, so there must not be a working sensor there. Because of the fluctuations, this number ranges from 0 to around 10 or so that I see regularly.  I had the cutoff set to 5.  Now, even 5 could be valid, depending on the sensor.   Let's look at a soil moisture sensor in very dry soil...  Read the sensor without power, let's say the random fluctuating reading is 245. So we apply power, and because the soil is really dry, it reads 250. The difference is 5, yet there IS a working sensor, and if we ignore this, we may not water the plant, and it dies...

That was the main reason I enabled the internal pull up resistors for each analog port, so that it would stabilize the pins when they are not connected, they will always read very close to 1024.
In the setup() method, I set the pinmode of all analog pins to INPUT_PULLUP, and the bad sensor detection reliabiolity improved drastically, in fact, foolproof...  

almost...

Globally setting this pinmode fixed the sensor testing, but I noticed it also influenced the actual sensor readings on valid sensors.  I'm not well versed in electronics, but assume the resistance of the sensor is too high to give the electrons reason to go there instead of through the pull up. I'm not even sure if that makes sense?  A pull-up resistor ties the analog pin to vcc via a resistor. With nothing else connected, we will see the 5v, and a very high number when we read the sensor.  When the sensor is plugged in, and working, it has a resistance, this is what generates the reading we get, but if this resistance is higher than the pull-up, current will continue to flow through the pull-up. We need a lower resistance so the current flows through the sensor instead, giving us an accurate reading. I suspect that current is flowing though both, and this is skewing the sensor readings. Ohms law, two 10K Ohm resistors in parallel equals 5K Ohm, less resistance, higher reading...

You get the gist, even if technically I'm not very accurate...

This modified value would be fine, but I'd have to re-calibrate every sensor, and I'd rather avoid that, so....

I removed the pinMode settings in setup() and just before the above code to test the sensor, I added the following line:

pinMode(54 + sensorCfg[sensorIndex].analogPin, INPUT_PULLUP);
and just after the above code, I added:

pinMode(54 + sensorCfg[sensorIndex].analogPin, INPUT);

The hope was it would enable the pull-up for the test, then disable it for the regular reads... It almost appears setting it back to INPUT has no effect, the next sensor readings are also very high, not floating values like I would expect... so instead, I flipped it to output and back to input again to see if that helped...

pinMode(54 + sensorCfg[sensorIndex].analogPin, OUTPUT);
pinMode(54 + sensorCfg[sensorIndex].analogPin, INPUT);


That didn't make any difference either.


Aha! I think I may have solved it!

There is one more thing I didn't try, someone on a forum suggested that doing a digitalWrite LOW to the analog port would disable the internal pull-up resistors.

So, armed with that bit of knowledge, I created a test matrix:


The green line shows the correct way to do this, hopefully. Local limited testing seems to work ok, now I'll install it on a live module and test with real data.

Everything seems to be working properly, and for each sensor, I checked the database records to look at the sensors value over the past hour or so, and it did not change abnormally, so I'll leave it run for a couple hours and watch it...

The code now looks like:
1867047


I am sort of puzzled though, why are the readings 0 now when before with the pull up enabled, they were near 1024?  I haven't wrapped my head around that yet, and I should, because if I don't understand why it changed, I may miss out on finding a bug...

I'll post an update right here when I know...


No comments:

Post a Comment

Any comments deemed off topic or offensive will be removed