Let’s use Arduino with a light sensor!

Arduino and light sensor

Arduino and light sensor

In a previous article, I introduced the basics of using LED circuits with Arduino.
Now, we’ll explore more possibilities by adding other electronic components.

Using a Sensor to Send Values to Arduino

We already learned how to use electrical signals to light up a LED.
This was an “output” from Arduino to the LED.
We controlled the LED by changing the pin number, the strength, and the timing to make it light up.
This time, we will control the strength and  the timing based on the input of a sensor.
What exactly is a sensor?
You’ve probably heard this word before, but do you know exactly what a sensor does?
Particularly in the field of electronics, you can often hear the term “____ sensor”.
A sensor is an electronic component that is used as an input device.

To put it simply, a sensor is used in place of the five senses (touch, sight, smell, taste, and sound).

Instead of the 5 senses that human have, Arduino and computers are using sensors to determine their surroundings conditions.
Humans have five senses (or maybe six in some cases…), but regarding electronic parts, there is a variety of types of sensors available.
Let’s use a light sensor.

Light sensor

Light Sensor

A light sensor (also known as a “CdS cell”) has a large resistance value when placed in a dark location and a low resistance value when places in a light location. When a project involves a light sensor, it’s natural to think about a project that will create an action when it gets bright in the morning or dark at night.
Let’s do some hands-on tests!

First, let’s test the sensor to make sure that a change of brightness will lead in a change of the resistance value. To do that, let’s set up a circuit to control the brightness of the LED.

Arduino Light Sensor

The light sensor is like a resistor. It has no plus-minus directionality. So, you can just connect it in. When it’s bright, the resistance value goes down. So, I’ve added a resistor to prevent the current from flowing into the LED. With this setup, let’s now use Arduino to run the program we wrote at the beginning to make the LED blink. When the LED starts blinking, put your hand over the light sensor and take it away. You should see the brightness of the LED change.

This is the basic property of a light sensor. In that case, it should be easy to make a useful tool that makes an LED light up when it gets dark. Let’s give that a try!

Let’s set aside Arduino for a moment, and just deal with the construction of the electronic circuits. In this case, by using additional components we can find a way to devise a circuit. Electricity basically flows across the path of least resistance. When the below circuit is placed in a well-lit area, the resistance value will be low, and electricity will flow towards the resistor. On the contrary, when placed in the dark, the resistance value will be high, and the electricity will flow toward the LED, causing it to light up.

Arduino Light Sensor

However, with this circuit, even if the light sensor is in a dark place, the LED won’t get very bright. When used like this, Arduino is just a box for carrying electric current and therefore not much more powerful than a battery. Let’s use Arduino to pick up the value from the light sensor and do something with it.

Arduino’s Analog Input

Let’s use Arduino’s analog input to measure the value of the sensor. Arduino’s analog input is shown here where it says “ANALOG IN.”

Arduino Analog In

To allow the analog input to get data, construct the circuit as shown below.

Arduino Analog In

We’ve now created an input circuit to get a value from the light sensor.
Next, let’s create an Arduino program to test it out.

We have created a new function.
We’d like to confirm the value from the sensor on our PC display, so in the setup function we wrote “Serial.begin(9800)”. By setting this, we can use Serial.println() within the loop to display the sensor value on the screen. This allows us to confirm input values. It’s a common method for debugging in Arduino.
Then, we get an input value from the analog pin. In this case, there’s no need to set the pin in the setup function.

The function to read input is:

analogRead(pin number)- function to get analog input

We will place the value obtained from analogRead into the “val” variable.
The “variable” keyword will play a very important role as we learn more about programming.
A variable is used by a program to temporarily store data (such as values or characters).

Variable: use when a program temporarily store data

Also, we will use the Serial.println() function to display the sensor value stored in “val” to the screen.
The Arduino analog pins accept input values between 0 and 1023. For this reason, to do something with the values that the program receives, we must write a program that knows what to do with these values.
To check the actual operation on the display, you can go to “Tools” and select “Serial Monitor” to check the input value from the light sensor.

Arduino IDE Serial Monitor

Can you see the values constantly changing?

Arduino IDE

Change the LED Brightness Based on the Light Sensor Value

Now that we’ve confirmed the light sensor values, let’s use them to affect the LED.
We will take the values input from the light sensor and send them to the analog output to control the LED.

Compared to the previous program, there is an extra analogWrite() function.

analogWrite([pin #],[output value]); send to analog output

There is no need to set the analog output in the setup function.
Unlike the analog input, which accepts values between 0 and 1023, the analog output values are between 0 and 255.
For this reason, we divide the input value by 4 before sending it to the output.

Arduino Light Sensor
Arduino Light Sensor

*If the brightness doesn’t change much even when you cover the sensor, it’s possible that your sensor has a narrow output value range. In this case, you should try changing to a resistor with a higher resistance value.

We have now completed both the setup for the input and the output. Until now, we have connected the Arduino to our USB port, so it has been getting 5V from the USB connection. But once the program has been written, you may want to use the Arduino without your PC. In that case, you can connect the Arduino to a 6-20V (recommended values from 7-12V) power adapter. If you place the Arduino in a case, then you will have a stand-alone light that gets brighter when in a dark environment.

If you would like to improve the functionality by changing the brightness gradually depending on the level of darkness, you can do so using “if” statements. It’s possible to have the LED turn off completely when it is bright out.

Let’s think about this example. Using the same technique, we could replace the LED with a piezoelectric buzzer, and have it buzz when it gets dark (or bright). By using a servo motor operating at a fixed angle of rotation, we could turn on a switch when it gets dark, and so on. Arduino can also be connected to internet via a LAN cable. So, the brightness value could be sent over the internet and displayed elsewhere, or it could be used for creating a totally different work by using different input and output. One of the funny things about Arduino is to remember all the things we’ve done, and to think about all the things that we could create.

Next, we’ll use a different parts to create even more stuff!