> For the complete documentation index, see [llms.txt](https://hopemoore.gitbook.io/coding-for-creatives/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hopemoore.gitbook.io/coding-for-creatives/arduino-info/the-setup/create-a-night-light.md).

# Create a Night Light

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

## Supplies

|                                                                     |                                                            |
| ------------------------------------------------------------------- | ---------------------------------------------------------- |
| <img src="/files/-MNayW2jUi_Nst3XnGzZ" alt="" data-size="original"> | Circuit Board                                              |
| <img src="/files/-MNayzgMRngmlGc0RjrR" alt="" data-size="original"> | Breadboard                                                 |
| <img src="/files/-MNazfyfWM718wc-6O6v" alt="" data-size="original"> | Jumper Cables                                              |
| <img src="/files/-MNb6aAM5m1RVHyJtNyQ" alt="" data-size="original"> | <p>Photoresistor</p><p>(light sensor)</p>                  |
| <img src="/files/-MNb1OYaVNmqktuIHYim" alt="" data-size="original"> | <p>Standalone LED Light</p><p>(any color with 2 leads)</p> |
| <img src="/files/-MNb05Z02MSj2Zk1pt9n" alt="" data-size="original"> | 100 Ohm Resistor                                           |
| <img src="/files/-MNb1decklIbKjOmK_1p" alt="" data-size="original"> | 220 Ohm Resistor                                           |

## Diagram

![](/files/-MNbIfL9Bp1B1ITvZ7Uu)

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.

```cpp
// 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);
}
```
