AS7341 Spectral Color Sensor

From Waveshare Wiki
Jump to: navigation, search
AS7341 Spectral Color Sensor
AS7341 Spectral Color Sensor

AS7341 Spectral Color Sensor, Visible Spectrum Sensor, Multi Channels, High Precision, I2C Bus
{{{name2}}}

{{{name3}}}

{{{name4}}}

{{{name5}}}

{{{name6}}}

Overview

Introduction

With AS7341 visible spectrum sensing IC as the core, this product can sense the visible light component values of different bands in the environment. It is also quite impressive in sensitivity and accuracy. At the same time, its volume is very small. To make a miniature spectrum analyzer, it will be a very good choice.

Feature

  • Incorporates AS7341 chip, which integrates 8 x visible spectrum channels, 1 x near-infrared channel, and 1 x no filter channel.
  • Embedded 6 x independent 16-bit ADC, which allows effective processing of data in parallel.
  • Dedicated channel to detect ambient light flicker on the specific frequency.
  • 2 x high brightness LEDs, can be used as fill light in a dim environment.
  • Interrupt pin to output inner ADC real-time operating status.
  • Features spectrum interrupt detection, with programable high/low thresholds.
  • Provides general-purpose input/output GPIO pin.
  • Onboard voltage translator, compatible with 3.3V/5V operating voltage.
  • Comes with online development resources and manual (examples for Raspberry Pi/Arduino/STM32).

Specification

  • Operating voltage: 3.3V/5V
  • Operating current: 20mA (LED off) 70mA (LED on)
  • Sensor: AS7341
  • Logical voltage: 3.3V/5V
  • Interface: I2C
  • Dimension: 30.5mm x 23mm
  • Mounting hole size: 2.0mm

Pinout

Pin number PIN Description
1 VCC 3.3V/5V Power input
2 GND Ground
3 SDA I2C data cable
4 SCL I2C clock cable
5 INT Interrupt output pin
6 GPIO input/output GPIO port

Hardware description

AS7341 Chip

This product uses AS7341-DLGM as the core, which is an 11-channel IC for spectrum recognition and color matching applications. The spectral response is defined at a wavelength of about 350nm to 1000nm. 6 channels can be processed in parallel by independent adcs, while the other channels can be accessed through a multiplexer. AS7341 integrates the filter into the silicon of standard CMOS through a nano-optical deposition interference filter. The technology and its package provide a built-in aperture to control the light entering the sensor array. Its control and spectral data access are realized through the serial I²C interface.

Working Protocol

This product uses I2C communication with one data line and one clock line. There are three types of signals in the I2C bus in the process of transmitting data: START signal, STOP signal, and Response signal.

TSL2591.png

START signal: When SCL is high level, SDA jumps from high to low to start transmitting data.
STOP signal: When SCL is high level, SDA jumps from low level to high level, ending the data transmission.
Response signal: After receiving the 8bit data, the data receiving IC sends a specific low-level pulse to the data sending IC to indicate that the data has been received.

  • I2C write data timing

Tsl2591 02.png

First, the host(ie, Raspberry Pi) will send a start signal, and then combine its I2C 7-bit address and write operation bits into 8-bit data and send it to the slave (ie TSL2581 sensor module), the slave will respond with a response signal after receiving it. The host sends the command register address to the slave, and then the slave receives this response signal. At this time, the master sends the value of the command register and the slave responds with a response signal. Until the host sends a STOP signal, the I2C write data operation ends.

  • I2C read data timing

TSL2591 03.png

First, the host will send a START signal, and then combine its I2C 7-bit address and write operation bit into 8-bit data and send it to the slave. After receiving it, the slave will respond with a response signal, and the host will send the command register address at this time. After the slave receives the sending response signal, the host will send a START signal again, and combines its 7-bit address and read operation bit into 8-bit data and sends it to the slave. The slave sends a response after receiving the signal, then sends the value in its register to the host. And the host gives a response signal until the host sends a stop signal, and this communication ends.

  • I2C address

The I2C device address of AS7341 is 0X39 which you can check on page 21 of the AS7341 data sheet.

AS7341-I2C-address.png

This example is tested on Arduino UNO. If you use other models of Arduino, please pay attention to whether the related pins are connected correctly.

Arduino

Hardware connection

AS7341 Spectral Color.jpg

Run the demo

  • After downloading the demo, unzip this .7z file on your PC.
  • The Arduino program is located in ~/Arduino/... Copy the Waveshare_AS7341 folder from this directory to the libraries in the Arduino installation directory, usually in C:\Users\XXX\Documents\Arduino\libraries or C:\Program Files (x86)\Arduino \libraries.
  • Open Arduino IDE, click File->Example, and check whether there has Waveshare_AS7341 option.
AS7341 clear Arduino.png
  • If the Waveshare_AS7341 library is imported successfully, open the ino project file in the Arduino/Waveshare_AS7341/example
  • Select the corresponding model of the development board and COM port, compile the program, and then download it to UNO, and open the serial monitor.
AS7341 clear Arduino 2.png
  • Demo phenomena:
AS7341 Arduino com.png

Demo description

There are several different test projects on the Arduino/Waveshare_AS7341/example directory, here we give some describes and precautions.

  • AS7341_Getdata is used to obtain 10 channels of test data. AS7341 has only 6 independent ADCs, but it has 11 channels, which requires multiplexer SMUX. For related configuration, please refer to the reference code of the datasheet manual.
  • AS7341_Getdata includes the driver code to turn on the fill light LED and adjust the brightness.
AS7341_EnableLED(true);// LED Enable
AS7341_ControlLed(false,10);//Turn on or off the LED and set the brightness of the LED

If you want to use the LED to fill the light, you can change the "false" of the two lines of code to "true".

  • AS7341_Getflicker is used to detect 100 or 120Hz ambient light flicker, you need to generate a flickering light of this frequency by yourself. Adjust the integration time, gain, etc. to detect a flicker of different frequencies.
  • AS7341_Syns configures the sensor mode as SYNS mode. In this mode, the GPIO port of the sensor needs to receive a falling edge signal to trigger measurement, and each falling edge triggers a measurement.

This module does not directly connect to the GPIO port by default. Therefore, during your testing, you can briefly touch the GPIO port with the 3.3v or 5V pin of the development board and then disconnect it to obtain a falling edge signal. And in your actual use, you can connect the GPIO port to the trigger source directly.

while(!AS7341_MeasureComplete());   // Jump out of this loop when GPIO receives a valid signal.
  • AS7341_INT is the spectrum interrupt test. It sets the upper and lower thresholds for interrupt generation. At the same time, you can set the channel triggered by the interrupt. The channel selection can be one of CH0-CH4. When the interrupt is triggered by the change of ambient light, read the value of the relevant register to check whether it is triggered.
AS7341_SetInterruptPersistence(0);   // Set the sensitivity of spectrum interruption
AS7341_SetSpectralThresholdChannel(4);  //Set the channel to detect interrupts
  • AS7341_pinINT is an experiment on the INT pin on the module. After each measurement of AS7341, the INT pin will become low level. You can configure the relevant register to set how often the sensor measures the environmental spectrum data, which also determines the INT pin How often does the foot jump. In this demo, the measurement time is set to 1s, and the level status of the INT pin is monitored at the same time.
  • AS7341_Clear is to reset all the register enable bits that are turned on in the AS7341.

This demo has been verified on NUCLEO-F103RB (chip model STM32RBT6) and OpenH743I-C (chip model STM32H743IIT6). If you need to migrate to other boards, please pay attention to the relevant configuration and Hardware connection.

STM32

Hardware connection

AS7341 Spectral Color02.jpg

AS7341 Spectral Color Sensor XNUCLEO-F103RB
VCC 3.3V/5V Power input
GND Ground
SDA SDA/D14/PB9
SCL SCL/D15/PB8
INT D8/PA9
GPIO /

Hardware connection with OpenH743I-C.

AS7341 Spectral Color Sensor OpenH743I-C
VCC 3.3V/5V Power input
GND Ground
SDA PD13(I2C4 SDA)
SCL PD12(I2C4 SCL)
INT PD11
GPIO /

Demo description

After downloading the demo, unzip this .7z file on your PC. The STM32 demo is located in ~/ STM32/… . You can see the two folders NUCLEO-F103RB and OpenH743I-C.

NUCLEO-F103RB

  • Open STM32\XNUCLEO-F103RB\MDK-ARM\demo.uvprojx on the demo folder, this demo uses HAL library.
  • If you need to change the chip or want to use the standard library, you just need to change DEV_Config.c and .h to implement the functions and macro definitions inside. The chip can also be configured by using STM32CubeMX. This demo uses serial port 2 (PA2, PA3) to output data.
  • The serial port baud rate is 115200, and other options use the default values: 8 data bits, 1 stop bit, and no parity bit. The serial port assistant tool is provided in the folder.

OpenH743I-C

  • Open STM32\OpenH743I-C\MDK-ARM\I2C.uvprojx, this demo also uses the HAL library. The two are different in terms of chip signals and peripheral configuration, but the test demo used is exactly the same. Here we take OpenH743I-C as an example.
  • Open main.c in the project, and uncomment the program that needs to be tested. Shown in the picture below:
AS7341 STM32 test.png
  • Connect the downloader, connect the serial data line to USART1, and click compile to download and verify.
  • The related demo usage and instructions have been explained in the Arduino tutorial, you can check it on the Arduino chapter page.
  • Test result:
AS7341 STM32 com.png

This example uses Raspberry Pi 3 Model B, provides BCM2835, WiringPi, file IO, RPI (Python) library demos.

Raspberry Pi

Hardware connection

AS7341 Spectral Color 03.jpg
AS7341 Spectral Color Sensor Raspberry Pi (BCM)
VCC 3.3V/5V Power input
GND Ground
SDA SDA(2)
SCL SCL(3)
INT 4
GPIO /

Enable I2C interface

  • Open the terminal, and use the command to enter the configuration page.
sudo raspi-config
Choose Interfacing Options -> I2C -> Yes to enable I2C interface.

