Arduino - 光敏传感器

news2024/11/23 15:48:14

Arduino - Light Sensor

Arduino - 光传感器

In this tutorial, we are going to learn:
在本教程中,我们将学习:

  • How light sensor works 光传感器的工作原理
  • How to connect the light sensor to Arduino
    如何将光传感器连接到Arduino
  • How to program Arduino to read the state from the light sensor
    如何对Arduino进行编程以从光传感器读取状态

If you are looking for a light sensor in the module form, please check out this Arduino - LDR Light Sensor Module tutorial.
如果您正在寻找模块形式的光传感器,请查看此 Arduino - LDR 光传感器模块教程。

Hardware Required 所需硬件

1×Arduino UNO or Genuino UNO Arduino UNO 或 Genuino UNO
1×USB 2.0 cable type A/B USB 2.0 电缆 A/B 型
1×Light Sensor 光传感器
1×10 kΩ resistor 10 kΩ 电阻

About Light Sensor 关于光传感器

The light sensor used in this tutorial is a photoresistor, which is also called light-dependent resistor or photocell.
本教程中使用的光传感器是光敏电阻,也称为光敏电阻或光电管。

It is used not only to detect light but also to measure the brightness/illuminance level of the ambient light.
它不仅用于检测光,还用于测量环境光的亮度/照度水平。

Pinout 引脚排列

A photoresistor has two pins. Since it is a kind of resistor, we do NOT need to distinguish these pins. They are symmetric.
光敏电阻有两个引脚。由于它是一种电阻器,因此我们不需要区分这些引脚。它们是对称的。

Light Sensor Pinout

How It Works 它是如何工作的

The more light the photoresistor’s face is exposed, the smaller its resistance is. Therefore, by measuring the photoresistor’s resistance, we can know how bright the ambient light is.
光敏电阻表面暴露的光线越多,其电阻就越小。因此,通过测量光敏电阻的电阻,我们可以知道环境光的亮度。

How Light Sensor Works

WARNING

The light sensor value only reflects the approximated trend of the intensity of light, it does NOT represent the exact luminous flux. Therefore, it should be used only in an application that does NOT require high accuracy.
光传感器值仅反映光强度的近似趋势,并不代表确切的光通量。因此,它应该只用于不需要高精度的应用。

Arduino - Light Sensor Arduino - 光传感器

Arduino Uno’s pin A0 to A5 can work as the analog input. The analog input pin converts the voltage (between 0v and VCC) into integer values (between 0 and 1023), called ADC value or analog value.
Arduino Uno 的引脚 A0 到 A5 可以用作模拟输入。模拟输入引脚将电压(0v和VCC之间)转换为整数值(0到1023之间),称为ADC值或模拟值。

By connecting a pin of the photoresistor to an analog input pin, we can read the analog value from the pin by using analogRead() function, and then we can know the light levels relatively.
通过将光敏电阻的引脚连接到模拟输入引脚,我们可以使用 analogRead() 函数从引脚读取模拟值,然后我们可以相对地知道光照水平。

Wiring Diagram

Arduino Light Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

The below code reads the value from photocell and determine the light level qualitatively

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor
 */

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // reads the input on analog pin A0 (value between 0 and 1023)
  int analogValue = analogRead(A0);

  Serial.print("Analog reading: ");
  Serial.print(analogValue);   // the raw analog reading

  // We'll have a few threshholds, qualitatively determined
  if (analogValue < 10) {
    Serial.println(" - Dark");
  } else if (analogValue < 200) {
    Serial.println(" - Dim");
  } else if (analogValue < 500) {
    Serial.println(" - Light");
  } else if (analogValue < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }

  delay(500);
}

Light Sensor and LED

  • The below code turns ON the LED when it is dark, otherwise turns OFF the LED
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor
 */

// constants won't change
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int LED_PIN          = 3;  // Arduino pin connected to LED's pin
const int ANALOG_THRESHOLD = 500;

// variables will change:
int analogValue;

