Indice
6.1 What Are Sensors?
In the previous chapters, we learned how to use a pushbutton (or push button, as it’s often written in the UK) to send a command to Arduino and control an LED. The button let us talk to the board using a digital signal, which can be in one of two states: HIGH or LOW.
But how can we get Arduino to react to what’s going on around it, without having to press a button every time? For example, how could we make it switch on a light when the room gets dark, or turn on a device when the temperature goes above a certain value?
To do this, we use sensors: electronic components designed to detect one or more physical quantities, such as light, heat or distance, and turn them into signals that an electronic circuit can use.
There are many types of sensors, each designed to detect a particular quantity. Here are a few examples:
- Light sensors, which detect how bright the surroundings are.
- Temperature sensors, which measure how hot or cold an object or the environment is.
- Distance sensors, which tell us how near or far away an object is.
- Motion sensors, which detect the movement of people or objects.
In robotics projects, sensors play a key role, because they let a system gather information about its environment and use it to decide how to behave.
Digital sensors and analog sensors
From Arduino’s point of view, there are two main categories of sensors: digital sensors and analog sensors (spelled “analogue” in British English, although “analog” is the term you’ll see on most Arduino boards, tutorials and datasheets).
A digital sensor gives us information as distinct states. In the simplest case there are just two: HIGH and LOW. An example is a sensor that tells us whether an object is present or not.
An analog sensor, on the other hand, gives us a signal that can change smoothly within a given range. This means Arduino can detect not just two conditions, but different intensities of a phenomenon. A light sensor, for example, can give different values depending on how much light it receives.
This distinction is about the type of signal used to pass on the information, not just the physical quantity being measured. In fact, temperature, light and distance sensors all come in versions with an analog output and versions with a digital output.
In the next sections, we’ll focus on analog sensors, learning how to read their values with Arduino and use them in our programs. For our first example, we’ll use a light sensor called a photoresistor (also known as an LDR, or light-dependent resistor), which is easy to wire up and perfect for our first experiments.

6.2 The Photoresistor: A Light Sensor for Arduino
The photoresistor, also known as an LDR (short for light-dependent resistor) or a photocell in the US, is an electronic component whose electrical resistance changes depending on how much light it receives.
It works in a simple way: when the light reaching the component increases, its resistance generally goes down; when the light decreases, the resistance goes up. This lets us use it to detect changes in the brightness of a room or of any other environment.
A photoresistor is easy to recognise: it’s usually round, with a light-sensitive surface on top and two metal leads. Unlike an LED or a diode, it has no polarity to worry about, so you can connect its two leads either way round.

How can we use a photoresistor with Arduino?
Arduino can’t measure the resistance of a photoresistor directly using a normal analog input. To get a value the board can read, we need to turn the change in resistance into a change in electrical voltage.
To do this, we connect the photoresistor to a fixed resistor, forming a circuit called a voltage divider (known as a potential divider in the UK). In this circuit, the voltage at the middle point changes as the resistance of the photoresistor changes and, therefore, as the brightness changes.
By connecting this point to an analog input on Arduino, we’ll be able to read a number that changes with the amount of light received.
It’s important to remember that the value we read is not a direct measurement of brightness in lux: it’s just a number worked out from the voltage in the circuit. To get a reading in lux, we’d need a suitable sensor and a calibration procedure.
A first experiment
Imagine connecting the photoresistor to Arduino and watching the value we read while we shine a lamp on the component, or cover it with a hand.
We’ll see that the value changes as the light changes. Whether the number goes up or down depends on how the photoresistor and the resistor are connected in the voltage divider.
In the next section, we’ll build the circuit, looking at the components we need and the connections to make on the breadboard.
6.3 Building the Light Sensor Circuit
To read the value of the photoresistor with Arduino, we need to build a circuit that turns the change in its resistance into a change in voltage. As we’ve seen, to do this we’ll use a voltage divider (or potential divider in the UK), made up of the photoresistor and a fixed resistor.
What you’ll need
For this experiment, let’s gather:
- An Arduino UNO R3 board.
- A photoresistor (LDR).
- A 10 kΩ resistor.
- A breadboard.
- A few jumper wires or jumper leads.
- A USB cable to connect Arduino to the computer.
How the voltage divider works
The circuit is made up of two resistors connected one after the other between Arduino’s 5 V pin and GND. One of the two is the photoresistor, and the other is the fixed 10 kΩ resistor.
The point where the two components meet is connected to Arduino’s analog input A0. This way, the voltage at that point changes with the resistance of the photoresistor.
To build the circuit, we make the following connections:
- Connect one lead of the photoresistor to the 5V pin on Arduino.
- Connect the other lead of the photoresistor to the analog pin A0.
- Connect a 10 kΩ resistor between A0 and GND.
The circuit therefore follows this path:
5V → Photoresistor → A0 → 10 kΩ resistor → GND
Pin A0 is connected to the middle point of the voltage divider, which lets Arduino measure the voltage at that point.

