Comment on page
APDS-9960 Light Sensor
This documentation includes installation guidelines and sample code for your hardware.
APDS9960Sensor library is responsible for communicating with the APDS-9960 sensor.
To use the library, add the following using statement to the top of your C# code.
using TurtaIoTHAT;
Then, create an instance of the APDS9960Sensor class.
APDS9960Sensor apds;
Now you're ready to access the library by calling the apds instance.
Initiates the APDS-9960 sensor to get ambient light, RGB light, proximity and gesture direction.
APDS9960Sensor(bool ambientAndRGBLightEnabled, bool proximityDetectionEnabled, bool gestureRecognitionEnabled)
Parameters
Bool ambientAndRGBLightEnabled: Ambient light and RGB light sense. Bool proximityDetectionEnabled: Proximity detection. Bool gestureRecognitionEnabled: Gesture recognition. (Not implemented.)
Reads the ambient light value.
int ReadAmbientLight()
Parameters
None Returns
Int: Ambient light.
Reads the RGB light values.
int[] ReadRGBLight()
Parameters
None Returns
Int[]: Red, green and blue light values respectively.
Reads the proximity value.
int ReadProximity()
Parameters
None Returns
Int: Proximity value.
To clean up the resources used by the class, call the Dispose() method it when the application is closing.
void Dispose()
This method stops the timers and cleans the resources used by the class.
You can copy the example codes from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Windows10IoTCore address. There are two examples of this library.
This application demonstrates reading ambient light from the sensor.
To run the example:
- 1.Open the sample project you downloaded.
- 2.Make sure "ARM" is selected from the solution platforms selection.
- 3.Deploy the sample to the Raspberry Pi using the "F5" key.
Sample Code
MainPage.xaml.cs
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using TurtaIoTHAT;
namespace APDS9960AmbientLightSampleApp
{
public sealed partial class MainPage : Page
{
// APDS-9960 Sensor
static APDS9960Sensor apds;
// Sensor timer
Timer sensorTimer;
public MainPage()
{
this.InitializeComponent();
// Initialize sensor and timer
Initialize();
}
private async void Initialize()
{
// Initialize and configure sensor
await InitializeAPDS9960();
// Configure timer to 2000ms delayed start and 2000ms interval
sensorTimer = new Timer(new TimerCallback(SensorTimerTick), null, 2000, 2000);
}
private async Task InitializeAPDS9960()
{
// Create sensor instance
// Ambient & RGB light: enabled, proximity: disabled, gesture: disabled
apds = new APDS9960Sensor(true, false, false);
// Delay 1ms
await Task.Delay(1);
}
private static void SensorTimerTick(object state)
{
// Write sensor data to output / immediate window
Debug.WriteLine("Ambient Light: " + apds.ReadAmbientLight().ToString());
Debug.WriteLine("-----");
}
}
}
Result
When you run the sample, it prints the ambient light reading to the Visual Studio Output / Immediate Window.
Ambient Light: 287
-----
To stop the application, use the "Stop" button on the Visual Studio.
This application demonstrates reading RGB light from the sensor.
To run the example:
- 1.Open the sample project you downloaded.
- 2.Make sure "ARM" is selected from the solution platforms selection.
- 3.Deploy the sample to the Raspberry Pi using the "F5" key.
Sample Code
MainPage.xaml.cs
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using TurtaIoTHAT;
namespace APDS9960RGBLightSampleApp
{
public sealed partial class MainPage : Page
{
// APDS-9960 Sensor
static APDS9960Sensor apds;
// Sensor timer
Timer sensorTimer;
public MainPage()
{
this.InitializeComponent();
// Initialize sensor and timer
Initialize();
}
private async void Initialize()
{
// Initialize and configure sensor
await InitializeAPDS9960();
// Configure timer to 2000ms delayed start and 2000ms interval
sensorTimer = new Timer(new TimerCallback(SensorTimerTick), null, 2000, 2000);
}
private async Task InitializeAPDS9960()
{
// Create sensor instance
// Ambient & RGB light: enabled, proximity: disabled, gesture: disabled
apds = new APDS9960Sensor(true, false, false);
// Delay 1ms
await Task.Delay(1);
}
private static void SensorTimerTick(object state)
{
// Read sensor data
int[] rgbLight = apds.ReadRGBLight();
// Write sensor data to output / immediate window
Debug.WriteLine("Red Light..: " + rgbLight[0].ToString());
Debug.WriteLine("Green Light: " + rgbLight[1].ToString());
Debug.WriteLine("Blue Light.: " + rgbLight[2].ToString());
Debug.WriteLine("-----");
}
}
}
Result
When you run the sample, it prints the RGB light readings to the Visual Studio Output / Immediate Window.
Red Light..: 124
Green Light: 345
Blue Light.: 212
-----
To stop the application, use the "Stop" button on the Visual Studio.
Last modified 4yr ago