# 3.5 Photoresistor

## Example 1:

## Hardware Wiring

![Photoresistor Example 1](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFE9QZtYA4gicZsBe2%2F015_photoresistor.jpg?generation=1526059560245435\&alt=media)

## Sketch

The code can be found at [Examples\_Arduino - sensor-kit-for-arduino - \_015\_PhotoResistor - \_015\_PhotoResistor.ino](https://github.com/LongerVisionRobot/Examples_Arduino/blob/master/sensor-kit-for-arduino/_015_PhotoResistor/_015_PhotoResistor.ino).

```
int sensorPin = 2;
int value = 0;
void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}
void loop() {
  value = analogRead(sensorPin);
  Serial.println(value, DEC);
  delay(50);
}
```

## Example 2:

## Hardware Wiring

![Photoresistor Example 2](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFE9TVgu-DFbIDLW48%2F005_photoresistor.jpg?generation=1526059551442162\&alt=media)

## Sketch

The code can be found at [Examples\_Arduino - geek-workshop - studynotes - \_005\_Photoresistor - \_005\_Photoresistor.ino](https://github.com/LongerVisionRobot/Examples_Arduino/blob/master/geek-workshop/studynotes/_005_Photoresistor/_005_Photoresistor.ino).

```
int photocellPin = 2; //define variable photocellsh=2, where we read the voltage
int ledPin = 12;  //define variable ledPin=12, led voltage output
int val = 0;    //define variable val


void setup() { 
  pinMode(ledPin, OUTPUT);  // set ledPin as mode OUTPUT
} 

void loop() { 
  val = analogRead(photocellPin);    //read analog signal from pin 2
  Serial.print(val);
  if(val<=512){      //512=2.5V, if more sensitive is expected, increase 512; otherwise, decrease 512
    digitalWrite(ledPin, HIGH); //if val < 512(2.5V), led is light up.
  }
  else{
    digitalWrite(ledPin, LOW);
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://longer-vision-robot.gitbook.io/arduino-full-stack/03_led_sensors/015_photoresistor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
