Skip to content

Commit 3e22fb4

Browse files
authored
Merge pull request #10 from sparkfun/release/v02.02.00
Release/v02.02.00
2 parents 55d4fc1 + 17264ab commit 3e22fb4

File tree

10 files changed

+926
-600
lines changed

10 files changed

+926
-600
lines changed

README.md

Lines changed: 128 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,138 @@
1-
SparkFun AS7331 Arduino Library
2-
========================================
31

4-
<table class="table table-hover table-striped table-bordered">
5-
<tr align="center">
6-
<td><a href="https://www.sparkfun.com/products/23517"><img src="https://cdn.sparkfun.com/r/600-600/assets/parts/2/3/8/0/8/23517-Spectral-UV-Sensor_Feature.jpg" alt="Standard"></a></td>
7-
<td><a href="https://www.sparkfun.com/products/23518"><img src="https://cdn.sparkfun.com/r/600-600/assets/parts/2/3/8/0/9/23518-Spectral-UV-Sensor-Mini_Feature.jpg" alt="Mini"></a></td>
8-
</tr>
9-
<tr align="center">
10-
<td><a href="https://www.sparkfun.com/products/23517">SparkFun Spectral UV Sensor - AS7331 (Qwiic)</a></td>
11-
<td><a href="https://www.sparkfun.com/products/23518">SparkFun Mini Spectral UV Sensor - AS7331 (Qwiic)</a></td>
12-
</tr>
13-
</table>
2+
![SparkFun Spectral UV Sensor - AS7331](docs/images/gh-banner-2025-as7331.png "SparkFun Spectral UV Sensor - AS7331 Sensor")
143

15-
Repository Contents
16-
-------------------
4+
# SparkFun Spectral UV Sensor - AS7331
175

18-
* **/workflows** - GitHub workflow actions files
19-
* **/examples** - Arduino examples for the AS7331
20-
* **/src** - Library source files (.cpp & .h)
6+
Arduino Library for the SparkFun Spectral UV Sensor - AS7331 Sensor
217

