Solved: Tufty light sensor

I’m following the Tufty 2040 getting started learn guide.
Using the as-delivered micropython installation.

The lux sensor doesn’t seem to be doing anything useful. Not really important for me, but just wondering.

from machine import ADC
from time import sleep

lux = ADC(26)

while True:
    reading = lux.read_u16()
    print(reading)
    sleep(1)

There’s little or no reaction to me putting my finger over the sensor or shining my ‘blinding’ smartphone torch LED at it. It bobbles around 288 or 272.
If I try using pin 27 instead, I get more of a reaction, it goes from 720 (dark) to 416 (bright white LED). So it works, albeit ‘upside-down’.
Any ideas, what I’m, or what the hardware/software could be doing wrong?

Just tried this on my Tufty:
Max, Min Ave range

ADC(26)
256 208 236.0 range: 48

ADC(27)
720 320 609.12 range: 400

ADC(28)
672 608 642.88 range: 64
Calculating Mix, Min and average

from machine import ADC
from time import sleep

lux = ADC(27)
maxx = 0
minn = 200000
total = 0
for i in range (100):
    reading = lux.read_u16()
    if reading < minn: minn = reading
    if reading > maxx: maxx = reading
    total = total + reading
    print(reading)
    sleep(0.3)
print(maxx, minn, total/100)

As you say, pretty insensitive and appears inverted with ADC 27 appearing best. This should be connected to ‘sensor power’.

Lightsensor

Something strange here.

According to this, lcd_badger.sch (shopify.com)
It’s connected to GPIO26 / ADC0
It’s a photo transistor. The higher the intensity of light is that hits it, the more it conducts.
If I read the schematic correctly, the voltage to ADC0 should increase as the light level increases.

Yes, thanks, we already know that, Tony posted a part of the schematic above.
What we don’t know is, why isn’t it working properly?
It really doesn’t seem to be all that useful as an ambient light sensor.

I’m thinking GPIO 27 needs to be set high to enable the sensor. Then use ADC0 to read it. I didn’t see an example file for it?

That’s it, as hinted in the schmatic Pin 27 needs to be set high and then reading the ADC value of Pin 26 works correctly!

The “LIGHT SENSING” example in the getting started with Tufty 2040 needs to be updated.
https://learn.pimoroni.com/article/getting-started-with-tufty-2040

from machine import ADC, Pin
from time import sleep

lux_pwr = Pin(27, Pin.OUT)
lux_pwr.value(1)

lux = ADC(26)

while True:
    reading = lux.read_u16()
    print(reading)
    sleep(1)

Yeah, GPIO 27 just happens to “also” be ADC1. It can be used as a GPIO function or ADC function. ;)

More heads working together. Great! Glad that one is fixed.

Thanks for the detective work folks - I’ve fixed the tutorial code :)

In the tufty getting started guide you’ve unfortunately not added Pin to the imports from machine.
You need it it to be:
from machine import ADC, Pin

Third time’s the charm - thanks!