【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
代码解读这段代码使用 TSL2561 光照传感器 读取光强数据,并通过 串口打印亮度值(Lux),核心逻辑如下:
1. 传感器连接
✅ 使用 I2C (Wire.h),连接 SCL(时钟)到模拟 5,SDA(数据)到模拟 4
✅ 传感器地址 → TSL2561_ADDR_FLOAT 默认浮动地址(0x39)
✅ 可修改地址 → TSL2561_ADDR_LOW(0x29) 或 TSL2561_ADDR_HIGH(0x49)
2. 初始化
✅ tsl.begin() → 检测传感器是否正确连接
✅ tsl.setGain(TSL2561_GAIN_16X); → 设置 16 倍增益,适用于低光环境
✅ tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); → 设定积分时间,短时间适用于强光环境
3. 读取光照数据
✅ tsl.getLuminosity(TSL2561_VISIBLE); → 获取可见光强度
✅ tsl.getLuminosity(TSL2561_INFRARED); → 获取红外线强度
✅ tsl.getLuminosity(TSL2561_FULLSPECTRUM); → 获取全光谱数据
4. 计算亮度
✅ getFullLuminosity(); → 获取 32 位光照数据(高 16 位红外光,低 16 位全光谱)
✅ calculateLux(full, ir); → 计算光照强度(Lux)
✅ Serial.println(lux); → 打印 Lux 值
5. 循环读取
✅ delay(100); → 每 100 毫秒更新数据
这段代码让 TSL2561 传感器不断读取光照强度,包括可见光、红外光和 Lux 值,并在串口打印数据。
【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
实验串口返回情况【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
实验串口绘图器返回情况【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)实验一百八十八:TSL2561数字I2C环境光传感器 GY-2561光照强度检测模块
项目之五:TSL2561 传感器使用Adafruit类库的通用测试
实验开源代码
/*
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百八十八:TSL2561数字I2C环境光传感器 GY-2561光照强度检测模块
项目之五:TSL2561 传感器使用Adafruit类库的通用测试
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
which provides a common 'type' for sensor data and some helper functions.
To use this driver you will also need to download the Adafruit_Sensor
library and include it in your libraries folder.
You should also assign a unique ID to this sensor for use with
the Adafruit Sensor API so that you can identify this particular
sensor in any data logs, etc.To assign a unique ID, simply
provide an appropriate value in the constructor below (12345
is used by default in this example).
Connections
===========
Connect SCL to I2C SCL Clock
Connect SDA to I2C SDA Data
Connect VCC/VDD to 3.3V or 5V (depends on sensor's logic level, check the datasheet)
Connect GROUND to common ground
I2C Address
===========
The address will be different depending on whether you leave
the ADDR pin floating (addr 0x39), or tie it to ground or vcc.
The default addess is 0x39, which assumes the ADDR pin is floating
(not connected to anything).If you set the ADDR pin high
or low, use TSL2561_ADDR_HIGH (0x49) or TSL2561_ADDR_LOW
(0x29) respectively.
History
=======
2013/JAN/31- First version (KTOWN)
*/
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
/**************************************************************************/
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print("Sensor: "); Serial.println(sensor.name);
Serial.print("Driver Ver: "); Serial.println(sensor.version);
Serial.print("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
Serial.print("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
/**************************************************************************/
/*
Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);/* medium resolution and speed */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);/* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
Serial.println("------------------------------------");
Serial.print("Gain: "); Serial.println("Auto");
Serial.print("Timing: "); Serial.println("13 ms");
Serial.println("------------------------------------");
}
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
Serial.println("Light Sensor Test"); Serial.println("");
/* Initialise the sensor */
//use tsl.begin() to default to Wire,
//tsl.begin(&Wire2) directs api to use Wire2, etc.
if(!tsl.begin())
{
/* There was a problem detecting the TSL2561 ... check your connections */
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
/* We're ready to go! */
Serial.println("");
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
if (event.light)
{
Serial.print(event.light); Serial.println(" lux");
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println("Sensor overload");
}
delay(250);
}
【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
代码解读这段代码使用 Adafruit TSL2561 传感器库 通过 I2C 读取光照强度,并打印出亮度数据(Lux),核心逻辑如下:
1. 初始化传感器
✅ tsl.begin(); → 检查传感器是否连接正常
✅ displaySensorDetails(); → 获取传感器信息(版本号、分辨率、最小/最大值)
✅ configureSensor(); → 设置增益、自动调整光照范围,确保低光环境检测精度
2. 传感器配置
✅ tsl.enableAutoRange(true); → 开启自动增益调整(在亮光与暗光之间自动切换)
✅ tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); → 设定积分时间,适合强光环境
✅ 可调整 积分时间 为 101ms 或 402ms 以提高精度
3. 读取光照数据
✅ sensors_event_t event; tsl.getEvent(&event); → 获取新的传感器数据
✅ event.light → 获取 Lux 光照值(如果为 0,可能是传感器过载)
✅ Serial.print(event.light); Serial.println(" lux"); → 打印光照值(单位 Lux)
4. 循环采集数据
✅ delay(250); → 每 250 毫秒更新一次光照数据
这段代码让 TSL2561 传感器不断测量环境光照强度,并在串口输出 Lux 值。
【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值
实验串口返回情况【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器
实验串口绘图器返回情况【花雕学编程】Arduino动手做(188)---TSL2561模块5个小实验
页:
1
[2]