void setup() {
  pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

  if(analogValue < ANALOG_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED
}

  • Wring diagram for the above code:

Arduino Light Sensor LED Wiring Diagram

This image is created using Fritzing. Click to enlarge image

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor
 */

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // reads the input on analog pin A0 (value between 0 and 1023)
  int analogValue = analogRead(A0);

  Serial.print("Analog reading: ");
  Serial.print(analogValue);   // the raw analog reading

  // We'll have a few threshholds, qualitatively determined
  if (analogValue < 10) {
    Serial.println(" - Dark");
  } else if (analogValue < 200) {
    Serial.println(" - Dim");
  } else if (analogValue < 500) {
    Serial.println(" - Light");
  } else if (analogValue < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }

  delay(500);
}

Arduino - LDR模块

Arduino - LDR 模块

The LDR light sensor module is capable of detecting and measuring light in the surrounding environment. The module provides two outputs: a digital output (LOW/HIGH) and an analog output.
LDR光传感器模块能够检测和测量周围环境中的光。该模块提供两个输出:一个数字输出(LOW/HIGH)和一个模拟输出。

Arduino Uno R4 WiFi Led Matrix
Arduino Uno R4 WiFi LED 矩阵

In this tutorial, we will learn how to use an Arduino and an LDR light sensor module to detect and measure the light level. Specifically, we will cover the following:
在本教程中,我们将学习如何使用 Arduino 和 LDR 光传感器模块来检测和测量光照水平。具体而言,我们将涵盖以下内容:

  • How to connect the LDR light sensor module to an Arduino.
    如何将 LDR 光传感器模块连接到 Arduino。
  • How to program the Arduino to detect light by reading the digital signal from the LDR light sensor module.
    如何对Arduino进行编程,通过读取来自LDR光传感器模块的数字信号来检测光。
  • How to program the Arduino to measure the light level by reading the analog signal from the LDR light sensor module.
    如何对Arduino进行编程,通过读取来自LDR光传感器模块的模拟信号来测量光照水平。

LDR Light Sensor Module

Afterward, you can modify the code to activate an LED or a light bulb (via a relay) when it detects light.
之后,您可以修改代码以在检测到光时激活 LED 或灯泡(通过继电器)。

If you prefer a light sensor in its raw form, I suggest exploring the tutorial on the Arduino - Light Sensor.
如果您更喜欢原始形式的光传感器,我建议您浏览有关Arduino-光传感器的教程。

About LDR Light Sensor Module 关于LDR光传感器模块

The LDR light sensor module can be utilized to detect the presence of light or measure the light level in the surrounding environment. It provides two options via a digital output pin and analog output pin.
LDR光传感器模块可用于检测光的存在或测量周围环境中的光水平。它通过数字输出引脚和模拟输出引脚提供两种选择。

Pinout 引脚排列

The LDR light sensor module includes four pins:
LDR 光传感器模块包括四个引脚:

  • VCC pin: It needs to be connected to VCC (3.3V to 5V).
    VCC引脚:需要连接到VCC(3.3V至5V)。
  • GND pin: It needs to be connected to GND (0V).
    GND引脚:需要接GND(0V)。
  • DO pin: It is a digital output pin. It is HIGH if it is dark and LOW if it is light. The threshold value between darkness and lightness can be adjusted using a built-in potentiometer.
    DO引脚:它是数字输出引脚。如果它是黑暗的,它是高的,如果它是光明的,它是低的。可以使用内置电位器调整暗度和亮度之间的阈值。
  • AO pin: It is an analog output pin. The output value decreases as the light gets brighter, and it increases as the light gets darker.
    AO引脚:它是一个模拟输出引脚。输出值随着光线变亮而降低,随着光线变暗而增加。

LDR Light Sensor Module Pinout

Furthermore, it has two LED indicators:
此外,它还有两个 LED 指示灯:

  • One PWR-LED indicator for power.
    一个 PWR-LED 电源指示灯。
  • One DO-LED indicator for the light state on the DO pin: it is on when light is present and off when it is dark.
    一个 DO-LED 指示灯用于指示 DO 引脚上的亮状态:有光时亮起,天暗时熄灭。

How It Works 它是如何工作的

