Difference between revisions of "Sensor-Light-A1"
(→Basic Functionality) |
(→Programming) |
||
Line 65: | Line 65: | ||
<tab name="Arduino for Microduino" style="width:100%;"> | <tab name="Arduino for Microduino" style="width:100%;"> | ||
==Introduction== | ==Introduction== | ||
− | The | + | The Light Sensor is used as an analog input pin. Therefore, the '''pinMode''' and '''analogRead''' functions will be used. |
==Key Functions== | ==Key Functions== | ||
*Required Libraries: None | *Required Libraries: None | ||
Line 72: | Line 72: | ||
***'''pin_number''' - is the pin number that the sensor is connected to | ***'''pin_number''' - is the pin number that the sensor is connected to | ||
***'''pin_mode''' - is the mode to set the pin to. Either '''INPUT''' or '''OUTPUT''' | ***'''pin_mode''' - is the mode to set the pin to. Either '''INPUT''' or '''OUTPUT''' | ||
− | ** ''' | + | ** '''analogRead(pin_number)''' - Reads the value of the pin, between 0 and 1023 |
***'''pin_number''' - is the pin number that the sensor is connected to | ***'''pin_number''' - is the pin number that the sensor is connected to | ||
==Example== | ==Example== | ||
− | This is a simple example which outputs the state of the | + | This is a simple example which outputs the state of the Light Sensor to the serial port terminal. |
'''Note''': Important lines of code are highlighted. | '''Note''': Important lines of code are highlighted. | ||
− | <syntaxhighlight lang="cpp" highlight="1,2,10,11,17,18"> | + | <syntaxhighlight lang="cpp" line highlight="1,2,10,11,17,18"> |
//Define the pin the sensor is connected to | //Define the pin the sensor is connected to | ||
− | const int | + | //Note: This sensor uses analog read, which means only pins denoted with an 'A' prefix will work. |
+ | const int LIGHT_SENSOR_PIN = A0; | ||
void setup(){ | void setup(){ | ||
Line 91: | Line 92: | ||
//Configure the pin into input mode | //Configure the pin into input mode | ||
− | pinMode( | + | pinMode(LIGHT_SENSOR_PIN, INPUT); |
} | } | ||
Line 98: | Line 99: | ||
//Perform a digital read and store the value into pin_state variable | //Perform a digital read and store the value into pin_state variable | ||
− | int | + | int pin_value = analogRead(LIGHT_SENSOR_PIN); |
− | // | + | //Print out the current value of the sensor |
− | + | Serial.println(pin_value); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
//delay 100ms between loops | //delay 100ms between loops | ||
− | delay( | + | delay(10); |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 18:26, 18 August 2017
Language: | English • 中文 |
---|
The product number of Sensor-Light-A1 is: MSDT42 Sensor-Light-A1 is a photosensitive brightness detection sensor module.
ContentsDescription of the Sensor Pin
Feature
Specification
Document
UsageBasic FunctionalityThe Light Sensor is an analog input sensor. It is able to detect light levels by using a photoresistor. A photoresistor is sensitive to the amount of light and changes its resistance based on light levels. When used in a voltage divider, different voltage levels will be produced based on the amount of light. A Core module can read the voltage value.
ProgrammingIntroductionThe Light Sensor is used as an analog input pin. Therefore, the pinMode and analogRead functions will be used. Key Functions
ExampleThis is a simple example which outputs the state of the Light Sensor to the serial port terminal. Note: Important lines of code are highlighted. //Define the pin the sensor is connected to
//Note: This sensor uses analog read, which means only pins denoted with an 'A' prefix will work.
const int LIGHT_SENSOR_PIN = A0;
void setup(){
// put your setup code here, to run once:
//Initial serial communication port at 9600 baud
Serial.begin(9600);
//Configure the pin into input mode
pinMode(LIGHT_SENSOR_PIN, INPUT);
}
void loop(){
// put your main code here, to run repeatedly:
//Perform a digital read and store the value into pin_state variable
int pin_value = analogRead(LIGHT_SENSOR_PIN);
//Print out the current value of the sensor
Serial.println(pin_value);
//delay 100ms between loops
delay(10);
} Copy and paste the code above to the Arduino IDE or Download the above example: Program Download
Programming
Hardware Setup
Result
ApplicationIt can feel the intensity of light around us. It can be used to detect the light intensity, and control the indoor lighting and object shading and so on. ProjectPurchaseHistoryGallery
|