├── .gitattributes ├── library.properties ├── examples ├── Example6_ReadTemperature │ └── Example6_ReadTemperature.ino ├── Example7_GetVersion │ └── Example7_GetVersion.ino ├── Example1_BasicReadings │ └── Example1_BasicReadings.ino ├── Example4_ReadRawValues │ └── Example4_ReadRawValues.ino ├── Example2_BasicReadingsWithLEDs │ └── Example2_BasicReadingsWithLEDs.ino ├── Example5_MaxReadRate │ └── Example5_MaxReadRate.ino └── Example3_Settings │ └── Example3_Settings.ino ├── README.md ├── LICENSE.md ├── .gitignore ├── keywords.txt └── src ├── SparkFun_AS7265X.h └── SparkFun_AS7265X.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=SparkFun Spectral Triad AS7265X 2 | version=1.0.5 3 | author=Nathan Seidle and Kevin Kuwata 4 | maintainer=SparkFun Electronics 5 | sentence=An Arduino library for the AS7265x Triple Spectroscopy Sensor Board 6 | paragraph=The SparkFun Spectral Triad detects 18 discrete channels of light from 372nm (UV) to 966nm (IR) enabling desktop spectroscopy and other fun applications like counterfeit dectection! Checkout the SparkFun Triad Sensor! 7 | category=Sensors 8 | url=https://github.com/sparkfun/SparkFun_AS7265X_Arduino_Library 9 | architectures=* 10 | -------------------------------------------------------------------------------- /examples/Example6_ReadTemperature/Example6_ReadTemperature.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example shows how to read the temperature of the ICs 10 | 11 | Feel like supporting open source hardware? 12 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 13 | 14 | Hardware Connections: 15 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 16 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 17 | Open the serial monitor at 115200 baud to see the output 18 | */ 19 | 20 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 21 | AS7265X sensor; 22 | 23 | #include 24 | 25 | void setup() 26 | { 27 | Serial.begin(115200); 28 | Serial.println("AS7265x Spectral Triad Example"); 29 | 30 | if (sensor.begin() == false) 31 | { 32 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 33 | while (1) 34 | ; 35 | } 36 | 37 | //Once the sensor is started we can increase the I2C speed 38 | Wire.setClock(400000); 39 | 40 | int oneSensorTemp = sensor.getTemperature(); //Returns the temperature of master IC 41 | Serial.print("Main IC temp: "); 42 | Serial.println(oneSensorTemp); 43 | 44 | float threeSensorTemp = sensor.getTemperatureAverage(); //Returns the average temperature of all three ICs 45 | Serial.print("Average IC temp: "); 46 | Serial.println(threeSensorTemp, 2); 47 | } 48 | 49 | void loop() 50 | { 51 | //Do nothing 52 | } 53 | -------------------------------------------------------------------------------- /examples/Example7_GetVersion/Example7_GetVersion.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example shows how to output the current hardware and firmware version of the Triad. 10 | 11 | Feel like supporting open source hardware? 12 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 13 | 14 | Hardware Connections: 15 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 16 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 17 | Open the serial monitor at 115200 baud to see the output 18 | */ 19 | 20 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 21 | AS7265X sensor; 22 | 23 | #include 24 | 25 | void setup() 26 | { 27 | Serial.begin(115200); 28 | Serial.println("AS7265x Spectral Triad Example"); 29 | 30 | if (sensor.begin() == false) 31 | { 32 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 33 | while (1) 34 | ; 35 | } 36 | 37 | byte deviceType = sensor.getDeviceType(); 38 | Serial.print("AMS Device Type: 0x"); 39 | Serial.println(deviceType, HEX); 40 | 41 | byte hardwareVersion = sensor.getHardwareVersion(); 42 | Serial.print("AMS Hardware Version: 0x"); 43 | Serial.println(hardwareVersion, HEX); 44 | 45 | byte majorFirmwareVersion = sensor.getMajorFirmwareVersion(); 46 | Serial.print("Major Firmware Version: 0x"); 47 | Serial.println(majorFirmwareVersion, HEX); 48 | 49 | byte patchFirmwareVersion = sensor.getPatchFirmwareVersion(); 50 | Serial.print("Patch Firmware Version: 0x"); 51 | Serial.println(patchFirmwareVersion, HEX); 52 | 53 | byte buildFirmwareVersion = sensor.getBuildFirmwareVersion(); 54 | Serial.print("Build Firmware Version: 0x"); 55 | Serial.println(buildFirmwareVersion, HEX); 56 | } 57 | 58 | void loop() 59 | { 60 | //Do nothing 61 | } 62 | -------------------------------------------------------------------------------- /examples/Example1_BasicReadings/Example1_BasicReadings.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example takes all 18 readings, 372nm to 966nm, over I2C and outputs 10 | them to the serial port. 11 | 12 | Feel like supporting open source hardware? 13 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 14 | 15 | Hardware Connections: 16 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 17 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 18 | Open the serial monitor at 115200 baud to see the output 19 | */ 20 | 21 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 22 | AS7265X sensor; 23 | 24 | void setup() 25 | { 26 | Serial.begin(115200); 27 | Serial.println("AS7265x Spectral Triad Example"); 28 | 29 | if (sensor.begin() == false) 30 | { 31 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 32 | while (1) 33 | ; 34 | } 35 | 36 | Serial.println("A,B,C,D,E,F,G,H,R,I,S,J,T,U,V,W,K,L"); 37 | } 38 | 39 | void loop() 40 | { 41 | sensor.takeMeasurements(); //This is a hard wait while all 18 channels are measured 42 | 43 | Serial.print(sensor.getCalibratedA()); //410nm 44 | Serial.print(","); 45 | Serial.print(sensor.getCalibratedB()); //435nm 46 | Serial.print(","); 47 | Serial.print(sensor.getCalibratedC()); //460nm 48 | Serial.print(","); 49 | Serial.print(sensor.getCalibratedD()); //485nm 50 | Serial.print(","); 51 | Serial.print(sensor.getCalibratedE()); //510nm 52 | Serial.print(","); 53 | Serial.print(sensor.getCalibratedF()); //535nm 54 | Serial.print(","); 55 | 56 | Serial.print(sensor.getCalibratedG()); //560nm 57 | Serial.print(","); 58 | Serial.print(sensor.getCalibratedH()); //585nm 59 | Serial.print(","); 60 | Serial.print(sensor.getCalibratedR()); //610nm 61 | Serial.print(","); 62 | Serial.print(sensor.getCalibratedI()); //645nm 63 | Serial.print(","); 64 | Serial.print(sensor.getCalibratedS()); //680nm 65 | Serial.print(","); 66 | Serial.print(sensor.getCalibratedJ()); //705nm 67 | Serial.print(","); 68 | 69 | Serial.print(sensor.getCalibratedT()); //730nm 70 | Serial.print(","); 71 | Serial.print(sensor.getCalibratedU()); //760nm 72 | Serial.print(","); 73 | Serial.print(sensor.getCalibratedV()); //810nm 74 | Serial.print(","); 75 | Serial.print(sensor.getCalibratedW()); //860nm 76 | Serial.print(","); 77 | Serial.print(sensor.getCalibratedK()); //900nm 78 | Serial.print(","); 79 | Serial.print(sensor.getCalibratedL()); //940nm 80 | Serial.print(","); 81 | 82 | Serial.println(); 83 | } 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SparkFun Triad Spectroscopy Sensor - AS7265x (Qwiic) 2 | =========================================================== 3 | 4 | [![SparkFun Triad Spectroscopy Sensor](https://cdn.sparkfun.com//assets/parts/1/3/3/9/3/15050-SparkFun_Triad_Spectroscopy_Sensor_-_AS7265x__Qwiic_-01.jpg)](https://www.sparkfun.com/products/15050) 5 | 6 | [*SparkFun Triad Spectroscopy Sensor (SEN-15050)*](https://www.sparkfun.com/products/15050) 7 | 8 | This is an Arduino library to control the AS7265X Spectral Sensors, which can be configured via I2C. You can download this library via the Arduino library manager by searching '*SparkFun Triad*'. 9 | 10 | The SparkFun Triad Spectroscopy Sensor is a powerful optical inspection sensor. Three AS7265x sensors are combined alongside a visible, UV, and IR LEDs to illuminate and test various surfaces for light spectroscopy. The Triad detects 18 discrete channels of light from 372nm (UV) to 966nm (IR) enabling desktop spectroscopy. 11 | 12 | What can you do with light spectroscopy? It’s an amazing field of study, and the SparkFun Triad brings what used to be prohibitively expensive equipment to the desktop. The AS7265x should not be confused with highly complex mass spectrometers, but the sensor array does give the user the ability to measure and characterize how different materials absorb and reflect 18 different frequencies of light. 13 | 14 | Want to use this board using python? Checkout hwreverse [pySectralTriad](https://github.com/hwreverse/pySpectralTriad)! 15 | 16 | **Thanks to:** 17 | 18 | * **pySpectralTriad** for the python application 19 | 20 | Repository Contents 21 | ------------------- 22 | 23 | * **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE. 24 | * **/src** - Source files for the library (.cpp, .h). 25 | * **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE. 26 | * **library.properties** - General library properties for the Arduino package manager. 27 | 28 | Documentation 29 | -------------- 30 | * **[Arduino Library](https://github.com/sparkfun/SparkFun_AS7265x_Arduino_Library)** - Arduino Library for the SparkFun Triad Spectroscopy Sensor. 31 | * **[GitHub Product Repo](https://github.com/sparkfun/Qwiic_Spectral_Sensor_AS7265x)** - Product repo for the SparkFun Triad Spectroscopy Sensor. 32 | * **[Hookup Guide](https://learn.sparkfun.com/tutorials/spectral-triad-as7265x-hookup-guide)** - Basic hookup guide for the SparkFun Triad Spectroscopy Sensor. 33 | 34 | License Information 35 | ------------------- 36 | 37 | This product is _**open source**_! 38 | 39 | Various bits of the code have different licenses applied. Anything SparkFun wrote is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round! 40 | 41 | Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license. 42 | 43 | Distributed as-is; no warranty is given. 44 | 45 | - Your friends at SparkFun. 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | SparkFun License Information 2 | ============================ 3 | 4 | SparkFun uses two different licenses for our files — one for hardware and one for code. 5 | 6 | Hardware 7 | --------- 8 | 9 | **SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** 10 | 11 | Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). 12 | 13 | You are free to: 14 | 15 | Share — copy and redistribute the material in any medium or format 16 | Adapt — remix, transform, and build upon the material 17 | for any purpose, even commercially. 18 | The licensor cannot revoke these freedoms as long as you follow the license terms. 19 | Under the following terms: 20 | 21 | Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. 22 | ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. 23 | No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. 24 | Notices: 25 | 26 | You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. 27 | No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 28 | 29 | 30 | Code 31 | -------- 32 | 33 | **SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).** 34 | 35 | The MIT License (MIT) 36 | 37 | Copyright (c) 2016 SparkFun Electronics 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy 40 | of this software and associated documentation files (the "Software"), to deal 41 | in the Software without restriction, including without limitation the rights 42 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 43 | copies of the Software, and to permit persons to whom the Software is 44 | furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in all 47 | copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 51 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 52 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 55 | SOFTWARE. 56 | -------------------------------------------------------------------------------- /examples/Example4_ReadRawValues/Example4_ReadRawValues.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example shows how to output the raw sensor values. This is probably never needed since the 10 | calibrated values are tuned to each sensor. But it does run faster (2 bytes per channel instead of 4) 11 | 12 | Feel like supporting open source hardware? 13 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 14 | 15 | Hardware Connections: 16 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 17 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 18 | Open the serial monitor at 115200 baud to see the output 19 | */ 20 | 21 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 22 | AS7265X sensor; 23 | 24 | #include 25 | 26 | void setup() 27 | { 28 | Serial.begin(115200); 29 | Serial.println("AS7265x Spectral Triad Example"); 30 | 31 | if (sensor.begin() == false) 32 | { 33 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 34 | while (1) 35 | ; 36 | } 37 | 38 | //Once the sensor is started we can increase the I2C speed 39 | Wire.setClock(400000); 40 | 41 | sensor.disableIndicator(); 42 | 43 | Serial.println("A,B,C,D,E,F,G,H,R,I,S,J,T,U,V,W,K,L"); 44 | } 45 | 46 | void loop() 47 | { 48 | sensor.takeMeasurements(); //This is a hard wait while all 18 channels are measured 49 | 50 | Serial.print(sensor.getA()); //410nm 51 | Serial.print(","); 52 | Serial.print(sensor.getB()); //435nm 53 | Serial.print(","); 54 | Serial.print(sensor.getC()); //460nm 55 | Serial.print(","); 56 | Serial.print(sensor.getD()); //485nm 57 | Serial.print(","); 58 | Serial.print(sensor.getE()); //510nm 59 | Serial.print(","); 60 | Serial.print(sensor.getF()); //535nm 61 | Serial.print(","); 62 | 63 | Serial.print(sensor.getG()); //560nm 64 | Serial.print(","); 65 | Serial.print(sensor.getH()); //585nm 66 | Serial.print(","); 67 | Serial.print(sensor.getR()); //610nm 68 | Serial.print(","); 69 | Serial.print(sensor.getI()); //645nm 70 | Serial.print(","); 71 | Serial.print(sensor.getS()); //680nm 72 | Serial.print(","); 73 | Serial.print(sensor.getJ()); //705nm 74 | Serial.print(","); 75 | 76 | Serial.print(sensor.getT()); //730nm 77 | Serial.print(","); 78 | Serial.print(sensor.getU()); //760nm 79 | Serial.print(","); 80 | Serial.print(sensor.getV()); //810nm 81 | Serial.print(","); 82 | Serial.print(sensor.getW()); //860nm 83 | Serial.print(","); 84 | Serial.print(sensor.getK()); //900nm 85 | Serial.print(","); 86 | Serial.print(sensor.getL()); //940nm 87 | Serial.print(","); 88 | 89 | Serial.println(); 90 | } 91 | -------------------------------------------------------------------------------- /examples/Example2_BasicReadingsWithLEDs/Example2_BasicReadingsWithLEDs.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example takes all 18 readings and blinks the illumination LEDs 10 | as it goes. We recommend you point the Triad away from your eyes, the LEDs are *bright*. 11 | 12 | Feel like supporting open source hardware? 13 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 14 | 15 | Hardware Connections: 16 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 17 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 18 | Open the serial monitor at 115200 baud to see the output 19 | */ 20 | 21 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 22 | AS7265X sensor; 23 | 24 | #include 25 | 26 | void setup() 27 | { 28 | Serial.begin(115200); 29 | Serial.println("AS7265x Spectral Triad Example"); 30 | 31 | Serial.println("Point the Triad away and press a key to begin with illumination..."); 32 | while (Serial.available() == false) 33 | { 34 | } //Do nothing while we wait for user to press a key 35 | Serial.read(); //Throw away the user's button 36 | 37 | if (sensor.begin() == false) 38 | { 39 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 40 | while (1) 41 | ; 42 | } 43 | 44 | sensor.disableIndicator(); //Turn off the blue status LED 45 | 46 | Serial.println("A,B,C,D,E,F,G,H,R,I,S,J,T,U,V,W,K,L"); 47 | } 48 | 49 | void loop() 50 | { 51 | sensor.takeMeasurementsWithBulb(); //This is a hard wait while all 18 channels are measured 52 | 53 | Serial.print(sensor.getCalibratedA()); //410nm 54 | Serial.print(","); 55 | Serial.print(sensor.getCalibratedB()); //435nm 56 | Serial.print(","); 57 | Serial.print(sensor.getCalibratedC()); //460nm 58 | Serial.print(","); 59 | Serial.print(sensor.getCalibratedD()); //485nm 60 | Serial.print(","); 61 | Serial.print(sensor.getCalibratedE()); //510nm 62 | Serial.print(","); 63 | Serial.print(sensor.getCalibratedF()); //535nm 64 | Serial.print(","); 65 | 66 | Serial.print(sensor.getCalibratedG()); //560nm 67 | Serial.print(","); 68 | Serial.print(sensor.getCalibratedH()); //585nm 69 | Serial.print(","); 70 | Serial.print(sensor.getCalibratedR()); //610nm 71 | Serial.print(","); 72 | Serial.print(sensor.getCalibratedI()); //645nm 73 | Serial.print(","); 74 | Serial.print(sensor.getCalibratedS()); //680nm 75 | Serial.print(","); 76 | Serial.print(sensor.getCalibratedJ()); //705nm 77 | Serial.print(","); 78 | 79 | Serial.print(sensor.getCalibratedT()); //730nm 80 | Serial.print(","); 81 | Serial.print(sensor.getCalibratedU()); //760nm 82 | Serial.print(","); 83 | Serial.print(sensor.getCalibratedV()); //810nm 84 | Serial.print(","); 85 | Serial.print(sensor.getCalibratedW()); //860nm 86 | Serial.print(","); 87 | Serial.print(sensor.getCalibratedK()); //900nm 88 | Serial.print(","); 89 | Serial.print(sensor.getCalibratedL()); //940nm 90 | Serial.print(","); 91 | 92 | Serial.println(); 93 | } 94 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## SparkFun Useful stuff 3 | ################# 4 | 5 | ## AVR Development 6 | *.eep 7 | *.elf 8 | *.lst 9 | *.lss 10 | *.sym 11 | *.d 12 | *.o 13 | *.srec 14 | *.map 15 | 16 | ## Notepad++ backup files 17 | *.bak 18 | 19 | ## BOM files 20 | *bom* 21 | 22 | ################# 23 | ## Eclipse 24 | ################# 25 | 26 | *.pydevproject 27 | .project 28 | .metadata 29 | bin/ 30 | tmp/ 31 | *.tmp 32 | *.bak 33 | *.swp 34 | *~.nib 35 | local.properties 36 | .classpath 37 | .settings/ 38 | .loadpath 39 | 40 | # External tool builders 41 | .externalToolBuilders/ 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | 53 | ############# 54 | ## Eagle 55 | ############# 56 | 57 | # Ignore the board and schematic backup files and lock files 58 | *.b#? 59 | *.s#? 60 | *.l#? 61 | *.lck 62 | 63 | 64 | ################# 65 | ## Visual Studio 66 | ################# 67 | 68 | ## Ignore Visual Studio temporary files, build results, and 69 | ## files generated by popular Visual Studio add-ons. 70 | 71 | # User-specific files 72 | *.suo 73 | *.user 74 | *.sln.docstates 75 | 76 | # Build results 77 | [Dd]ebug/ 78 | [Rr]elease/ 79 | *_i.c 80 | *_p.c 81 | *.ilk 82 | *.meta 83 | *.obj 84 | *.pch 85 | *.pdb 86 | *.pgc 87 | *.pgd 88 | *.rsp 89 | *.sbr 90 | *.tlb 91 | *.tli 92 | *.tlh 93 | *.tmp 94 | *.vspscc 95 | .builds 96 | *.dotCover 97 | 98 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 99 | #packages/ 100 | 101 | # Visual C++ cache files 102 | ipch/ 103 | *.aps 104 | *.ncb 105 | *.opensdf 106 | *.sdf 107 | 108 | # Visual Studio profiler 109 | *.psess 110 | *.vsp 111 | 112 | # ReSharper is a .NET coding add-in 113 | _ReSharper* 114 | 115 | # Installshield output folder 116 | [Ee]xpress 117 | 118 | # DocProject is a documentation generator add-in 119 | DocProject/buildhelp/ 120 | DocProject/Help/*.HxT 121 | DocProject/Help/*.HxC 122 | DocProject/Help/*.hhc 123 | DocProject/Help/*.hhk 124 | DocProject/Help/*.hhp 125 | DocProject/Help/Html2 126 | DocProject/Help/html 127 | 128 | # Click-Once directory 129 | publish 130 | 131 | # Others 132 | [Bb]in 133 | [Oo]bj 134 | sql 135 | TestResults 136 | *.Cache 137 | ClientBin 138 | stylecop.* 139 | ~$* 140 | *.dbmdl 141 | Generated_Code #added for RIA/Silverlight projects 142 | 143 | # Backup & report files from converting an old project file to a newer 144 | # Visual Studio version. Backup files are not needed, because we have git ;-) 145 | _UpgradeReport_Files/ 146 | Backup*/ 147 | UpgradeLog*.XML 148 | 149 | 150 | ############ 151 | ## Windows 152 | ############ 153 | 154 | # Windows image file caches 155 | Thumbs.db 156 | 157 | # Folder config file 158 | Desktop.ini 159 | 160 | 161 | ############# 162 | ## Mac OS 163 | ############# 164 | 165 | .DS_Store 166 | 167 | 168 | ############# 169 | ## Linux 170 | ############# 171 | 172 | # backup files (*.bak on Win) 173 | *~ 174 | 175 | 176 | ############# 177 | ## Python 178 | ############# 179 | 180 | *.py[co] 181 | 182 | # Packages 183 | *.egg 184 | *.egg-info 185 | dist 186 | build 187 | eggs 188 | parts 189 | bin 190 | var 191 | sdist 192 | develop-eggs 193 | .installed.cfg 194 | 195 | # Installer logs 196 | pip-log.txt 197 | 198 | # Unit test / coverage reports 199 | .coverage 200 | .tox 201 | 202 | #Translations 203 | *.mo 204 | 205 | #Mr Developer 206 | .mr.developer.cfg 207 | -------------------------------------------------------------------------------- /examples/Example5_MaxReadRate/Example5_MaxReadRate.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example shows how to setup the sensor for max, calibrated read rate. 10 | Printing floats is the greatest bottleneck so we increase it to 115200. 11 | 12 | Feel like supporting open source hardware? 13 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 14 | 15 | Hardware Connections: 16 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 17 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 18 | Open the serial monitor at 115200 baud to see the output 19 | */ 20 | 21 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 22 | AS7265X sensor; 23 | 24 | #include 25 | 26 | void setup() { 27 | Serial.begin(115200); 28 | Serial.println("AS7265x Spectral Triad Example"); 29 | 30 | if(sensor.begin() == false) 31 | { 32 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 33 | while(1); 34 | } 35 | 36 | //Once the sensor is started we can increase the I2C speed 37 | Wire.setClock(400000); 38 | 39 | sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_6CHAN_CONTINUOUS); //All 6 channels on all devices 40 | 41 | sensor.setIntegrationCycles(1); 42 | //0 seems to cause the sensors to read very slowly 43 | //1*2.8ms = 5.6ms per reading 44 | //But we need two integration cycles so 89Hz is aproximately the fastest read rate 45 | 46 | sensor.disableIndicator(); 47 | 48 | //Rather than toggle the LEDs with each measurement, turn on LEDs all the time 49 | sensor.enableBulb(AS7265x_LED_WHITE); 50 | sensor.enableBulb(AS7265x_LED_IR); 51 | sensor.enableBulb(AS7265x_LED_UV); 52 | 53 | Serial.println("A,B,C,D,E,F,G,H,R,I,S,J,T,U,V,W,K,L"); 54 | } 55 | 56 | void loop() { 57 | long startTime = millis(); 58 | //We must wait two integration cycles to get all values 59 | while(sensor.dataAvailable() == false) {} //Do nothing 60 | long endTime = millis(); 61 | 62 | float readRate = 1000.0 / (endTime - startTime); 63 | Serial.print(readRate, 2); 64 | Serial.print("Hz,"); 65 | Serial.print(sensor.getCalibratedA()); //410nm 66 | Serial.print(","); 67 | Serial.print(sensor.getCalibratedB()); //435nm 68 | Serial.print(","); 69 | Serial.print(sensor.getCalibratedC()); //460nm 70 | Serial.print(","); 71 | Serial.print(sensor.getCalibratedD()); //485nm 72 | Serial.print(","); 73 | Serial.print(sensor.getCalibratedE()); //510nm 74 | Serial.print(","); 75 | Serial.print(sensor.getCalibratedF()); //535nm 76 | Serial.print(","); 77 | 78 | Serial.print(sensor.getCalibratedG()); //560nm 79 | Serial.print(","); 80 | Serial.print(sensor.getCalibratedH()); //585nm 81 | Serial.print(","); 82 | Serial.print(sensor.getCalibratedR()); //610nm 83 | Serial.print(","); 84 | Serial.print(sensor.getCalibratedI()); //645nm 85 | Serial.print(","); 86 | Serial.print(sensor.getCalibratedS()); //680nm 87 | Serial.print(","); 88 | Serial.print(sensor.getCalibratedJ()); //705nm 89 | Serial.print(","); 90 | 91 | Serial.print(sensor.getCalibratedT()); //730nm 92 | Serial.print(","); 93 | Serial.print(sensor.getCalibratedU()); //760nm 94 | Serial.print(","); 95 | Serial.print(sensor.getCalibratedV()); //810nm 96 | Serial.print(","); 97 | Serial.print(sensor.getCalibratedW()); //860nm 98 | Serial.print(","); 99 | Serial.print(sensor.getCalibratedK()); //900nm 100 | Serial.print(","); 101 | Serial.print(sensor.getCalibratedL()); //940nm 102 | Serial.print(","); 103 | 104 | Serial.println(); 105 | } 106 | 107 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | AS7265X KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | AS7265X KEYWORD2 16 | begin KEYWORD2 17 | isConnected KEYWORD2 18 | getDeviceType KEYWORD2 19 | getHardwareVersion KEYWORD2 20 | getMajorFirmwareVersion KEYWORD2 21 | getPatchFirmwareVersion KEYWORD2 22 | getBuildFirmwareVersion KEYWORD2 23 | getTemperature KEYWORD2 24 | getTemperatureAverage KEYWORD2 25 | takeMeasurements KEYWORD2 26 | takeMeasurementsWithBulb KEYWORD2 27 | enableIndicator KEYWORD2 28 | disableIndicator KEYWORD2 29 | enableBulb KEYWORD2 30 | disableBulb KEYWORD2 31 | setGain KEYWORD2 32 | setMeasurementMode KEYWORD2 33 | setIntegrationCycles KEYWORD2 34 | setBulbCurrent KEYWORD2 35 | setIndicatorCurrent KEYWORD2 36 | enableInterrupt KEYWORD2 37 | disableInterrupt KEYWORD2 38 | softReset KEYWORD2 39 | dataAvailable KEYWORD2 40 | getCalibratedA KEYWORD2 41 | getCalibratedB KEYWORD2 42 | getCalibratedC KEYWORD2 43 | getCalibratedD KEYWORD2 44 | getCalibratedE KEYWORD2 45 | getCalibratedF KEYWORD2 46 | getCalibratedG KEYWORD2 47 | getCalibratedH KEYWORD2 48 | getCalibratedI KEYWORD2 49 | getCalibratedJ KEYWORD2 50 | getCalibratedK KEYWORD2 51 | getCalibratedL KEYWORD2 52 | getCalibratedR KEYWORD2 53 | getCalibratedS KEYWORD2 54 | getCalibratedT KEYWORD2 55 | getCalibratedU KEYWORD2 56 | getCalibratedV KEYWORD2 57 | getCalibratedW KEYWORD2 58 | getA KEYWORD2 59 | getB KEYWORD2 60 | getC KEYWORD2 61 | getD KEYWORD2 62 | getE KEYWORD2 63 | getF KEYWORD2 64 | getG KEYWORD2 65 | getH KEYWORD2 66 | getI KEYWORD2 67 | getJ KEYWORD2 68 | getK KEYWORD2 69 | getL KEYWORD2 70 | getR KEYWORD2 71 | getS KEYWORD2 72 | getT KEYWORD2 73 | getU KEYWORD2 74 | getV KEYWORD2 75 | getW KEYWORD2 76 | 77 | ####################################### 78 | # Constants (LITERAL1) 79 | ####################################### 80 | 81 | _SPARKFUN_AS7265X_H LITERAL1 82 | AS7265X_ADDR LITERAL1 83 | AS7265X_STATUS_REG LITERAL1 84 | AS7265X_WRITE_REG LITERAL1 85 | AS7265X_READ_REG LITERAL1 86 | AS7265X_TX_VALID LITERAL1 87 | AS7265X_RX_VALID LITERAL1 88 | AS7265X_HW_VERSION_HIGH LITERAL1 89 | AS7265X_HW_VERSION_LOW LITERAL1 90 | AS7265X_FW_VERSION_HIGH LITERAL1 91 | AS7265X_FW_VERSION_LOW LITERAL1 92 | AS7265X_CONFIG LITERAL1 93 | AS7265X_INTERGRATION_TIME LITERAL1 94 | AS7265X_DEVICE_TEMP LITERAL1 95 | AS7265X_LED_CONFIG LITERAL1 96 | AS7265X_R_G_A LITERAL1 97 | AS7265X_S_H_B LITERAL1 98 | AS7265X_T_I_C LITERAL1 99 | AS7265X_U_J_D LITERAL1 100 | AS7265X_V_K_E LITERAL1 101 | AS7265X_W_L_F LITERAL1 102 | AS7265X_R_G_A_CAL LITERAL1 103 | AS7265X_S_H_B_CAL LITERAL1 104 | AS7265X_T_I_C_CAL LITERAL1 105 | AS7265X_U_J_D_CAL LITERAL1 106 | AS7265X_V_K_E_CAL LITERAL1 107 | AS7265X_W_L_F_CAL LITERAL1 108 | AS7265X_DEV_SELECT_CONTROL LITERAL1 109 | AS7265X_COEF_DATA_0 LITERAL1 110 | AS7265X_COEF_DATA_1 LITERAL1 111 | AS7265X_COEF_DATA_2 LITERAL1 112 | AS7265X_COEF_DATA_3 LITERAL1 113 | AS7265X_COEF_DATA_READ LITERAL1 114 | AS7265X_COEF_DATA_WRITE LITERAL1 115 | AS7265X_POLLING_DELAY LITERAL1 116 | AS72651_NIR LITERAL1 117 | AS72652_VISIBLE LITERAL1 118 | AS72653_UV LITERAL1 119 | AS7265x_LED_WHITE LITERAL1 120 | AS7265x_LED_IR LITERAL1 121 | AS7265x_LED_UV LITERAL1 122 | AS7265X_LED_CURRENT_LIMIT_12_5MA LITERAL1 123 | AS7265X_LED_CURRENT_LIMIT_25MA LITERAL1 124 | AS7265X_LED_CURRENT_LIMIT_50MA LITERAL1 125 | AS7265X_LED_CURRENT_LIMIT_100MA LITERAL1 126 | AS7265X_INDICATOR_CURRENT_LIMIT_1MA LITERAL1 127 | AS7265X_INDICATOR_CURRENT_LIMIT_2MA LITERAL1 128 | AS7265X_INDICATOR_CURRENT_LIMIT_4MA LITERAL1 129 | AS7265X_INDICATOR_CURRENT_LIMIT_8MA LITERAL1 130 | AS7265X_GAIN_1X LITERAL1 131 | AS7265X_GAIN_37X LITERAL1 132 | AS7265X_GAIN_16X LITERAL1 133 | AS7265X_GAIN_64X LITERAL1 134 | AS7265X_MEASUREMENT_MODE_4CHAN LITERAL1 135 | AS7265X_MEASUREMENT_MODE_4CHAN_2 LITERAL1 136 | AS7265X_MEASUREMENT_MODE_6CHAN_CONTINUOUS LITERAL1 137 | AS7265X_MEASUREMENT_MODE_6CHAN_ONE_SHOT LITERAL1 138 | -------------------------------------------------------------------------------- /examples/Example3_Settings/Example3_Settings.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Read the 18 channels of spectral light over I2C using the Spectral Triad 3 | By: Nathan Seidle 4 | SparkFun Electronics 5 | Date: October 25th, 2018 6 | License: MIT. See license file for more information but you can 7 | basically do whatever you want with this code. 8 | 9 | This example shows how to change the gain, mode, and LED drive currents 10 | 11 | Feel like supporting open source hardware? 12 | Buy a board from SparkFun! https://www.sparkfun.com/products/15050 13 | 14 | Hardware Connections: 15 | Plug a Qwiic cable into the Spectral Triad and a BlackBoard 16 | If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) 17 | Open the serial monitor at 115200 baud to see the output 18 | */ 19 | 20 | #include "SparkFun_AS7265X.h" //Click here to get the library: http://librarymanager/All#SparkFun_AS7265X 21 | AS7265X sensor; 22 | 23 | #include 24 | 25 | void setup() 26 | { 27 | Serial.begin(115200); 28 | Serial.println("AS7265x Spectral Triad Example"); 29 | 30 | if (sensor.begin() == false) 31 | { 32 | Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing..."); 33 | while (1) 34 | ; 35 | } 36 | 37 | //Once the sensor is started we can increase the I2C speed 38 | Wire.setClock(400000); 39 | 40 | //There are four gain settings. It is possible to saturate the reading so don't simply jump to 64x. 41 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 42 | //sensor.setGain(AS7265X_GAIN_1X); //Default 43 | //sensor.setGain(AS7265X_GAIN_37X); //This is 3.7x 44 | sensor.setGain(AS7265X_GAIN_16X); 45 | //sensor.setGain(AS7265X_GAIN_64X); 46 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 47 | 48 | //There are four measurement modes - the datasheet describes it best 49 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 50 | //sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_4CHAN); //Channels STUV on x51 51 | //sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_4CHAN_2); //Channels RTUW on x51 52 | //sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_6CHAN_CONTINUOUS); //All 6 channels on all devices 53 | sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_6CHAN_ONE_SHOT); //Default: All 6 channels, all devices, just once 54 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 55 | 56 | //Integration cycles is from 0 (2.78ms) to 255 (711ms) 57 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 58 | //sensor.setIntegrationCycles(49); //Default 50*2.8ms = 140ms per reading 59 | sensor.setIntegrationCycles(1); //2*2.8ms = 5.6ms per reading 60 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 61 | 62 | //Drive current can be set for each LED 63 | //4 levels: 12.5, 25, 50, and 100mA 64 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 65 | //White LED has max forward current of 120mA 66 | sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_12_5MA, AS7265x_LED_WHITE); //Default 67 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_25MA, AS7265x_LED_WHITE); //Allowed 68 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_50MA, AS7265x_LED_WHITE); //Allowed 69 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_100MA, AS7265x_LED_WHITE); //Allowed 70 | 71 | //UV LED has max forward current of 30mA so do not set the drive current higher 72 | sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_12_5MA, AS7265x_LED_UV); //Default 73 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_25MA, AS7265x_LED_UV-bad); //Not allowed 74 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_50MA, AS7265x_LED_UV-bad); //Not allowed 75 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_100MA, AS7265x_LED_UV-bad); //Not allowed 76 | 77 | //IR LED has max forward current of 65mA 78 | sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_12_5MA, AS7265x_LED_IR); //Default 79 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_25MA, AS7265x_LED_IR); //Allowed 80 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_50MA, AS7265x_LED_IR); //Allowed 81 | //sensor.setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_100MA, AS7265x_LED_IR-bad); //Not allowed 82 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 83 | 84 | //The status indicator (Blue LED) can be enabled/disabled and have its current set 85 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 86 | sensor.enableIndicator(); //Default 87 | //sensor.disableIndicator(); 88 | 89 | //sensor.setIndicatorCurrent(AS7265X_INDICATOR_CURRENT_LIMIT_1MA); 90 | //sensor.setIndicatorCurrent(AS7265X_INDICATOR_CURRENT_LIMIT_2MA); 91 | //sensor.setIndicatorCurrent(AS7265X_INDICATOR_CURRENT_LIMIT_4MA); 92 | sensor.setIndicatorCurrent(AS7265X_INDICATOR_CURRENT_LIMIT_8MA); //Default 93 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 94 | 95 | //The interrupt pin is active low and can be enabled or disabled 96 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 97 | sensor.enableInterrupt(); //Default 98 | //sensor.disableInterrupt(); 99 | //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 100 | 101 | sensor.disableIndicator(); 102 | 103 | Serial.println("A,B,C,D,E,F,G,H,R,I,S,J,T,U,V,W,K,L"); 104 | } 105 | 106 | void loop() 107 | { 108 | sensor.takeMeasurementsWithBulb(); //This is a hard wait while all 18 channels are measured 109 | 110 | Serial.print(sensor.getCalibratedA()); //410nm 111 | Serial.print(","); 112 | Serial.print(sensor.getCalibratedB()); //435nm 113 | Serial.print(","); 114 | Serial.print(sensor.getCalibratedC()); //460nm 115 | Serial.print(","); 116 | Serial.print(sensor.getCalibratedD()); //485nm 117 | Serial.print(","); 118 | Serial.print(sensor.getCalibratedE()); //510nm 119 | Serial.print(","); 120 | Serial.print(sensor.getCalibratedF()); //535nm 121 | Serial.print(","); 122 | 123 | Serial.print(sensor.getCalibratedG()); //560nm 124 | Serial.print(","); 125 | Serial.print(sensor.getCalibratedH()); //585nm 126 | Serial.print(","); 127 | Serial.print(sensor.getCalibratedR()); //610nm 128 | Serial.print(","); 129 | Serial.print(sensor.getCalibratedI()); //645nm 130 | Serial.print(","); 131 | Serial.print(sensor.getCalibratedS()); //680nm 132 | Serial.print(","); 133 | Serial.print(sensor.getCalibratedJ()); //705nm 134 | Serial.print(","); 135 | 136 | Serial.print(sensor.getCalibratedT()); //730nm 137 | Serial.print(","); 138 | Serial.print(sensor.getCalibratedU()); //760nm 139 | Serial.print(","); 140 | Serial.print(sensor.getCalibratedV()); //810nm 141 | Serial.print(","); 142 | Serial.print(sensor.getCalibratedW()); //860nm 143 | Serial.print(","); 144 | Serial.print(sensor.getCalibratedK()); //900nm 145 | Serial.print(","); 146 | Serial.print(sensor.getCalibratedL()); //940nm 147 | Serial.print(","); 148 | 149 | Serial.println(); 150 | } 151 | -------------------------------------------------------------------------------- /src/SparkFun_AS7265X.h: -------------------------------------------------------------------------------- 1 | /* 2 | This is a library written for the AMS AS7265x Spectral Triad (Moonlight) 3 | SparkFun sells these at its website: www.sparkfun.com 4 | Do you like this library? Help support SparkFun. Buy a board! 5 | https://www.sparkfun.com/products/15050 6 | 7 | Written by Nathan Seidle & Kevin Kuwata @ SparkFun Electronics, October 25th, 2018 8 | 9 | The Spectral Triad is a three sensor platform to do 18-channel spectroscopy. 10 | 11 | https://github.com/sparkfun/SparkFun_AS7265X_Arduino_Library 12 | 13 | Development environment specifics: 14 | Arduino IDE 1.8.5 15 | 16 | This program is distributed in the hope that it will be useful, 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | GNU General Public License for more details. 20 | 21 | You should have received a copy of the GNU General Public License 22 | along with this program. If not, see . 23 | */ 24 | 25 | #ifndef _SPARKFUN_AS7265X_H 26 | #define _SPARKFUN_AS7265X_H 27 | #include "Arduino.h" 28 | #include "Wire.h" 29 | 30 | #define AS7265X_ADDR 0x49 //7-bit unshifted default I2C Address 31 | 32 | #define AS7265X_STATUS_REG 0x00 33 | #define AS7265X_WRITE_REG 0X01 34 | #define AS7265X_READ_REG 0x02 35 | 36 | #define AS7265X_TX_VALID 0x02 37 | #define AS7265X_RX_VALID 0x01 38 | 39 | //Register addresses 40 | #define AS7265X_HW_VERSION_HIGH 0x00 41 | #define AS7265X_HW_VERSION_LOW 0x01 42 | 43 | #define AS7265X_FW_VERSION_HIGH 0x02 44 | #define AS7265X_FW_VERSION_LOW 0x03 45 | 46 | #define AS7265X_CONFIG 0x04 47 | #define AS7265X_INTERGRATION_TIME 0x05 48 | #define AS7265X_DEVICE_TEMP 0x06 49 | #define AS7265X_LED_CONFIG 0x07 50 | 51 | //Raw channel registers 52 | #define AS7265X_R_G_A 0x08 53 | #define AS7265X_S_H_B 0x0A 54 | #define AS7265X_T_I_C 0x0C 55 | #define AS7265X_U_J_D 0x0E 56 | #define AS7265X_V_K_E 0x10 57 | #define AS7265X_W_L_F 0x12 58 | 59 | //Calibrated channel registers 60 | #define AS7265X_R_G_A_CAL 0x14 61 | #define AS7265X_S_H_B_CAL 0x18 62 | #define AS7265X_T_I_C_CAL 0x1C 63 | #define AS7265X_U_J_D_CAL 0x20 64 | #define AS7265X_V_K_E_CAL 0x24 65 | #define AS7265X_W_L_F_CAL 0x28 66 | 67 | #define AS7265X_DEV_SELECT_CONTROL 0x4F 68 | 69 | #define AS7265X_COEF_DATA_0 0x50 70 | #define AS7265X_COEF_DATA_1 0x51 71 | #define AS7265X_COEF_DATA_2 0x52 72 | #define AS7265X_COEF_DATA_3 0x53 73 | #define AS7265X_COEF_DATA_READ 0x54 74 | #define AS7265X_COEF_DATA_WRITE 0x55 75 | 76 | //Settings 77 | 78 | #define AS7265X_POLLING_DELAY 5 //Amount of ms to wait between checking for virtual register changes 79 | 80 | #define AS72651_NIR 0x00 81 | #define AS72652_VISIBLE 0x01 82 | #define AS72653_UV 0x02 83 | 84 | #define AS7265x_LED_WHITE 0x00 //White LED is connected to x51 85 | #define AS7265x_LED_IR 0x01 //IR LED is connected to x52 86 | #define AS7265x_LED_UV 0x02 //UV LED is connected to x53 87 | 88 | #define AS7265X_LED_CURRENT_LIMIT_12_5MA 0b00 89 | #define AS7265X_LED_CURRENT_LIMIT_25MA 0b01 90 | #define AS7265X_LED_CURRENT_LIMIT_50MA 0b10 91 | #define AS7265X_LED_CURRENT_LIMIT_100MA 0b11 92 | 93 | #define AS7265X_INDICATOR_CURRENT_LIMIT_1MA 0b00 94 | #define AS7265X_INDICATOR_CURRENT_LIMIT_2MA 0b01 95 | #define AS7265X_INDICATOR_CURRENT_LIMIT_4MA 0b10 96 | #define AS7265X_INDICATOR_CURRENT_LIMIT_8MA 0b11 97 | 98 | #define AS7265X_GAIN_1X 0b00 99 | #define AS7265X_GAIN_37X 0b01 100 | #define AS7265X_GAIN_16X 0b10 101 | #define AS7265X_GAIN_64X 0b11 102 | 103 | #define AS7265X_MEASUREMENT_MODE_4CHAN 0b00 104 | #define AS7265X_MEASUREMENT_MODE_4CHAN_2 0b01 105 | #define AS7265X_MEASUREMENT_MODE_6CHAN_CONTINUOUS 0b10 106 | #define AS7265X_MEASUREMENT_MODE_6CHAN_ONE_SHOT 0b11 107 | 108 | class AS7265X 109 | { 110 | public: 111 | AS7265X(); 112 | 113 | boolean begin(TwoWire &wirePort = Wire); 114 | boolean isConnected(); //Checks if sensor ack's the I2C request 115 | 116 | uint8_t getDeviceType(); 117 | uint8_t getHardwareVersion(); 118 | uint8_t getMajorFirmwareVersion(); 119 | uint8_t getPatchFirmwareVersion(); 120 | uint8_t getBuildFirmwareVersion(); 121 | 122 | uint8_t getTemperature(uint8_t deviceNumber = 0); //Get temp in C of the master IC 123 | float getTemperatureAverage(); //Get average of all three ICs 124 | 125 | void takeMeasurements(); 126 | void takeMeasurementsWithBulb(); 127 | 128 | void enableIndicator(); //Blue status LED 129 | void disableIndicator(); 130 | 131 | void enableBulb(uint8_t device); 132 | void disableBulb(uint8_t device); 133 | 134 | void setGain(uint8_t gain); //1 to 64x 135 | void setMeasurementMode(uint8_t mode); //4 channel, other 4 channel, 6 chan, or 6 chan one shot 136 | void setIntegrationCycles(uint8_t cycleValue); 137 | 138 | void setBulbCurrent(uint8_t current, uint8_t device); // 139 | void setIndicatorCurrent(uint8_t current); //0 to 8mA 140 | 141 | void enableInterrupt(); 142 | void disableInterrupt(); 143 | 144 | void softReset(); 145 | 146 | boolean dataAvailable(); //Returns true when data is available 147 | 148 | //Returns the various calibration data 149 | float getCalibratedA(); 150 | float getCalibratedB(); 151 | float getCalibratedC(); 152 | float getCalibratedD(); 153 | float getCalibratedE(); 154 | float getCalibratedF(); 155 | 156 | float getCalibratedG(); 157 | float getCalibratedH(); 158 | float getCalibratedI(); 159 | float getCalibratedJ(); 160 | float getCalibratedK(); 161 | float getCalibratedL(); 162 | 163 | float getCalibratedR(); 164 | float getCalibratedS(); 165 | float getCalibratedT(); 166 | float getCalibratedU(); 167 | float getCalibratedV(); 168 | float getCalibratedW(); 169 | 170 | //Get the various raw readings 171 | uint16_t getA(); 172 | uint16_t getB(); 173 | uint16_t getC(); 174 | uint16_t getD(); 175 | uint16_t getE(); 176 | uint16_t getF(); 177 | 178 | uint16_t getG(); 179 | uint16_t getH(); 180 | uint16_t getI(); 181 | uint16_t getJ(); 182 | uint16_t getK(); 183 | uint16_t getL(); 184 | 185 | uint16_t getR(); 186 | uint16_t getS(); 187 | uint16_t getT(); 188 | uint16_t getU(); 189 | uint16_t getV(); 190 | uint16_t getW(); 191 | 192 | private: 193 | TwoWire *_i2cPort; 194 | uint16_t getChannel(uint8_t channelRegister, uint8_t device); 195 | float getCalibratedValue(uint8_t calAddress, uint8_t device); 196 | float convertBytesToFloat(uint32_t myLong); 197 | 198 | void selectDevice(uint8_t device); //Change between the x51, x52, or x53 for data and settings 199 | 200 | uint8_t virtualReadRegister(uint8_t virtualAddr); 201 | void virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite); 202 | 203 | uint8_t readRegister(uint8_t addr); 204 | boolean writeRegister(uint8_t addr, uint8_t val); 205 | 206 | //Integration time is 2.8 * integration cycles. 207 | //We will wait for integration time + 50% 208 | //Since maximum number of integration cycles is 255, absolute max wait time is 2.8 * 255 * 1.5 = 1071 209 | uint16_t maxWaitTime = 1071; 210 | }; 211 | 212 | #endif -------------------------------------------------------------------------------- /src/SparkFun_AS7265X.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This is a library written for the AMS AS7265x Spectral Triad (Moonlight) 3 | SparkFun sells these at its website: www.sparkfun.com 4 | Do you like this library? Help support SparkFun. Buy a board! 5 | https://www.sparkfun.com/products/15050 6 | 7 | Written by Nathan Seidle & Kevin Kuwata @ SparkFun Electronics, October 25th, 2018 8 | 9 | The Spectral Triad is a three sensor platform to do 18-channel spectroscopy. 10 | 11 | https://github.com/sparkfun/SparkFun_AS7265X_Arduino_Library 12 | 13 | Development environment specifics: 14 | Arduino IDE 1.8.5 15 | 16 | This program is distributed in the hope that it will be useful, 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | GNU General Public License for more details. 20 | 21 | You should have received a copy of the GNU General Public License 22 | along with this program. If not, see . 23 | */ 24 | 25 | #include "SparkFun_AS7265X.h" 26 | #include "Arduino.h" 27 | 28 | //Constructor 29 | AS7265X::AS7265X() 30 | { 31 | } 32 | 33 | //Initializes the sensor with basic settings 34 | //Returns false if sensor is not detected 35 | boolean AS7265X::begin(TwoWire &wirePort) 36 | { 37 | _i2cPort = &wirePort; 38 | _i2cPort->begin(); //This resets any setClock() the user may have done 39 | 40 | if (isConnected() == false) 41 | return (false); //Check for sensor presence 42 | 43 | //Check to see if both slaves are detected 44 | uint8_t value = virtualReadRegister(AS7265X_DEV_SELECT_CONTROL); 45 | if ((value & 0b00110000) == 0) 46 | return (false); //Test if Slave1 and 2 are detected. If not, bail. 47 | 48 | setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_12_5MA, AS7265x_LED_WHITE); 49 | setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_12_5MA, AS7265x_LED_IR); 50 | setBulbCurrent(AS7265X_LED_CURRENT_LIMIT_12_5MA, AS7265x_LED_UV); 51 | 52 | disableBulb(AS7265x_LED_WHITE); //Turn off bulb to avoid heating sensor 53 | disableBulb(AS7265x_LED_IR); 54 | disableBulb(AS7265x_LED_UV); 55 | 56 | setIndicatorCurrent(AS7265X_INDICATOR_CURRENT_LIMIT_8MA); //Set to 8mA (maximum) 57 | enableIndicator(); 58 | 59 | setIntegrationCycles(49); //50 * 2.8ms = 140ms. 0 to 255 is valid. 60 | //If you use Mode 2 or 3 (all the colors) then integration time is double. 140*2 = 280ms between readings. 61 | 62 | setGain(AS7265X_GAIN_64X); //Set gain to 64x 63 | 64 | setMeasurementMode(AS7265X_MEASUREMENT_MODE_6CHAN_ONE_SHOT); //One-shot reading of VBGYOR 65 | 66 | enableInterrupt(); 67 | 68 | return (true); //We're all setup! 69 | } 70 | 71 | uint8_t AS7265X::getDeviceType() 72 | { 73 | return (virtualReadRegister(AS7265X_HW_VERSION_HIGH)); 74 | } 75 | uint8_t AS7265X::getHardwareVersion() 76 | { 77 | return (virtualReadRegister(AS7265X_HW_VERSION_LOW)); 78 | } 79 | 80 | uint8_t AS7265X::getMajorFirmwareVersion() 81 | { 82 | virtualWriteRegister(AS7265X_FW_VERSION_HIGH, 0x01); //Set to 0x01 for Major 83 | virtualWriteRegister(AS7265X_FW_VERSION_LOW, 0x01); //Set to 0x01 for Major 84 | 85 | return (virtualReadRegister(AS7265X_FW_VERSION_LOW)); 86 | } 87 | 88 | uint8_t AS7265X::getPatchFirmwareVersion() 89 | { 90 | virtualWriteRegister(AS7265X_FW_VERSION_HIGH, 0x02); //Set to 0x02 for Patch 91 | virtualWriteRegister(AS7265X_FW_VERSION_LOW, 0x02); //Set to 0x02 for Patch 92 | 93 | return (virtualReadRegister(AS7265X_FW_VERSION_LOW)); 94 | } 95 | 96 | uint8_t AS7265X::getBuildFirmwareVersion() 97 | { 98 | virtualWriteRegister(AS7265X_FW_VERSION_HIGH, 0x03); //Set to 0x03 for Build 99 | virtualWriteRegister(AS7265X_FW_VERSION_LOW, 0x03); //Set to 0x03 for Build 100 | 101 | return (virtualReadRegister(AS7265X_FW_VERSION_LOW)); 102 | } 103 | 104 | //Returns true if I2C device ack's 105 | boolean AS7265X::isConnected() 106 | { 107 | //Give IC 660ms for startup - max 1000ms 108 | for (uint8_t x = 0; x < 100; x++) 109 | { 110 | _i2cPort->beginTransmission((uint8_t)AS7265X_ADDR); 111 | #ifdef ENERGIA 112 | _i2cPort->write(0x00); //See issue: https://github.com/sparkfun/SparkFun_AS7265x_Arduino_Library/issues/4 113 | #endif 114 | if (_i2cPort->endTransmission() == 0) 115 | return (true); //Sensor ACK'd 116 | delay(10); 117 | } 118 | return (false); //Sensor did not ACK 119 | } 120 | 121 | //Tells IC to take all channel measurements and polls for data ready flag 122 | void AS7265X::takeMeasurements() 123 | { 124 | setMeasurementMode(AS7265X_MEASUREMENT_MODE_6CHAN_ONE_SHOT); //Set mode to all 6-channels, one-shot 125 | 126 | //Wait for data to be ready 127 | unsigned long startTime = millis(); 128 | while (dataAvailable() == false) 129 | { 130 | if(millis() - startTime > maxWaitTime) return; //Sensor failed to respond 131 | delay(AS7265X_POLLING_DELAY); 132 | } 133 | 134 | //Readings can now be accessed via getCalibratedA(), getJ(), etc 135 | } 136 | 137 | //Turns on all bulbs, takes measurements of all channels, turns off all bulbs 138 | void AS7265X::takeMeasurementsWithBulb() 139 | { 140 | enableBulb(AS7265x_LED_WHITE); 141 | enableBulb(AS7265x_LED_IR); 142 | enableBulb(AS7265x_LED_UV); 143 | 144 | takeMeasurements(); 145 | 146 | disableBulb(AS7265x_LED_WHITE); //Turn off bulb to avoid heating sensor 147 | disableBulb(AS7265x_LED_IR); 148 | disableBulb(AS7265x_LED_UV); 149 | } 150 | 151 | //Get the various color readings 152 | uint16_t AS7265X::getG() 153 | { 154 | return (getChannel(AS7265X_R_G_A, AS72652_VISIBLE)); 155 | } 156 | uint16_t AS7265X::getH() 157 | { 158 | return (getChannel(AS7265X_S_H_B, AS72652_VISIBLE)); 159 | } 160 | uint16_t AS7265X::getI() 161 | { 162 | return (getChannel(AS7265X_T_I_C, AS72652_VISIBLE)); 163 | } 164 | uint16_t AS7265X::getJ() 165 | { 166 | return (getChannel(AS7265X_U_J_D, AS72652_VISIBLE)); 167 | } 168 | uint16_t AS7265X::getK() 169 | { 170 | return (getChannel(AS7265X_V_K_E, AS72652_VISIBLE)); 171 | } 172 | uint16_t AS7265X::getL() 173 | { 174 | return (getChannel(AS7265X_W_L_F, AS72652_VISIBLE)); 175 | } 176 | 177 | //Get the various NIR readings 178 | uint16_t AS7265X::getR() 179 | { 180 | return (getChannel(AS7265X_R_G_A, AS72651_NIR)); 181 | } 182 | uint16_t AS7265X::getS() 183 | { 184 | return (getChannel(AS7265X_S_H_B, AS72651_NIR)); 185 | } 186 | uint16_t AS7265X::getT() 187 | { 188 | return (getChannel(AS7265X_T_I_C, AS72651_NIR)); 189 | } 190 | uint16_t AS7265X::getU() 191 | { 192 | return (getChannel(AS7265X_U_J_D, AS72651_NIR)); 193 | } 194 | uint16_t AS7265X::getV() 195 | { 196 | return (getChannel(AS7265X_V_K_E, AS72651_NIR)); 197 | } 198 | uint16_t AS7265X::getW() 199 | { 200 | return (getChannel(AS7265X_W_L_F, AS72651_NIR)); 201 | } 202 | 203 | //Get the various UV readings 204 | uint16_t AS7265X::getA() 205 | { 206 | return (getChannel(AS7265X_R_G_A, AS72653_UV)); 207 | } 208 | uint16_t AS7265X::getB() 209 | { 210 | return (getChannel(AS7265X_S_H_B, AS72653_UV)); 211 | } 212 | uint16_t AS7265X::getC() 213 | { 214 | return (getChannel(AS7265X_T_I_C, AS72653_UV)); 215 | } 216 | uint16_t AS7265X::getD() 217 | { 218 | return (getChannel(AS7265X_U_J_D, AS72653_UV)); 219 | } 220 | uint16_t AS7265X::getE() 221 | { 222 | return (getChannel(AS7265X_V_K_E, AS72653_UV)); 223 | } 224 | uint16_t AS7265X::getF() 225 | { 226 | return (getChannel(AS7265X_W_L_F, AS72653_UV)); 227 | } 228 | 229 | //A the 16-bit value stored in a given channel registerReturns 230 | uint16_t AS7265X::getChannel(uint8_t channelRegister, uint8_t device) 231 | { 232 | selectDevice(device); 233 | uint16_t colorData = virtualReadRegister(channelRegister) << 8; //High uint8_t 234 | colorData |= virtualReadRegister(channelRegister + 1); //Low uint8_t 235 | return (colorData); 236 | } 237 | 238 | //Returns the various calibration data 239 | float AS7265X::getCalibratedA() 240 | { 241 | return (getCalibratedValue(AS7265X_R_G_A_CAL, AS72653_UV)); 242 | } 243 | float AS7265X::getCalibratedB() 244 | { 245 | return (getCalibratedValue(AS7265X_S_H_B_CAL, AS72653_UV)); 246 | } 247 | float AS7265X::getCalibratedC() 248 | { 249 | return (getCalibratedValue(AS7265X_T_I_C_CAL, AS72653_UV)); 250 | } 251 | float AS7265X::getCalibratedD() 252 | { 253 | return (getCalibratedValue(AS7265X_U_J_D_CAL, AS72653_UV)); 254 | } 255 | float AS7265X::getCalibratedE() 256 | { 257 | return (getCalibratedValue(AS7265X_V_K_E_CAL, AS72653_UV)); 258 | } 259 | float AS7265X::getCalibratedF() 260 | { 261 | return (getCalibratedValue(AS7265X_W_L_F_CAL, AS72653_UV)); 262 | } 263 | 264 | //Returns the various calibration data 265 | float AS7265X::getCalibratedG() 266 | { 267 | return (getCalibratedValue(AS7265X_R_G_A_CAL, AS72652_VISIBLE)); 268 | } 269 | float AS7265X::getCalibratedH() 270 | { 271 | return (getCalibratedValue(AS7265X_S_H_B_CAL, AS72652_VISIBLE)); 272 | } 273 | float AS7265X::getCalibratedI() 274 | { 275 | return (getCalibratedValue(AS7265X_T_I_C_CAL, AS72652_VISIBLE)); 276 | } 277 | float AS7265X::getCalibratedJ() 278 | { 279 | return (getCalibratedValue(AS7265X_U_J_D_CAL, AS72652_VISIBLE)); 280 | } 281 | float AS7265X::getCalibratedK() 282 | { 283 | return (getCalibratedValue(AS7265X_V_K_E_CAL, AS72652_VISIBLE)); 284 | } 285 | float AS7265X::getCalibratedL() 286 | { 287 | return (getCalibratedValue(AS7265X_W_L_F_CAL, AS72652_VISIBLE)); 288 | } 289 | 290 | float AS7265X::getCalibratedR() 291 | { 292 | return (getCalibratedValue(AS7265X_R_G_A_CAL, AS72651_NIR)); 293 | } 294 | float AS7265X::getCalibratedS() 295 | { 296 | return (getCalibratedValue(AS7265X_S_H_B_CAL, AS72651_NIR)); 297 | } 298 | float AS7265X::getCalibratedT() 299 | { 300 | return (getCalibratedValue(AS7265X_T_I_C_CAL, AS72651_NIR)); 301 | } 302 | float AS7265X::getCalibratedU() 303 | { 304 | return (getCalibratedValue(AS7265X_U_J_D_CAL, AS72651_NIR)); 305 | } 306 | float AS7265X::getCalibratedV() 307 | { 308 | return (getCalibratedValue(AS7265X_V_K_E_CAL, AS72651_NIR)); 309 | } 310 | float AS7265X::getCalibratedW() 311 | { 312 | return (getCalibratedValue(AS7265X_W_L_F_CAL, AS72651_NIR)); 313 | } 314 | 315 | //Given an address, read four bytes and return the floating point calibrated value 316 | float AS7265X::getCalibratedValue(uint8_t calAddress, uint8_t device) 317 | { 318 | selectDevice(device); 319 | 320 | uint8_t b0, b1, b2, b3; 321 | b0 = virtualReadRegister(calAddress + 0); 322 | b1 = virtualReadRegister(calAddress + 1); 323 | b2 = virtualReadRegister(calAddress + 2); 324 | b3 = virtualReadRegister(calAddress + 3); 325 | 326 | //Channel calibrated values are stored big-endian 327 | uint32_t calBytes = 0; 328 | calBytes |= ((uint32_t)b0 << (8 * 3)); 329 | calBytes |= ((uint32_t)b1 << (8 * 2)); 330 | calBytes |= ((uint32_t)b2 << (8 * 1)); 331 | calBytes |= ((uint32_t)b3 << (8 * 0)); 332 | 333 | return (convertBytesToFloat(calBytes)); 334 | } 335 | 336 | //Given 4 bytes returns the floating point value 337 | float AS7265X::convertBytesToFloat(uint32_t myLong) 338 | { 339 | float myFloat; 340 | memcpy(&myFloat, &myLong, 4); //Copy bytes into a float 341 | return (myFloat); 342 | } 343 | 344 | //Mode 0: 4 channels out of 6 (see datasheet) 345 | //Mode 1: Different 4 channels out of 6 (see datasheet) 346 | //Mode 2: All 6 channels continuously 347 | //Mode 3: One-shot reading of all channels 348 | void AS7265X::setMeasurementMode(uint8_t mode) 349 | { 350 | if (mode > 0b11) 351 | mode = 0b11; //Error check 352 | 353 | //Read, mask/set, write 354 | uint8_t value = virtualReadRegister(AS7265X_CONFIG); //Read 355 | value &= 0b11110011; //Clear BANK bits 356 | value |= (mode << 2); //Set BANK bits with user's choice 357 | virtualWriteRegister(AS7265X_CONFIG, value); //Write 358 | } 359 | 360 | //Sets the gain value 361 | //Gain 0: 1x (power-on default) 362 | //Gain 1: 3.7x 363 | //Gain 2: 16x 364 | //Gain 3: 64x 365 | void AS7265X::setGain(uint8_t gain) 366 | { 367 | if (gain > 0b11) 368 | gain = 0b11; 369 | 370 | //Read, mask/set, write 371 | uint8_t value = virtualReadRegister(AS7265X_CONFIG); //Read 372 | value &= 0b11001111; //Clear GAIN bits 373 | value |= (gain << 4); //Set GAIN bits with user's choice 374 | virtualWriteRegister(AS7265X_CONFIG, value); //Write 375 | } 376 | 377 | //Sets the integration cycle amount 378 | //Give this function a byte from 0 to 255. 379 | //Time will be 2.8ms * [integration cycles + 1] 380 | void AS7265X::setIntegrationCycles(uint8_t cycleValue) 381 | { 382 | maxWaitTime = (int)(cycleValue * 2.8 * 1.5) + 1; //Wait for integration time + 50% 383 | virtualWriteRegister(AS7265X_INTERGRATION_TIME, cycleValue); //Write 384 | } 385 | 386 | void AS7265X::enableInterrupt() 387 | { 388 | //Read, mask/set, write 389 | uint8_t value = virtualReadRegister(AS7265X_CONFIG); //Read 390 | value |= (1 << 6); //Set INT bit 391 | virtualWriteRegister(AS7265X_CONFIG, value); //Write 392 | } 393 | 394 | //Disables the interrupt pin 395 | void AS7265X::disableInterrupt() 396 | { 397 | //Read, mask/set, write 398 | uint8_t value = virtualReadRegister(AS7265X_CONFIG); //Read 399 | value &= ~(1 << 6); //Clear INT bit 400 | virtualWriteRegister(AS7265X_CONFIG, value); //Write 401 | } 402 | 403 | //Checks to see if DRDY flag is set in the control setup register 404 | boolean AS7265X::dataAvailable() 405 | { 406 | uint8_t value = virtualReadRegister(AS7265X_CONFIG); 407 | return (value & (1 << 1)); //Bit 1 is DATA_RDY 408 | } 409 | 410 | //Enable the LED or bulb on a given device 411 | void AS7265X::enableBulb(uint8_t device) 412 | { 413 | selectDevice(device); 414 | 415 | //Read, mask/set, write 416 | uint8_t value = virtualReadRegister(AS7265X_LED_CONFIG); 417 | value |= (1 << 3); //Set the bit 418 | virtualWriteRegister(AS7265X_LED_CONFIG, value); 419 | } 420 | 421 | //Disable the LED or bulb on a given device 422 | void AS7265X::disableBulb(uint8_t device) 423 | { 424 | selectDevice(device); 425 | 426 | //Read, mask/set, write 427 | uint8_t value = virtualReadRegister(AS7265X_LED_CONFIG); 428 | value &= ~(1 << 3); //Clear the bit 429 | virtualWriteRegister(AS7265X_LED_CONFIG, value); 430 | } 431 | 432 | //Set the current limit of bulb/LED. 433 | //Current 0: 12.5mA 434 | //Current 1: 25mA 435 | //Current 2: 50mA 436 | //Current 3: 100mA 437 | void AS7265X::setBulbCurrent(uint8_t current, uint8_t device) 438 | { 439 | selectDevice(device); 440 | 441 | // set the current 442 | if (current > 0b11) 443 | current = 0b11; //Limit to two bits 444 | uint8_t value = virtualReadRegister(AS7265X_LED_CONFIG); //Read 445 | value &= 0b11001111; //Clear ICL_DRV bits 446 | value |= (current << 4); //Set ICL_DRV bits with user's choice 447 | virtualWriteRegister(AS7265X_LED_CONFIG, value); //Write 448 | } 449 | 450 | //As we read various registers we have to point at the master or first/second slave 451 | void AS7265X::selectDevice(uint8_t device) 452 | { 453 | //Set the bits 0:1. Just overwrite whatever is there because masking in the correct value doesn't work. 454 | virtualWriteRegister(AS7265X_DEV_SELECT_CONTROL, device); 455 | 456 | //This fails 457 | //uint8_t value = virtualReadRegister(AS7265X_DEV_SELECT_CONTROL); 458 | //value &= 0b11111100; //Clear lower two bits 459 | //if(device < 3) value |= device; //Set the bits 460 | //virtualWriteRegister(AS7265X_DEV_SELECT_CONTROL, value); 461 | } 462 | 463 | //Enable the onboard indicator LED 464 | void AS7265X::enableIndicator() 465 | { 466 | selectDevice(AS72651_NIR); 467 | 468 | //Read, mask/set, write 469 | uint8_t value = virtualReadRegister(AS7265X_LED_CONFIG); 470 | value |= (1 << 0); //Set the bit 471 | 472 | virtualWriteRegister(AS7265X_LED_CONFIG, value); 473 | } 474 | 475 | //Disable the onboard indicator LED 476 | void AS7265X::disableIndicator() 477 | { 478 | selectDevice(AS72651_NIR); 479 | 480 | //Read, mask/set, write 481 | uint8_t value = virtualReadRegister(AS7265X_LED_CONFIG); 482 | value &= ~(1 << 0); //Clear the bit 483 | 484 | virtualWriteRegister(AS7265X_LED_CONFIG, value); 485 | } 486 | 487 | //Set the current limit of onboard LED. Default is max 8mA = 0b11. 488 | void AS7265X::setIndicatorCurrent(uint8_t current) 489 | { 490 | selectDevice(AS72651_NIR); 491 | 492 | if (current > 0b11) 493 | current = 0b11; 494 | //Read, mask/set, write 495 | uint8_t value = virtualReadRegister(AS7265X_LED_CONFIG); //Read 496 | value &= 0b11111001; //Clear ICL_IND bits 497 | value |= (current << 1); //Set ICL_IND bits with user's choice 498 | 499 | virtualWriteRegister(AS7265X_LED_CONFIG, value); //Write 500 | } 501 | 502 | //Returns the temperature of a given device in C 503 | uint8_t AS7265X::getTemperature(uint8_t deviceNumber) 504 | { 505 | selectDevice(deviceNumber); 506 | return (virtualReadRegister(AS7265X_DEVICE_TEMP)); 507 | } 508 | 509 | //Returns an average of all the sensor temps in C 510 | float AS7265X::getTemperatureAverage() 511 | { 512 | float average = 0; 513 | 514 | for (uint8_t x = 0; x < 3; x++) 515 | average += getTemperature(x); 516 | 517 | return (average / 3); 518 | } 519 | 520 | //Does a soft reset 521 | //Give sensor at least 1000ms to reset 522 | void AS7265X::softReset() 523 | { 524 | //Read, mask/set, write 525 | uint8_t value = virtualReadRegister(AS7265X_CONFIG); //Read 526 | value |= (1 << 7); //Set RST bit, automatically cleared after reset 527 | virtualWriteRegister(AS7265X_CONFIG, value); //Write 528 | } 529 | 530 | //Read a virtual register from the AS7265x 531 | uint8_t AS7265X::virtualReadRegister(uint8_t virtualAddr) 532 | { 533 | uint8_t status; 534 | 535 | //Do a prelim check of the read register 536 | status = readRegister(AS7265X_STATUS_REG); 537 | if ((status & AS7265X_RX_VALID) != 0) //There is data to be read 538 | { 539 | readRegister(AS7265X_READ_REG); //Read the byte but do nothing with it 540 | } 541 | 542 | //Wait for WRITE flag to clear 543 | unsigned long startTime = millis(); 544 | while (1) 545 | { 546 | if(millis() - startTime > maxWaitTime) return(0); //Sensor failed to respond 547 | status = readRegister(AS7265X_STATUS_REG); 548 | if ((status & AS7265X_TX_VALID) == 0) 549 | break; // If TX bit is clear, it is ok to write 550 | delay(AS7265X_POLLING_DELAY); 551 | } 552 | 553 | // Send the virtual register address (bit 7 should be 0 to indicate we are reading a register). 554 | writeRegister(AS7265X_WRITE_REG, virtualAddr); 555 | 556 | //Wait for READ flag to be set 557 | startTime = millis(); 558 | while (1) 559 | { 560 | if(millis() - startTime > maxWaitTime) return(0); //Sensor failed to respond 561 | status = readRegister(AS7265X_STATUS_REG); 562 | if ((status & AS7265X_RX_VALID) != 0) 563 | break; // Read data is ready. 564 | delay(AS7265X_POLLING_DELAY); 565 | } 566 | 567 | uint8_t incoming = readRegister(AS7265X_READ_REG); 568 | return (incoming); 569 | } 570 | 571 | //Write to a virtual register in the AS726x 572 | void AS7265X::virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite) 573 | { 574 | uint8_t status; 575 | 576 | //Wait for WRITE register to be empty 577 | unsigned long startTime = millis(); 578 | while (1) 579 | { 580 | if(millis() - startTime > maxWaitTime) return; //Sensor failed to respond 581 | status = readRegister(AS7265X_STATUS_REG); 582 | if ((status & AS7265X_TX_VALID) == 0) 583 | break; // No inbound TX pending at slave. Okay to write now. 584 | delay(AS7265X_POLLING_DELAY); 585 | } 586 | 587 | // Send the virtual register address (setting bit 7 to indicate we are writing to a register). 588 | writeRegister(AS7265X_WRITE_REG, (virtualAddr | 1 << 7)); 589 | 590 | //Wait for WRITE register to be empty 591 | startTime = millis(); 592 | while (1) 593 | { 594 | if(millis() - startTime > maxWaitTime) return; //Sensor failed to respond 595 | status = readRegister(AS7265X_STATUS_REG); 596 | if ((status & AS7265X_TX_VALID) == 0) 597 | break; // No inbound TX pending at slave. Okay to write now. 598 | delay(AS7265X_POLLING_DELAY); 599 | } 600 | 601 | // Send the data to complete the operation. 602 | writeRegister(AS7265X_WRITE_REG, dataToWrite); 603 | } 604 | 605 | //Reads from a give location from the AS726x 606 | uint8_t AS7265X::readRegister(uint8_t addr) 607 | { 608 | _i2cPort->beginTransmission(AS7265X_ADDR); 609 | _i2cPort->write(addr); 610 | if (_i2cPort->endTransmission() != 0) 611 | { 612 | //Serial.println("No ack!"); 613 | return (0); //Device failed to ack 614 | } 615 | 616 | _i2cPort->requestFrom((uint8_t)AS7265X_ADDR, (uint8_t)1); 617 | if (_i2cPort->available()) 618 | { 619 | return (_i2cPort->read()); 620 | } 621 | 622 | //Serial.println("No ack!"); 623 | return (0); //Device failed to respond 624 | } 625 | 626 | //Write a value to a spot in the AS726x 627 | boolean AS7265X::writeRegister(uint8_t addr, uint8_t val) 628 | { 629 | _i2cPort->beginTransmission(AS7265X_ADDR); 630 | _i2cPort->write(addr); 631 | _i2cPort->write(val); 632 | if (_i2cPort->endTransmission() != 0) 633 | { 634 | //Serial.println("No ack!"); 635 | return (false); //Device failed to ack 636 | } 637 | 638 | return (true); 639 | } --------------------------------------------------------------------------------