Luminosity and Orange Pi Zéro

Analog Brightness Sensor

The LM393, purchased from AliExpress, is a brightness sensor that can be connected to nano computers such as the Orange PI. It has a digital output providing a 0 or a 1 depending on the brightness and the threshold adjustable by a variable resistor. Here we will be interested in the analog output which provides a voltage between 0 and 3.3v (for a 3.3v power supply) depending on the level of brightness. In strong light we are close to 0v, in weak light close to 3.3v.

The difficulty with the Orange Pi like other nano computers, it does not have an analog input to measure a voltage but only digital inputs. We will convert the analog voltage measurement into a capacitor charging time measurement starting from 0v until the digital input goes to 1.

Connection

Connect the LM393 to the + 3.3v of the orange pi zero and for example the resistance and capacitance assembly on the PA15 pin as shown in the diagram.

In this diagram, the pull up resistor is missing at input PA15 of orange pi by a few tens of kΩ. It makes a bridge with the 200kΩ which even if A0 is at 0V, the input level is seen as a 1.

First, the PA15 pin is set to zero for 200ms to empty the 1µF capacitor. Then the PA15 pin goes into input, the capacitor is charged with the pull up resistor and the 200kΩ resistor which is connected to the A0 output of the light sensor. By measuring the loading time T in ms, we have a value representing the level of illumination.

Code

For the installation of the Armbian system, see the following article:
https://f1atb.fr/index.php/2020/08/06/getting-started-orange-pi-zero/

First for python 3, you have to update and add the pip installer. Then add the pyA20 GPIO access library.

apt-get update
apt-get upgrade
apt install python3-pip
pip3 install pyA20

In root access, create the source test.py with nano.

nano test.py

import time
import math
from pyA20.gpio import gpio
from pyA20.gpio import port

anal_in_out = port.PA15
gpio.init()

Offset=70


while True:
    gpio.setcfg(anal_in_out, gpio.OUTPUT) 
    gpio.output(anal_in_out, 0) 
    time.sleep(0.2)
    gpio.setcfg(anal_in_out, gpio.INPUT)
    gpio.pullup(anal_in_out, gpio.PULLUP)
    T0=time.time() * 1000                  # temps en ms
    T=T0
    Tmonte=0
    
    while T-T0<400:
        state = gpio.input(anal_in_out)
        T=time.time() * 1000
        if state==1:
            Tmonte=T-T0
            T=T0+400
    
    
    Lumi=math.floor(Tmonte-Offset)  
    print ("Lumi",Lumi)
    
    time.sleep(1)

Test the set with:

python3 test.py