Arduino Library for the SparkFun Spectral UV Sensor - AS7331 Sensor
The SparkFun Spectral UV Sensor - AS7331 (SEN-23517) and SEN-23518 features the AS7331 UV sensor from ams OSRAM©. It measures UV radiation on three channels: UVA (320-400nm), UVB (280-320nm), and UVC (200-280nm) with high sensitivity and accuracy. The three channels on the AS7331 each have individual photodiodes with built-in interference filters. This Mini-sized breakout board is only 0.5in. by 1in. big and has three operating modes: Single Measurement (CMD), Continuous Measurement (CONT), SYNchronized Start (SYNS), and SYNchronized Start & End (SYND), with an automatic power-down sequence between measurements for low current consumption in all three modes.
Looking for the board that matches this library - pick up a SparkFun Spectral UV Sensor - AS7331 (SEN-23517) or a SparkFun Mini Spectral UV Sensor - AS7331 (SEN-23518) at www.sparkfun.com.
This library provides an interface that enables the following functionality when using one of the SparkFun Spectral UV Sensor - AS7331 Sensors:
- Read values from the three UV channels of the sensor (UVA, UVB, UVC) separately or as a combined value
- Adjust the sensors gain values
- Set sensors clock speed and timing values
- Change the operating modes of the sensor
- Control the sensors power-down state
- Change the I2C address of the sensor
The following outlines the general use of the library in an Arduino Sketch.
At the start of your sketch, the library header file is included using the following statement:
#include <SparkFun_AS7331.h>
Before the arduino setup()
function, create a Soil Sensor object in your file with the following declaration:
SfeAS7331ArdI2C myUVSensor // Create an instance of the sensor class
In the Arduino setup()
function, initialize the sensor by calling the begin method. This method is called after the Arduino Wire
(I2C) library is initialized.
// Initialize sensor and run default setup.
if (myUVSensor.begin() == false)
{
Serial.println("Sensor failed to begin. Please check your wiring!");
Serial.println("Halting...");
while (1)
;
}
The begin method returns true if the sensor is connected and available, and false if it is not. If a value of false is returned in the above example, the sketch execution is halted.
After the sensor is initialized, any configuration values are normally set. The following demonstrates how to configure the operating mode of the sensor.
// Set measurement mode and change device operating mode to measure.
if (myUVSensor.prepareMeasurement(MEAS_MODE_CMD) == false)
{
Serial.println("Sensor did not get set properly.");
Serial.println("Halting...");
while (1)
;
}
The above command sets up the sensor to operate in a OneShot mode.
To read a value, the first step is to start a measurement, and wait for at least the sensors "conversion time" to expire.
// Send a start measurement command.
if (ksfTkErrOk != myUVSensor.setStartState(true))
Serial.println("Error starting reading!");
// Wait for a bit longer than the conversion time.
delay(2 + myUVSensor.getConversionTimeMillis());
Once the conversion period is complete, the values are read from the device with the following command:
// Read UV values.
if (ksfTkErrOk != myUVSensor.readAllUV())
Serial.println("Error reading UV.");
At this point the values from the sensor are available locally in the library and accessed using the following commands:
Serial.print("UVA:");
Serial.print(myUVSensor.getUVA());
Serial.print(" UVB:");
Serial.print(myUVSensor.getUVB());
Serial.print(" UVC:");
Serial.println(myUVSensor.getUVC());
The capabilities of this library are far more that outlined in this section. For more information consult the examples that are part of this library or the libraries documentation.
The following examples are provided with the library
- Basic One Shot - This example shows how operate the AS7331 in the default CMD mode.
- CONT Mode - Shows how operate the AS7331 in CONT mode. The break time register sets the delay between measurements so that the processor can read out the results without interfering with the ADC.
- SYNS Mode - Shows how operate the AS7331 in SYNS mode. This uses the active low SYN pin to start the conversion and relies on an interrupt to signal the end of conversion.
- SYND Mode - Shows how operate the AS7331 in SYND mode. This uses the active low SYN pin to both start and stop the conversion. The conversion time is calculated and stored in the
measures.outputConversionTime
field in units of number of clock cycles.
The full API and use documentation for this library is provided here. For a quick reference, the main methods available in the library are listed here.
Curious about the hardware this board works with - visit the SparkFun Spectral UV Sensor hardware repository.
The Hookup Guide for the SparkFun Spectral UV Sensor is available here.
This product is open source!
This product is licensed using the MIT Open Source License.