# Night light - Arduino project

Time flows, and as it flows, I managed to force myself to make this video tutorial for ArduinoThis time the theme is a circuit that turns the night light on or off based on the brightness of the room. This is a nice thing, especially when there are small children in the house, so it is necessary for the light to be turned on at all times. Of course, there is also a modification of this circuit, so that it can be used for, say, street lighting. The positive aspects of such a project can be easily seen and enumerated.

## Required components

This project is one of simple ones, so it is very easy to make and does not take much time. Required components are 2 resistors. Resistance values are 220Ω for LED protection and 10kΩ. I also owe one clarification, 1 resistor is shown on the electrical diagram, while on the real circuit you can see 2 resistors connected in series. For 10kΩ resistance, I had to connect two 5k otpor resistors, I have such at my disposal. Then the LED (blue, although it can be any other), photoresistor, jumpers and test board.

## The principle of operation of the circuit

The situation in which this type of project is applied has already been mentioned in the introductory part. If for any reason there is a need to keep the light on in low light conditions (eg basements, bedroom or study, street lighting…) we need this circuit. A novelty in this project is the use of photoresistors (LDR - Light Dependent Resistor) with which we will measure the brightness of the room (in this case it plays the role of a sensor). If the room light is low, the circuit will turn on the LED, otherwise the LED will be off. Measurements are performed constantly.

The Arduino can only measure voltage signals, so the resistance that develops on the LDR must be translated into voltage. In order to obtain information about the brightness, a voltage divider was formed, which consists of a series-connected LDR and a 10kΩ resistor (also this resistor protects the Arduino). The middle terminal of this divider (the point between the LDR and the resistor) is led to the input A0 of the Arduino. The voltage that develops on the LRD depends on the resistance, and the resistance on the lighting of the room. Depending on that voltage, the appropriate signal is sent to pin 13. We connect pin 13 of the Arduino to the anode of the LED, the cathode is connected via the "-" end on the test board with the ground of the Arduino. From pin 13 we send the signal "on" or "off" depending on the information about the brightness of the room.

### Photoresistor (LDR)

A LDR is a component that allows you to turn room lighting into resistance. The resistance is inversely proportional to the brightness, basically the higher the brightness the lower the resistance and vice versa and it is expressed in kΩ.

The formula for calculating resistance is:

`R = 500 / L`

In conditions of low light, ie darkness, the value of the brightness of the room is about 50lx, so the value of the resistance of the photoresistor is about 10kΩ (that's why we need a resistor or 10kΩ resistors).

## Sketch

```#define led 13
#define sensor A0

void setup()
{
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
}

void loop()
{
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
delay(500);
}```

### Sketch explanation

#### Definition section

In the definition section, we define two constants light and sensor. The constant light has a value of 13 and represents information about the pin we use to control the LED. While the constant sensor has the value A0 and represents the input pin to the Arduino to which information about the brightness of the room arrives.

#### Setup section

A light constant with a value of 13 is defined as an output, while a sensor constant is defined as an input using the function pinMode. This function has two arguments, the first is the pin we set, and the second is the mode of operation, ie input (INPUT) or output (OUTPUT). The second argument must be written in capital letters!

#### Loop section

The measurement is performed and depending on the measurement value (in this case we examine whether the brightness is less or greater than 300lxwe send the signal ON (HIGH) or OFF (LOW) to the output defined by the constant light . The value of the brightness of the room is obtained using the function analogRead(sensor). It can literally be read as a read value from an analog input defined by a constant sensor. Using the digitalWrite function, we send a HIGH or LOW signal to the output defined by the light constant, depending on the fulfillment of the conditions in the branching block.

## Project improvement ideas

Finally, here are some ideas for improving this project. In addition to measuring room brightness, a motion sensor, sound sensor, and possibly an infrared sensor can be added. A motion sensor would provide information about the presence of people in the room, so it could be possible to turn on the light when that condition is met. A sound sensor would allow voice control of the assembly, for example to turn the light on or off on a palm strike. The same would apply to an infrared sensor that would allow remote control.

## Other Arduino projects and tutorials 