RPI open i2c.png

And then reboot the system:

sudo reboot

Libraries Installation

  • Install lg library:
#Open the Raspberry Pi terminal and run the following commands:  
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
make
sudo make install
#For more details, you can refer to https://github.com/gpiozero/lg
  • Install gpiod library (Optional)
#Open the Raspberry Pi terminal and run the following commands:  
sudo apt-get update
sudo apt install gpiod libgpiod-dev
  • Install BCM2835 (Optional)
#Open the Raspberry Pi terminal and run the following commands:
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure && sudo make && sudo make check && sudo make install
#for more details, you can refer to http://www.airspayce.com/mikem/bcm2835/
  • Install wiringPi (Optional)
#Open the Raspberry Pi terminal and run the following commands:  
sudo apt-get install wiringpi
#For Raspberry Pi systems after May 2019 (earlier may not be performed), an upgrade may be required:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
#Run gpio -v, it will display 2.52 version; if not, the installation fails.  

#For Bullseye branch system:
git clone https://github.com/WiringPi/WiringPi
cd WiringPi
./build
gpio -v
# Running gpio -v, it will display 2.60 version; if not, the installation fails  

Download the demo

Open the terminal of the Raspberry Pi, execute command to download demo codes:

sudo apt-get install p7zip-full
wget https://files.waveshare.com/upload/b/b3/AS7341_Spectral_Color_Sensor_code.7z
7z x AS7341_Spectral_Color_Sensor_code.7z -r -o./AS7341_Spectral_Color_Sensor_code
sudo chmod 777 -R  AS7341_Spectral_Color_Sensor_code

C

cd AS7341_Spectral_Color_Sensor_code/AS7341_Spectral_Color_Sensor_code/RaspberryPi/c
make clean
make

Enter the following command to execute the demo:

sudo ./main data
  • 【Note】The 'data' here can be changed to flicker, syns, int, pinint, clear to verify different test demos, and its meaning is explained in the code.

data corresponds to the Arduino's AS7341_Getdata demo
flicker corresponds to Arduino's AS7341_Getflicker demo
syns corresponds to Arduino's AS7341_Syns demo
int corresponds to Arduino's AS7341_INT demo
pinint corresponds to Arduino's AS7341_pinINT demo
clear corresponds to the AS7341_Clear demo of Arduino

  • Take the execution of sudo ./main data as an example, the test result is:
AS7341 Raspberry com c.png

python

  • Install libraries:
sudo apt-get update
sudo apt-get install python3-pip
sudo apt-get install python3-pil
sudo apt-get install python3-numpy
sudo pip3 install smbus
  • Install libraries: (python2)
sudo apt-get update
sudo apt-get install python-pip
sudo apt-get install python-pil
sudo apt-get install python-numpy
sudo pip install smbus
  • Install gpiozero libraries: (The system is installed by default, if not please follow the command below to install it.)
sudo apt-get update
# python3
sudo apt install python3-gpiozero
# python2
sudo apt install python-gpiozero

Download and Test Demo

sudo apt-get install p7zip-full
wget https://files.waveshare.com/upload/b/b3/AS7341_Spectral_Color_Sensor_code.7z
7z x AS7341_Spectral_Color_Sensor_code.7z -r -o./AS7341_Spectral_Color_Sensor_code
sudo chmod 777 -R  AS7341_Spectral_Color_Sensor_code
cd
cd AS7341_Spectral_Color_Sensor_code/AS7341_Spectral_Color_Sensor_code/RaspberryPi/python/examples

Execute:

 # python3
 sudo python3 data.py
 # python2
 sudo python data.py

to validate the test demo.

  • 【Note】The "data" here can be changed to flicker, syns, int, pinint, clear to verify different test demos and its meaning is explained in the code.

data corresponds to the Arduino's AS7341_Getdata demo
flicker corresponds to Arduino's AS7341_Getflicker demo
syns corresponds to Arduino's AS7341_Syns demo
int corresponds to Arduino's AS7341_INT demo
pinint corresponds to Arduino's AS7341_pinINT demo
clear corresponds to the AS7341_Clear demo of Arduino

  • Take the execution of data.py as an example, the test result is:
AS7341 Raspberry com python.png

Demo description

The functions of all test demos and the points that need attention have been introduced in the Arduino tutorial. When executing sudo ./main syns or python syns.py, you need to pull up the GPIO port and then pull it down to generate a falling edge signal. You can connect the GPIO pin to the high-level pin for a short time and then released to generate a falling edge signal.

Resources

Document

Demo

Datasheet

Software

FAQ

 Answer:
Please check whether the hardware connection is OK, especially if the line sequence of SDA and SCL should not be reversed, and reconnect the sensor after powering off and re-run the program.

{{{5}}}


Support



Technical Support

If you need technical support or have any feedback/review, please click the Submit Now button to submit a ticket, Our support team will check and reply to you within 1 to 2 working days. Please be patient as we make every effort to help you to resolve the issue.
Working Time: 9 AM - 6 PM GMT+8 (Monday to Friday)