226浏览
楼主: 驴友花雕

[项目] 【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 昨天 11:18

【花雕学编程】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 值,并在串口打印数据。





回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:19

【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值

实验串口返回情况

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:22

【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值

实验串口绘图器返回情况

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图2

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:35

【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图1

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图2

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图3
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:44

【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值

  【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百八十八:TSL2561数字I2C环境光传感器 GY-2561光照强度检测模块
  项目之五:TSL2561 传感器使用Adafruit类库的通用测试

实验开源代码

  1. /*
  2.   【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百八十八:TSL2561数字I2C环境光传感器 GY-2561光照强度检测模块
  4.   项目之五:TSL2561 传感器使用Adafruit类库的通用测试
  5. */
  6. #include <Wire.h>
  7. #include <Adafruit_Sensor.h>
  8. #include <Adafruit_TSL2561_U.h>
  9. /* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
  10.    which provides a common 'type' for sensor data and some helper functions.
  11.    
  12.    To use this driver you will also need to download the Adafruit_Sensor
  13.    library and include it in your libraries folder.
  14.    You should also assign a unique ID to this sensor for use with
  15.    the Adafruit Sensor API so that you can identify this particular
  16.    sensor in any data logs, etc.  To assign a unique ID, simply
  17.    provide an appropriate value in the constructor below (12345
  18.    is used by default in this example).
  19.    
  20.    Connections
  21.    ===========
  22.    Connect SCL to I2C SCL Clock
  23.    Connect SDA to I2C SDA Data
  24.    Connect VCC/VDD to 3.3V or 5V (depends on sensor's logic level, check the datasheet)
  25.    Connect GROUND to common ground
  26.    I2C Address
  27.    ===========
  28.    The address will be different depending on whether you leave
  29.    the ADDR pin floating (addr 0x39), or tie it to ground or vcc.
  30.    The default addess is 0x39, which assumes the ADDR pin is floating
  31.    (not connected to anything).  If you set the ADDR pin high
  32.    or low, use TSL2561_ADDR_HIGH (0x49) or TSL2561_ADDR_LOW
  33.    (0x29) respectively.
  34.    
  35.    History
  36.    =======
  37.    2013/JAN/31  - First version (KTOWN)
  38. */
  39.    
  40. Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
  41. /**************************************************************************/
  42. /*
  43.     Displays some basic information on this sensor from the unified
  44.     sensor API sensor_t type (see Adafruit_Sensor for more information)
  45. */
  46. /**************************************************************************/
  47. void displaySensorDetails(void)
  48. {
  49.   sensor_t sensor;
  50.   tsl.getSensor(&sensor);
  51.   Serial.println("------------------------------------");
  52.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  53.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  54.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  55.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
  56.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
  57.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
  58.   Serial.println("------------------------------------");
  59.   Serial.println("");
  60.   delay(500);
  61. }
  62. /**************************************************************************/
  63. /*
  64.     Configures the gain and integration time for the TSL2561
  65. */
  66. /**************************************************************************/
  67. void configureSensor(void)
  68. {
  69.   /* You can also manually set the gain or enable auto-gain support */
  70.   // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  71.   // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  72.   tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  73.   
  74.   /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  75.   tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  76.   // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  77.   // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */
  78.   /* Update these values depending on what you've set above! */  
  79.   Serial.println("------------------------------------");
  80.   Serial.print  ("Gain:         "); Serial.println("Auto");
  81.   Serial.print  ("Timing:       "); Serial.println("13 ms");
  82.   Serial.println("------------------------------------");
  83. }
  84. /**************************************************************************/
  85. /*
  86.     Arduino setup function (automatically called at startup)
  87. */
  88. /**************************************************************************/
  89. void setup(void)
  90. {
  91.   Serial.begin(9600);
  92.   Serial.println("Light Sensor Test"); Serial.println("");
  93.   
  94.   /* Initialise the sensor */
  95.   //use tsl.begin() to default to Wire,
  96.   //tsl.begin(&Wire2) directs api to use Wire2, etc.
  97.   if(!tsl.begin())
  98.   {
  99.     /* There was a problem detecting the TSL2561 ... check your connections */
  100.     Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
  101.     while(1);
  102.   }
  103.   
  104.   /* Display some basic information on this sensor */
  105.   displaySensorDetails();
  106.   
  107.   /* Setup the sensor gain and integration time */
  108.   configureSensor();
  109.   
  110.   /* We're ready to go! */
  111.   Serial.println("");
  112. }
  113. /**************************************************************************/
  114. /*
  115.     Arduino loop function, called once 'setup' is complete (your own code
  116.     should go here)
  117. */
  118. /**************************************************************************/
  119. void loop(void)
  120. {  
  121.   /* Get a new sensor event */
  122.   sensors_event_t event;
  123.   tsl.getEvent(&event);
  124.   /* Display the results (light is measured in lux) */
  125.   if (event.light)
  126.   {
  127.     Serial.print(event.light); Serial.println(" lux");
  128.   }
  129.   else
  130.   {
  131.     /* If event.light = 0 lux the sensor is probably saturated
  132.        and no reliable data could be generated! */
  133.     Serial.println("Sensor overload");
  134.   }
  135.   delay(250);
  136. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:48

【花雕学编程】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 值。





回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:50

【花雕学编程】Arduino动手做(188)--TSL2561读取全光谱值

实验串口返回情况

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 11:53

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器

实验串口绘图器返回情况

【花雕学编程】Arduino动手做(188)---TSL2561环境光传感器图1
回复

使用道具 举报

12
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail