628 views|3 replies

251

Posts

4

Resources
The OP
 

[Digi-Key Follow me Issue 3] Task 6-3: Light On Reminder [Copy link]

 This post was last edited by HonestQiao on 2023-12-9 22:05

In this event, in addition to Seeed Studio XIAO ESP32C3 and expansion board, sensors also bought ATH20 temperature and humidity sensor and analog light sensor.

This post shares the specific use of this fiber optic sensor.

1. Hardware Connection

On the floor, there is a dedicated analog sensor interface A0:

Therefore, use the data cable that comes with the analog light sensor to connect:

2. Read the measured value of the light sensor

This light sensor is an analog light sensor. It cannot directly read the measured value. What is actually obtained is an analog value, which needs to undergo a certain conversion before it can be converted into an actual voltage value.

Refer to the official information of Seeed Studio. The code corresponding to this post is processed in the following way:

# 模拟量
sensor_dev = ADC(Pin(2))
sensor_dev.atten(ADC.ATTN_11DB)  # 这里配置测量量程为0~3.6V


while True:
    sensor_value = 0
    for i in range(0,16):
        sensor_value = sensor_value + sensor_dev.read()
    sensor_value = sensor_value / 16
    Vbattf = 2 * sensor_value / 1000.0 

In the above code, sensor_dev.read() is used continuously to obtain 16 values, then the average value is removed to obtain the sensor value, which is then converted into a voltage value.

Once the above data is obtained, it can be further used, such as detecting the light intensity and whether the lights need to be turned on.

threshold = 10
resistance = (float)(1023 - sensor_value) * 10 / sensor_value
if resistance > threshold:
    status = "光线弱,需要开灯"
else:
    status = "光线足够"

The above calculation is officially provided by Seeed Studio. You can test and verify the actual situation.

3. Read the value and display it on the screen

The values have been read before, and the next step is to display them on the screen. Combined with [Digi-Key Electronics Follow me Issue 3] Task 2: Drive the OLED screen on the expansion board (characters, Chinese, instruments, poems) , first generate the Chinese font file:

python micropython-font-to-py/font_to_py.py -x -f -c "光线弱,需要开灯光线足够" OPPOSans-M.ttf 16 test_font_cn_16_light.py

Then upload test_font_cn_16_light.py to the corresponding directory:

Write the complete code again:

mport utime
from machine import Pin, ADC, PWM

from color_setup import ssd
from color_setup import i2c

from gui.core.nanogui import refresh
from gui.core.writer import CWriter
from gui.widgets.textbox import Textbox
from gui.widgets.label import Label
from gui.core.colors import *

# 字体调用
import gui.fonts.arial10 as arial10
import gui.fonts.test_font_cn_16_light as font_cn_light

# Buzzer settings
buzzer_pin = Pin(5, Pin.OUT)
buzzer = PWM(buzzer_pin)
buzzer.freq(1047)
buzzer.duty(0)

# 清屏
refresh(ssd, True)

# 显示输出
CWriter.set_textpos(ssd, 0, 0)
wri = CWriter(ssd, arial10, verbose=False)
wri.set_clip(True, True, False)

wri2 = CWriter(ssd, font_cn_light, verbose=False)
wri2.set_clip(True, True, False)

# 显示日期
txt = Label(wri, 0, 0, "Environmental information")

pargs = (13, 2, 124, 2)  # Row, Col, Width, nlines
pargs2 = (36, 2, 124, 1)  # Row, Col, Width, nlines

tb = Textbox(wri, *pargs, clip=False)
tb2 = Textbox(wri2, *pargs2, clip=False)
 
 
# 模拟量
sensor_dev = ADC(Pin(2))
sensor_dev.atten(ADC.ATTN_11DB)  # 这里配置测量量程为0~3.6V

threshold = 10

# 循环检测
while True:
    sensor_value = 0
    for i in range(0,16):
        sensor_value = sensor_value + sensor_dev.read()
    sensor_value = sensor_value / 16
    Vbattf = 2 * sensor_value / 1000.0 

    resistance = (float)(1023 - sensor_value) * 10 / sensor_value

    print("\nVbattf: %0.2f V" % Vbattf)
    print("sensor_value: %d" % sensor_value)
    print("resistance: %0.2f" % resistance)
    
    tb.clear()
    tb.append("\n", ntrim = 100)
    tb.append(" sensor_value: %d" % sensor_value, ntrim = 100)
    tb.append(" resistance:      %0.2f" % resistance, ntrim = 100)
    
    tb2.clear()
    if resistance > threshold:
        status = "光线弱,需要开灯"
        buzzer.duty(50)
        utime.sleep(0.5)
        buzzer.duty(0)
        utime.sleep(0.5)
        buzzer.duty(50)
        utime.sleep(0.5)
        buzzer.duty(0)
    else:
        status = "光线足够"
    print(status)
    tb2.append(status, ntrim = 100)
    
    refresh(ssd)
        
    utime.sleep(1)

The above code first reads the value of the sensor, then displays it on the screen, and based on the conversion result, displays whether the light needs to be turned on. If the light needs to be turned on, it will also drive the buzzer to remind you.

Note that two CWriter instances are used in the code:

# 显示输出
CWriter.set_textpos(ssd, 0, 0)
wri = CWriter(ssd, arial10, verbose=False)
wri.set_clip(True, True, False)

wri2 = CWriter(ssd, font_cn_light, verbose=False)
wri2.set_clip(True, True, False)

A CWriter instance can only use one font, so the Chinese and English displays must be distinguished.

Of course, when generating Chinese fonts, it is also possible to generate the glyph information of the corresponding English characters to be displayed at the same time.

4. Run the test

After running the above code, the specific results are as follows:

1. When there is enough light:

2. When the light is insufficient: the buzzer will sound to remind you to turn on the light

Of course, you can also adjust the threshold value according to your actual needs.

V. Conclusion

Seeed Studio XIAO ESP32C3 expansion board is really very convenient. It comes with IIC, analog sensor, and UART interface, which makes it very convenient to connect to the Seeed Studio series of sensors.

Simulation data acquisition is a very common type of data acquisition in embedded development, and it is necessary to master it. In MicroPython, it is also very simple and convenient to obtain its raw data.

This post is from DigiKey Technology Zone

Latest reply

Well, radar sensors can do animal detection   Details Published on 2023-12-10 09:23
 
 

6027

Posts

6

Resources
2
 

You can just turn on the light directly, or add human body detection

This post is from DigiKey Technology Zone

Comments

To control the light, just connect an LED. To detect the human body, you only need a sensor.  Details Published on 2023-12-10 09:11
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

251

Posts

4

Resources
3
 
Qintianqintian0303 posted on 2023-12-10 08:51 Just turn on the light directly, or add human body detection

To control the light, just connect an LED.

Human body detection only requires one sensor.

This post is from DigiKey Technology Zone

Comments

Well, radar sensors can do animal detection  Details Published on 2023-12-10 09:23
 
 
 

6027

Posts

6

Resources
4
 
HonestQiao posted on 2023-12-10 09:11 To control the light, just connect an LED. Human body detection only requires a sensor.

Well, radar sensors can do animal detection

This post is from DigiKey Technology Zone
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list