What happens when the light changes?
When the light increases, the resistance of the photoresistor goes down. In the circuit we’ve built, this makes the voltage on pin A0 go up.
On the other hand, when the light decreases, the resistance of the photoresistor goes up and the voltage on pin A0 goes down.
So we can sum up how our circuit behaves like this:
- More light → lower photoresistor resistance → higher voltage on A0.
- Less light → higher photoresistor resistance → lower voltage on A0.
This relationship applies to the connection we’ve just described: if we swapped the positions of the photoresistor and the fixed resistor, the voltage would behave the opposite way.
Before moving on, let’s carefully check all the connections and make sure the circuit is built correctly. In the next section, we’ll write the program that reads the voltage through the analog input A0 and shows the values Arduino gives us.
6.4 Reading an Analog Value with Arduino
Now that we’ve built the photoresistor circuit, we can write a program that lets Arduino read the voltage on analog pin A0 and turn it into a number, which will be shown on the Arduino Serial Monitor.
This sequence will repeat in a loop, with a half-second wait between one cycle and the next, so we have time to read the value on the screen.
The block diagram
Let’s start by drawing a block diagram (or flowchart) of the process we’ve just described:

Writing the program code
In the previous chapters we used the digitalRead() function to read the state of a digital input, which can be HIGH or LOW. Now we’ll use a new function, analogRead(), which lets us read an analog value.
Open the Arduino IDE and type in the following program (called a “sketch” in Arduino jargon):
/* Exercise No. 06.4
Reads the light level with a photoresistor and prints the value to the Arduino IDE Serial Monitor */
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Starts serial communication with the Arduino IDE Serial Monitor and sets the speed (9600 baud)
}
void loop() {
// put your main code here, to run repeatedly:
int lightValue = analogRead(A0); // Reads the analog value from pin A0
Serial.println(lightValue); // Prints the value to the Serial Monitor
delay(500); // Half-second pause
}
After verifying the sketch and uploading it to the board, we’ll be able to see the values returned by the sensor.
How does the program work?
In the setup() function we find this line:
Serial.begin(9600);
The Serial.begin() function starts serial communication between Arduino and the computer. The number 9600 is the communication speed, measured in baud. For now, all we need to know is that we’ll have to select the same speed in the Serial Monitor of the Arduino IDE.
In the loop() function, we have this instead:
int lightValue = analogRead(A0);
The analogRead(A0) function reads the voltage on analog pin A0 and returns a number. On the Arduino UNO R3, this value normally ranges from 0 to 1023: 0 corresponds to a voltage close to 0 V, while 1023 corresponds to a voltage close to 5 V.
The result is stored in the variable lightValue, which we’ve declared as an int (integer), a type that’s perfect for holding whole numbers like these.
The next line is:
Serial.println(lightValue);
This function sends the contents of the variable to the computer, where we can see it in the Serial Monitor. The println() function ends each value with a line break, so the numbers are displayed one under the other.
Finally:
delay(500);
adds a pause of 500 milliseconds, that is, half a second, before the program repeats the reading. This way, the values don’t scroll past too quickly.
Showing the values in the Serial Monitor
After uploading the program, open the Serial Monitor in the Arduino IDE and set the speed to 9600 baud, so that it matches the one in Serial.begin(9600).
We’ll see a stream of numbers appear. Try shining a lamp on the photoresistor and then covering it with your hand: you’ll notice that the values change with the amount of light it receives.
With the circuit we built in the previous section, more light should generally give higher values, while less light should give lower values. The exact results depend on the characteristics of the component and on the lighting conditions.
Remember: Arduino isn’t measuring light directly, but the voltage on pin A0. The number we see is that voltage converted into a digital value.
Now that we know how to read and watch the sensor’s values, in the next section we’ll use this information to make Arduino take a simple decision, controlling an LED based on the brightness it detects.
6.5 Controlling an LED with Light
In the previous section, we learned how to read the value of the photoresistor and show it in the Serial Monitor. Now we’ll use that value to control an LED, getting Arduino to react automatically to changes in the light.
To do this, we need to set a threshold, that is, a reference value that Arduino will use to decide when to turn the LED on or off.
The block diagram
So let’s modify the block diagram to match the new behaviour we want:

