Arduino Tutorial: Light sensing

The wonderful world of automatic brightness amazes and calls to you, but don’t know where to get started? Don’t worry about it, in this blog I will show you how to use an Arduino to measure the surrounding light level. Let’s get started!

What exactly are we dealing with?

Brightness or dimness is nothing but the measurement of the amount of light in an area. To measure this, we need to use something called a photocell/photoresistor/LDR (Light Dependent resistor). This is the most common type of light sensor used and is much better than an infra-red sensor or a color sensor (those two have their own benefits though).

Small photoresistor
Image result for photoresistor sizes
Large photoresistor
Image result for photoresistor sizes
digital photoresistor

We can see from its name itself that it is a resistor and it works in a pretty straightforward way as well. The resistance between the 2 pins of the LDR reduces as the intensity of light falling on its surface increases and vice-versa. The LDR is non-polarized and the orientation of its pins do not matter, i.e. it allows current to flow in both ways(unlike an LED or diode). The digitial LDR (third image) is also an LDR but instead it is connected to a small circuit board with additional electronics which convert the output to digital signals. Instead of measuring an “analogue” resistance, in this we measure the state of a third pin which is held to low(or 0v) most of the time but changes to high(or 5/3.3v) when the amount of light goes above a certain threshold. This threshold can be manipulated by turning the potentiometer(blue with white dial in the center) and adjusting.

Step 1: Gathering the parts

In this step, I will be listing all the parts which you will need for the project (You might already have them if you have read my other tutorials). The links to purchase these parts can be found at the end.

  • Arduino UNO (any Arduino or equivalent will work but this one is the best for beginners)
Arduino UNO
  • USB cable (this should come included with your Arduino)
Image result for arduino usb cable
USB cable for Arduino
  • LDR/Photoresistor
Image result for photoresistor sizes
Digital LDR
This image has an empty alt attribute; its file name is image.jpeg
Regular LDR
  • 220 kilo ohm resistor (Any value between 100K and 300K will work)
Image result for 220 K resistor
a 220K Ω resistor
  • LED (optional)
Image result for 5mm LED bulb image
5 mm LED
  • Mini breadboard (optional)

As I have mentioned above, any Arduino will work, but if you are a beginner, then it is best to use an UNO due to its convenient layout, support and less distracting features.

As for the LDR, different types and sizes might look different, but there are actually only two different categories, digital and the analogue(regular) which need to be treated differently. All the different sizes in each of the two can be used in the same way. In this tutorial, I will show how to use both of them and different ways of using both of them.

The LED and breadboard are optional. By using the breadboard, it will become much easier for you to connect everything and then disconnect (once you are done). The LED is just there for visual output.

Just remember that you don’t have to stick to exactly 200K ohms for the project to work, just find anything between 100K and 300K and it will work. You can find the purchase links at the end.

Step 2a: Connecting everything up

In this step, I will be showing the two ways of connecting a regular LDR to the Arduino. If you want the connections and code for the digital version, go to steps 2b and 3b. An LED can be used additionally to give visual output.

Start by connecting one of the sensor’s pins to 5v/VCC and the other to one of the analogue pins (A0 to A5) on the Arduino. Remember, if you do not use the analogue pins, your code won’t work! I will give the reason for this at the end of the next step. Then connect the LED to one of the PWM capable pins of the Arduino. If you are not familiar with using LEDs with Arduino, go to my tutorial on getting started with Arduino (It’s good, trust me) and if you have never heard of PWM before, go to my tutorial on PWM and LED brightness control (It’s worth it). Finally, take you resistor and connect it between the LDR and a GND pin on the Arduino.

You can try changing the value of the resistor and measuring how it affects the range of brightness that can be measured and the resolution of the sensor values.

Step 3a: Coding and Uploading

In this step, I will be writing the code required for reading the values coming from the regular, board-less LDR, if you are not familiar with writing and uploading code to an Arduino, I have linked my tutorial for beginners in the first step (you can alternatively click here).

First, you will need to create two constants outside the loop and give them the values of the pins you are going to use for connecting the LED and LDR.

In this case, I have names them sensePin and ledPin and given them the values of A5 and 5, but you can name them whatever you want. Just make sure that the names and values are valid and that you use them throughout the program. Also remember that you have to use the pins between A0 and A5 for sensePin. I will give the reason for this at the end of this step.

Next, in the setup, set the appropriate pin modes for the pins and start Serial communication with your PC.

sensePin is input and ledPin is output. The baud rate I have selected for Serial communication is 9600 because it is the most commonly used, but you can select any speed. Just remember to select the same one in the Serial monitor, which will be used for displaying the values we get after reading the sensor. If you are not familiar with serial, have a look at my tutorial on using Serial with Arduino.

If you have never used sensors with Arduino before, you must be getting excited and maybe nervous. How will you read the state of the LDR? It’s actually extremely easy and the Arduino gives you built in functions to do so. Enter this code into the loop.

After using analogRead(), you might be wondering about the existence of digitalRead(), and sure enough it does exist. it takes the pin to perform the read on, just like analogRead, but instead reads the voltage of the pin (0 to 5v) and if it is above 3.3v, it returns a boolean 1, otherwise a boolean 0. Your code should finally look like this.

