# Distance sensor

The distance sensor extension board is based on the **VCNL4040** by Vishay Semiconductors. The sensor uses I2C to communicate with the Picoclick (SDA = GPIO2, SCL = GPIO8). It has an ultra low power voltage regulator on board and can be used to activate the Picoclick by firing an interrupt. The interrupt can be configured via software.

## Hardware

### Overview

### Technical drawing

<figure><img src="/files/32G8W4i0UhOt3THavDPW" alt=""><figcaption></figcaption></figure>

* PCB: 18mm x 10mm
* Thickness: 1mm
* Mounting holes: 3.2mm
* Corner radius: 2mm

### Schematics

<figure><img src="/files/jHWyZqa3ZnBtGLPhDHz3" alt=""><figcaption></figcaption></figure>

## Software

Hardwaretest can be found on [GitHub](https://github.com/makermoekoe/Picoclick-C3/tree/main/code/Picoclick_C3/PC3_Hardwaretest_Motion).

```
#include <Arduino.h>
#include <FastLED.h>
#include <Wire.h>
#include "SparkFun_VCNL4040_Arduino_Library.h"
#include "config.h"

VCNL4040 vcnl;

void setup(){
  pinMode(BUTTON_PIN, INPUT);
  pinMode(ADC_ENABLE_PIN, OUTPUT);
  pinMode(ADC_PIN, INPUT);
  analogReadResolution(12);
  digitalWrite(ADC_ENABLE_PIN, HIGH);

  WiFi.mode(WIFI_OFF);

  FastLED.addLeds<APA102, APA102_SDI_PIN, APA102_CLK_PIN, BGR>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  delay(50);

  set_fastled(CRGB::Blue);
  
  Wire.begin(SDA_PIN, SCL_PIN);

  if(vcnl.begin() == false){
    printf("Device not found. Please check wiring.\r\n");
    set_fastled(CRGB::Red);
    while(1); //Freeze!
  }
}

unsigned long led_timer = millis();
int hue1 = 0, hue2 = 0;
int brightness = 255;
unsigned long t_sensor = 500;

void loop() {
  if(digitalRead(BUTTON_PIN) == 1){
    set_fastled(CRGB::Red);
    delay(1000);
    esp_deep_sleep_start();
  }

  if(millis() >= t_sensor + 100){
    t_sensor = millis();
    unsigned int proxValue = vcnl.getProximity();
    printf("Proximity: %i\r\n", proxValue);
  }

  if(millis() >= led_timer + 15){
    led_timer = millis();
    set_fastled(CHSV(hue1, 255, brightness), CHSV(hue2, 255, brightness));
    hue1 = (hue1 + 1)%255;
    hue2 = (hue1 + 127)%255;
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://makermoekoe.gitbook.io/picoclick-c3/extension-boards/distance-sensor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
