7

Lesson 7: Sensors and Analog Input with Arduino

Use Arduino to read changing values from the real world

32-Lesson Course • Semester 1

You already know how to make decisions with conditionals, repeat actions with loops, and organize code with functions. Now you will learn how Arduino reads information from sensors so your programs can react to the outside world.

Learning Objectives

By the end of this Lesson, you will:

  • Understand what a sensor does in an Arduino project
  • Explain the difference between digital input and analog input
  • Use analogRead() to read values from a sensor
  • Print changing sensor values to the Serial Monitor
  • Use the Serial Plotter to see sensor values as a live graph

Skills Developed:

  • Reading hardware input step by step
  • Using the Serial Monitor for testing and debugging
  • Connecting simple sensor circuits safely
  • Preparing for sensor-controlled projects later

Section 1: What Is a Sensor?

A sensor is a component that measures something in the environment and sends that information to Arduino. Some sensors measure light, temperature, motion, sound, or position.

Everyday idea

A button tells Arduino whether it is pressed or not. A potentiometer tells Arduino how far a knob is turned. A light sensor tells Arduino how bright it is. Sensors help the program know what is happening outside the code.

Examples of beginner sensors

  • Button: pressed or not pressed
  • Potentiometer: knob position
  • Photoresistor: light level
  • Temperature sensor: hot or cold

What Arduino does with a sensor

  • Read the sensor value
  • Store it in a variable
  • Print it or compare it
  • Use it to control hardware later

Section 2: Digital Input vs Analog Input

Some inputs only have two possible states. Other inputs can change through a wide range of values.

Digital input

Digital input is usually just one of two states:

  • HIGH
  • LOW

A button is a common digital input because it is either pressed or not pressed.

Analog input

Analog input can change smoothly through many values.

  • A potentiometer can turn a little or a lot
  • A light sensor can see dim or bright light
  • The reading is not just yes or no
On many Arduino boards, analog input pins are labeled A0, A1, A2, and so on. These pins are used with analogRead().

Section 3: Reading an Analog Value

Arduino uses analogRead() to measure a value from an analog pin. On most beginner Arduino boards, the value is usually between 0 and 1023.

int sensorValue = analogRead(A0);

What the reading means

  • 0 means very low input voltage
  • 1023 means very high input voltage
  • Values in between show different levels

Important note

The value is not usually the final real-world unit yet. For example, a light sensor reading is just a number until you decide how to use it.

Section 4: Potentiometer Wiring Setup

A potentiometer is a great first analog sensor because it is stable, easy to wire, and easy to test. Turning the knob changes the value Arduino reads.

Hardware setup: potentiometer on A0

Left pin

Connect one outer pin of the potentiometer to 5V.

Middle pin

Connect the middle pin to analog pin A0.

Right pin

Connect the other outer pin to GND.

If the values change in the opposite direction from what you expect, that is okay. The two outside pins can be swapped, and the potentiometer will still work.

Section 5: Print Sensor Values to the Serial Monitor

Before using a sensor to control hardware, it is smart to print the readings so you can see what the sensor is doing.

const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(200);
}

What happens?

  • analogRead(sensorPin) gets the current sensor reading
  • Serial.println(sensorValue) shows the value in the Serial Monitor
  • delay(200) slows the printing so the values are easier to read

Section 6: Visualize Sensor Values with the Serial Plotter

The Arduino IDE has another useful tool called the Serial Plotter. Instead of showing numbers as text, it draws a live graph of your sensor values. This makes it easy to see patterns and changes over time.

How to open the Serial Plotter

  1. Upload your program to Arduino
  2. In the Arduino IDE, look for the magnifying glass icon with a small graph line (next to the Serial Monitor icon)
  3. Click the Serial Plotter icon, or go to Tools → Serial Plotter
  4. The plotter window will open and start drawing a live graph
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(50);
}

What the plotter shows

  • Horizontal axis: time (the graph moves from right to left)
  • Vertical axis: the sensor value (0 to 1023)
  • Line color: blue for the first value printed
  • Live updates: the graph updates continuously as new values arrive

Tips for using the Serial Plotter

  • Use Serial.println() with one value at a time for a single line graph
  • A smaller delay() (like 50ms) makes the graph update faster
  • You can plot multiple values by printing them with spaces instead of newlines
  • The Serial Plotter uses the same baud rate as the Serial Monitor (usually 9600)

Section 7: Using Functions with a Sensor

You just learned functions in Lesson 6, so now you can use them to keep sensor code organized.

const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  showSensorReading();
  delay(200);
}

void showSensorReading() {
  int sensorValue = analogRead(sensorPin);
  Serial.print("Sensor reading: ");
  Serial.println(sensorValue);
}
This version does the same job, but the function name showSensorReading() makes the program easier to read.

Section 8: Reading and Understanding the Numbers

The most important beginner skill here is learning to watch the numbers change and connect them to what the sensor is doing.

If you use a potentiometer

  • Turn the knob slowly
  • Watch the values rise and fall
  • Near one end, the value should be close to 0
  • Near the other end, the value should be close to 1023

If you use a light sensor later

  • Cover the sensor and uncover it
  • Compare darker and brighter readings
  • Notice that the values may not stay perfectly still
  • Real sensors often change a little bit all the time

Section 9: Common Beginner Mistakes

Mistake 1

Using a digital pin number like 2 when the sensor should be connected to an analog pin like A0.

Mistake 2

Forgetting to open the Serial Monitor after uploading the program.

Mistake 3

Printing values too quickly and making the output hard to read.

Mistake 4

Assuming a sensor value must always be exactly the same every time.

Hands-On Activity: Explore a Potentiometer

Build the potentiometer circuit and run a program that prints the value from A0. Then test what happens as you turn the knob.

  • Predict what values you might see before you start
  • Turn the knob to one end and record the reading
  • Turn it to the middle and record the reading
  • Turn it to the other end and record the reading

Extension: write a function that prints Low, Medium, or High depending on the sensor value.

Assessment and Homework

Lesson 7 quiz topics

  • What sensors do
  • Digital input vs analog input
  • How analogRead() works
  • Why readings often range from 0 to 1023
  • How to test a sensor with the Serial Monitor
  • How to use the Serial Plotter to visualize sensor values

Homework ideas

  • Practice explaining the difference between digital and analog input
  • Trace what each line in the sensor reading code does
  • Record several real potentiometer readings and compare them
  • Try the Serial Plotter with your potentiometer and describe what you see
  • Be ready to use sensor values in decisions next
Good programmers do not guess what a sensor is doing. They read the values, test the hardware, and observe the pattern.