├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── githubci.yml ├── Adafruit_AS726x.cpp ├── Adafruit_AS726x.h ├── README.md ├── examples ├── AS7262_test │ └── AS7262_test.ino └── color_graph │ └── color_graph.ino └── library.properties /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit AS726x Arduino Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /Adafruit_AS726x.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_AS726x.cpp 3 | * 4 | * @mainpage Adafruit AS726x spectral sensor 5 | * 6 | * @section intro_sec Introduction 7 | * 8 | * Driver for the AS726x family of spectral sensors 9 | * 10 | * This is a library for the Adafruit AS726x breakout 11 | * ----> https://www.adafruit.com/products/3779 12 | * 13 | * Adafruit invests time and resources providing this open source code, 14 | * please support Adafruit and open-source hardware by purchasing 15 | * products from Adafruit! 16 | * 17 | * @section author Author 18 | * 19 | * Written by Dean Miller for Adafruit Industries. 20 | * 21 | * @section license License 22 | * 23 | * BSD license, all text here must be included in any redistribution. 24 | * 25 | */ 26 | 27 | #include "Adafruit_AS726x.h" 28 | 29 | Adafruit_AS726x::~Adafruit_AS726x(void) { 30 | if (i2c_dev) 31 | delete i2c_dev; 32 | } 33 | 34 | /**************************************************************************/ 35 | /*! 36 | @brief Set up hardware and begin communication with the sensor 37 | @param theWire a TwoWire object to use for I2C communication 38 | @return true on success, fale otherwise. 39 | */ 40 | /**************************************************************************/ 41 | bool Adafruit_AS726x::begin(TwoWire *theWire) { 42 | if (i2c_dev) 43 | delete i2c_dev; 44 | i2c_dev = new Adafruit_I2CDevice(_i2caddr, theWire); 45 | if (!i2c_dev->begin()) { 46 | return false; 47 | } 48 | 49 | _control_setup.RST = 1; 50 | virtualWrite(AS726X_CONTROL_SETUP, _control_setup.get()); 51 | _control_setup.RST = 0; 52 | 53 | // wait for it to boot up 54 | delay(1000); 55 | 56 | // try to read the version reg to make sure we can connect 57 | uint8_t version = virtualRead(AS726X_HW_VERSION); 58 | 59 | // TODO: add support for other devices 60 | if (version != 0x40) 61 | return false; 62 | 63 | enableInterrupt(); 64 | 65 | setDrvCurrent(LIMIT_12MA5); 66 | drvOff(); 67 | 68 | setIntegrationTime(50); 69 | 70 | setGain(GAIN_64X); 71 | 72 | setConversionType(ONE_SHOT); 73 | 74 | return true; 75 | } 76 | 77 | /**************************************************************************/ 78 | /*! 79 | @brief turn on the driver LED 80 | */ 81 | /**************************************************************************/ 82 | void Adafruit_AS726x::drvOn() { 83 | _led_control.LED_DRV = 1; 84 | virtualWrite(AS726X_LED_CONTROL, _led_control.get()); 85 | } 86 | 87 | /**************************************************************************/ 88 | /*! 89 | @brief turn off the driver LED 90 | */ 91 | /**************************************************************************/ 92 | void Adafruit_AS726x::drvOff() { 93 | _led_control.LED_DRV = 0; 94 | virtualWrite(AS726X_LED_CONTROL, _led_control.get()); 95 | } 96 | 97 | /**************************************************************************/ 98 | /*! 99 | @brief set the current limit for the driver LED. 100 | @param current the current limit setting. Should be one of LIMIT_12MA5, 101 | LIMIT_25MA, LIMIT_50MA, or LIMIT_100MA = 0b11. 102 | */ 103 | /**************************************************************************/ 104 | void Adafruit_AS726x::setDrvCurrent(uint8_t current) { 105 | _led_control.ICL_DRV = current; 106 | virtualWrite(AS726X_LED_CONTROL, _led_control.get()); 107 | } 108 | 109 | /**************************************************************************/ 110 | /*! 111 | @brief turn on/off the indicator LED 112 | @param on True if you want the LED on, False to turn off 113 | */ 114 | /**************************************************************************/ 115 | void Adafruit_AS726x::indicateLED(boolean on) { 116 | _led_control.LED_IND = on; 117 | virtualWrite(AS726X_LED_CONTROL, _led_control.get()); 118 | } 119 | 120 | /**************************************************************************/ 121 | /*! 122 | @brief set the current limit for the driver LED. 123 | @param current the current limit setting. Should be one of LIMIT_1MA, 124 | LIMIT_2MA, LIMIT_4MA, or LIMIT_8MA 125 | */ 126 | /**************************************************************************/ 127 | void Adafruit_AS726x::setIndicateCurrent(uint8_t current) { 128 | _led_control.ICL_IND = current; 129 | virtualWrite(AS726X_LED_CONTROL, _led_control.get()); 130 | } 131 | 132 | /**************************************************************************/ 133 | /*! 134 | @brief Set the conversion mode. 135 | @param type the mode to set the sensor to. Should be one of MODE_0, MODE_1, 136 | MODE_2, ONE_SHOT. 137 | */ 138 | /**************************************************************************/ 139 | void Adafruit_AS726x::setConversionType(uint8_t type) { 140 | _control_setup.BANK = type; 141 | virtualWrite(AS726X_CONTROL_SETUP, _control_setup.get()); 142 | } 143 | 144 | /**************************************************************************/ 145 | /*! 146 | @brief Set the sensor gain. 147 | @param gain the gain to set the sensor to. Should be one of GAIN_1X, 148 | GAIN_3X7, GAIN_16X, or GAIN_64X = 0b11. 149 | */ 150 | /**************************************************************************/ 151 | void Adafruit_AS726x::setGain(uint8_t gain) { 152 | _control_setup.GAIN = gain; 153 | virtualWrite(AS726X_CONTROL_SETUP, _control_setup.get()); 154 | } 155 | 156 | /**************************************************************************/ 157 | /*! 158 | @brief Set the integration time for the sensor. 159 | @param time the integration time to set. The actual integration time will be 160 | time*2.8ms 161 | */ 162 | /**************************************************************************/ 163 | void Adafruit_AS726x::setIntegrationTime(uint8_t time) { 164 | _int_time.INT_T = time; 165 | virtualWrite(AS726X_INT_T, _int_time.get()); 166 | } 167 | 168 | /**************************************************************************/ 169 | /*! 170 | @brief enable the device interrupt 171 | */ 172 | /**************************************************************************/ 173 | void Adafruit_AS726x::enableInterrupt() { 174 | _control_setup.INT = 1; 175 | virtualWrite(AS726X_CONTROL_SETUP, _control_setup.get()); 176 | } 177 | 178 | /**************************************************************************/ 179 | /*! 180 | @brief disable the device interrupt 181 | */ 182 | /**************************************************************************/ 183 | void Adafruit_AS726x::disableInterrupt() { 184 | _control_setup.INT = 0; 185 | virtualWrite(AS726X_CONTROL_SETUP, _control_setup.get()); 186 | } 187 | 188 | /**************************************************************************/ 189 | /*! 190 | @brief begin a measurement. This sets the conversion mode to ONE_SHOT. 191 | */ 192 | /**************************************************************************/ 193 | void Adafruit_AS726x::startMeasurement() { 194 | _control_setup.DATA_RDY = 0; 195 | virtualWrite(AS726X_CONTROL_SETUP, _control_setup.get()); 196 | 197 | setConversionType(ONE_SHOT); 198 | } 199 | 200 | /**************************************************************************/ 201 | /*! 202 | @brief read an individual raw spectral channel 203 | @param channel the channel to read 204 | @return the reading as a raw 16-bit integer 205 | */ 206 | /**************************************************************************/ 207 | uint16_t Adafruit_AS726x::readChannel(uint8_t channel) { 208 | return (virtualRead(channel) << 8) | virtualRead(channel + 1); 209 | } 210 | 211 | /**************************************************************************/ 212 | /*! 213 | @brief read the raw channels 214 | @param buf the buffer to read the data into 215 | @param num Optional number of channels to read. Defaults to 216 | AS726x_NUM_CHANNELS 217 | */ 218 | /**************************************************************************/ 219 | void Adafruit_AS726x::readRawValues(uint16_t *buf, uint8_t num) { 220 | for (int i = 0; i < num; i++) { 221 | switch (i) { 222 | case AS726x_VIOLET: 223 | buf[i] = readViolet(); 224 | break; 225 | case AS726x_BLUE: 226 | buf[i] = readBlue(); 227 | break; 228 | case AS726x_GREEN: 229 | buf[i] = readGreen(); 230 | break; 231 | case AS726x_YELLOW: 232 | buf[i] = readYellow(); 233 | break; 234 | case AS726x_ORANGE: 235 | buf[i] = readOrange(); 236 | break; 237 | case AS726x_RED: 238 | buf[i] = readRed(); 239 | break; 240 | default: 241 | break; 242 | } 243 | } 244 | } 245 | 246 | /**************************************************************************/ 247 | /*! 248 | @brief read the calibrated channels 249 | @param buf the buffer to read the data into 250 | @param num Optional number of channels to read. Defaults to 251 | AS726x_NUM_CHANNELS 252 | */ 253 | /**************************************************************************/ 254 | void Adafruit_AS726x::readCalibratedValues(float *buf, uint8_t num) { 255 | for (int i = 0; i < num; i++) { 256 | switch (i) { 257 | case AS726x_VIOLET: 258 | buf[i] = readCalibratedViolet(); 259 | break; 260 | case AS726x_BLUE: 261 | buf[i] = readCalibratedBlue(); 262 | break; 263 | case AS726x_GREEN: 264 | buf[i] = readCalibratedGreen(); 265 | break; 266 | case AS726x_YELLOW: 267 | buf[i] = readCalibratedYellow(); 268 | break; 269 | case AS726x_ORANGE: 270 | buf[i] = readCalibratedOrange(); 271 | break; 272 | case AS726x_RED: 273 | buf[i] = readCalibratedRed(); 274 | break; 275 | default: 276 | break; 277 | } 278 | } 279 | } 280 | 281 | /**************************************************************************/ 282 | /*! 283 | @brief read an individual calibrated spectral channel 284 | @param channel the channel to read 285 | @return the reading as a raw 16-bit integer 286 | */ 287 | /**************************************************************************/ 288 | float Adafruit_AS726x::readCalibratedValue(uint8_t channel) { 289 | uint32_t val = 0; 290 | val = ((uint32_t)virtualRead(channel) << 24) | 291 | ((uint32_t)virtualRead(channel + 1) << 16) | 292 | ((uint32_t)virtualRead(channel + 2) << 8) | 293 | (uint32_t)virtualRead(channel + 3); 294 | 295 | float ret; 296 | memcpy(&ret, &val, 4); 297 | return ret; 298 | } 299 | 300 | void Adafruit_AS726x::write8(byte reg, byte value) { 301 | this->write(reg, &value, 1); 302 | } 303 | 304 | uint8_t Adafruit_AS726x::read8(byte reg) { 305 | uint8_t ret; 306 | this->read(reg, &ret, 1); 307 | 308 | return ret; 309 | } 310 | 311 | uint8_t Adafruit_AS726x::virtualRead(uint8_t addr) { 312 | volatile uint8_t status, d; 313 | while (1) { 314 | // Read slave I²C status to see if the read buffer is ready. 315 | status = read8(AS726X_SLAVE_STATUS_REG); 316 | if ((status & AS726X_SLAVE_TX_VALID) == 0) 317 | // No inbound TX pending at slave. Okay to write now. 318 | break; 319 | } 320 | // Send the virtual register address (setting bit 7 to indicate a pending 321 | // write). 322 | write8(AS726X_SLAVE_WRITE_REG, addr); 323 | while (1) { 324 | // Read the slave I²C status to see if our read data is available. 325 | status = read8(AS726X_SLAVE_STATUS_REG); 326 | if ((status & AS726X_SLAVE_RX_VALID) != 0) 327 | // Read data is ready. 328 | break; 329 | } 330 | // Read the data to complete the operation. 331 | d = read8(AS726X_SLAVE_READ_REG); 332 | return d; 333 | } 334 | 335 | void Adafruit_AS726x::virtualWrite(uint8_t addr, uint8_t value) { 336 | volatile uint8_t status; 337 | while (1) { 338 | // Read slave I²C status to see if the write buffer is ready. 339 | status = read8(AS726X_SLAVE_STATUS_REG); 340 | if ((status & AS726X_SLAVE_TX_VALID) == 0) 341 | // No inbound TX pending at slave. Okay to write now. 342 | break; 343 | } 344 | // Send the virtual register address (setting bit 7 to indicate a pending 345 | // write). 346 | write8(AS726X_SLAVE_WRITE_REG, (addr | 0x80)); 347 | // Serial.print("Address $"); Serial.print(addr, HEX); 348 | while (1) { 349 | // Read the slave I²C status to see if the write buffer is ready. 350 | status = read8(AS726X_SLAVE_STATUS_REG); 351 | if ((status & AS726X_SLAVE_TX_VALID) == 0) 352 | // No inbound TX pending at slave. Okay to write data now. 353 | break; 354 | } 355 | // Send the data to complete the operation. 356 | write8(AS726X_SLAVE_WRITE_REG, value); 357 | // Serial.print(" = 0x"); Serial.println(value, HEX); 358 | } 359 | 360 | void Adafruit_AS726x::read(uint8_t reg, uint8_t *buf, uint8_t num) { 361 | uint8_t buffer[1] = {reg}; 362 | i2c_dev->write_then_read(buffer, 1, buf, num); 363 | } 364 | 365 | void Adafruit_AS726x::write(uint8_t reg, uint8_t *buf, uint8_t num) { 366 | uint8_t buffer[1] = {reg}; 367 | i2c_dev->write(buf, num, true, buffer, 1); 368 | } 369 | -------------------------------------------------------------------------------- /Adafruit_AS726x.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_AS726x.h 3 | * 4 | * This is a library for the Adafruit AS726x breakout board 5 | * ----> https://www.adafruit.com/products/3779 6 | * 7 | * Adafruit invests time and resources providing this open source code, 8 | * please support Adafruit and open-source hardware by purchasing 9 | * products from Adafruit! 10 | * 11 | * Written by Dean Miller for Adafruit Industries. 12 | * 13 | * BSD license, all text here must be included in any redistribution. 14 | * 15 | */ 16 | 17 | #ifndef LIB_ADAFRUIT_AS276X 18 | #define LIB_ADAFRUIT_AS276X 19 | 20 | #if (ARDUINO >= 100) 21 | #include "Arduino.h" 22 | #else 23 | #include "WProgram.h" 24 | #endif 25 | 26 | #include 27 | 28 | /*========================================================================= 29 | I2C ADDRESS/BITS 30 | -----------------------------------------------------------------------*/ 31 | #define AS726x_ADDRESS (0x49) ///< default I2C address 32 | /*=========================================================================*/ 33 | 34 | /**************************************************************************/ 35 | /*! 36 | @brief virtual registers 37 | */ 38 | /**************************************************************************/ 39 | enum { 40 | AS726X_HW_VERSION = 0x00, 41 | AS726X_FW_VERSION = 0x02, 42 | AS726X_CONTROL_SETUP = 0x04, 43 | AS726X_INT_T = 0x05, 44 | AS726X_DEVICE_TEMP = 0x06, 45 | AS726X_LED_CONTROL = 0x07, 46 | 47 | // for reading sensor data 48 | AS7262_V_HIGH = 0x08, 49 | AS7262_V_LOW = 0x09, 50 | AS7262_B_HIGH = 0x0A, 51 | AS7262_B_LOW = 0x0B, 52 | AS7262_G_HIGH = 0x0C, 53 | AS7262_G_LOW = 0x0D, 54 | AS7262_Y_HIGH = 0x0E, 55 | AS7262_Y_LOW = 0x0F, 56 | AS7262_O_HIGH = 0x10, 57 | AS7262_O_LOW = 0x11, 58 | AS7262_R_HIGH = 0x12, 59 | AS7262_R_LOW = 0x13, 60 | 61 | AS7262_V_CAL = 0x14, 62 | AS7262_B_CAL = 0x18, 63 | AS7262_G_CAL = 0x1C, 64 | AS7262_Y_CAL = 0x20, 65 | AS7262_O_CAL = 0x24, 66 | AS7262_R_CAL = 0x28 67 | }; 68 | 69 | /**************************************************************************/ 70 | /*! 71 | @brief hardware registers 72 | */ 73 | /**************************************************************************/ 74 | enum { 75 | AS726X_SLAVE_STATUS_REG = 0x00, 76 | AS726X_SLAVE_WRITE_REG = 0x01, 77 | AS726X_SLAVE_READ_REG = 0x02, 78 | AS726X_SLAVE_TX_VALID = 0x02, 79 | AS726X_SLAVE_RX_VALID = 0x01, 80 | }; 81 | 82 | /**************************************************************************/ 83 | /*! 84 | @brief color registers 85 | */ 86 | /**************************************************************************/ 87 | enum { 88 | AS7262_VIOLET = 0x08, 89 | AS7262_BLUE = 0x0A, 90 | AS7262_GREEN = 0x0C, 91 | AS7262_YELLOW = 0x0E, 92 | AS7262_ORANGE = 0x10, 93 | AS7262_RED = 0x12, 94 | AS7262_VIOLET_CALIBRATED = 0x14, 95 | AS7262_BLUE_CALIBRATED = 0x18, 96 | AS7262_GREEN_CALIBRATED = 0x1C, 97 | AS7262_YELLOW_CALIBRATED = 0x20, 98 | AS7262_ORANGE_CALIBRATED = 0x24, 99 | AS7262_RED_CALIBRATED = 0x28, 100 | }; 101 | 102 | /**************************************************************************/ 103 | /*! 104 | @brief conversion modes. Default is Mode 2 105 | */ 106 | /**************************************************************************/ 107 | enum conversion_types { 108 | MODE_0 = 0b00, 109 | MODE_1 = 0b01, 110 | MODE_2 = 0b10, // default 111 | ONE_SHOT = 0b11, 112 | }; 113 | 114 | /**************************************************************************/ 115 | /*! 116 | @brief gain settings. Default is 1x gain 117 | */ 118 | /**************************************************************************/ 119 | enum channel_gain { 120 | GAIN_1X = 0b00, // default 121 | GAIN_3X7 = 0b01, 122 | GAIN_16X = 0b10, 123 | GAIN_64X = 0b11, 124 | }; 125 | 126 | /**************************************************************************/ 127 | /*! 128 | @brief indicator LED current limit settings. Default is 1mA 129 | */ 130 | /**************************************************************************/ 131 | enum ind_led_current_limits { 132 | LIMIT_1MA = 0b00, // default 133 | LIMIT_2MA = 0b01, 134 | LIMIT_4MA = 0b10, 135 | LIMIT_8MA = 0b11, 136 | }; 137 | 138 | /**************************************************************************/ 139 | /*! 140 | @brief Driver LED current limit settings. Default is 12.5 mA 141 | */ 142 | /**************************************************************************/ 143 | enum drv_led_current_limits { 144 | LIMIT_12MA5 = 0b00, // default 145 | LIMIT_25MA = 0b01, 146 | LIMIT_50MA = 0b10, 147 | LIMIT_100MA = 0b11, 148 | }; 149 | 150 | /*=========================================================================*/ 151 | 152 | #define AS726x_INTEGRATION_TIME_MULT 2.8 ///< multiplier for integration time 153 | #define AS726x_NUM_CHANNELS 6 ///< number of sensor channels 154 | 155 | /**************************************************************************/ 156 | /*! 157 | @brief Color definitions used by the library 158 | */ 159 | /**************************************************************************/ 160 | enum { 161 | AS726x_VIOLET = 0, 162 | AS726x_BLUE, 163 | AS726x_GREEN, 164 | AS726x_YELLOW, 165 | AS726x_ORANGE, 166 | AS726x_RED, 167 | }; 168 | 169 | /**************************************************************************/ 170 | /*! 171 | @brief Class that stores state and functions for interacting with AS726x 172 | spectral sensors 173 | */ 174 | /**************************************************************************/ 175 | class Adafruit_AS726x { 176 | public: 177 | /*! 178 | @brief Class constructor 179 | @param addr Optional I2C address the sensor can be found on. Defaults to 180 | 0x49. 181 | */ 182 | Adafruit_AS726x(int8_t addr = AS726x_ADDRESS) { _i2caddr = addr; }; 183 | ~Adafruit_AS726x(void); 184 | 185 | bool begin(TwoWire *theWire = &Wire); 186 | 187 | /*========= LED STUFF =========*/ 188 | 189 | // Set indicator LED current 190 | void setIndicateCurrent(uint8_t current); 191 | // turn on/off indicator 192 | void indicateLED(boolean on); 193 | 194 | // turn on the drv led 195 | void drvOn(); 196 | // turn off the drv led 197 | void drvOff(); 198 | 199 | // set current through drv led 200 | void setDrvCurrent(uint8_t current); 201 | 202 | /*===== END LED STUFF ======*/ 203 | 204 | void setConversionType(uint8_t type); 205 | void setGain(uint8_t gain); 206 | void setIntegrationTime(uint8_t time); 207 | void enableInterrupt(); 208 | void disableInterrupt(); 209 | 210 | /*====== MEASUREMENTS ========*/ 211 | 212 | // read sensor data 213 | void startMeasurement(); 214 | 215 | /*! 216 | @brief Check if the sensor is ready to return data 217 | @return true if data is ready to be read, false otherwise. 218 | */ 219 | bool dataReady() { return virtualRead(AS726X_CONTROL_SETUP) & 0x02; } 220 | 221 | /*! 222 | @brief Read the on-board temperature sensor 223 | @return the temperature in Centigrade. 224 | */ 225 | uint8_t readTemperature() { return virtualRead(AS726X_DEVICE_TEMP); } 226 | uint16_t readChannel(uint8_t channel); 227 | 228 | /*! 229 | @brief Read raw violet color value (AS7262 only) 230 | @return the violet reading as an unsigned 16-bit integer 231 | */ 232 | uint16_t readViolet() { return (readChannel(AS7262_VIOLET)); } 233 | /*! 234 | @brief Read raw blue color value (AS7262 only) 235 | @return the blue reading as an unsigned 16-bit integer 236 | */ 237 | uint16_t readBlue() { return (readChannel(AS7262_BLUE)); } 238 | /*! 239 | @brief Read raw green color value (AS7262 only) 240 | @return the green reading as an unsigned 16-bit integer 241 | */ 242 | uint16_t readGreen() { return (readChannel(AS7262_GREEN)); } 243 | /*! 244 | @brief Read raw yellow color value (AS7262 only) 245 | @return the yellow reading as an unsigned 16-bit integer 246 | */ 247 | uint16_t readYellow() { return (readChannel(AS7262_YELLOW)); } 248 | /*! 249 | @brief Read raw orange color value (AS7262 only) 250 | @return the orange reading as an unsigned 16-bit integer 251 | */ 252 | uint16_t readOrange() { return (readChannel(AS7262_ORANGE)); } 253 | /*! 254 | @brief Read raw red color value (AS7262 only) 255 | @return the red reading as an unsigned 16-bit integer 256 | */ 257 | uint16_t readRed() { return (readChannel(AS7262_RED)); } 258 | 259 | void readRawValues(uint16_t *buf, uint8_t num = AS726x_NUM_CHANNELS); 260 | 261 | float readCalibratedValue(uint8_t channel); 262 | 263 | /*! 264 | @brief Read calibrated violet color value (AS7262 only) 265 | @return the violet reading as a 32-bit floating point number 266 | */ 267 | float readCalibratedViolet() { 268 | return (readCalibratedValue(AS7262_VIOLET_CALIBRATED)); 269 | } 270 | /*! 271 | @brief Read calibrated blue color value (AS7262 only) 272 | @return the blue reading as a 32-bit floating point number 273 | */ 274 | float readCalibratedBlue() { 275 | return (readCalibratedValue(AS7262_BLUE_CALIBRATED)); 276 | } 277 | /*! 278 | @brief Read calibrated green color value (AS7262 only) 279 | @return the green reading as a 32-bit floating point number 280 | */ 281 | float readCalibratedGreen() { 282 | return (readCalibratedValue(AS7262_GREEN_CALIBRATED)); 283 | } 284 | /*! 285 | @brief Read calibrated yellow color value (AS7262 only) 286 | @return the yellow reading as a 32-bit floating point number 287 | */ 288 | float readCalibratedYellow() { 289 | return (readCalibratedValue(AS7262_YELLOW_CALIBRATED)); 290 | } 291 | /*! 292 | @brief Read calibrated orange color value (AS7262 only) 293 | @return the orange reading as a 32-bit floating point number 294 | */ 295 | float readCalibratedOrange() { 296 | return (readCalibratedValue(AS7262_ORANGE_CALIBRATED)); 297 | } 298 | /*! 299 | @brief Read calibrated red color value (AS7262 only) 300 | @return the red reading as a 32-bit floating point number 301 | */ 302 | float readCalibratedRed() { 303 | return (readCalibratedValue(AS7262_RED_CALIBRATED)); 304 | } 305 | 306 | void readCalibratedValues(float *buf, uint8_t num = AS726x_NUM_CHANNELS); 307 | 308 | /*==== END MEASUREMENTS =====*/ 309 | 310 | private: 311 | Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface 312 | uint8_t _i2caddr; ///< the I2C address of the sensor 313 | 314 | void write8(byte reg, byte value); 315 | uint8_t read8(byte reg); 316 | 317 | uint8_t virtualRead(uint8_t addr); 318 | void virtualWrite(uint8_t addr, uint8_t value); 319 | 320 | void read(uint8_t reg, uint8_t *buf, uint8_t num); 321 | void write(uint8_t reg, uint8_t *buf, uint8_t num); 322 | void _i2c_init(); 323 | 324 | struct control_setup { 325 | 326 | uint8_t unused : 1; 327 | 328 | /* 1: data ready to be read, sets int active if int is enabled */ 329 | uint8_t DATA_RDY : 1; 330 | 331 | /* conversion type 332 | * 0b00 = Mode 0 333 | * 0b01 = Mode 1 334 | * 0b10 = Mode 2 335 | * 0b11 = Mode 3 One shot 336 | */ 337 | uint8_t BANK : 2; 338 | 339 | /* Channel gain setting (all channels) 340 | * 0b00 = 1x 341 | * 0b01 = 3.7x 342 | * 0b10 = 16x 343 | * 0b11 = 64x 344 | */ 345 | uint8_t GAIN : 2; 346 | 347 | /* enable or disable interrupt */ 348 | uint8_t INT : 1; 349 | uint8_t RST : 1; 350 | 351 | uint8_t get() { 352 | return ((DATA_RDY << 1) | (BANK << 2) | (GAIN << 4) | (INT << 6) | 353 | (RST << 7)); 354 | }; 355 | }; 356 | control_setup _control_setup; 357 | 358 | struct int_time { 359 | 360 | // integration time (multiplied by INTEGRATION_TIME_MULT) in ms 361 | uint8_t INT_T : 8; 362 | 363 | uint8_t get() { return INT_T; }; 364 | }; 365 | int_time _int_time; 366 | 367 | struct led_control { 368 | // enable or disable indicator LED 369 | uint8_t LED_IND : 1; 370 | 371 | // indicator led current limit 372 | uint8_t ICL_IND : 2; 373 | 374 | // enable or disable led_drv 375 | uint8_t LED_DRV : 1; 376 | 377 | uint8_t ICL_DRV : 2; 378 | 379 | uint8_t get() { 380 | return (LED_IND | (ICL_IND << 1) | (LED_DRV << 3) | (ICL_DRV << 4)); 381 | }; 382 | }; 383 | led_control _led_control; 384 | }; 385 | 386 | #endif 387 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adafruit AS726x Library [![Build Status](https://github.com/adafruit/Adafruit_AS726x/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_AS726x/actions) 2 | 3 | 4 | 5 | This is a library for the Adafruit AS7262 spectral sensor breakout: 6 | * https://www.adafruit.com/products/3779 7 | 8 | Also supports other chips in the AS726x sensor family. 9 | 10 | Check out the links above for our tutorials and wiring diagrams. This chip uses I2C to communicate 11 | 12 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 13 | 14 | Written by Dean Miller for Adafruit Industries. 15 | MIT license, all text above must be included in any redistribution 16 | -------------------------------------------------------------------------------- /examples/AS7262_test/AS7262_test.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | This is a library for the Adafruit AS7262 6-Channel Visible Light Sensor 3 | 4 | This sketch reads the sensor 5 | 6 | Designed specifically to work with the Adafruit AS7262 breakout 7 | ----> http://www.adafruit.com/products/3779 8 | 9 | These sensors use I2C to communicate. The device's I2C address is 0x49 10 | Adafruit invests time and resources providing this open source code, 11 | please support Adafruit andopen-source hardware by purchasing products 12 | from Adafruit! 13 | 14 | Written by Dean Miller for Adafruit Industries. 15 | BSD license, all text above must be included in any redistribution 16 | ***************************************************************************/ 17 | 18 | #include 19 | #include "Adafruit_AS726x.h" 20 | 21 | //create the object 22 | Adafruit_AS726x ams; 23 | 24 | //buffer to hold raw values 25 | uint16_t sensorValues[AS726x_NUM_CHANNELS]; 26 | 27 | //buffer to hold calibrated values (not used by default in this example) 28 | //float calibratedValues[AS726x_NUM_CHANNELS]; 29 | 30 | void setup() { 31 | Serial.begin(9600); 32 | while(!Serial); 33 | 34 | // initialize digital pin LED_BUILTIN as an output. 35 | pinMode(LED_BUILTIN, OUTPUT); 36 | 37 | //begin and make sure we can talk to the sensor 38 | if(!ams.begin()){ 39 | Serial.println("could not connect to sensor! Please check your wiring."); 40 | while(1); 41 | } 42 | } 43 | 44 | void loop() { 45 | 46 | //read the device temperature 47 | uint8_t temp = ams.readTemperature(); 48 | 49 | //ams.drvOn(); //uncomment this if you want to use the driver LED for readings 50 | ams.startMeasurement(); //begin a measurement 51 | 52 | //wait till data is available 53 | bool rdy = false; 54 | while(!rdy){ 55 | delay(5); 56 | rdy = ams.dataReady(); 57 | } 58 | //ams.drvOff(); //uncomment this if you want to use the driver LED for readings 59 | 60 | //read the values! 61 | ams.readRawValues(sensorValues); 62 | //ams.readCalibratedValues(calibratedValues); 63 | 64 | Serial.print("Temp: "); Serial.print(temp); 65 | Serial.print(" Violet: "); Serial.print(sensorValues[AS726x_VIOLET]); 66 | Serial.print(" Blue: "); Serial.print(sensorValues[AS726x_BLUE]); 67 | Serial.print(" Green: "); Serial.print(sensorValues[AS726x_GREEN]); 68 | Serial.print(" Yellow: "); Serial.print(sensorValues[AS726x_YELLOW]); 69 | Serial.print(" Orange: "); Serial.print(sensorValues[AS726x_ORANGE]); 70 | Serial.print(" Red: "); Serial.print(sensorValues[AS726x_RED]); 71 | Serial.println(); 72 | Serial.println(); 73 | } 74 | -------------------------------------------------------------------------------- /examples/color_graph/color_graph.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | This is a library for the Adafruit AS7262 6-Channel Visible Light Sensor 3 | 4 | This sketch reads the sensor and creates a color bar graph on a tiny TFT 5 | 6 | Designed specifically to work with the Adafruit AS7262 breakout and 160x18 tft 7 | ----> http://www.adafruit.com/products/3779 8 | ----> http://www.adafruit.com/product/3533 9 | 10 | These sensors use I2C to communicate. The device's I2C address is 0x49 11 | Adafruit invests time and resources providing this open source code, 12 | please support Adafruit andopen-source hardware by purchasing products 13 | from Adafruit! 14 | 15 | Written by Dean Miller for Adafruit Industries. 16 | BSD license, all text above must be included in any redistribution 17 | ***************************************************************************/ 18 | 19 | #include 20 | #include "Adafruit_AS726x.h" 21 | 22 | #include // Core graphics library 23 | #include // Hardware-specific library 24 | #include 25 | 26 | 27 | // For the breakout, you can use any 2 or 3 pins 28 | // These pins will also work for the 1.8" TFT shield 29 | #define TFT_CS 10 30 | #define TFT_RST 9 // you can also connect this to the Arduino reset 31 | // in which case, set this #define pin to -1! 32 | #define TFT_DC 8 33 | 34 | #define SENSOR_MAX 5000 35 | 36 | #define BLACK 0x0000 37 | #define GRAY 0x8410 38 | #define WHITE 0xFFFF 39 | #define RED 0xF800 40 | #define ORANGE 0xFA60 41 | #define YELLOW 0xFFE0 42 | #define LIME 0x07FF 43 | #define GREEN 0x07E0 44 | #define CYAN 0x07FF 45 | #define AQUA 0x04FF 46 | #define BLUE 0x001F 47 | #define MAGENTA 0xF81F 48 | #define PINK 0xF8FF 49 | 50 | uint16_t colors[] = { 51 | MAGENTA, 52 | BLUE, 53 | GREEN, 54 | YELLOW, 55 | ORANGE, 56 | RED 57 | }; 58 | 59 | Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); 60 | 61 | //create the object 62 | Adafruit_AS726x ams; 63 | 64 | //buffer to hold raw values (these aren't used by default in this example) 65 | //uint16_t sensorValues[AS726x_NUM_CHANNELS]; 66 | 67 | //buffer to hold calibrated values 68 | float calibratedValues[AS726x_NUM_CHANNELS]; 69 | 70 | uint16_t barWidth; 71 | 72 | void setup() { 73 | 74 | Serial.begin(9600); 75 | 76 | tft.initR(INITR_MINI160x80); // initialize a ST7735S chip, mini display 77 | tft.setRotation(3); 78 | 79 | tft.fillScreen(ST7735_BLACK); 80 | 81 | barWidth = tft.width() / AS726x_NUM_CHANNELS; 82 | 83 | // initialize digital pin LED_BUILTIN as an output. 84 | pinMode(LED_BUILTIN, OUTPUT); 85 | 86 | //begin and make sure we can talk to the sensor 87 | if(!ams.begin()){ 88 | Serial.println("could not connect to sensor! Please check your wiring."); 89 | while(1); 90 | } 91 | 92 | ams.setConversionType(MODE_2); 93 | 94 | //uncomment this if you want to use the driver LED (off by default) 95 | //ams.drvOn(); 96 | } 97 | 98 | void loop() { 99 | 100 | if(ams.dataReady()){ 101 | 102 | //read the values! 103 | //ams.readRawValues(sensorValues); 104 | ams.readCalibratedValues(calibratedValues); 105 | 106 | for(int i=0; i 5 | sentence=Adafruit Channel Visible Light / Color Sensor Breakout 6 | paragraph=Adafruit Channel Visible Light / Color Sensor Breakout 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_AS726x 9 | architectures=* 10 | depends=Adafruit ST7735 and ST7789 Library, Adafruit GFX Library, Adafruit BusIO 11 | --------------------------------------------------------------------------------