The program
Keep the photoresistor circuit connected to pin A0 and add an LED to digital pin 13, with a 220 Ω or 330 Ω resistor connected in series. We can also use the built-in LED on the Arduino UNO R3 board, marked with the letter L.
Let’s write the following program:
/* Exercise No. 06.5
Reads the light level with a photoresistor and turns an LED on if the value is greater than lightThreshold */
const int lightThreshold = 500; // Declares the constant for the threshold value that switches the LED on/off
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Starts serial communication with the Arduino IDE Serial Monitor and sets the speed (9600 baud)
pinMode(13, OUTPUT); // Sets pin 13 as an output to turn the LED on
}
void loop() {
// put your main code here, to run repeatedly:
int lightValue = analogRead(A0); // Reads the analog value from pin A0
Serial.println(lightValue); // Prints the value to the Serial Monitor
if (lightValue > lightThreshold) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(100); // Pause of a tenth of a second
}
How does it work?
At the beginning of the program, we find:
const int lightThreshold = 500;
This line defines a constant called lightThreshold and gives it the value 500. The word const means that the value must not change while the program is running. This way, we can easily change the threshold without having to hunt for the number inside the conditions.
In the setup() function, we set pin 13 as an output, because it will be used to control the LED.
In the loop() function, we read the value of the photoresistor with analogRead(A0) and store it in the variable lightValue.
Next, the program checks this condition:
if (lightValue > lightThreshold)
The > symbol means greater than. If the value we read is higher than 500, Arduino runs the digitalWrite(13, HIGH) instruction and turns the LED on. If the value is less than or equal to 500, the else part runs instead and the LED turns off.
The 100-millisecond pause slows down the loop slightly, without stopping the system from reacting quickly to changes in the light.
Let’s try the program
Upload the program to Arduino and watch what the LED does. If the light on the photoresistor increases enough to push the value we read above the threshold, the LED turns on; if the value drops below the threshold, the LED turns off.
The value 500 is just a starting point: it doesn’t correspond to a specific level of brightness. We can change it depending on the sensor, the environment and the values we see in the Serial Monitor. If the LED stays on or off all the time, check the values we’re reading and try a more suitable threshold.
We can also add Serial.begin(9600); to the setup() function and Serial.println(lightValue); to the loop() function, to keep watching the values in the Serial Monitor while the LED is working. This is useful while testing and for tracking down any problems.
With this experiment, we’ve built a simple automatic system: Arduino reads a physical quantity through a sensor, compares the value with a threshold and controls an output based on the result. This is one of the basic principles of automation and robotics.
6.6 Testing and Troubleshooting the Program
Once we’ve built the circuit and uploaded the program, we need to check that the light sensor and the LED are working properly. At this stage, it’s important to watch both what the circuit does and the values shown in the Serial Monitor.
The sensor value doesn’t change
If the number shown in the Serial Monitor always stays the same, even when we shine a light on the photoresistor or cover it, the first thing to do is check the connections on the breadboard.
Make sure the photoresistor is connected between 5V and A0, and that the 10 kΩ resistor is connected between A0 and GND. Pin A0 must be connected to the point where the two components meet.
We should also make sure the program has been uploaded correctly and that the Serial Monitor is set to 9600 baud, as specified in the Serial.begin(9600); line.
The LED stays on or off all the time
If the sensor value changes but the LED doesn’t react as expected, we may need to change the threshold set in the program.
Remember that the value 500 we used in our example is just a starting point. Look at the values the sensor gives us when it’s lit and when it’s covered, then choose a threshold that suits the conditions of our experiment.
If the LED never turns on, also check that it’s connected correctly, with the right polarity and a series resistor. If we’re using the built-in LED on the Arduino UNO R3 board, make sure the program is controlling digital pin 13.
The Serial Monitor shows strange characters or no data
If the Serial Monitor shows unreadable characters (garbled text), check that the selected speed is 9600 baud and matches the one set in the program.
If no data appears at all, make sure the Serial.begin(9600); line is in the setup() function and the Serial.println(lightValue); line is in the loop() function. Also check that the correct port is selected and that the program has been uploaded to the board.
A useful method for finding errors
When something doesn’t work, let’s go step by step:
- Check the connections of the photoresistor and the resistor.
- Check that Arduino reads changing values through A0.
- Check the threshold used in the program.
- Check the connection and control of the LED.
This way, we can tell a circuit problem from a programming error, and avoid changing everything at once.
Being able to check results and track down the cause of a fault is a key skill in robotics.

