# Create a Night Light

This is an example of using an input (sensor) component to change an output of an output component.

## Supplies

![](/files/-MXGX7u7c29DDqzxIOGd)

![](/files/-MXGXEaCWEZT4XdPJiDg)

## Diagram

![](/files/-MXGXHZNbNzUPgSwhHQE)

The photoresistor data (yellow) is connected to pin A0 (analog) and the LED data (orange) is connected to pin 2 (digital).

## Code

This code gets the amount of light coming in via the sensor and if it's dark enough, the LED will light up.

```c
// Sensor pin is connected to pin A0 (analog) on the Arduino circuit board
int sensorPin = 0;

// LED pin is connected to pin 2 (digital)
int LEDpin = 2;

// Define variables
int lightValue;

void setup() {
  // Establish the component connection and its type (output/input)
  pinMode(sensorPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  
  // Start Serial connection
  Serial.begin(9600);
}

void loop() {
  // Reads and stores the data coming in from the sensor
  lightValue = analogRead(sensorPin);

  // Lights up if dark enough
  if (lightValue <= 5) {
    digitalWrite(LEDpin, HIGH);
  }
  else {
    digitalWrite(LEDpin, LOW);
  }
  
  delay(100);

  // Sends information to the Serial Monitor
  Serial.print("Light Value: ");
  Serial.print(lightValue);
  delay(100);
}
```


---

# 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://hopemoore.gitbook.io/coding-for-creatives-spring-2021/arduino-info/the-setup/create-a-night-light.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.
