Cảm biến ánh sáng quang trở MKE-S02 LDR light sensor: Difference between revisions
Xuanngoc1992 (talk | contribs) |
Xuanngoc1992 (talk | contribs) |
||
Line 65: | Line 65: | ||
==Chương trình mẫu== | ==Chương trình mẫu== | ||
{{kxncode | {{kxncode | ||
|name=Arduino code : File>Examples> | |name=Arduino code : File>Examples>MakerLabVN>Wiki_Examples>Sensors>AnalogSensor>AnalogSensor_Basic_Plotter | ||
|code= | |code= | ||
<syntaxhighlight> | <syntaxhighlight> | ||
/* | /* | ||
* MakerLab.vn Demo Read Analog Sensor | |||
* | |||
*/ | * How the code work? | ||
* To understand code, we must know 2 thing about "Read Analog" and "Serial Plotter" | |||
* | |||
* Read Analog: | |||
* _ Arduino boards contain a multichannel, 10-bit analog to digital converter | |||
* _ This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023 | |||
* | | |||
* _ Therefore [5.0V] = 1023 | |||
* | [3.3V] ~ 676 | |||
* | [0.0V] = 0 | |||
* | | |||
* # To know more detail you can refer in here: https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/ | |||
* | |||
* Serial Plotter: | |||
* _ Serial Plotter is one of tools in Arduino IDE | |||
* _ It receives data from Arduino and visualizes data as waveforms | |||
* _ It can visualize not only single but also multiple sensor data in the same graph | |||
* | | |||
* # To know more detail you can refer in here: https://arduinogetstarted.com/tutorials/arduino-serial-plotter | |||
*/ | |||
int analogPin | // Use pin A1 for function Read Analog | ||
int analogPin = A1; | |||
void setup(){ | void setup() | ||
{ | |||
Serial.begin(9600); | |||
/* Set the names for the graph lines */ | |||
Serial.print("[0.0V]"); Serial.print(" "); | |||
Serial.print("[3.3V]"); Serial.print(" "); | |||
Serial.print("[AnalogValue]"); Serial.println(); | |||
} | } | ||
void loop(){ | void loop() | ||
{ | |||
// Read value Analog from Sensor, then convert to value Digital | |||
int sensorValue = analogRead(analogPin); | |||
/* | /* Add each value to the corresponding graph line */ | ||
Serial.print(0); Serial.print(" "); | |||
Serial.print(676); Serial.print(" "); | |||
Serial.print(sensorValue); Serial.println(); | |||
*/ | |||
Serial.print(0);Serial.print(" "); | |||
Serial.print(676);Serial.print(" "); | |||
delay(1); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 01:57, 8 July 2021
Giới thiệu
Cảm biến ánh sáng quang trở MKL-S02 LDR light sensor được sử dụng để đo cường độ ánh sáng bằng quang trở (LDR-Light Dependent Resistor), thích hợp với các ứng dụng: đo cường độ sáng môi trường, bật tắt đèn tự động,..., cảm biến trả ra giá trị điện áp Analog tuyến tính tương ứng với cường độ ánh sáng của môi trường giúp bạn có thể ghi nhận và xử lý thông tin một cách chính xác nhất, ngoài ra cảm biến còn được bổ sung các thiết kế ổn định, chống nhiễu.
Cảm biến ánh sáng quang trở MKL-S02 LDR light sensor được thiết kế để có thể sử dụng trực tiếp an toàn với các board mạch giao tiếp ở mức điện áp 3.3/5VDC: Arduino, Raspberry Pi, Jetson Nano, Micro:bit,....
Cảnh báo: Xin nạp (upload) chương trình trước khi kết nối cảm biến vào mạch Arduino để chắc chắn rằng các chân giao tiếp với cảm biến đã được cấu hình đúng! |
Thông số kỹ thuật
- Điện áp hoạt động: 5VDC
- Chuẩn giao tiếp: Analog
- Điện áp giao tiếp: 0~3.3VDC
- Đo cường độ ánh sáng bằng quang trở (LDR-Light Dependent Resistor)
- Kích thước: 27.6 x 35.5mm
Các chân tín hiệu
MKL-S02 | Ghi chú |
---|---|
GND | Chân cấp nguồn âm 0VDC |
5V | Chân cấp nguồn dương 5VDC |
SIG | Chân tín hiệu ngõ ra Analog 0~3.3VDC |
Kết nối phần cứng
Bước 1: Chuẩn bị phần cứng:
- 1 x Mạch Vietduino Uno (Arduino Uno Compatible)
- 1 x Mạch MakerEDU Shield
- 1 x Cảm biến ánh sáng quang trở MKL-S02 LDR light sensor
Bước 2: Cắm MakerEdu Shield vào mạch Vietduino Uno.
Bước 3: Kết nối cảm biến vào Port (A1) của MakerEDU Shield.
Bước 4: Kết nối Vietduino Uno với máy tính thông qua cáp USB.
Lưu ý: Nếu không có sẵn MakerEDU Shield, bạn có thể kết nối trực tiếp cảm biến với Arduino/Vietduino như bảng dưới đây. |
Arduino/Vietduino | Cảm biến ánh sáng quang trở MKL-S02 LDR light sensor |
---|---|
GND | GND |
5V | 5V |
SIG | A1 |
Chương trình mẫu
Kết quả
Template:KxndoneFile:Demo AnalogRead SerialPlotter 9600 LDR.JPG