All the hardware you need to use the Proximity Trinkey is built right into the board which makes getting started with it super fast.
Required Libraries
There are three required libraries for these examples: Adafruit NeoPixel, Adafruit FreeTouch, and Adafruit APDS9960.
Click Sketch > Include Libraries > Manage Libraries... to open the library manager.
Then search for NeoPixel and install the Adafruit NeoPixel library (make sure the name is correct!).
Next search for FreeTouch and install the Adafruit FreeTouch library.
Finally, search for APDS9960 and install the Adafruit APDS9960 Library.
When asked about dependencies, click Install all.
APDS9960 Demo
This simple demo prints the proximity value to the serial monitor. It requires you to make one change for it to work with the Proximity Trinkey. You need to update the INT_PIN
to match the Proximity Trinkey.
/*************************************************************************** This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor This sketch puts the sensor in proximity mode and enables the interrupt to fire when proximity goes over a set value Designed specifically to work with the Adafruit APDS9960 breakout ----> http://www.adafruit.com/products/3595 These sensors use I2C to communicate. The device's I2C address is 0x39 Adafruit invests time and resources providing this open source code, please support Adafruit andopen-source hardware by purchasing products from Adafruit! Written by Dean Miller for Adafruit Industries. BSD license, all text above must be included in any redistribution ***************************************************************************/ #include "Adafruit_APDS9960.h" //the pin that the interrupt is attached to #define INT_PIN 3 //create the APDS9960 object Adafruit_APDS9960 apds; void setup() { Serial.begin(115200); pinMode(INT_PIN, INPUT_PULLUP); if(!apds.begin()){ Serial.println("failed to initialize device! Please check your wiring."); } else Serial.println("Device initialized!"); //enable proximity mode apds.enableProximity(true); //set the interrupt threshold to fire when proximity reading goes above 175 apds.setProximityInterruptThreshold(0, 175); //enable the proximity interrupt apds.enableProximityInterrupt(); } void loop() { //print the proximity reading when the interrupt pin goes low if(!digitalRead(INT_PIN)){ Serial.println(apds.readProximity()); //clear the interrupt apds.clearInterrupt(); } }
To update this demo to work with the Proximity Trinkey update the following line:
#define INT_PIN 3
To the following pin:
#define INT_PIN PIN_INTERRUPT
Once you upload the code, open the serial monitor. Try moving your hand closer to the Proximity Trinkey to see the values change.
// SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries // // SPDX-License-Identifier: MIT #include <Adafruit_NeoPixel.h> #include "Adafruit_FreeTouch.h" #include "Adafruit_APDS9960.h" // Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255) // Create the two touch pads on pins 1 and 2: Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); Adafruit_APDS9960 apds; void setup() { Serial.begin(9600); //while (!Serial); strip.begin(); strip.setBrightness(neo_brightness); strip.show(); // Initialize all pixels to 'off' if (! qt_1.begin()) Serial.println("Failed to begin qt on pin 1"); if (! qt_2.begin()) Serial.println("Failed to begin qt on pin 2"); pinMode(PIN_INTERRUPT, INPUT_PULLUP); if(!apds.begin()){ Serial.println("failed to initialize device! Please check your wiring."); while (1) { strip.fill(0xFF0000); strip.show(); delay(100); strip.fill(0x00); strip.show(); delay(100); } } Serial.println("APDS initialized!"); apds.enableProximity(true); apds.setProxGain(APDS9960_PGAIN_8X); apds.setLED(APDS9960_LEDDRIVE_100MA, APDS9960_LEDBOOST_300PCNT); apds.setProxPulse(APDS9960_PPULSELEN_16US, 1); //set the interrupt threshold to fire when proximity reading goes above 2 apds.setProximityInterruptThreshold(0, 2); apds.enableProximityInterrupt(); } uint8_t j=0; void loop() { // print the proximity reading when the interrupt pin goes low if (!digitalRead(PIN_INTERRUPT)){ uint16_t prox = apds.readProximity(); Serial.print("Proximity: "); Serial.println(prox); if (prox < 3) prox = 0; // ignore 1 and 2 readings strip.setBrightness(prox); //clear the interrupt apds.clearInterrupt(); } else { strip.setBrightness(0); } strip.fill(Wheel(j)); strip.show(); // measure the captouches uint16_t touch1 = qt_1.measure(); uint16_t touch2 = qt_2.measure(); // If the first pad is touched, go forward if (touch1 > 500) { Serial.println("QT 1 touched"); j++; } // If the second pad is touched, go backward if (touch2 > 500) { Serial.println("QT 2 touched"); j--; } delay(10); } // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { if(WheelPos < 85) { return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } }
Once you've uploaded the code, try moving your hand closer to the Proximity Trinkey to see the NeoPixel brightness increase. Try touching the touch pads to change the color of the LEDs!
Note, the LEDs may not be on after you upload the code. This is because the proximity value is 0 if there is nothing near the sensor, so the brightness value is therefore 0 and the LEDs are not on!
Text editor powered by tinymce.