Friday, December 1, 2017

Analog Pull-Down resistor

We all realise the importance of the pull-up resistor on a digital input pin.
Without a pull-up resistor the initial state of a digital pin is floating and we never will get an accurate reading.

However I never realised how important the pull-up resistor is in an analog circuit. And now I understand it fully I can give you an excellent simple example that demonstrates how valuable such a pull-up resistor is.

Normally an Arduino or an ESP-8266 can read it's analog input and gives it a value between 0 and 1023.
Lets see what happens when we do not use a pull-down resistor and attach an LDR directly to the analog input. In this example I am using an ESP-8266.


Look at the breadboard. The LDR is on one side connected to 3.3 Volts and the other side directly to the analog input of the NodeMCU.






timer 100,[test]

wprint |<h1 style="text-align:center;">Luc Volders</br>Light Tester</br>|
wprint "<br/>"
textbox value
wprint "<br/><br/>"
button "<h2>Off</h2>", [Off]
wprint "<br/>"
wait

[test]
sensor =  io(ai)

value = sensor
wait

[Off]
end


So I wrote a small BASIC program that just reads the analog input port and displays it on a web-page.




When fully exposed to the light the analog reading was 958. When fully covered the reading was 984. Well it kind of works however there is not a lot margin for error. So this is not very usable.

I started searching the net for an LDR setup and indeed there were many examples. They all used a 10K pull-down resistor.



I altered my breadboard setup and attached a 10K pull-down resistor. And indeed it worked.








The value I measured when the LDR was in full light was 527 and when covered with my finger I measured 933. Now this is a much wider range so there is a wider range measurable when the LDR is just partly covered. However it was still not using the full range.


AXEL BENZ FORMULA




Then I found the  Axel-Benz formula.
The Axel-Benz formula is honorably named after one of the first teachers of Physical Computing at the FH Postdam.

How does this formula actually works out.

The formula R-ref = SQR (R-min * R-max) tells us that the value of the of the pull-down resistor should be the Square Root of the (minimal resistance * maximum resistance).

Lets start with measuring the minimal and maximum resistance. We'll do that using our trusted multimeter.





As you can see the value I measured when the LDR was exposed to full light was 420 Ohm. When I covered the LDR with my finger I measured a value of 18.500 Ohm. Now I would have expected a much higher resistance when the LDR was covered so there might be some light leaking around my finger.

Now let us use these figures in the formula.
The formula is: R-REF = SQR (MINIMAL * MAXIMAL)

Lets substitute our figures into the formula:
R-REF = SQR (420 * 18500)
R-REF = SQR (7770000)
R-REF = 2787

This gives us a resistor vaule of 2787 OHM. The nearest by existing value that we can use is a resistor of 2.7K


Therefore I substituted the 10K resistor on the breadboard to a 2.7K version.








And the result is fantastic. The measured values now vary between 257 and 847. This a much broader range in which we can measure more accurately the amount of light that is exposed onto the LDR.

Arduino test


int sensorPin = A0;   
int sensorValue = 0;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  sensorValue = analogRead(sensorPin);
  delay(sensorValue);
  Serial.println(sensorValue);
}

The above experiment is done with an ESP-8266. But how would it work on an Arduino. Well here is the code I used for measuring the Analog port.


I measured the value on port A0.



And here you can see that the values I achieved are almost the same as the ESP gave.

Is everybody Stark Raving Mad then ???

Why on earth is everyone using a 10K resistor then. Has the world gone mad. Well actually yes it has. Just look around you. however not concerning this  particular case.

When you have a full bright light the LDR will have a value of about 0 to 10 OHM. In absolute adrkness it will be around 10000000 (10 meg). Now if we use these values in the formula it will work out as follows:

R-REF = SQR (10 * 10000000)
R-REF = SQR (100000000)
R-REF = 10000

And there we have the 10K value.

However as we saw in the beginning of this story this value did not work out well in this particular case.

So the lesson learned is to always calculate the best value for the project you have at hand.

Till next time
Have fun

Luc Volders