Introduction

We live in a colorful world, but how much do you really know about color? You eyes may deceive you, while the sensors don’t lie. This AS7341sensor can tell you the truth about color.
Gravity: AS7341 Visible Light Sensor employs the new generation of AS7341 spectral sensor IC launched by the well-known AMS company. The sensor features eight channels for the visible light, one channel for near-IR, and one channel without a filter. Also it integrates a dedicated channel to detect ambient light flicker. Besides that, this sensor comes with 6 independent 16-bit ADC channels for data processing in parallel. The two on-board extra-bright LEDs can supply light in dark environment.

AS7341

RGB Color Sensor vs AS7341 Sensor

Yellow LED WS2812 Simulated Yellow
Recognition result of RGB color sensor Red+Green Red+Green
Recognition result of AS7341 visible light sensor Yellow Red+Green

You can click here find information about what visible light is.

Visible Light

Features

Application

Specification

Board Overview

Board Overview

Num Silkscreen Description
1 + Positive
2 - Negative
3 SCL I2C Clock line
4 SDA I2C Data line

Tutorial

Requirements

  /**
   * @brief Set gain value (0~10 corresponds to X0.5,X1,X2,X4,X8,X16,X32,X64,X128,X256,X512)
   * @param The value of register CFG1
   */
  void setAGAIN(uint8_t value);

  /**
   * @brief Read the value of sensor data channel 0~5, under eF1F4ClearNIR
   * @return The data of sModeOneData_t
   */
  sModeOneData_t readSpectralDataOne();

  /**
   * @brief Read the value of sensor data channel 0~5, under eF5F8ClearNIR
   * @return The data of sModeTwoData_t
   */
  sModeTwoData_t readSpectralDataTwo();

  /**
   * @brief Read the value of register flicker, through which the flicker frequency of the light source can be predicted
   * @return The data of register flicker
   */
  uint8_t readFlickerData();

  /**
   * @brief Turn the LED on or off
   * @param true or false
   */
  void enableLed(bool on);

  /**
   * @brief Set pin current to control brightness (1~20 corresponds to current 4mA,6mA,8mA,10mA,12mA,......,42mA)
   * @param 1~20
   */
  void controlLed(uint8_t current);

Connection Diagram

Connection Diagram

Sample Code 1- Data Reading

Read the values of 10 optical channels of the AS7341 spectral sensor, the more light of a certain wavelength of the light source, the greater the corresponding channel value.

The two on-board extra-bright LEDs can supply light in dark environment.

/*!
 * @file getData.ino
 * @brief Read the values of 10 optical channels of the AS7341 spectral sensor, the more light of a certain wavelength of the light source,
 * the greater the corresponding channel value.
 *
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [fengli](li.feng@dfrobot.com)
 * @version  V1.0
 * @date  2020-07-16
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_AS7341
 */
#include "DFRobot_AS7341.h"
/*!
 * @brief Construct the function
 * @param pWire IC bus pointer object and construction device, can both pass or not pass parameters, Wire in default.
 */
DFRobot_AS7341 as7341;