In the next section, we’ll put what we’ve learned into practice with some exercises and variations, changing the program and experimenting with new behaviours.
6.7 Exercises and Variations
Now that we know how to read the values of an analog sensor and use them to control an LED, we can try out some changes to the program. The following exercises will help us understand better how a photoresistor works and how Arduino can react to changes in the light.
Exercise 1 – Changing the threshold
In the previous program, we used a threshold of 500.
Let’s try changing the value of lightThreshold, first to 300 and then to 700. Notice how the behaviour of the LED changes when we shine a light on the photoresistor or cover it.
Question: why can the LED turn on in different lighting conditions when we change the threshold?
Exercise 2 – Reversing the behaviour of the LED
Let’s change the condition in the program so that the LED turns on when the value read from the sensor is lower than the threshold, and turns off when it’s higher than or equal to it.
To do this, we need to change the if condition, keeping the instructions that control the LED in the two cases.
Question: which symbol do we need to use to check whether one value is less than another?
Exercise 3 – Changing how often the values are displayed
In the program, we used delay(100);, which adds a pause of 100 milliseconds between one cycle and the next.
Let’s try replacing this value with delay(500); and then with delay(1000);. Notice how the rate at which the data appears in the Serial Monitor changes.
Question: what happens when we increase the waiting time?
Exercise 4 – Customising the program
Let’s change the program to use a constant called lightThreshold with a value of our own choosing. We’ll also add the instructions needed to show the value read from the sensor in the Serial Monitor.
Then let’s try to find a threshold that makes the LED turn on when we cover the photoresistor and turn off when we shine a light on it. To get this behaviour, we may need to reverse the if condition, depending on how the circuit is connected.
A final challenge
Imagine we want to tell two environmental conditions apart: bright light and low light.
Let’s change the program so that, as well as controlling the LED, it also uses the Serial Monitor to show a text message:
- If the value we read is above the threshold, show “Bright light”.
- If the value is lower than or equal to the threshold, show “Low light”.
To do this, we can use Serial.println() to send text as well, not just numbers.
Conclusion
In this chapter, we’ve learned that Arduino can read information from its environment through an analog sensor, turn it into numerical values and use them to make decisions. We’ve also used the Serial Monitor to watch the data and check that the circuit is working properly.
In the next chapter, we’ll introduce the 1602 LCD display with an I²C interface, which will let us show text and values directly on the device, without having to keep going back to the computer.
Coming up in the next chapter...
So far, to find out what our light sensor was reading, we’ve always needed the computer and the Serial Monitor. In the next chapter, Chapter 7 – How to Connect and Program an I2C LCD 1602 with Arduino, we’ll give Arduino a little voice of its own: we’ll discover the 1602 LCD display, a two-line screen where our circuit can write messages and show numbers, without depending on the computer any more.
We’ll see how to wire it up with just a few wires and how to get it to display our first welcome message. Then we’ll go back to the light sensor from this chapter and make it talk directly on the display: all it will take is covering it with your hand to see the numbers change in real time.
Along the way, we’ll also learn how to fix the little hiccups that happen to everyone, and we’ll put ourselves to the test with a few exercises to build a real light-level monitor.
If you’re curious to see where this journey will take us, have a look at the page Beginner's Guide to Robotics with Arduino: there you’ll find the full contents list, from the first chapter to the final project, and you can choose whether to carry on in order or go back and review a topic.
If you’ve found this course useful and would like to explore other robotics or hobby electronics projects, you can keep reading by visiting the other content in the Electronics and Robotics section of the site, where you’ll find plenty more practical guides and step-by-step explanations.
I’d like to point out that some of the images in the course were generated by artificial intelligence (AI), and I apologise for any inaccuracies they may contain. The course itself was also written with the help of AI, but the structure is the result of my own experience: I’m a retired programmer and, over the course of my career, I’ve taught several courses. So I chose the topics, organised the whole structure of the course and reviewed the text myself.
I care a great deal about the quality and originality of the content I publish. That’s why I’d prefer it not to be copied or republished elsewhere. If you think this guide could be useful to other people, though, I’ll be really happy if you share the link to the page: it’s the right way to spread the information and to support the work behind every article.
Thank you
Luciano (Capitan Farloc)

English (United Kingdom)
Italiano (Italia)