AS7341 Spectral Color Sensor

From Waveshare Wiki
Revision as of 09:25, 21 April 2021 by Eng35 (talk | contribs) (Created page with "{{Infobox item| |name=AS7341 Spectral Color Sensor |img=File:AS7341_Spectral_Color_Sensor-1.jpeg|300px|link=https://www.waveshare.com/as7341-spectral-color-sensor.htm | AS73...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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}}}



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 BCM2835 libraries:
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz
tar zxvf bcm2835-1.60.tar.gz 
cd bcm2835-1.60/
sudo ./configure
sudo make
sudo make check
sudo make install
  • Install wiringPi libraries:
sudo apt-get install wiringpi
#For Pi4B, you need to upgrade it:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v

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

cd
cd AS7341_Spectral_Color_Sensor_code/AS7341_Spectral_Color_Sensor_code/RaspberryPi/python/examples

Enter the following command to execute the demo:

sudo python data.py
  • 【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.

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

Resources

FAQ

Support

Support

If you require technical support, please go to the Support page and open a ticket.