11#include " Adafruit_AS7331.h"
22
3- #define AS7331_SENS_UVA 385 .0f // counts/(µW/cm²) at GAIN=2048x, TIME=64ms
3+ /* *
4+ * Base irradiance responsivity values from AS7331 datasheet (DS001047 v4-00)
5+ * Table in Section 5 "Electrical Characteristics", parameter ReGAIN2048.
6+ * Units: counts per (µW/cm²) at GAIN=2048x, TIME=64ms (65536 clocks),
7+ * fCLK=1.024MHz
8+ *
9+ * These are typical values at the peak wavelengths:
10+ * UVA (λ=360nm): 385 counts/(µW/cm²)
11+ * UVB (λ=300nm): 347 counts/(µW/cm²)
12+ * UVC (λ=260nm): 794 counts/(µW/cm²)
13+ */
14+ #define AS7331_SENS_UVA 385 .0f
415#define AS7331_SENS_UVB 347 .0f
516#define AS7331_SENS_UVC 794 .0f
617
@@ -183,6 +194,28 @@ bool Adafruit_AS7331::readAllUV(uint16_t *uva, uint16_t *uvb, uint16_t *uvc) {
183194 return true ;
184195}
185196
197+ /* *
198+ * Convert raw ADC counts to irradiance in µW/cm².
199+ *
200+ * Algorithm from AS7331 datasheet (DS001047 v4-00), Section 7.4
201+ * "Measurement Result".
202+ *
203+ * The effective sensitivity scales with gain and integration time:
204+ * effective_sens = base_sens × (gain_factor / 2048) × (time_factor)
205+ *
206+ * Where:
207+ * - base_sens: Responsivity at GAIN=2048x, TIME=64ms (from datasheet Table 5)
208+ * - gain_factor = 2^(11 - gain_setting), ranging from 1 (GAIN_1X) to 2048
209+ * (GAIN_2048X)
210+ * - time_factor = 2^time_setting / 64, where TIME_64MS=6 gives factor=1.0
211+ *
212+ * Irradiance = counts / effective_sensitivity
213+ *
214+ * @param counts Raw ADC value from MRES register
215+ * @param baseSensitivity Base responsivity for the channel
216+ * (AS7331_SENS_UVA/B/C)
217+ * @return Irradiance in µW/cm²
218+ */
186219float Adafruit_AS7331::_countsToIrradiance (uint16_t counts,
187220 float baseSensitivity) {
188221 // Use cached values instead of reading registers
@@ -201,21 +234,44 @@ float Adafruit_AS7331::_countsToIrradiance(uint16_t counts,
201234 return (float )counts / effective_sens;
202235}
203236
237+ /* *
238+ * Read UVA channel and convert to irradiance (µW/cm²).
239+ * Uses cached gain/time settings for conversion.
240+ * @return UVA irradiance in µW/cm²
241+ */
204242float Adafruit_AS7331::readUVA_uWcm2 (void ) {
205243 uint16_t counts = readUVA ();
206244 return _countsToIrradiance (counts, AS7331_SENS_UVA);
207245}
208246
247+ /* *
248+ * Read UVB channel and convert to irradiance (µW/cm²).
249+ * Uses cached gain/time settings for conversion.
250+ * @return UVB irradiance in µW/cm²
251+ */
209252float Adafruit_AS7331::readUVB_uWcm2 (void ) {
210253 uint16_t counts = readUVB ();
211254 return _countsToIrradiance (counts, AS7331_SENS_UVB);
212255}
213256
257+ /* *
258+ * Read UVC channel and convert to irradiance (µW/cm²).
259+ * Uses cached gain/time settings for conversion.
260+ * @return UVC irradiance in µW/cm²
261+ */
214262float Adafruit_AS7331::readUVC_uWcm2 (void ) {
215263 uint16_t counts = readUVC ();
216264 return _countsToIrradiance (counts, AS7331_SENS_UVC);
217265}
218266
267+ /* *
268+ * Read all UV channels and convert to irradiance (µW/cm²).
269+ * Uses cached gain/time settings for conversion.
270+ * @param uva Optional storage for UVA irradiance.
271+ * @param uvb Optional storage for UVB irradiance.
272+ * @param uvc Optional storage for UVC irradiance.
273+ * @return True on success, false on read failure.
274+ */
219275bool Adafruit_AS7331::readAllUV_uWcm2 (float *uva, float *uvb, float *uvc) {
220276 uint16_t uva_raw = 0 ;
221277 uint16_t uvb_raw = 0 ;
0 commit comments