Lesson 9: Basic Sensors and Input Devices

Explore the world of sensors and digital/analog inputs

πŸ” From hardware to sensing the world around us

πŸ” Lesson 8 Recap: From Sensor Values to Actions

In Lesson 8, you learned the core pattern used in real Arduino projects: read β†’ decide β†’ act. Lesson 9 builds on that by adding new kinds of inputs (digital buttons and analog knobs) and combining them into a single program.

What you used in Lesson 8

You practiced reading sensor values with analogRead(), comparing them to thresholds, organizing code with functions, and even smoothing readings by taking an average.

Use Wokwi as your default workspace for these sensor and input experiments so you can debug readings and Serial Monitor output before switching to physical components.

πŸ“š Learning Objectives

By the end of this lesson, you will:

  • β€’ Understand different types of sensors and their applications
  • β€’ Use an ultrasonic distance sensor (HC-SR04) to measure distance
  • β€’ Implement button inputs with proper debouncing
  • β€’ Use a potentiometer as a live "threshold knob" (setpoint)

Key Concepts:

  • β€’ Time-of-flight distance measurement
  • β€’ Digital inputs with pull-ups and debouncing
  • β€’ Thresholds, levels, and smoothing (averaging)
  • β€’ Mapping analog values to useful ranges (setpoints)

🌑️ Section 1: Introduction to Sensors

What are Sensors?

Sensors are devices that detect and measure physical properties from the environment and convert them into electrical signals that Arduino can understand. They are the "eyes and ears" of your Arduino projects.

Sensor Categories:

Digital Sensors:
  • β€’ Output HIGH (1) or LOW (0)
  • β€’ Examples: buttons, switches, motion detectors
  • β€’ Simple on/off or true/false readings
Analog Sensors:
  • β€’ Output variable voltage (0-5V)
  • β€’ Examples: temperature, light, pressure sensors
  • β€’ Provide continuous range of values

οΏ½ Section 2: Ultrasonic Distance Sensor (HC-SR04)

How it works

The HC-SR04 measures distance by sending an ultrasonic "ping" and timing how long it takes for the echo to return. Arduino measures that time and converts it into distance.

πŸ”Œ HC-SR04 Wiring

  • VCC β†’ 5V
  • GND β†’ GND
  • TRIG β†’ pin 9
  • ECHO β†’ pin 10

⚠️ Note

If you get unstable readings, that’s normal. We’ll apply Lesson 8 techniques (thresholds and averaging) in the project section.

// HC-SR04 distance reading example
const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

float getDistanceCm() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long durationUs = pulseIn(echoPin, HIGH, 30000);
  if (durationUs == 0) return -1;
  return durationUs * 0.0343f / 2.0f;
}

void loop() {
  float d = getDistanceCm();
  if (d < 0) {
    Serial.println("No echo (out of range)");
  } else {
    Serial.print("Distance: ");
    Serial.print(d);
    Serial.println(" cm");
  }
  delay(100);
}

πŸ”˜ Section 3: Digital Input Devices

Buttons and Switches

Buttons and switches let the user control your program. In the Lesson 9 project, the button will be used to toggle modes (like enabling/disabling an alarm).

πŸ”Œ Button Wiring

External Push Button Wiring:
  • Connect one leg of button to digital pin 2
  • Connect other leg of button to GND
  • No external resistor needed - code uses INPUT_PULLUP
  • The built-in button on the expansion board is connected to reset, not pin 2
  • You'll need an external push button for this exercise

πŸ“Š Section 4: Analog Input Devices

Potentiometers

Potentiometers are variable resistors that provide analog input, perfect for controlling brightness, volume, or speed.

πŸ”Œ Potentiometer Wiring Options

Option 1: Use Built-in Potentiometer on Expansion Board
  • The expansion board has a built-in potentiometer connected to analog pin A0
  • Turn the knob to test the code - no additional wiring needed!
Option 2: External Potentiometer
  • Connect left pin to GND
  • Connect middle pin (wiper) to analog pin A0
  • Connect right pin to 5V
  • Note: Pin order may vary by potentiometer model
// Potentiometer Reading Example
const int potPin = A0;      // Potentiometer connected to analog pin A0
const int ledPin = 9;       // PWM LED connected to pin 9

int potValue = 0;           // Variable to store pot reading
int ledBrightness = 0;      // Variable to store LED brightness

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Potentiometer Control - Turn pot to adjust LED brightness");
}

void loop() {
  potValue = analogRead(potPin);
  ledBrightness = map(potValue, 0, 1023, 0, 255);
  analogWrite(ledPin, ledBrightness);
  
  Serial.print("Pot Value: ");
  Serial.print(potValue);
  Serial.print(" | LED Brightness: ");
  Serial.println(ledBrightness);
  
  delay(100);
}

πŸ› οΈ Hands-On Activity: Multi-Sensor Dashboard

Project: Interactive Sensor Monitor

Create a sensor monitoring system that reads multiple input devices and provides real-time feedback through the Serial Monitor.

Activity Requirements

  1. Read HC-SR04 distance and display it in cm
  2. Use the potentiometer as a live setpoint (minimum safe distance)
  3. Use the button to toggle an "alarm enabled" mode (debounced)
  4. Apply the Lesson 8 pattern (read β†’ decide β†’ act) using thresholds and levels
  5. Optional: average the last N distance readings to smooth noise
  6. Create a user-friendly Serial Monitor dashboard

πŸ“ Assessment & Homework

Quick Check Questions

  1. What is the difference between digital and analog sensors?
  2. Why do we need pull-up resistors for button inputs?
  3. What is button debouncing and why is it important?
  4. How does a potentiometer work as an analog input device?
  5. What are the benefits of filtering sensor data?