Cảm biến ánh sáng quang trở MKE-S02 LDR light sensor: Difference between revisions

From MakerLab Wiki
Jump to navigation Jump to search
Line 65: Line 65:
==Chương trình mẫu==
==Chương trình mẫu==
{{kxncode
{{kxncode
|name=Arduino code : File>Examples>MakerLabVn>Wiki_Examples>Sensors>AnalogSensor>AnalogSensor_Basic
|name=Arduino code : File>Examples>MakerLabVN>Wiki_Examples>Sensors>AnalogSensor>AnalogSensor_Basic_Plotter
|code=
|code=
<syntaxhighlight>
<syntaxhighlight>
/*
/*
    How the code work:
*  MakerLab.vn Demo Read Analog Sensor
        - Read ADC value (0->1023) from pin A1 => Display on Serial Plotter with Baudrate speed 9600.
*
*/
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       = A1;               // Declare input ADC pin
// Use pin A1 for function Read Analog
int analogPin = A1;


void setup(){
void setup()
    Serial.begin(9600);
{
    delay(1000);
  Serial.begin(9600);
    gridName_Plotter();
 
//    Serial.println("MakerLab.vn Demo Read Analog Sensor");
  /* 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 (0->1023) from analogPin (A1)
{
    int sensorValue = analogRead(analogPin);
  // Read value Analog from Sensor, then convert to value Digital
 
  int sensorValue = analogRead(analogPin);
    // show base line in Serial Plotter
    gridLine_Plotter();
    // Display value on Serial Plotter
    Serial.println(sensorValue);
 
    delay(1); // Wait 1ms then repeat
 
}


/*
  /* Add each value to the corresponding graph line */
  5V  -> 1023
   Serial.print(0);           Serial.print(" ");
  3V3 -> 676
   Serial.print(676);         Serial.print(" ");
 
  Serial.print(sensorValue);  Serial.println();
*/
void gridLine_Plotter(){
   Serial.print(0);Serial.print(" ");
   Serial.print(676);Serial.print(" ");
}


void gridName_Plotter(){
   delay(1);
  Serial.print("[0.0V]");Serial.print(" ");
  Serial.print("[3.3V]");Serial.print(" ");
   Serial.println("[AnalogValue]");
}
}


</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:57, 8 July 2021

File:Mkl-s02 ldr light sensor.jpg
MKL-S02 LDR light sensor

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,....

thumb
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

File:Mkl-s02 ldr light sensor back.jpg
MKL-S02 LDR light sensor back
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:

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.

File:LDR light sensor ket noi MakerEDU Shield.jpg
LDR light sensor kết nối MakerEDU Shield


thumb
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

Template:Kxncode

Kết quả

Template:KxndoneFile:Demo AnalogRead SerialPlotter 9600 LDR.JPG