The delay at the end is to slow down the number of times the Arduino reads and prints the value. Without it, the Arduino will execute the code in the loop MANY MANY THOUSANDS of times per second. It basically gives you some breathing room and time to react.

Now that you have finished writing and understanding the code, it is time to compile and upload it to the Arduino. Make sure that you have connected everything properly, your board and COM port are selected correctly, and them upload it. You should also have some way of easily varying the amount of light falling on the sensor (IMPORTANT).

Open the Serial monitor, select the correct baud rate from the bottom right, and start varying the amount of light falling on the sensor by shining a torch on it, covering it with your hand, pointing it towards and away from various light sources. You can also change the rotate the potentiometer (using a screwdriver or your nail) and seeing how it changes the sensitivity of the LDR. That’s it, you have a working brightness sensor! Easy right?

Step 2b: connecting everything up

In this step, I will be showing the connections for the digital version of the sensor with the control board, if you want the connections and code for the regular version, go back and have a look at steps 2a and 3a.

Connect the VCC or 5v pin of the sensor to the 5v or 3.3v pin of the Arduino and the GND pin of the sensor to any GND pin on the Arduino. This will provie power to the circuit and the main sensor itself. Next, connect the output pin of the module to any pin on the Arduino which can perform a digital read (which all pins can, so any pin will do). The LED can be connected to any pin on the Arduino (No PWM because the sensor does not give digital values and only sends Highs and Lows). If you are not familiar with using an LED with an Arduino then read my tutorial on getting started with Arduino (It will be helpful) and if you have never heard about PWM before, read my tutorial on LED brightness control and PWM (It’s worth it, trust me).

Step 3b: Coding and Uploading

In this step, I will be writing the code necessary for controlling the digital photoresistor. If you are not familiar with writing and uploading Arduino code, I have already linked my tutorial for beginners in the previous step (you can alternatively click here).

First, create two constants outside the setup and give them the values of the pins you are going to use for connecting the photoresistor and the LED (which is optional).

I have called them sensePin and ledPin and given them the values of 2 and 13 (built in LED) respectively. You can use whatever names and values you want, just make sure that they are valid and you use them throughout the program.

Next, set the pin modes of each of the pins accordingly and also begin Serial communication with your PC. If you are not familiar with serial, click here to read my tutorial on using serial with Arduino.

sensePin is input and ledPin is output. I have used the baud rate of 9600 for Serial as this is the most common but you choose what speed you want. The Serial monitor will be used to display the values we get after reading the state of the photoresistor.

If you have never used sensors with Arduino before, you must be getting excited and maybe nervous. How will you read the state of the LDR? It’s actually extremely easy and the Arduino gives you built in functions to do so. Enter this code into the loop.

We first create a boolean variable called senseVal (sensor value). We then perform a digital read on sensePin and then put the value into senseVal. This value gets printed on the Serial monitor and then sent to the LED. Because senseVal is a boolean variable, it can directly be written to the LED without any if statements or conversions. If it is 1, the LED turns on and if it is 0, it turns off. Your code should finally look like this.

After using digitalRead(), you might be wondering about the existence of analogRead(), and sure enough it does exist. it takes the pin to perform the read on, just like digitalRead, but instead reads the voltage of the pin (0 to 5v) and then gives a number between 0 and 1023. This is the same as analogWrite, but instead it reads the value of the pin rather than writing it (obviously).

The delay at the end is to slow down the number of times the Arduino reads and prints the value. Without it, the Arduino will execute the code in the loop MANY MANY THOUSANDS of times per second. It basically gives you some breathing room and time to react.

Now that you have finished writing and understanding the code, it is time to compile and upload it to the Arduino. Make sure that you have connected everything properly, your board and COM port are selected correctly, and them upload it. You should also have some way of easily varying the amount of light falling on the sensor (IMPORTANT).

Open the Serial monitor, select the correct baud rate from the bottom right, and start varying the amount of light falling on the sensor by shining a torch on it, covering it with your hand, pointing it towards and away from various light sources. You can also change the rotate the potentiometer (using a screwdriver or your nail) and seeing how it changes the sensitivity of the LDR. That’s it, you have a working brightness sensor! Easy right?

Step 6: What now?

Even though you have succesfully used the LDR with the Arduino, there are still many things left to try and the fun has just begun. Try to add transparent tinted paper on the top of the sensor and then shining lights of different colors but the same intensity (you have just discovered how color sensors work). Try to change the resistor values (If you have used the second technique for the boardless photoresistor).

There are many projects as well which can do great with a photoresistor, try to think of some of your own perhaps? As I said, the fun has just begun.

Additional Resources (Give them a read too)

  1. Arduino reference – digitalRead
  2. Arduino reference – analogRead

Purchase links

While these are the parts you want to look out for, you don’t have to buy exactly these ones. You can buy equivalent parts from different sellers. A quick search on google will yield many.

  1. Arduino UNO
  2. LDR module
  3. LDR sensor
  4. Resistor pack

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s