22-
Documentation
23-
--------------
24-
* **[Hardware Repository](https://github.com/sparkfun/SparkFun_Spectral_UV_Sensor_AS7331)** - Hardware repository for the Spectral UV Sensor - AS7331 (Qwiic).
25-
* **[Hookup Guide](https://sparkfun.github.io/SparkFun_Spectral_UV_Sensor_AS7331)** - Basic hookup guide for the Spectral UV Sensor - AS7331 (Qwiic).
8+
![License](https://img.shields.io/github/license/sparkfun/SparkFun_AS7331_Arduino_Library)
9+
![Release](https://img.shields.io/github/v/release/sparkfun/SparkFun_AS7331_Arduino_Library)
10+
![Release Date](https://img.shields.io/github/release-date/sparkfun/SparkFun_AS7331_Arduino_Library)
11+
![Documentation - build](https://img.shields.io/github/actions/workflow/status/sparkfun/SparkFun_AS7331_Arduino_Library/build-deploy-ghpages.yml?label=doc%20build)
12+
![Compile - Test](https://img.shields.io/github/actions/workflow/status/sparkfun/SparkFun_AS7331_Arduino_Library/compile-sketch.yml?label=compile%20test)
13+
![GitHub issues](https://img.shields.io/github/issues/sparkfun/SparkFun_AS7331_Arduino_Library)
2614

27-
License Information
28-
-------------------
2915

30-
This product is _**open source**_!
16+
The [SparkFun Spectral UV Sensor - AS7331 (SEN-23517)](https://www.sparkfun.com/sparkfun-spectral-uv-sensor-as7331-qwiic.html) and [SEN-23518](https://www.sparkfun.com/sparkfun-mini-spectral-uv-sensor-as7331-qwiic.html) 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.
3117

32-
Please review the LICENSE.md file for license information.
18+
Looking for the board that matches this library - pick up a [SparkFun Spectral UV Sensor - AS7331 (SEN-23517)](https://www.sparkfun.com/sparkfun-spectral-uv-sensor-as7331-qwiic.html) or a [SparkFun Mini Spectral UV Sensor - AS7331 (SEN-23518)](https://www.sparkfun.com/sparkfun-mini-spectral-uv-sensor-as7331-qwiic.html) at www.sparkfun.com.
3319

34-
If you have any questions or concerns on licensing, please contact technical support on our [SparkFun forums](https://forum.sparkfun.com/viewforum.php?f=152).
20+
## Functionality
3521

36-
Distributed as-is; no warranty is given.
22+
This library provides an interface that enables the following functionality when using one of the SparkFun Spectral UV Sensor - AS7331 Sensors:
3723

38-
- Your friends at SparkFun.
24+
* Read values from the three UV channels of the sensor (UVA, UVB, UVC) separately or as a combined value
25+
* Adjust the sensors gain values
26+
* Set sensors clock speed and timing values
27+
* Change the operating modes of the sensor
28+
* Control the sensors power-down state
29+
* Change the I2C address of the sensor
30+
31+
## General Use
32+
33+
The following outlines the general use of the library in an Arduino Sketch.
34+
35+
### Declaration
36+
37+
At the start of your sketch, the library header file is included using the following statement:
38+
39+
~~~cpp
40+
#include <SparkFun_AS7331.h>
41+
~~~
42+
43+
Before the arduino ```setup()``` function, create a Soil Sensor object in your file with the following declaration:
44+
45+
~~~c
46+
SfeAS7331ArdI2C myUVSensor // Create an instance of the sensor class
47+
~~~
48+
49+
### Initialization
50+
51+
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.
52+
53+
~~~cpp
54+
// Initialize sensor and run default setup.
55+
if (myUVSensor.begin() == false)
56+
{
57+
Serial.println("Sensor failed to begin. Please check your wiring!");
58+
Serial.println("Halting...");
59+
while (1)
60+
;
61+
}
62+
~~~
63+
64+
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.
65+
66+
After the sensor is initialized, any configuration values are normally set. The following demonstrates how to configure the operating mode of the sensor.
67+
68+
~~~cpp
69+
// Set measurement mode and change device operating mode to measure.
70+
if (myUVSensor.prepareMeasurement(MEAS_MODE_CMD) == false)
71+
{
72+
Serial.println("Sensor did not get set properly.");
73+
Serial.println("Halting...");
74+
while (1)
75+
;
76+
}
77+
~~~
78+
79+
The above command sets up the sensor to operate in a *OneShot* mode.
80+
81+
### Usage
82+
83+
#### Read Value
84+
85+
To read a value, the first step is to start a measurement, and wait for at least the sensors "conversion time" to expire.
86+
87+
~~~cpp
88+
// Send a start measurement command.
89+
if (ksfTkErrOk != myUVSensor.setStartState(true))
90+
Serial.println("Error starting reading!");
91+
92+
// Wait for a bit longer than the conversion time.
93+
delay(2 + myUVSensor.getConversionTimeMillis());
94+
~~~
95+
96+
Once the conversion period is complete, the values are read from the device with the following command:
97+
98+
~~~cpp
99+
// Read UV values.
100+
if (ksfTkErrOk != myUVSensor.readAllUV())
101+
Serial.println("Error reading UV.");
102+
~~~
103+
104+
At this point the values from the sensor are available locally in the library and accessed using the following commands:
105+
106+
~~~cpp
107+
Serial.print("UVA:");
108+
Serial.print(myUVSensor.getUVA());
109+
Serial.print(" UVB:");
110+
Serial.print(myUVSensor.getUVB());
111+
Serial.print(" UVC:");
112+
Serial.println(myUVSensor.getUVC());
113+
~~~
114+
115+
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.
116+
117+
## Examples
118+
119+
The following examples are provided with the library
120+
121+
- [Basic One Shot](examples/Example01_Basic_OneShot/Example01_Basic_OneShot.ino) - This example shows how operate the AS7331 in the default CMD mode.
122+
- [CONT Mode](examples/Example02_CONT_Mode/Example02_CONT_Mode.ino) - 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.
123+
- [SYNS Mode](examples/Example_03_LEDFlashMoisture/Example_03_LEDFlashMoisture.ino) - 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.
124+
- [SYND Mode](examples/Example04_SYND_Mode/Example04_SYND_Mode.ino) - 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.
125+
126+
## Documentation
127+
128+
The full API and use documentation for this library is provided [here](http://docs.sparkfun.com/SparkFun_AS7331_Arduino_Library/). For a quick reference, the main methods available in the library are listed [here](https://docs.sparkfun.com/SparkFun_AS7331_Arduino_Library/class_sfe_a_s7331_ard_i2_c.html).
129+
130+
Curious about the hardware this board works with - visit the SparkFun Spectral UV Sensor [hardware repository](https://github.com/sparkfun/SparkFun_Spectral_UV_Sensor_AS7331).
131+
132+
The Hookup Guide for the SparkFun Spectral UV Sensor is available [here](https://sparkfun.github.io/SparkFun_Spectral_UV_Sensor_AS7331).
133+
134+
## License Information
135+
136+
This product is ***open source***!
137+
138+
This product is licensed using the [MIT Open Source License](https://opensource.org/license/mit).

docs/images/gh-banner-2025-as7331.png

401 KB
Loading
Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
Using the AMS AS7331 Spectral UV Sensor in Command/One Shot (CMD) Mode.
33
4-
This example shows how operate the AS7331 in the default CMD mode. The start
4+
This example shows how operate the AS7331 in the default CMD mode. The start
55
command is sent, then delays until the conversion time has passed before
66
reading out the UV values.
77
88
By: Alex Brudner
99
SparkFun Electronics
1010
Date: 2023/11/17
1111
SparkFun code, firmware, and software is released under the MIT License.
12-
Please see LICENSE.md for further details.
12+
Please see LICENSE.md for further details.
1313
1414
Hardware Connections:
1515
IoT RedBoard --> AS7331
@@ -23,57 +23,65 @@
2323
*/
2424

2525
#include <Arduino.h>
26-
#include <Wire.h>
2726
#include <SparkFun_AS7331.h>
27+
#include <Wire.h>
2828

2929
SfeAS7331ArdI2C myUVSensor;
3030

31-
void setup() {
32-
Serial.begin(115200);
33-
while(!Serial){delay(100);};
34-
Serial.println("AS7331 UV A/B/C Command (One-shot) mode Example.");
35-
36-
Wire.begin();
37-
38-
// Initialize sensor and run default setup.
39-
if(myUVSensor.begin() == false) {
40-
Serial.println("Sensor failed to begin. Please check your wiring!");
41-
Serial.println("Halting...");
42-
while(1);
43-
}
44-
45-
Serial.println("Sensor began.");
46-
47-
// Set measurement mode and change device operating mode to measure.
48-
if(myUVSensor.prepareMeasurement(MEAS_MODE_CMD) == false) {
49-
Serial.println("Sensor did not get set properly.");
50-
Serial.println("Halting...");
51-
while(1);
52-
}
53-
54-
Serial.println("Set mode to command.");
31+
void setup()
32+
{
33+
Serial.begin(115200);
34+
while (!Serial)
35+
{
36+
delay(100);
37+
};
38+
Serial.println("AS7331 UV A/B/C Command (One-shot) mode Example.");
39+
40+
Wire.begin();
41+
42+
// Initialize sensor and run default setup.
43+
if (myUVSensor.begin() == false)
44+
{
45+
Serial.println("Sensor failed to begin. Please check your wiring!");
46+
Serial.println("Halting...");
47+
while (1)
48+
;
49+
}
50+
51+
Serial.println("Sensor began.");
52+
53+
// Set measurement mode and change device operating mode to measure.
54+
if (myUVSensor.prepareMeasurement(MEAS_MODE_CMD) == false)
55+
{
56+
Serial.println("Sensor did not get set properly.");
57+
Serial.println("Halting...");
58+
while (1)
59+
;
60+
}
61+
62+
Serial.println("Set mode to command.");
5563
}
5664

57-
void loop() {
58-
59-
// Send a start measurement command.
60-
if(kSTkErrOk != myUVSensor.setStartState(true))
61-
Serial.println("Error starting reading!");
62-
63-
// Wait for a bit longer than the conversion time.
64-
delay(2+myUVSensor.getConversionTimeMillis());
65+
void loop()
66+
{
67+
68+
// Send a start measurement command.
69+
if (ksfTkErrOk != myUVSensor.setStartState(true))
70+
Serial.println("Error starting reading!");
6571

66-
// Read UV values.
67-
if(kSTkErrOk != myUVSensor.readAllUV())
68-
Serial.println("Error reading UV.");
72+
// Wait for a bit longer than the conversion time.
73+
delay(2 + myUVSensor.getConversionTimeMillis());
6974

70-
Serial.print("UVA:");
71-
Serial.print(myUVSensor.getUVA());
72-
Serial.print(" UVB:");
73-
Serial.print(myUVSensor.getUVB());
74-
Serial.print(" UVC:");
75-
Serial.println(myUVSensor.getUVC());
75+
// Read UV values.
76+
if (ksfTkErrOk != myUVSensor.readAllUV())
77+
Serial.println("Error reading UV.");
7678

77-
delay(2000);
79+
Serial.print("UVA:");
80+
Serial.print(myUVSensor.getUVA());
81+
Serial.print(" UVB:");
82+
Serial.print(myUVSensor.getUVB());
83+
Serial.print(" UVC:");
84+
Serial.println(myUVSensor.getUVC());
7885

86+
delay(2000);
7987
}

0 commit comments

Comments
 (0)