void setup(void)
{
  Serial.begin(115200);
  //Detect if IIC can communicate properly 
  while (as7341.begin() != 0) {
    Serial.println("IIC init failed, please check if the wire connection is correct");
    delay(1000);
  }
//  //Integration time = (ATIME + 1) x (ASTEP + 1) x 2.78µs
//  //Set the value of register ATIME(1-255), through which the value of Integration time can be calculated. The value represents the time that must be spent during data reading.
//  as7341.setAtime(29);
//  //Set the value of register ASTEP(0-65534), through which the value of Integration time can be calculated. The value represents the time that must be spent during data reading.
//  as7341.setAstep(599);
//  //Set gain value(0~10 corresponds to X0.5,X1,X2,X4,X8,X16,X32,X64,X128,X256,X512)
//  as7341.setAGAIN(7);
//  //Enable LED
//  //as7341.enableLed(true);
//  //Set pin current to control brightness (1~20 corresponds to current 4mA,6mA,8mA,10mA,12mA,......,42mA)
//  //as7341.controlLed(10);
}
void loop(void)
{
  DFRobot_AS7341::sModeOneData_t data1;
  DFRobot_AS7341::sModeTwoData_t data2;

  //Start spectrum measurement 
  //Channel mapping mode: 1.eF1F4ClearNIR,2.eF5F8ClearNIR
  as7341.startMeasure(as7341.eF1F4ClearNIR);
  //Read the value of sensor data channel 0~5, under eF1F4ClearNIR
  data1 = as7341.readSpectralDataOne();

  Serial.print("F1(405-425nm):");
  Serial.println(data1.ADF1);
  Serial.print("F2(435-455nm):");
  Serial.println(data1.ADF2);
  Serial.print("F3(470-490nm):");
  Serial.println(data1.ADF3);
  Serial.print("F4(505-525nm):");   
  Serial.println(data1.ADF4);
  //Serial.print("Clear:");
  //Serial.println(data1.ADCLEAR);
  //Serial.print("NIR:");
  //Serial.println(data1.ADNIR);
  as7341.startMeasure(as7341.eF5F8ClearNIR);
  //Read the value of sensor data channel 0~5, under eF5F8ClearNIR
  data2 = as7341.readSpectralDataTwo();
  Serial.print("F5(545-565nm):");
  Serial.println(data2.ADF5);
  Serial.print("F6(580-600nm):");
  Serial.println(data2.ADF6);
  Serial.print("F7(620-640nm):");
  Serial.println(data2.ADF7);
  Serial.print("F8(670-690nm):");
  Serial.println(data2.ADF8);
  Serial.print("Clear:");
  Serial.println(data2.ADCLEAR);
  Serial.print("NIR:");
  Serial.println(data2.ADNIR);
  delay(1000);
}

Expected Results

Visible light spectrum

SEN0364result1

Sample Code 2- Measure light source flicker frequency

Measure whether the flicker frequency of ambient light is 50Hz or 60Hz. Example Code 2.1 and code 2.2 can be used to simulate the flicker of ambient light at 50Hz or 60Hz.

/*!
 * @file getFlicker.ino
 * @brief Read the flicker frequency of light source 
 * 
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [fengli](li.feng@dfrobot.com)
 * @version  V1.0
 * @date  2020-07-16
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_AS7341
 */
#include "DFRobot_AS7341.h"
/*!
 * @brief Construct the function
 * @param pWire IC bus pointer object and construction device, can both pass or not pass parameters, Wire in default.
 */
DFRobot_AS7341 as7341;


void setup(void)
{
  Serial.begin(115200);
  //Detect if IIC can communicate properly
  while (as7341.begin() != 0) {
    Serial.println("IIC init failed, please check if the wire connection is correct");
    delay(1000);
  }
}

void loop(void){
  uint8_t freq = 0;
  //Read the value of register flicker, through which the flicker frequency of the light source can be predicted
  freq = as7341.readFlickerData();
  if (freq == 1) {
    Serial.println("Unknown frequency");
  } else if (freq == 0) {
    Serial.println("No flicker");
  } else {
    Serial.print(freq);
    Serial.println("Hz");
  }
}

Result 2

SEN0364result2

Sample Code 2.1 - Simulate 50Hz light flicker

Burn the codes to another main-controller, and connect an LED to the Digital pin 10 to simulate the light source of 50Hz.

//50HZ
//Burn the codes to another main-controller, and connect an LED onto the Digital pin 10 to provide 50Hz ambient light for getFlicker.ino. 

void setup() {
  pinMode(10, OUTPUT);
}

void loop() {
  digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(5);
  digitalWrite(10, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(5);
}

Sample Code 2.2 - Simulate 60Hz light flicker

Burn the codes to another main-controller, and connect an LED to the Digital pin 10 to simulate the light source of 60Hz.

//60HZ
//Burn the codes to another main-controller, and connect an LED onto the Digital pin 10 to provide 60Hz ambient light for getFlicker.ino.

void setup() {
  pinMode(10, OUTPUT);
}

void loop() {
  digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(4167);
  digitalWrite(10, LOW);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(4167);
}

FAQ

On-board LED spectrum

Q&A Some general Arduino Problems/FAQ/Tips
Q I have a question!
A Please click the topic link on DFRobot Forum.
A For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

DFshopping_car1.png Get Gravity: AS7341 11-Channel Visible Light Sensor from DFRobot Store or DFRobot Distributor.

Turn to the Top