# Create a Night Light

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

## Supplies

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MXGWiuoJwDyzzKbruLJ%2F-MXGX7u7c29DDqzxIOGd%2Fimage.png?alt=media\&token=3dfe5d9e-ab75-4062-b32c-a2df784bd13e)

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MXGWiuoJwDyzzKbruLJ%2F-MXGXEaCWEZT4XdPJiDg%2Fimage.png?alt=media\&token=e025b46c-f6d2-4272-9c89-3fd1f1e5af1d)

## Diagram

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MXGWiuoJwDyzzKbruLJ%2F-MXGXHZNbNzUPgSwhHQE%2Fimage.png?alt=media\&token=4f940697-668f-47ac-9218-de119db8868d)

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