| 
 | 1 | +<!-- Please do not change this logo with link -->  | 
 | 2 | + | 
 | 3 | +<a target="_blank" href="https://www.microchip.com/" id="top-of-page">  | 
 | 4 | +   <picture>  | 
 | 5 | +      <source media="(prefers-color-scheme: light)" srcset="images/mchp_logo_light.png" width="350">  | 
 | 6 | +      <source media="(prefers-color-scheme: dark)" srcset="images/mchp_logo_dark.png" width="350">  | 
 | 7 | +      <img alt="Microchip Technologies Inc." src="https://www.microchip.com/content/experience-fragments/mchp/en_us/site/header/master/_jcr_content/root/responsivegrid/header/logo.coreimg.100.300.png/1605828081463/microchip.png">  | 
 | 8 | +   </picture>  | 
 | 9 | +</a>  | 
 | 10 | + | 
 | 11 | +# Interface VCNL4200 Ambient Light Sensor on Curiosity Nano Explorer with PIC18F56Q24 Curiosity Nano Board  | 
 | 12 | + | 
 | 13 | +This example demonstrates how the VCNL4200 Ambient Light Sensor on the Curiosity Nano Explorer board operates when interfaced with the PIC18F56Q24 microcontroller. The PIC18F56Q24 Curiosity Nano development board (host) communicates with the VCNL4200 Ambient Light Sensor (client) using the I2C protocol. The example application displays the current light intensity (in lux) every second.  | 
 | 14 | + | 
 | 15 | +## Related Documentation  | 
 | 16 | + | 
 | 17 | +- [PIC18F26/45/46/55/56Q24 Data Sheet](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/DataSheets/PIC18F26-45-46-55-56Q24-Microcontroller-Data-Sheet-XLP-DS40002503.pdf)  | 
 | 18 | +- [I2C Host Driver](https://onlinedocs.microchip.com/oxy/GUID-420E6AAC-9141-47BF-A4C7-A6EA17246D0D-en-US-23/GUID-9123ED22-5064-448E-AB55-11C967BB20A4.html?hl=i2c%2Chost)  | 
 | 19 | +- [VCNL4200 Data Sheet](https://www.vishay.com/docs/84430/vcnl4200.pdf)  | 
 | 20 | +- [VCNL4200 App Note](https://www.vishay.com/docs/84327/designingvcnl4200.pdf)  | 
 | 21 | +- [Curiosity Nano Explorer board EV58G97A](https://www.microchip.com/en-us/development-tool/EV58G97A?_ga=2.255984587.1527562019.1718650260-1302344245.1675103399)  | 
 | 22 | +- [MPLAB XC8 C Compiler User's Guide for PIC MCU](https://ww1.microchip.com/downloads/en/DeviceDoc/50002737C%20XC8%20C%20Compiler%20UG%20for%20PIC.pdf)  | 
 | 23 | + | 
 | 24 | +## Software Used  | 
 | 25 | + | 
 | 26 | +- MPLAB® X IDE 6.20.0 or newer [(MPLAB® X IDE 6.20)](https://www.microchip.com/en-us/development-tools-tools-and-software/mplab-x-ide)  | 
 | 27 | +- MPLAB® XC8 3.00 or newer compiler [(MPLAB® XC8 3.00)](https://www.microchip.com/en-us/tools-resources/develop/mplab-xc-compilers/xc8)  | 
 | 28 | +- MPLAB® Code Configurator [(MCC Melody)](https://www.microchip.com/en-us/tools-resources/configure/mplab-code-configurator/melody)  | 
 | 29 | + | 
 | 30 | +## Hardware Used  | 
 | 31 | + | 
 | 32 | +- [Curiosity Nano Explorer board EV58G97A](https://www.microchip.com/en-us/development-tool/EV58G97A?_ga=2.255984587.1527562019.1718650260-1302344245.1675103399)  | 
 | 33 | +  - [User guide](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/UserGuides/CNANO-Explorer-UserGuide-DS50003716.pdf)  | 
 | 34 | +  - [Schematics](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/BoardDesignFiles/Curiosity-Nano-Explorer-Schematics.pdf  | 
 | 35 | +  )  | 
 | 36 | + | 
 | 37 | +- [PIC18F56Q24 Curiosity Nano](https://www.microchip.com/en-us/development-tool/ev01e86a)  | 
 | 38 | +  - [PIC18F56Q24 Curiosity Nano User Guide](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/UserGuides/PIC18F56Q24-CNANO-UserGuide-DS50003618.pdf)  | 
 | 39 | +  - [Schematics](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/BoardDesignFiles/PIC18F56Q24-Curiosity-Nano-Schematics.PDF)  | 
 | 40 | + | 
 | 41 | +- [PIC18F56Q24 product page](https://www.microchip.com/en-us/product/pic18f56q24)  | 
 | 42 | + | 
 | 43 | +## Block Diagram  | 
 | 44 | + | 
 | 45 | +The block diagram shows a brief summary of the high level connection beween the microcontroller and the sensor.<br>  | 
 | 46 | + - In the [Schematics](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/BoardDesignFiles/Curiosity-Nano-Explorer-Schematics.pdf  | 
 | 47 | +  ),  a 47 kΩ resistor is connected between the Interrupt pin of the VCNL4200 sensor and VCC.  | 
 | 48 | + - To achieve the recommended resistance from the [VCNL4200 App Note](https://www.vishay.com/docs/84327/designingvcnl4200.pdf) a 26.5 kΩ resistor is added in parallel, bringing the total resistance down to around 17 kΩ.    | 
 | 49 | +  | 
 | 50 | + | 
 | 51 | +###### Figure 1: Block diagram<br><br><br><br>  | 
 | 52 | + | 
 | 53 | +## Setup  | 
 | 54 | + | 
 | 55 | +Connect the PIC18F56Q24 Curiosity Nano development board to the host PC using a USB-C cable. Then, insert the Curiosity Nano into the Curiosity Nano socket on the Curiosity Nano Explorer board, as shown in the figure below.<br><br>  | 
 | 56 | +<img src="./images/setup.png" alt="setup.png" width="619" height="561" style="float: left; margin-right: 10px;">  | 
 | 57 | + | 
 | 58 | +###### Figure 2: Hardware Setup<br><br>  | 
 | 59 | + | 
 | 60 | +### Pins utilized  | 
 | 61 | +| Pins     |  Function  | 
 | 62 | +|----------|----------  | 
 | 63 | +| PROX_INT    | Interrupt output from the VCNL4200 for proximity or ambient light changes.     | 
 | 64 | +| RB0   | Digital input pin on PIC18F56Q24,receives interrupts from PROX_INT     | 
 | 65 | +| 3.3V(from MikroE BUS)   | Provides 3.3V input voltage to the pull-up resitor used(26.5kohm)     | 
 | 66 | +|GND(from MikroE BUS)       | Ground pin from MikroE BUS  | 
 | 67 | + | 
 | 68 | + | 
 | 69 | +Remove the jumper wire on PROX_INT pin and pin 16 to allow the PIC18F56Q24 to receive interrupts from the VCNL4200.<br><br>  | 
 | 70 | +<img src="./images/prox_int_jumper.png" alt="prox_int_jumper.png" width="250" height="150">  | 
 | 71 | +<br><br>Connect the left pin of the PROX_INT 2-pin header to RB0 (pin 30) as shown in the figure below<br><br>  | 
 | 72 | +<img src="./images/prox_int_rb0.png" alt="prox_int_rb0.png" width="220" height="150">  | 
 | 73 | + | 
 | 74 | +The next three figures illustrate this setup, which represents the hardware implementation of the connections depicted in the block diagram (Figure 1). In this configuration, PROX_INT and RB0 are connected to one terminal of a resistor, while the opposite terminal is connected to the 3.3V supply via a red wire. Additionally, a wire runs from the GND pin to the breadboard. <br><br>  | 
 | 75 | +<img src="./images/setup_1.png" alt="setup_1.png" width="350" height="350">  | 
 | 76 | + | 
 | 77 | +###### Figure 3: Setup Part - I<br><br><br><br>  | 
 | 78 | + | 
 | 79 | +  | 
 | 80 | + | 
 | 81 | +###### Figure 4: Setup Part - II<br><br><br><br>  | 
 | 82 | + | 
 | 83 | +  | 
 | 84 | + | 
 | 85 | +###### Figure 5: Setup Part - III<br><br><br><br>  | 
 | 86 | + | 
 | 87 | + | 
 | 88 | + | 
 | 89 | +1. Open the project 'pic18f56q24-curiosity-nano-explorer-vcnl4200-ambient-sensor' as shown in figure below.<br><br>  | 
 | 90 | + | 
 | 91 | +  | 
 | 92 | +  ###### Figure 6: Open Project Window<br><br>  | 
 | 93 | + | 
 | 94 | +2. Right click on the Project in the projects tab and click **Properties** to open the Project Properties window. Select the:  | 
 | 95 | +    - Curiosity Nano device from the **Connected Hardware Tool** drop-down menu, as shown in figure. 7  | 
 | 96 | +    - Select the latest tool pack  | 
 | 97 | +    - Select the latest compiler and click **OK**.<br><br>  | 
 | 98 | + | 
 | 99 | +  | 
 | 100 | +###### Figure 7: Select the Hardware Tool in Project Properties Window<br><br>  | 
 | 101 | + | 
 | 102 | +3. Once the project is open, click the **MCC** button in the top toolbar to open it.  | 
 | 103 | + | 
 | 104 | +  | 
 | 105 | +###### Figure 8: Open MCC<br><br>  | 
 | 106 | + | 
 | 107 | +##### Clock Control:  | 
 | 108 | +4. Under <u>*Project Resources*</u>><u>*System*</u>><u>*Clock Control*</u>  | 
 | 109 | + | 
 | 110 | +   - The Clock Driver settings are as in the image below<br><br>  | 
 | 111 | + | 
 | 112 | +  | 
 | 113 | +###### Figure 9: Clock Driver  | 
 | 114 | +  - We choose a High-Frequency Internal Oscillator (64 Mhz) with a clock divider of 2  | 
 | 115 | +      <br><br>  | 
 | 116 | + | 
 | 117 | +##### Timer Setup:  | 
 | 118 | +5. Under <u>*Project Resources*</u>><u>*Drivers*</u>><u>*Select TMR0*</u>  | 
 | 119 | + | 
 | 120 | +  - The Timer Driver settings are as in the image below.<br><br>  | 
 | 121 | + | 
 | 122 | +  | 
 | 123 | +###### Figure 10: Timer Driver  | 
 | 124 | + | 
 | 125 | +  - Settings for Timer  | 
 | 126 | +      1. Timer Mode - 8-bit  | 
 | 127 | +      2. Clock Pre-Scaler - 1:16384  | 
 | 128 | +      3. Postscaler - 1:16  | 
 | 129 | +      4. Clock source - HFINTOSC  | 
 | 130 | +      5. Requested Period - 1s  | 
 | 131 | +      6. TMR interrupt - Enabled  | 
 | 132 | +      7. Callback function Rate - 1  | 
 | 133 | +    <br><br>  | 
 | 134 | + | 
 | 135 | +##### UART Setup:  | 
 | 136 | +6. Under Project Resources go to Drivers and Select UART  | 
 | 137 | + | 
 | 138 | +  - The UART Driver settings are as in the image below.  | 
 | 139 | + | 
 | 140 | +  | 
 | 141 | +###### Figure 11: UART Driver  | 
 | 142 | + | 
 | 143 | +  - Setting for UART:  | 
 | 144 | +      1. Baud rate - 115200  | 
 | 145 | +      2. Parity  - None  | 
 | 146 | +      3. Data Size - 8 bits  | 
 | 147 | +      4. Stop bits - 1  | 
 | 148 | +      5. Flow Control Mode - None  | 
 | 149 | +      6. Interrupt Driven - Yes  | 
 | 150 | +      7. Software Transmit and Receive Buffer Size - 8 bytes  | 
 | 151 | +      8. UART PLIB Selector - UART2  | 
 | 152 | + | 
 | 153 | +  - RB5 and RB4 are the UART2 TX and RX pins, respectively, as selected below in the pin grid view (in the red circle).  | 
 | 154 | + | 
 | 155 | +  | 
 | 156 | +###### Figure 12: UART Pin Grid View<br><br>  | 
 | 157 | + | 
 | 158 | +##### I2C Setup:  | 
 | 159 | +7. Under Project Resources go to Drivers and Select I2C1_Host  | 
 | 160 | + | 
 | 161 | +  - The I2C Host Driver settings are as in the image below.  | 
 | 162 | + | 
 | 163 | +    | 
 | 164 | +  ###### Figure 13: I2C Host <br><br>  | 
 | 165 | + | 
 | 166 | +  - The I2C Peripheral settings are as in the image below.  | 
 | 167 | + | 
 | 168 | +    | 
 | 169 | +  ###### Figure 14: I2C Peripheral <br><br>  | 
 | 170 | + | 
 | 171 | +  - RB1 and RB2 are the SCL and SDA pins for the I2C1 peripheral as selected below in the pin grid view (in the red circle).  | 
 | 172 | + | 
 | 173 | +    | 
 | 174 | +  ###### Figure 15: I2C Pin Grid View  | 
 | 175 | + | 
 | 176 | +## Operation  | 
 | 177 | + | 
 | 178 | +1. After configuring the peripherals in the Setup section, click the **Generate** button on the right side panel in **Project Resources** as shown in the figure below.  | 
 | 179 | + | 
 | 180 | +  | 
 | 181 | +###### Figure 16: MCC Generate Button<br><br>  | 
 | 182 | + | 
 | 183 | +2. In the right side panel of the **Projects** tab, you will see "MCC Generated Files" in your project folder.  | 
 | 184 | + | 
 | 185 | +  | 
 | 186 | +###### Figure 17: Project Tree<br><br>  | 
 | 187 | + | 
 | 188 | +3. The code execution starts from the ``main.c`` file.  | 
 | 189 | + | 
 | 190 | +4. The ``app.c`` file handles the initialization of the Ambient Light Sensor (ALS) and reads the light intensity value by interfacing the PIC18F56Q24 microcontroller with the VCNL Ambient Light Sensor.  | 
 | 191 | + | 
 | 192 | +5. To compile the project and generate the binaries, go to the Production menu in the menu bar and select "Clean and Build Main Project".<br>  | 
 | 193 | +  | 
 | 194 | +###### Figure 18: Compile Button<br><br>  | 
 | 195 | + | 
 | 196 | +6. To flash the binaries on the PIC18F56Q24 click the 'Make and Program Device Main Project' icon in the toolbar.<br>  | 
 | 197 | +  | 
 | 198 | +###### Figure 19: Flash Button<br><br>  | 
 | 199 | + | 
 | 200 | +7. Now, to observe the live temperature readings follow the below steps:  | 
 | 201 | +    - Click the "MPLAB Data Visualizer" icon in the toolbar (Step 1 in Figure 20).  | 
 | 202 | +    - Click the gear icon for the COM port where the PIC MCU is connected to the host PC (Step 2 in Figure 20).  | 
 | 203 | +    - Set the UART settings as shown below (Step 3 in Figure 20).  | 
 | 204 | + | 
 | 205 | +      | 
 | 206 | +    ###### Figure 20: Data Visualizer<br><br>  | 
 | 207 | + | 
 | 208 | +8. Now Click the **Play** button next to the gear icon in the COM port and click the **Send to Terminal** button in the window that pops up and then click **Close** to close the window.  | 
 | 209 | + | 
 | 210 | +  | 
 | 211 | +###### Figure 21: Play Button<br><br>  | 
 | 212 | + | 
 | 213 | +  | 
 | 214 | +###### Figure 22: Send to Terminal Button<br><br>  | 
 | 215 | + | 
 | 216 | +9. On the terminal window live light intensity is observed every one second.  | 
 | 217 | + | 
 | 218 | +  | 
 | 219 | +###### Figure 23: Light Intensity Readings<br><br>  | 
 | 220 | + | 
 | 221 | +## Demo  | 
 | 222 | +<p align="left">  | 
 | 223 | +  <img src="images/demo.gif" alt="Description" width="500">  | 
 | 224 | +</p>  | 
 | 225 | + | 
 | 226 | +## Summary  | 
 | 227 | + | 
 | 228 | +This demo demonstrates how to configure the peripherals of the PIC18F56Q24, including Clock, Timer, UART, and I2C, to communicate with the VCNL4200 Ambient Light Sensor using the I2C communication protocol. The light intensity values are then displayed on a UART terminal every second.  | 
0 commit comments