Skip to content

Port to SparkFun Toolkit v1.0 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 53 additions & 45 deletions examples/Example01_Basic_OneShot/Example01_Basic_OneShot.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
Using the AMS AS7331 Spectral UV Sensor in Command/One Shot (CMD) Mode.

This example shows how operate the AS7331 in the default CMD mode. The start
This example shows how operate the AS7331 in the default CMD mode. The start
command is sent, then delays until the conversion time has passed before
reading out the UV values.

By: Alex Brudner
SparkFun Electronics
Date: 2023/11/17
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Please see LICENSE.md for further details.

Hardware Connections:
IoT RedBoard --> AS7331
Expand All @@ -23,57 +23,65 @@
*/

#include <Arduino.h>
#include <Wire.h>
#include <SparkFun_AS7331.h>
#include <Wire.h>

SfeAS7331ArdI2C myUVSensor;

void setup() {
Serial.begin(115200);
while(!Serial){delay(100);};
Serial.println("AS7331 UV A/B/C Command (One-shot) mode Example.");

Wire.begin();

// 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);
}

Serial.println("Sensor began.");

// 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);
}

Serial.println("Set mode to command.");
void setup()
{
Serial.begin(115200);
while (!Serial)
{
delay(100);
};
Serial.println("AS7331 UV A/B/C Command (One-shot) mode Example.");

Wire.begin();

// 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)
;
}

Serial.println("Sensor began.");

// 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)
;
}

Serial.println("Set mode to command.");
}

void loop() {

// Send a start measurement command.
if(kSTkErrOk != myUVSensor.setStartState(true))
Serial.println("Error starting reading!");

// Wait for a bit longer than the conversion time.
delay(2+myUVSensor.getConversionTimeMillis());
void loop()
{

// Send a start measurement command.
if (ksfTkErrOk != myUVSensor.setStartState(true))
Serial.println("Error starting reading!");

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

Serial.print("UVA:");
Serial.print(myUVSensor.getUVA());
Serial.print(" UVB:");
Serial.print(myUVSensor.getUVB());
Serial.print(" UVC:");
Serial.println(myUVSensor.getUVC());
// Read UV values.
if (ksfTkErrOk != myUVSensor.readAllUV())
Serial.println("Error reading UV.");

delay(2000);
Serial.print("UVA:");
Serial.print(myUVSensor.getUVA());
Serial.print(" UVB:");
Serial.print(myUVSensor.getUVB());
Serial.print(" UVC:");
Serial.println(myUVSensor.getUVC());

delay(2000);
}
131 changes: 71 additions & 60 deletions examples/Example02_CONT_Mode/Example02_CONT_Mode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
SparkFun Electronics
Date: 2023/11/27
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Please see LICENSE.md for further details.

Hardware Connections:
IoT RedBoard --> AS7331
Expand All @@ -24,77 +24,88 @@
*/

#include <Arduino.h>
#include <Wire.h>
#include <SparkFun_AS7331.h>
#include <Wire.h>

SfeAS7331ArdI2C myUVSensor;

const uint8_t interruptPin = 26;
volatile bool newDataReady = false;

void setup() {
Serial.begin(115200);
while(!Serial){delay(100);};
Serial.println("AS7331 UV A/B/C Continuous mode example.");

Wire.begin();

// Configure Interrupt.
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyInterrupt, RISING);

// 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);
}

Serial.println("Sensor began.");

// Set the delay between measurements so that the processor can read out the
// results without interfering with the ADC.
// Set break time to 900us (112 * 8us) to account for the time it takes to poll data.
if(kSTkErrOk != myUVSensor.setBreakTime(112)) {
Serial.println("Sensor did not set break time properly.");
Serial.println("Halting...");
while(1);
}

// Set measurement mode and change device operating mode to measure.
if(myUVSensor.prepareMeasurement(MEAS_MODE_CONT) == false) {
Serial.println("Sensor did not get set properly.");
Serial.println("Spinning...");
while(1);
}

Serial.println("Set mode to continuous. Starting measurement...");

// Begin measurement.
if(kSTkErrOk != myUVSensor.setStartState(true))
Serial.println("Error starting reading!");

void setup()
{
Serial.begin(115200);
while (!Serial)
{
delay(100);
};
Serial.println("AS7331 UV A/B/C Continuous mode example.");

Wire.begin();

// Configure Interrupt.
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyInterrupt, RISING);

// 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)
;
}

Serial.println("Sensor began.");

// Set the delay between measurements so that the processor can read out the
// results without interfering with the ADC.
// Set break time to 900us (112 * 8us) to account for the time it takes to poll data.
if (ksfTkErrOk != myUVSensor.setBreakTime(112))
{
Serial.println("Sensor did not set break time properly.");
Serial.println("Halting...");
while (1)
;
}

// Set measurement mode and change device operating mode to measure.
if (myUVSensor.prepareMeasurement(MEAS_MODE_CONT) == false)
{
Serial.println("Sensor did not get set properly.");
Serial.println("Spinning...");
while (1)
;
}

Serial.println("Set mode to continuous. Starting measurement...");

// Begin measurement.
if (ksfTkErrOk != myUVSensor.setStartState(true))
Serial.println("Error starting reading!");
}

void loop() {
void loop()
{

// If an interrupt has been generated...
if(newDataReady) {
newDataReady = false;
// If an interrupt has been generated...
if (newDataReady)
{
newDataReady = false;

if(kSTkErrOk != myUVSensor.readAllUV())
Serial.println("Error reading UV.");

Serial.print("UVA:");
Serial.print(myUVSensor.getUVA());
Serial.print(" UVB:");
Serial.print(myUVSensor.getUVB());
Serial.print(" UVC:");
Serial.println(myUVSensor.getUVC());
}
if (ksfTkErrOk != myUVSensor.readAllUV())
Serial.println("Error reading UV.");

Serial.print("UVA:");
Serial.print(myUVSensor.getUVA());
Serial.print(" UVB:");
Serial.print(myUVSensor.getUVB());
Serial.print(" UVC:");
Serial.println(myUVSensor.getUVC());
}
}

void dataReadyInterrupt() {
newDataReady = true;
void dataReadyInterrupt()
{
newDataReady = true;
}
Loading