## Measure resistance by digital GPIO

If doesn't fit into any other category ....
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

### Measure resistance by digital GPIO

image.png
I have assembled this circuit and I am trying to reproduce Pic Basic's POT instruction. The LDR value ranges from 1K to 30K ohms. The electrolytic capacitor is charged through the LDR and discharged through the 220 ohm resistor by GPIO1 (TXT) of the ESP-01. The results are satisfactory, the readings being quite stable, with values of 1 in bright light, 6 in ambient light and 35 in the dark.
But every so often, the variable t returns the actual milliseconds. Do any of you have experience with this type of montage? Can you advise me on the most suitable values for capacitor and resistor?

Code: [Local Link Removed for Guests]

t = 0
pin.mode 1, input
interrupt 1, charged
timer0 5000, discharged
wait
END

charged:
if pin(1) = 0 then return
t = millis - t
wlog t
return

discharged:
pin.mode 1, output
pin(1) = 0
pause 100
t = millis
pin.mode 1, input
return
You do not have the required permissions to view the files attached to this post.
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

### Re: Measure resistance by digital GPIO

I have changed the discharge resistor to 330 ohms to limit the current through GPIO1 to 10 mA.
I have simplified the code and now it seems to work correctly.
Still, if anyone can help me improve it, I will be grateful.

Code: [Local Link Removed for Guests]

wlog "Start measures"
t = 0
timer0 10000, measure
wait
END

measure:
pin.mode 1, output
pin(1) = 0
pause 15
pin.mode 1, input
t = millis
while pin(1) = 0
wend
t = millis - t
wlog t; " at "; time\$
return