Page 1 of 1

Measure resistance by digital GPIO

Posted: Tue Apr 20, 2021 11:32 am
by Fernando Perez
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

Re: Measure resistance by digital GPIO

Posted: Wed Apr 21, 2021 9:25 am
by Fernando Perez
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