# 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="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNaxbhjUd7KYoevsTMF%2F-MNayW2jUi_Nst3XnGzZ%2Fimage.png?alt=media&#x26;token=685eadb7-1fad-4f7e-924d-9553d6a8acc9" alt="" data-size="original"> | Circuit Board                                              |
| <img src="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNaxbhjUd7KYoevsTMF%2F-MNayzgMRngmlGc0RjrR%2Fimage.png?alt=media&#x26;token=e10f37c3-a70c-4180-9696-5902558ba106" alt="" data-size="original"> | Breadboard                                                 |
| <img src="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNaxbhjUd7KYoevsTMF%2F-MNazfyfWM718wc-6O6v%2Fimage.png?alt=media&#x26;token=f3df972a-2913-43dc-abe3-048b86265ba2" alt="" data-size="original"> | Jumper Cables                                              |
| <img src="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNb6DOE-JUpjgR203AL%2F-MNb6aAM5m1RVHyJtNyQ%2Fimage.png?alt=media&#x26;token=f2b0d3ef-e43d-4d7b-8a8b-f88a16fbbcaa" alt="" data-size="original"> | <p>Photoresistor</p><p>(light sensor)</p>                  |
| <img src="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNb155tF2Eb8_UvmIhy%2F-MNb1OYaVNmqktuIHYim%2Fimage.png?alt=media&#x26;token=0353b969-9de4-4a05-bc07-a09871217d7f" alt="" data-size="original"> | <p>Standalone LED Light</p><p>(any color with 2 leads)</p> |
| <img src="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNb-SIl1jlV52PS8g-G%2F-MNb05Z02MSj2Zk1pt9n%2Fimage.png?alt=media&#x26;token=35ee6ffd-f212-46e7-908e-e4d904bd0550" alt="" data-size="original"> | 100 Ohm Resistor                                           |
| <img src="https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNb155tF2Eb8_UvmIhy%2F-MNb1decklIbKjOmK_1p%2Fimage.png?alt=media&#x26;token=1b27f65c-8728-45a7-97a8-7d7735a1cf32" alt="" data-size="original"> | 220 Ohm Resistor                                           |

## Diagram

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MNbFrIoXqd1406VllBp%2F-MNbIfL9Bp1B1ITvZ7Uu%2Fimage.png?alt=media\&token=a0ac03c5-cc94-4ec5-9a8e-fa42e6d18685)

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