Proximity & Light Sensing

Proximity Examples

Examples that demostrates the use of apds9960 as a proximity sensor

Simple Example

Basic proximity test program.

examples/prox/simple_proximity_apds9960.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import machine
from time import sleep_ms
from uPy_APDS9960.apds9960LITE import APDS9960LITE

#Init I2C Buss
i2c =  machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

apds9960=APDS9960LITE(i2c)      # Enable sensor
apds9960.prox.enableSensor()    # Enable Proximit sensing

while True:
        sleep_ms(25) # wait for readout to be ready
        print(apds9960.prox.proximityLevel)   #Print the proximity value

Regular Example

Example exposing more functions

examples/prox/proximity_apds9960.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import machine
from time import sleep_ms
from uPy_APDS9960.apds9960LITE import APDS9960LITE

# Proximity Gain (PGAIN) values
APDS9960_PGAIN_1X = const(0)
APDS9960_PGAIN_2X = const(1)
APDS9960_PGAIN_4X = const(2)
APDS9960_PGAIN_8X = const(3)

# LED Drive values
APDS9960_LED_DRIVE_100MA  = const(0)
APDS9960_LED_DRIVE_50MA   = const(1)
APDS9960_LED_DRIVE_25MA   = const(2)
APDS9960_LED_DRIVE_12_5MA = const(3)

i2c =  machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

apds9960=APDS9960LITE(i2c)
apds9960.prox.eLEDCurrent   =APDS9960_LED_DRIVE_100MA    
apds9960.prox.eProximityGain=APDS9960_PGAIN_8X   
apds9960.prox.enableSensor()

sleep_ms(50)

while True:
    sleep_ms(50)
    print("proximity:", apds9960.prox.proximityLevel )

Simple IRQ

Example showing use of a hardware IRQ raised at a given proximity value

examples/prox/simple_irq_proximity_apds9960.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import machine
from time import sleep_ms
from uPy_APDS9960.apds9960LITE import APDS9960LITE

i2c =  machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

apds9960=APDS9960LITE(i2c)
apds9960.prox.eLEDCurrent    =0 # LED_DRIVE_100MA    
apds9960.prox.eProximityGain =3 # PGAIN_8X   
apds9960.prox.enableSensor()

#IRQ Functionalities
apds9960.prox.setInterruptThreshold(high=10,low=0,persistance=7)
apds9960.prox.enableInterrupt()

ProxThPin=machine.Pin(0, machine.Pin.IN ,machine.Pin.PULL_UP)

sleep_ms(50)

while True:
    sleep_ms(50)

    if(ProxThPin.value()==0):
        print("proximity:", apds9960.prox.proximityLevel )
        apds9960.prox.clearInterrupt()  

IRQ Example

Example showing use of interrupts exposing more functionalities

examples/prox/irq_proximity_apds9960.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import machine
from time import sleep_ms
from uPy_APDS9960.apds9960LITE import APDS9960LITE

# Proximity Gain (PGAIN) values
APDS9960_PGAIN_1X = const(0)
APDS9960_PGAIN_2X = const(1)
APDS9960_PGAIN_4X = const(2)
APDS9960_PGAIN_8X = const(3)

# LED Drive values
APDS9960_LED_DRIVE_100MA  = const(0)
APDS9960_LED_DRIVE_50MA   = const(1)
APDS9960_LED_DRIVE_25MA   = const(2)
APDS9960_LED_DRIVE_12_5MA = const(3)

i2c =  machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
print("Lite APDS-9960 Proximity test ")

apds9960=APDS9960LITE(i2c)
apds9960.prox.eLEDCurrent   =APDS9960_LED_DRIVE_100MA    
apds9960.prox.eProximityGain=APDS9960_PGAIN_8X   

apds9960.prox.enableSensor()
apds9960.prox.setInterruptThreshold(high=10,low=0,persistance=7)
apds9960.prox.enableInterrupt()

ProxThPin=machine.Pin(0, machine.Pin.IN ,machine.Pin.PULL_UP)

sleep_ms(50)

while True:
    sleep_ms(50)

    if(ProxThPin.value()==0):
        print("proximity:", apds9960.prox.proximityLevel )
        apds9960.prox.clearInterrupt()  

Light Sensor Examples

Light sensing example

Ambient Light and RGB Color Sensing test program.

examples/als/simple_light_apds9960.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import machine
from time import sleep_ms
from uPy_APDS9960.apdsS9960LITE import APDS9960LITE

#Init I2C Buss
i2c =  machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

apds9960=APDS9960LITE(i2c)         # Enable sensor
print("Enable light Sensor")

apds9960.als.enableSensor()   # Enable Light sensor
apds9960.als.eLightGain=3          # x64 gain
#apds9960.prox.enableProximity()
sleep_ms(50)
print("Clear Light level: ", apds9960.als.ambientLightLevel)
print("Red   Light level: "  , apds9960.als.redLightLevel)
print("Green Light level: ", apds9960.als.greenLightLevel)
print("Blue  Light level: " , apds9960.als.blueLightLevel)

Ambient light IRQ

Ambient Light IRQ test program.

examples/als/simple_light_irq_apds9960.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import machine
from time import sleep_ms
from uPy_APDS9960.apds9960LITE import APDS9960LITE

#Init I2C Buss
i2c =  machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

apds9960=APDS9960LITE(i2c)         # Enable sensor
apds9960.als.enableSensor()   # Enable Light sensor
apds9960.als.eLightGain=3          # x64 gain
apds9960.als.setInterruptThreshold(high=100,low=0,persistance=7)
apds9960.als.enableInterrupt(True)     # Enable interrupt
apds9960.als.clearInterrupt()          # Clear interrupt
sleep_ms(50)

IrqThPin=machine.Pin(0, machine.Pin.IN ,machine.Pin.PULL_UP)
sleep_ms(50)

while True:
    sleep_ms(50)

    if(IrqThPin.value()==0):
        print("Ambient light level:", apds9960.als.ambientLightLevel )
        apds9960.als.clearInterrupt()

Debug

I2C Debug Example

Test program for testing the I2C connection the the aps9960

examples/debug/i2c_test.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import machine
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

print('Scan i2c bus...')
devices = i2c.scan()

if len(devices) == 0:
  print("No i2c device !")
else:
  print('i2c devices found:',len(devices))

  for device in devices:
    print("Decimal address: ",device," | Hexa address: ",hex(device))

    if(device==0x39): # APDS9960 Address = 0x39
        deviceID=i2c.readfrom_mem(devices[0],0x92, 1) #G et deviceID
        print("Found ADPS9960: Device ID: ",deviceID)