For the DO pin:
对于 DO 引脚:

  • The module has a built-in potentiometer for setting the light threshold (sensitivity).
    该模块具有一个内置电位器,用于设置光阈值(灵敏度)。
  • When the light intensity in the surrounding environment is above the threshold value (light), the output pin of the sensor is LOW, and the DO-LED is on.
    当周围环境中的光强度高于阈值(光)时,传感器的输出引脚为低电平,DO-LED亮起。
  • When the light intensity in the surrounding environment is below the threshold value (dark), the output pin of the sensor is HIGH, and the DO-LED is off.
    当周围环境中的光强度低于阈值(暗)时,传感器的输出引脚为高电平,DO-LED 熄灭。

For the AO pin:
对于 AO 引脚:

  • The higher the light intensity in the surrounding environment (light), the lower the value read from the AO pin.
    周围环境(光)中的光强度越高,从AO引脚读取的值就越低。
  • The lower the light intensity in the surrounding environment (dark), the higher the value read from the AO pin.
    周围环境(黑暗)中的光强度越低,从AO引脚读取的值就越高。

Note that the potentiometer does not affect the value on the AO pin.
请注意,电位器不会影响 AO 引脚上的值。

Wiring Diagram 接线图

Since the light sensor module has two outputs, you can choose to use one or both of them, depending on what you need.
由于光传感器模块有两个输出,您可以根据需要选择使用其中一个或两个输出。

  • The wiring diagram between Arduino and the LDR light sensor module when using DO only.
    仅使用 DO 时 Arduino 和 LDR 光传感器模块之间的接线图。

在这里插入图片描述

This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片

  • The wiring diagram between Arduino and the LDR light sensor module when using AO only.
    仅使用 AO 时 Arduino 和 LDR 光传感器模块之间的接线图。

Arduino LDR Module wiring diagram

  • The wiring diagram between Arduino and the LDR light sensor module when using both AO an DO.
    Arduino和LDR光传感器模块使用AO和DO时的接线图。

在这里插入图片描述

The real wiring: 真正的接线:

Arduino LDR Light Sensor Module connection

Arduino Code - Read value from DO pin Arduino 代码 - 从 DO 引脚读取值

/*

 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ldr-module
   */

#define DO_PIN 2  // Arduino's pin connected to DO pin of the ldr module

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  // initialize the Arduino's pin as an input
  pinMode(DO_PIN, INPUT);
}

void loop() {
  int lightState = digitalRead(DO_PIN);

  if (lightState == HIGH)
    Serial.println("The light is NOT present");
  else
    Serial.println("The light is present");
}
Quick Steps 快速步骤
  • Copy the above code and open with Arduino IDE
    复制上面的代码并使用Arduino IDE打开
  • Click Upload button on Arduino IDE to upload code to Arduino
    单击Arduino IDE上的“上传”按钮,将代码上传到Arduino
  • Cover and uncover the LDR light sensor module by your hand or something
    用手或其他东西盖住并揭开 LDR 光传感器模块
  • See the result on Serial Monitor.
    在串行监视器上查看结果。

Please keep in mind that if you notice the LED status remaining on constantly or off even when there is light, you can adjust the potentiometer to fine-tune the light sensitivity of the sensor.
请记住,如果您发现 LED 状态即使在有光的情况下也保持亮起或熄灭,您可以调整电位器以微调传感器的感光度。

Now we can customize the code to activate an LED or a light when the light is detected, or even make a servo motor rotate. You can find more information and step-by-step instructions in the tutorials provided at the end of this tutorial.
现在我们可以自定义代码以在检测到光时激活 LED 或灯,甚至可以使伺服电机旋转。您可以在本教程末尾提供的教程中找到更多信息和分步说明。

Arduino Code - Read value from AO pin Arduino 代码 - 从 AO 引脚读取值

/*

 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ldr-module
   */

#define AO_PIN A0  // Arduino's pin connected to AO pin of the ldr module

void setup() {
  // initialize serial communication
  Serial.begin(9600);
}

void loop() {
  int lightValue = analogRead(AO_PIN);

  Serial.println(lightValue);
}
Quick Steps 快速步骤
  • Copy the above code and open with Arduino IDE
    复制上面的代码并使用Arduino IDE打开
  • Click Upload button on Arduino IDE to upload code to Arduino
    单击Arduino IDE上的“上传”按钮,将代码上传到Arduino
  • Cover and uncover the LDR light sensor module by your hand or something
    用手或其他东西盖住并揭开 LDR 光传感器模块
  • See the result on Serial Monitor.
    在串行监视器上查看结果。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1870545.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C#udpClient组播

