欢迎光临散文网 会员登陆 & 注册

ESP32 光敏传感器实验

2023-06-15 08:29 作者:南冥的鸟  | 我要投稿

1.1 项目介绍:                                                                  

光敏传感器它对环境光线最敏感,S输出一个模拟信号,一般用来检测周围环境的光线的亮度,触发单片机或继电器模块等。传感器自带2个定位孔,方便你将传感器固定在其他设备。

相关资料下载链接:https://sourl.cn/YxyUvG


1.3连接            

 


1.4测试代码 

Arduino IDE测试程序

//**********************************************************************************

/*  

 * Filename    : Photoresistance

 * Description : Read the basic usage of ADC,DAC and Voltage

 * Auther      : http//www.keyestudio.com

*/

#define PIN_ANALOG_IN  34  //the pin of the Photoresistance

void setup() {

  Serial.begin(9600);

}

//In loop(),the analogRead() function is used to obtain the ADC value,

//and then the map() function is used to convert the value into an 8-bit precision DAC value.

//The input and output voltage are calculated according to the previous formula,

//and the information is finally printed out.

void loop() {

  int adcVal = analogRead(PIN_ANALOG_IN);

  int dacVal = map(adcVal, 0, 4095, 0, 255);

  double voltage = adcVal / 4095.0 * 3.3;

  Serial.printf("ADC Val: %d, \t DAC Val: %d, \t Voltage: %.2fV\n", adcVal, dacVal, voltage);

  delay(200);

}

//**********************************************************************************

 

Thonny IDE测试程序

# Import Pin, ADC and DAC modules.

from machine import ADC,Pin,DAC

import time

 

# Turn on and configure the ADC with the range of 0-3.3V

adc=ADC(Pin(34))

adc.atten(ADC.ATTN_11DB)

adc.width(ADC.WIDTH_12BIT)

 

# Read ADC value once every 0.1seconds, convert ADC value to DAC value and output it,

# and print these data to “Shell”.

try:

    while True:

        adcVal=adc.read()

        dacVal=adcVal//16

        voltage = adcVal / 4095.0 * 3.3

        print("ADC Val:",adcVal,"DACVal:",dacVal,"Voltage:",voltage,"V")

        time.sleep(0.1)

except:

    pass

 

1.5测试结果                                           

按照实验接线图连接好线,编译并上传代码到ESP32,代码上传成功后,利用USB线上电后,打开串口监视器,设置波特率为9600,串口监视器显示出光敏传感器的ADC值,DAC值和电压值,光照越强,可以看到ADC值,DAC值和电压值变大。如下图。

 



 


ESP32 光敏传感器实验的评论 (共 条)

分享到微博请遵守国家法律