According to StarFive, developers have successfully used photosensitive sensors on VisionFive/VisionFive 2, and the specific instructions are as follows:
1. Preparation
- Development board: VisionFive/VisionFive 2
- Light sensor: Photosensitive sensor with DO output
- Analog to digital conversion module: ADS1115
- DuPont Line: Many
2. Principle of photosensitive sensors
The specific photosensitive sensor modules used in this demo are as follows:
On this photosensitive sensor, a digital output interface (DO) and an analog output interface (AO) are provided.
Because the VisionFive 2 SBC itself does not have an ADC module, this demo specifically uses an ADC module to help the board read the analog output information of the sensor, thereby obtaining the value of the fire illumination intensity.
At the top of the photosensitive sensor, there is a photoresistor. Due to the photoelectric effect, the stronger the light is, the lower the resistance value will be. As the light intensity increases, the resistance value will rapidly decrease. As the resistance value changes, the voltage signal of AO will also change. By using the ADC module, the voltage signal can be converted into a digital signal.
The ADC module used in this demo is ADS1115, as follows:
ADS1115 is a 16-bit precision ADC signal conversion module that uses the IIC interface to communicate with the board. It can simultaneously measure 4 sets of voltage signals for conversion, and can work between 2 V and 5.5 V. It can be used in conjunction with various development boards and microcontrollers, and can be used in many applications such as automobiles, televisions, and sensors.
3. Use of photosensitive sensors
Through the principle of the photosensitive sensor mentioned above, we can know that to obtain the specific light intensity value measured, analog-to-digital conversion is required through ADS1115.
Firstly, connect the AO interface of the sensor module to the AO interface of ADS1115, so that ADS1115 can obtain the analog voltage signal output by the sensor.
Then, refer to the following figure and connect ADS1115 to the VisionFive:
Connection
Note:
The power supply voltage should be determined based on the actual sensor used. The photosensitive sensor module and ADS1115 are both uses a 5 V power supply voltage.
Then, write the following program:
# -*- coding: utf-8 -*-
# file: ~/projects/light/light_ao.py
import ADS1115
import time
import numpy
# Initialize ADS 1115 module
ads = ADS1115.ADS1115()
while True:
# Read the data of channel 0 of the module (AO)
volt = ads.readADCSingleEnded(0)
# Convert
per = numpy.interp(volt, [0, 5000], [0, 100])
# Output info
print("%d mV of A0, val is %f" % (volt, per))
# Delay
time.sleep(0.1)
The logic of the above program detects the AO output of the sensor module through ADS1115, and then converts it to the corresponding light intensity value.
To run the above programs, it is also necessary to install the corresponding third-party support libraries numpy and ADS1115. However, the ADS1115 extension library uses i2c-1 by default, and our board uses i2c-0. Therefore, after installing the extension library, it needs to be processed to use it, as follows:
# Install extension libraries
pip install smbus
pip install numpy
pip install ADS1115
# Revise ADS1115 library files
vi ~/.local/lib/python3.10/site-packages/ADS1115/__init__.py
# Revise smbus2.SMBus(1) to smbus2.SMBus(0),save and exit
Run light_ao.py, and then try different lighting conditions
python3 light_ao.py
The actual operation results are as follows:
The results of testing in a bright environment are as follows:
Wrap the photosensitive part of the light sensor with opaque material:
Use a flashlight to illuminate the photosensitive part of the light sensor:
4. Summary
In this demo, we learned how to read AO signals of photosensitive sensors.
Obtaining current ambient light intensity data through photosensitive sensors has been used in many situations. Nowadays, there are many desk lamps that can automatically adjust their lighting according to the current ambient light intensity, which used light sensors.
Author: @HonestQiao