一、0udpClient 控件&#xff1a; button&#xff08;打开&#xff0c;关闭&#xff0c;发送&#xff09;&#xff0c;textbox&#xff0c;richTextBox 打开UDP&#xff1a; UdpClient udp: namespace _01udpClient {public partial class Form1 : Form{public Form1(){Initi…

如何在Windows 11上设置默认麦克风和相机?这里有详细步骤

如果你的Windows 11计算机上连接了多个麦克风或网络摄像头&#xff0c;并且希望自动使用特定设备&#xff0c;而不必每次都在设置中乱动&#xff0c;则必须将首选设备设置为默认设备。我们将向你展示如何做到这一点。 如何在Windows 11上更改默认麦克风 有两种方法可以将麦克…

[游戏开发][UE5]引擎使用学习记录

C Log和蓝图Log C Log 方法 UE_Log(参数1&#xff0c;参数2&#xff0c;参数3) //举例: UE_LOG(LogTemp, Error, TEXT("Log Info: %s"),"Test Log"); 三个参数的作用 参数1&#xff1a;输出窗口归类使用&#xff0c;你写什么它就显示什么 参数2&#x…

网络安全入门必选:十款免费的抓包工具有哪些?

下面给大家推荐几款好用的免费的抓包工具软件&#xff0c;有需要的小伙伴们来了解一下。 1. Wireshark抓包分析工具 4.0.1 Wireshark是一款功能强大的网络协议分析器&#xff0c;可以实时检测和抓取网络通讯数据。它支持多种协议和媒体类型&#xff0c;并具备丰富的显示过滤…

从0-1搭建一个web项目(package.json)详解

本章分析package.json文件详解 本文主要对packge.json配置子文件详解 ObJack-Admin一款基于 Vue3.3、TypeScript、Vite3、Pinia、Element-Plus 开源的后台管理框架。在一定程度上节省您的开发效率。另外本项目还封装了一些常用组件、hooks、指令、动态路由、按钮级别权限控制等…

干货:ANR日志分析全面解析

ANR类型 出现ANR的一般有以下几种类型&#xff1a; 1:KeyDispatchTimeout&#xff08;常见&#xff09; input事件在5S内没有处理完成发生了ANR。 logcat日志关键字&#xff1a;Input event dispatching timed out 2:BroadcastTimeout 前台Broadcast&#xff1a;onReceiver在…

MYSQL 四、mysql进阶 5(InnoDB数据存储结构)

一、数据库的存储结构&#xff1a;页 索引结构给我们提供了高效的索引方式&#xff0c;不过索引信息以及数据记录都是保存在文件上的&#xff0c;确切说时存储在页结构中&#xff0c;另一方面&#xff0c;索引是在存储引擎中实现的&#xff0c;Mysql服务器上的存储引擎负责对表…

eNSP中VRRP的配置和使用

一、基础配置 1.新建拓扑图 2.配置vlan a.CORE-S1 <Huawei>system-view [Huawei]sysname CORE-S1 [CORE-S1]vlan 10 [CORE-S1-vlan10]vlan 20 [CORE-S1-vlan20]vlan 30 b.CORE-S2 <Huawei>system-view [Huawei]sysname CORE-S2 [CORE-S2]vlan 10 [CORE…

君諾外匯:为什么巴菲特现在加倍下注油气股票?油价上涨是主因吗?

近年来&#xff0c;以巴菲特为代表的一些顶级投资者开始在能源领域加大投资力度&#xff0c;特别是油气股票。这一转变引发了广泛关注&#xff0c;特别是在油价上涨的背景下。本文将Juno markets外匯深入分析巴菲特投资策略的变化原因&#xff0c;探讨其在能源市场的布局及未来…

Linux OpenGrok搭建

文章目录 一、目的二、环境三、相关概念3.1 OpenGrok3.2 CTags3.3 Tomcat 四、OpenGrok搭建4.1 安装jdk4.2 安装ctags依赖4.3 安装universal-ctags4.3.1 下载universal-ctags4.3.2 编译&&安装universal-ctags 4.4 安装Tomcat4.4.1 下载&&解压Tomcat4.4.2 启动T…

IDEA无法输入中文,怎么破

1.导航栏处&#xff0c;点击help菜单&#xff0c;选择Edit Custom VM Options.. 2.编辑文件&#xff0c;在文件末尾添加&#xff1a; -Drecreate.x11.input.methodtrue 3.保存文件即可&#xff0c;如果还是不行&#xff0c;就关闭所有Idea程序&#xff0c;重新启动Idea

Linux:进程和计划任务管理

目录 一、程序和进程 1.1、程序 1.2、进程 1.3、线程 1.4、协程 二、查看进程相关命令 2.1、ps命令&#xff08;查看静态的进程统计信息&#xff09; 第一行为列表标题&#xff0c;其中各字段的含义描述如下 2.2、top命令&#xff08;查看进程动态信息&#xff09; 2…

Session会话与请求域的区别

session会话和请求域&#xff08;也称为request域&#xff09;都是用于存储和管理用户特定信息的重要概念&#xff0c;但它们在作用范围和生命周期上有显著的不同。 请求域 (Request Domain) 作用范围&#xff1a;请求域是面向单次请求的。每次HTTP请求都会创建一个新的request…

Vue报错:Component name “xxx” should always be multi-word vue/multi-word-component

问题&#xff1a;搭建脚手架时报错&#xff0c;具体错误如下&#xff1a; ERROR in [eslint] E:\personalProject\VueProjects\vueproject2\src\components\Student.vue10:14 error Component name "Student" should always be multi-word vue/multi-word-compon…

找不到xinput1_3.dll怎么办?几种靠谱的修复xinput1_3.dll的方法

找不到xinput1_3.dll怎么办&#xff1f;如果你不知道&#xff0c;那么你就详细的看看本文吧&#xff0c;今天我们会给大家详细的讲解找不到xinput1_3.dll这个情况&#xff0c;以及分析xinput1_3.dll这个文件&#xff0c;只要我们熟悉这个文件&#xff0c;那么要搞定修复还是比较…

如何使用Hugging Face Transformers为情绪分析微调BERT?

情绪分析指用于判断文本中表达的情绪的自然语言处理(NLP)技术&#xff0c;它是客户反馈评估、社交媒体情绪跟踪和市场研究等现代应用背后的一项重要技术。情绪可以帮助企业及其他组织评估公众意见、提供改进的客户服务&#xff0c;并丰富产品或服务。 BERT的全称是来自Transfo…

【UE5.3】笔记5-蓝图类

什么是蓝图类&#xff1a;其实就是C类&#xff0c;只不过是UE封装好的且可以直接拖出来可视化使用。 如何创建蓝图类&#xff1f;蓝图类有哪些&#xff1f; 蓝图类分为基于关卡的&#xff0c;基于Actor的&#xff0c;基于组件Component的。 基于关卡的蓝图类 一个关卡只能有…

Jupyter Notebook 说明 和 安装教程【WIN MAC】

一、Jupyter Notebook 简介&#xff08;来源百度百科&#xff09; Jupyter Notebook&#xff08;此前被称为 Python notebook&#xff09;是一个交互式笔记本&#xff0c;支持运行40多种编程语言。 Jupyter Notebook 的本质是一个Web应用程序&#xff0c;便于创建和共享程序文…

综合管廊挂轨巡检机器人:安全高效管理的新力量

综合管廊、电力管廊等作为承载着各类电缆和管线的重要通道&#xff0c;管廊的安全和可靠性对城市的运行至关重要。传统人工巡检效率低、劳动强度大&#xff0c;且可能存在巡检不及时、不准确等问题。难以满足日益复杂和庞大的管廊系统的监控需求。为了解决这些问题&#xff0c;…

物理服务器会不会被DDOS攻击?

物理服务器同样可能遭受分布式拒绝服务&#xff08;DDoS&#xff09;攻击。DDoS攻击的目的是通过大量的请求淹没目标服务器或网络&#xff0c;使其无法处理合法用户的请求&#xff0c;从而导致服务不可用。这种攻击并不区分服务器是物理的还是虚拟的&#xff0c;只要服务器连接…