Lesson 8: Sensor-Controlled Outputs with Arduino
Use sensor readings to control LEDs, buzzers, and other outputs
32-Lesson Course • Semester 1
Now that you can read sensor values with analogRead(), you'll learn to use those values to make decisions and control outputs like LEDs. This combines everything you've learned: conditionals, loops, functions, and sensors.
Learning Objectives
By the end of this Lesson, you will:
- Use sensor values in conditional statements
- Control LEDs based on sensor thresholds
- Write functions that respond to sensor data
- Create a complete sensor-controlled project
Skills Developed:
- Combining sensors with output devices
- Setting thresholds for sensor decisions
- Building responsive hardware systems
- Debugging sensor-controlled circuits
Section 1: From Input to Output
In Lesson 7, you learned to read sensor values. Now you'll use those values to control outputs. The pattern is simple: read → decide → act.
The control loop pattern
- Read the sensor with
analogRead() - Compare the value to a threshold (conditionals)
- Control the output accordingly
This pattern works for any sensor and any output. For example: read a light sensor, check if it's dark, turn on an LED.
Section 2: LED Controlled by Potentiometer
Let's start with a simple example: turn on an LED when the potentiometer reads above a certain value.
Hardware setup: potentiometer and LED
Potentiometer (from Lesson 7)
- Left pin →
5V - Middle pin →
A0 - Right pin →
GND
LED (with resistor)
- Long leg → Pin
13(through 220Ω resistor) - Short leg →
GND
const int sensorPin = A0;
const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor: ");
Serial.println(sensorValue);
if (sensorValue > 500) {
digitalWrite(ledPin, HIGH); // LED on
} else {
digitalWrite(ledPin, LOW); // LED off
}
delay(200);
} What happens?
- When
sensorValue > 500, the LED turns on - When
sensorValue <= 500, the LED turns off - The threshold (500) is roughly in the middle of the 0-1023 range
Section 3: Choosing a Threshold
The threshold value (like 500) is important. It determines when your output changes. Here's how to think about it:
Finding a good threshold
- Run the sensor reading program from Lesson 7
- Watch the Serial Monitor or Plotter
- Note the values when the condition is true/false
- Pick a value in between
Example thresholds
- Potentiometer:
512(middle) - Light sensor:
300(dim/dark) - Temperature: depends on your sensor
Section 4: Using Functions for Sensor Control
You can organize your code with functions (from Lesson 6). This makes it easier to read and maintain.
const int sensorPin = A0;
const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
checkSensorAndControlLED();
delay(200);
}
void checkSensorAndControlLED() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor: ");
Serial.println(sensorValue);
if (sensorValue > 500) {
turnOnLED();
} else {
turnOffLED();
}
}
void turnOnLED() {
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
}
void turnOffLED() {
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
} Section 5: Multiple Thresholds
You can use multiple thresholds to create different response levels. For example, show "Low", "Medium", or "High" based on the sensor value.
const int sensorPin = A0;
const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor: ");
Serial.print(sensorValue);
Serial.print(" - Level: ");
printSensorLevel(sensorValue);
delay(300);
}
void printSensorLevel(int value) {
if (value < 341) {
Serial.println("Low");
digitalWrite(ledPin, LOW);
} else if (value < 682) {
Serial.println("Medium");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("High");
digitalWrite(ledPin, HIGH);
}
} How it works
0-340: Low → LED off341-681: Medium → LED on682-1023: High → LED on- You can add more LEDs for visual feedback
Section 6: Looping with Sensors
You can combine sensor readings with loops (from Lesson 5). For example, take 5 readings and average them for a more stable value.
const int sensorPin = A0;
const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int averageValue = getAverageReading();
Serial.print("Average: ");
Serial.println(averageValue);
controlLED(averageValue);
delay(500);
}
int getAverageReading() {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += analogRead(sensorPin);
delay(50);
}
return sum / 5;
}
void controlLED(int value) {
if (value > 500) {
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
} Why average readings?
Sensor readings can be noisy. Taking multiple readings and averaging them gives a smoother, more stable value. This is a common technique in real-world projects.
Section 7: Common Mistakes
Mistake 1
Forgetting pinMode(ledPin, OUTPUT) in setup().
Mistake 2
Using the wrong pin number (e.g., A0 for LED output instead of a digital pin).
Mistake 3
Not using a resistor with the LED, which can damage the LED.
Mistake 4
Choosing a threshold that's too close to the edge of the range.
Hands-On Activity: Light-Activated LED
Create a system that turns on an LED when it's dark. You can use a potentiometer to simulate a light sensor, or if you have a photoresistor, use that instead.
Steps:
- Wire a potentiometer to
A0(or use a light sensor) - Wire an LED with resistor to pin
13 - Write a program that turns the LED on when the sensor reads below a threshold (dark)
- Test by covering/uncovering the sensor or turning the potentiometer
- Use functions to organize your code
Extension: Add a second LED and create three brightness levels: dim, medium, bright.
Sneak Peek: Variable Brightness
Arduino has special pins (marked with ~: 3, 5, 6, 9, 10, 11) that can dim an LED smoothly instead of just on/off. Using analogWrite(), a sensor can control brightness like a dimmer switch. We'll cover this in a future lesson!
Assessment and Homework
Lesson 8 quiz topics
- Using sensor values in conditionals
- Choosing thresholds for sensor decisions
- Controlling LEDs based on sensor input
- Writing functions for sensor control
- Using loops with sensor readings
Homework ideas
- Create a sensor-controlled LED system
- Experiment with different threshold values
- Write a function that returns a sensor "level" (low/medium/high)
- Try averaging multiple sensor readings
- Be ready to build a complete project next