Skip to content

Commit

Permalink
Added APDS9301 light sensor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Jan 19, 2019
1 parent b937587 commit 83ccb40
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
56 changes: 56 additions & 0 deletions LightSensors/APDS9301_basic/APDS9301_basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
APDS9301 simple example
Reads the APDS9301 light sensor. See readme.md for more details.
Circuit:
* APDS9301 sensor attached to SDA and SCL pins
created 19 Jan 2019
by Tom Igoe
*/
#include "Wire.h"
#include <Sparkfun_APDS9301_Library.h>

APDS9301 apds; // instance of the sensor library
int sensorDelay = 0; // delay for sensor integration time
int lastReadingTime = 0; // last time sensor was read

void setup() {
Serial.begin(9600);
Wire.begin(); // not sure why this isn't included in the APDS lib

// APDS9301 sensor setup
// using default sensor I2C address:
apds.begin(0x39);
// default is low gain:
apds.setGain(APDS9301::HIGH_GAIN);
// integration time can be 13_7, 101, or 402 ms:
apds.setIntegrationTime(APDS9301::INT_TIME_402_MS);

// sets the sensor reading delay time based on the integration time:
switch (apds.getIntegrationTime()) {
case 0:
sensorDelay = 14; // integration time 13.7ms
break;
case 1:
sensorDelay = 101; // integration time 101ms
break;
case 2:
sensorDelay = 407; // integration time 402ms
break;
}
}

void loop() {
String results = "CH0: ";
// if the integration time has passed, read the sensor:
if (millis() - lastReadingTime >= sensorDelay) {
results += String(apds.readCH0Level());
results += String("\tCh 1: ");
results += String(apds.readCH1Level());
results += String("\tlux: ");
results += String(apds.readLuxLevel());
Serial.println(results);
lastReadingTime = millis();
}
}
9 changes: 9 additions & 0 deletions LightSensors/APDS9301_basic/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# APDS9301 Basic Example

This example shows how to do a basic reading on the APDS9301 light sensor. It will print out the readings on the visible and infrared channels and the calculated lux level. You can use this example to experiment with the integration time and gain settings.

Sparkfun has a decent[ APDS9301 library](https://github.com/sparkfun/APDS-9301_Breakout) for this sensor, and a [hookup guide](https://learn.sparkfun.com/tutorials/apds-9301-sensor-hookup-guide) that explains its use. This sensor has two photodiodes, one that reads visible light (CH0, 640nm) and one that reads infrared light (CH1, 940nm)

If you change the integration time setting the sketch will change the delay for when it reads the sensor accordingly. Changing the integration time will change the values you get on channels 0 and 1, but it shouldn't make a huge difference in the overall lux reading. That reading is based on the ratio of CH0 to CH1, as described on page 4 of the [APS9301 datasheet](https://cdn.sparkfun.com/assets/3/2/c/0/8/AV02-2315EN0.pdf).

If you change the gain settings, you should also see a change in the CH0 and CH1 readings, but not the overall lux reading.
67 changes: 67 additions & 0 deletions LightSensors/APDS9301_interrupts/APDS9301_interrupts.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
APDS9301 interrupt example
Reads the APDS9301 light sensor when thresholds are crossed.
See readme.md for more details.
Circuit:
* APDS9301 sensor attached to SDA and SCL pins
* APDS9301 INT pin attached to INT_PIN of Arduino
created 19 Jan 2019
by Tom Igoe
*/
#include "Wire.h"
#include <Sparkfun_APDS9301_Library.h>

APDS9301 apds; // instance of the sensor library
const int INT_PIN = 5; // sensor interrupt pin. Use 2 or 3 for Uno.
bool interruptFlag = false; // interrupt flag

int sensorDelay = 0; // delay for sensor integration time
int lastReadingTime = 0;
void setup() {
Serial.begin(9600);
Wire.begin(); // not sure why this isn't included in the APDS lib

// APDS9301 sensor setup
// using default sensor I2C address:
apds.begin(0x39);
// default is low gain:
apds.setGain(APDS9301::LOW_GAIN);
// integration time can be 13_7, 101, or 402 ms:
apds.setIntegrationTime(APDS9301::INT_TIME_13_7_MS);

// interrupts are generated when CH0 reading is low or high.
// low 0 disables the low interrupt:
apds.setLowThreshold(5);
// high 65535 disables the high interrupt:
apds.setHighThreshold(65535);
// set number of readings to base the interrupt on:
apds.setCyclesForInterrupt(1);
// enable the interrupt and clear interrupt flag:
apds.enableInterrupt(APDS9301::INT_ON);
apds.clearIntFlag();

// Interrupt setup
pinMode(INT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(5), readInterrupt, FALLING);
}

void loop() {
if (interruptFlag) {
Serial.println("Interrupt");
Serial.print("Ch 0: ");
Serial.print(apds.readCH0Level());
Serial.print("\tCh 1: ");
Serial.print(apds.readCH1Level());
Serial.print("\tlux: ");
Serial.println(apds.readLuxLevel());
lastReadingTime = millis();
apds.clearIntFlag();
interruptFlag = false;
}
}

void readInterrupt() {
interruptFlag = true;
}
13 changes: 13 additions & 0 deletions LightSensors/APDS9301_interrupts/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# APDS9301 Interrupt Example

This example is for experimenting with the interrupts on the APDS9301 light sensor. The readings will only be printed if the interrupt is triggered.

Sparkfun has a decent[ APDS9301 library](https://github.com/sparkfun/APDS-9301_Breakout) for this sensor, and a [hookup guide](https://learn.sparkfun.com/tutorials/apds-9301-sensor-hookup-guide) that explains its use. This sensor has two photodiodes, one that reads visible light (CH0, 640nm) and one that reads infrared light (CH1, 940nm)

The interrupt can be triggered in one of two ways: either the reading on CH0 goes below the low threshold, or it goes above the high threshold. The threshold settings in the sketch are set to 10 (low) and 100 (high) to begin with.

To effectively disable the high threshold, set it to the maximum value, 65535. To effectively disable the low threshold, set it to the maximum value, 0.Try different readings for the thresholds to see how they operate. When you understand them, try changing the integration time and the gain to see how they affect the thresholds.

For more about the sensor's settings, see the Sparkfun [hookup guide](https://learn.sparkfun.com/tutorials/apds-9301-sensor-hookup-guide) and the [APS9301 datasheet](https://cdn.sparkfun.com/assets/3/2/c/0/8/AV02-2315EN0.pdf).


0 comments on commit 83ccb40

Please sign in to comment.