Lesson 9: Basic Sensors and Input Devices

Explore the world of sensors and digital/analog inputs

🔍 From hardware to sensing the world around us

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
  • Learn to read digital and analog sensor values
  • Implement button inputs with proper debouncing
  • Work with potentiometers and variable resistors

Key Concepts:

  • Digital vs analog sensors
  • Pull-up and pull-down resistors
  • Signal conditioning and filtering
  • Sensor calibration and mapping

🌡️ 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: Digital Input Devices

Buttons and Switches

Buttons and switches are the simplest digital input devices. They provide a direct way for users to interact with your Arduino projects.

🔌 Button Wiring Options

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
// Basic Button Reading Example
const int buttonPin = 2;    // Button connected to pin 2
const int ledPin = 13;      // Built-in LED
int buttonState = 0;        // Variable to store button state

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Button Test - Press button to toggle LED");
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);   // Turn LED on
    Serial.println("Button pressed - LED ON");
  } else {
    digitalWrite(ledPin, LOW);    // Turn LED off
    Serial.println("Button released - LED OFF");
  }
  delay(50);  // Small delay for stability
}

📊 Section 3: 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. Implement button input with proper debouncing
  2. Read potentiometer values and convert to percentages
  3. Simulate light sensor readings
  4. Create a user-friendly Serial Monitor dashboard
  5. Add interactive commands for settings

📝 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?