More than two weeks have passed since I published my Altitude Click code. If you don’t have an MPLAB Xpress Development board yet it’s time to order one: the part number is DM164140. On the Microchip Direct site you’ll find this little development board with an estimated ship date of 1 June 2016. Some other big distributors have announced it too: you can find it on Farnell (order code 2543062), Mouser (part no 579-DM164140) and TME.
OK, enough with the hardware. Time for another MPLAB Xpress project: a demo code for the Proximity click board from MikroElektronika.
This little click board carries one VCNL4010 proximity sensor from Vishay, plus all the additional components required for its use. It’s one of the few MikroElektronika click boards that can be configured for 5V operation – so you can use it also with 5V microcontrollers. However, as the PIC167F18855 is a 3.3V microcontroller I’ll just leave it on its factory setting.
The VCNL4010 combines ambient light detection and proximity detection in one single chip. Ambient light is measured in 16-bit precision on a range of 0.25 lx to 16 klx, with 100 Hz and 120 Hz flicker noise rejection. Proximity measurements are also returned on 16 bit, with the distance range from 0 to 20cm.
Interfacing with the microcontroller is done via I2C, plus an interrupt pin. VCNL4010 has a number of 16 internal registers, of which register #0 is used to configure the operating mode and also as a flag register for completing a measurement. Register #1 stores the device version (current version is 0x21).
Parameters for ambient light measurement are stored in registers #4, while ambient measurement results are stored in registers #5 and #6.
Proximity measurements are configured via registers #2 (proximity measurement rate), #3 (IR LED current) and register #15 (proximity modulator timing adjustments). Results are stored in registers #7 and #8.
VCNL4010 has also an interrupt system, which can be configured to issue an interrupt when specific thresholds are reached. There is a 16-bit low threshold level stored in registers #10 and #11, and a high threshold level stored in registers #12 and #13.
Interrupts are configured via register #9, with the interrupts status stored in register #14.
MPLAB Xpress: proximity measurement
The hardware of this project is just as simple as placing the Proximity click in the provided mikroBUS socket of the MPLAB Xpress development board.
As for the software, it follows the same configuration steps as in the Altitude Click project, and it uses the same I2C functions I have described there. Thus, I won’t repeat it here.
Although the VCNL4010 has an auto-increment function that allows reading the data from two consecutive registers in one single I2C transaction, this would require a dedicated I2C function. As this project just reads proximity or ambient data and sends it to PC via USB-UART, the slowest part of the code is the UART communication, I see no point in optimizing I2C communication. So, I will just read the ambient and proximity data in two separate transactions, one for the MSB and one for the LSB.
Again, I see no point in performing periodic measurements, as this mode will gather more data that I display with ease on the UART terminal. Instead, I will configure the VCNL4010 to perform measurements on-demand, and in the software, I will poll the corresponding flag in the CONFIG register to determine when a measurement was completed.
To keep things simple I will also disable the interrupt system.
Proximity click C code
To make the code more human-readable I have created a header file containing some definitions of the VCNL4010 address, registers and function names.
The code listing is: