313 views|0 replies

4

Posts

1

Resources
The OP
 

【Follow me Season 2 Episode 1】 Common tasks: light sensor and temperature sensor [Copy link]

 

The basic tasks of this evaluation mainly revolve around the use of several sensors to monitor ambient temperature and light.

1. Basic Task 2 (Required): Monitor ambient temperature and light, and display comfort level through onboard LEDs

This task mainly involves the use of temperature and light sensors.

The first is the light sensor. For usage, please refer to the tutorial Light module [ Light | CircuitPython Made Easy on Circuit Playground Express and Bluefruit | Adafruit Learning System ]. You can use cp.light to read the values collected by the light sensor.

In addition, the tutorial further provides an example of a function that quantifies light intensity and converts it into the number of LED lights, as follows:

def scale_range(value):
	return round(value /320*9)

This function converts the sensor reading into an integer between 0 and 9, representing the number of lights on. Although intuitive, we will explore more optimized ways to write it in later tasks.

At the same time, the tutorial also mentioned that the data collected by the light sensor can be sent to the computer through the serial port, which is very important for determining the threshold for lighting in the later tasks. This operation is also very simple and can be achieved through print(cp.light) .

Next, let's look at the temperature sensor. The method used is the same as the light sensor. You can refer to the Temperature section of the tutorial [ Temperature | CircuitPython Made Easy on Circuit Playground Express and Bluefruit | Adafruit Learning System ]. Through cp.temperature , we get the temperature reading in Celsius. If it is Fahrenheit, you need to convert it yourself.

Likewise, to quantify the temperature readings, the tutorial recommends another method, as follows:

def scale_range(value):
	return int((value - minimum_temp) / (maximum_temp - minimum_temp) *10)

This method is not only intuitive, but also has higher versatility and reusability, making it easy to migrate to other sensor applications.

Back to the actual task, we first use the following code to print the current light intensity and temperature as the standard for the comfortable temperature we set.

import time
from adafruit_circuitplayground import cp

while True:
	print(cp.light)
  print(cp.temperature)
  time.sleep(1)

Open the serial port, the output is as follows:

It can be seen that the light intensity is about 160 and the temperature is about 26℃. Based on this, we set the upper limit of light to 200 and the lower limit to 100 ; the upper limit of temperature to 30℃ and the lower limit to 24℃ . In order to unify the quantitative representation of light and temperature, we optimized the quantization function as follows:

def scale_range(value, minimum_value, maximum_value):
	return min(int((value - minimum_value) / (maximum_value - minimum_value) * 5), 5)

The two calculation formulas are merged, and 5 LEDs on each side are used to quantify the light and temperature respectively. At the same time, considering that the actual collected value may exceed the threshold we set, to prevent the lights on both sides from interfering with each other, this function limits the maximum number of lights on to 5, ensuring the consistency and intuitiveness of the LED display.

Since the LED lights on the board are numbered counterclockwise, we use 0-4 to represent light, 5-9 to represent temperature, and the light starts from 0 and lights up in sequence to indicate that the light is gradually increasing; symmetrically, the temperature side needs to light up in sequence starting from 9 to indicate that the temperature is increasing. The actual order of lighting is opposite to the numbering order.

In order to facilitate the order of lighting each time, the LED status update is implemented by writing a special function. This function not only takes into account the numbering order of the LED lights, but also distinguishes different environmental states through color coding. The light part is all white light; the temperature part, the green light is on when the temperature is suitable, the blue light is on when it is lower than the suitable temperature, and the red light is on when it is higher than the suitable temperature, to show the difference. The function to update the LED status is as follows:

def update_leds(peak_light, peak_temp):
  for i in range(10):
    pixels[i] = (0, 0, 0)
  for i in range(peak_light):
    pixels[i] = (255, 255, 255)
  if peak_temp < 3:
    color = (0, 0, 255)
  elif peak_temp == 3:
    color = (0, 255, 0)
  else:
    color = (255, 0, 0)
  for i in range(10 - peak_temp, 10):
    pixels[i] = color
  return pixels

Finally, call the function that updates the LED status in the main loop to light up all the lights according to the updated status. The complete code is as follows:

import time
from adafruit_circuitplayground import cp

cp.pixels.auto_write = False
cp.pixels.brightness = 0.1
minimum_light = 0
maximum_light = 200
minimum_temp = 25
maximum_temp = 32
pixels = [0] * 10

while True:
  print((cp.light,cp.temperature))
  peak_light = scale_range(cp.light, minimum_light, maximum_light)
  peak_temp = scale_range(cp.temperature, minimum_temp, maximum_temp)
  pixels = update_leds(peak_light, peak_temp)
  for i in range(10):
    cp.pixels[i] = pixels[i]

	cp.pixels.show()
    time.sleep(0.05)

We can see that the temperature indicator on the left and the light indicator on the right are lit. The video also demonstrates how the number of indicator lights changes when you block the light with your fingers or touch the temperature sensor to change the light intensity and temperature.

See the first half of the video for a video demonstration.


2. Basic Task 3 (Required): Proximity Detection - Set a safe distance and display it through the onboard LED. When an intrusion is detected, an audible alarm is triggered

This task aims to set a safe distance and use the onboard LED to display the approach status, and issue a sound alarm when an intrusion is detected. Although infrared sensors are widely recommended for their measurement accuracy, I have searched through the tutorials and could not find where to call the infrared sensor module. Although I saw that there are two IR ports, TX and RX, on the board, the routines seem to use them for inter-board communication. After referring to other friends' design reports, I decided to use a light sensor.

Our design idea is to simulate ambient light by illuminating the development board with a fixed light source and lighting up all the LED lights on the board. When an object approaches, it will block the external light source, causing the light sensor to only receive light from the onboard LED, resulting in a decrease in readings. Conversely, when an object is very close, the reflection of its surface will enhance the sensor reading. Based on this principle, we set it to trigger the buzzer to sound an alarm when the sensor reading increases abnormally.

The buzzer module reference tutorial Play Tone [ Play Tone | CircuitPython Made Easy on Circuit Playground Express and Bluefruit | Adafruit Learning System ] and Play File [ Play File | CircuitPython Made Easy on Circuit Playground Express and Bluefruit | Adafruit Learning System ] sections show that the buzzer can effectively make a warning sound, whether by playing a tone directly or playing an audio file.

In order to better understand the impact of different distances on the light sensor readings, we wrote the following code to record and plot the change curve of light intensity:

import time
from adafruit_circuitplayground import cp

while True:
  print((cp.light,))
    time.sleep(0.1)

You can see the following graphs corresponding to normal light intensity, light intensity with occlusion, and light intensity when the occluder is very close.

In the case of no occlusion and normal occlusion, the sampling value of the light sensor is relatively stable, about 120 and about 5 respectively . However, when the object is very close, although the sampling value of the light sensor increases, the maximum value can exceed 250. We set 250 as the safe distance, but due to the movement of the object or unevenness, there are relatively large fluctuations, which may cause some misjudgments.

We set the safety distance threshold to 250 based on experimental data. When there is no obstruction, the LED displays green; when the object is obstructed but far away, the LED displays blue; and when the object is very close, the LED displays red and triggers the buzzer alarm. The following is the specific code implementation:

import time
from adafruit_circuitplayground import cp

while True:
    if cp.light < 140:
        for i in range(10):
            cp.pixels[i] = (0, 0, 255)
        cp.pixels.show()
    elif cp.light > 250:
        for i in range(10):
            cp.pixels[i] = (255, 0, 0)
        cp.pixels.show()
        cp.play_file("dip.wav")
    else:
        for i in range(10):
            cp.pixels[i] = (0, 255, 0)
        cp.pixels.show()
    time.sleep(0.01)

The experimental results show that when an object blocks the light source but is far away, the onboard LED turns blue; when the object is very close to the board, the LED turns red and the buzzer sounds an alarm. As shown in the figure below:

Of course, the cardboard we use is reflective, so when we are very close to the board, it can actually enhance the reading of the light sensor. However, for non-reflective materials, this monitoring method may no longer be applicable. In addition, since the object must pass through the intermediate state of normal light between blocking and approaching the board, it may cause misjudgment at certain moderate distances.

Although proximity detection based on light sensors has certain limitations, it provides us with a low-cost and easy-to-implement solution. In the future, we plan to further study how to effectively utilize infrared modules to improve the accuracy and reliability of proximity detection.

See the second half of the video for a video demonstration.

温度光照传感器+距离监测

This post is from DigiKey Technology Zone
 
 

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