VCNL4010 driver


#1

Hi, it’s my first post on the zerynth comunity.
I am trying to write the driver for the ambient light and proximity sensor (VCNL4010) with DOIT esp32, but since it’s my first time ever trying to write a driver, it doesn’t work. I was hoping you could help me (probably its just stupid mistakes anyway)

The error shows up the first i2c.write in the init_VCNL4010 function.
P.S. since its my first post please be patient if i violated code of conduct

import streams
import i2c

streams.serial()
def my_init():
global port
port=i2c.I2C(I2C0, 0x13)
port.start()
print(‘initialised’)

def init_VCNL4010(my_p):
# sensor software reset
#my_p.write([0xFE])
#sleep(500)
#send the command for enabling the sampling mode

my_p.write_bytes(0x80,0x02)
"""
Addressing. The first byte of an I2C transfer contains the slave address and the data 
direction. The address is 7 bits long, followed by the direction bit. ... 
Thus, only 112 addresses are available with the 7 bit address scheme.
To get rid of this a special method for using 10 bit addresses is defined.
"""
#my_p.write(0x80)
#my_p.write(0x02)
print("init went good")

def build_value(lo,hi):
return (lo << 8 | (hi & 0xFF))
#resolution is on 16-bits so i need 0xFF in this case
#shifting leaves first 8 bits to 0
#while the & with hi makes it so that if needed some bits can
#be set to 0, given a lower-than-16-bits resolution

try:
global port
my_init()
init_VCNL4010 (port)

while True:
    #read proximity 
    port.write(0x87)
    
    sleep(20)
    data=port.read(2)
    proximity_raw_value = build_value(data[0], data[1])
    print('Proximity raw value = ', proximity_raw_value)
    
    print('Proximity value = ', proximity_raw_value)
    print('_____________________________________')

    sleep(2000)

except Exception as e:
print(e)


#2

Maybe you could try to execute your I2C initialization without using the I2C bus as an argument for the function.
You could try to execute your initialization before the main loop and check it using a Try and catch statement.
Let me know how it goes.

Could you also post the code with proper indentation, because it’s a bit hard to follow along like that.:smile: