VCNL4010 driver


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

def my_init():
global port
port=i2c.I2C(I2C0, 0x13)

def init_VCNL4010(my_p):
# sensor software reset
#send the command for enabling the sampling mode

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.
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

global port
init_VCNL4010 (port)

while True:
    #read proximity 
    proximity_raw_value = build_value(data[0], data[1])
    print('Proximity raw value = ', proximity_raw_value)
    print('Proximity value = ', proximity_raw_value)


except Exception as e:


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: