[ /ard/ - arduino & embedded systems ]

>>> BSc CompSci - U.Calicut <<<
Post #16

What Even Is a Sensor? Types & Classification

Definition

A sensor is a device that detects physical quantities (temperature, light, pressure, distance) and converts them into electrical signals that Arduino can read. Think of it as Arduino's way of "sensing" the real world.

Sensors are transducers - they convert energy from one form (physical phenomenon) to another (electrical signal).

Analog vs Digital Sensors - The Real Difference

Property Analog Sensors Digital Sensors
Output Signal Continuous voltage (0-5V range) Discrete values (HIGH/LOW, 0 or 1)
Data Transmission Requires ADC conversion Already digital, no conversion needed
Precision Can measure ranges (0-1023) Binary states only (ON/OFF)
Arduino Pins Analog pins (A0-A5) Digital pins (D0-D13)

Common Sensor Types

Analog Sensors

  • LDR (Light Sensor) - Measures light intensity as resistance
  • Temperature Sensors - LM35, TMP36 output voltage proportional to temperature
  • Potentiometer - Variable resistor for position sensing
  • Moisture/Humidity Sensors - Soil moisture, DHT11

Digital Sensors

  • Ultrasonic Sensors - HC-SR04 for distance measurement
  • IR Sensors - Infrared object detection
  • PIR Motion Sensors - Detect movement
  • Push Buttons - Simple binary input
Some sensors can work in BOTH modes! For example, an IR sensor can output analog values (distance/intensity) or digital (object detected: yes/no).
Post #17

ADC Explained & The Pull-Up/Pull-Down Conspiracy

What's an ADC?

The Analog-to-Digital Converter (ADC) is Arduino's built-in circuit that converts continuous analog voltages (0-5V) into digital values your code can understand.

Arduino Uno has a 10-bit ADC
2^10 = 1024 possible values
Range: 0 to 1023

0V     → 0
2.5V   → 511
5V     → 1023

Resolution = 5V / 1024 = 4.9mV per unit

Pull-Up Resistors

A pull-up resistor connects a digital input pin to VCC (5V) through a resistor (typically 10kΩ). This ensures the pin reads HIGH by default when nothing is connected.

Why Use Pull-Up?

Without it, floating pins pick up electrical noise and randomly fluctuate between HIGH and LOW. Pull-up resistors prevent this "floating" state.

// External Pull-Up Circuit
//        5V
//        |
//       10kΩ
//        |
//    Pin 2 -------- Button -------- GND
//
// Button NOT pressed: Pin reads HIGH
// Button pressed: Pin reads LOW

Pull-Down Resistors

A pull-down resistor connects the pin to GND through a resistor. Pin reads LOW by default.

// External Pull-Down Circuit
//        5V -------- Button -------- Pin 2
//                                      |
//                                    10kΩ
//                                      |
//                                     GND
//
// Button NOT pressed: Pin reads LOW
// Button pressed: Pin reads HIGH
Arduino has INTERNAL pull-up resistors (~20kΩ)! Enable with pinMode(pin, INPUT_PULLUP). No external resistor needed! But there's NO internal pull-down on most Arduino boards.

Arduino Code Examples

// Using Internal Pull-Up
pinMode(7, INPUT_PULLUP);
int state = digitalRead(7);
// Pressed = LOW, Not Pressed = HIGH

// Using External Pull-Down
pinMode(8, INPUT);
int state = digitalRead(8);
// Pressed = HIGH, Not Pressed = LOW

When to Use Which?

Scenario Use This
Digital buttons/switches Pull-up (internal or external)
I2C Communication External pull-up (4.7kΩ typical)
Floating analog pins Not typically used (use external circuit)
Save components Internal pull-up (no external resistor)
Post #18

Interfacing Sensors to Arduino Uno - The Field Guide

Temperature Sensor (LM35 / TMP36)

Wiring

LM35 Pinout (flat side facing you):
[VCC] [Vout] [GND]
  |      |      |
  5V    A0    GND

Code

int tempPin = A0;

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

void loop() {
  int reading = analogRead(tempPin);
  float voltage = reading * (5.0 / 1023.0);
  float tempC = voltage * 100; // LM35: 10mV per °C
  
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" °C");
  
  delay(1000);
}

Light Sensor (LDR)

Wiring (Voltage Divider)

     5V
      |
     LDR
      |
     A0 ----[10kΩ]---- GND

Code

int ldrPin = A0;

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

void loop() {
  int lightLevel = analogRead(ldrPin);
  
  Serial.print("Light Level: ");
  Serial.println(lightLevel);
  
  if (lightLevel < 300) {
    Serial.println("Dark");
  } else if (lightLevel < 700) {
    Serial.println("Dim");
  } else {
    Serial.println("Bright");
  }
  
  delay(500);
}

Ultrasonic Distance Sensor (HC-SR04)

Wiring

HC-SR04 Pins:
VCC  → 5V
TRIG → Pin 9
ECHO → Pin 10
GND  → GND

Code

const int trigPin = 9;
const int echoPin = 10;

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

void loop() {
  // Send pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read echo
  long duration = pulseIn(echoPin, HIGH);
  
  // Calculate distance (cm)
  float distance = duration * 0.034 / 2;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(500);
}

Humidity Sensor (DHT11)

Wiring

DHT11 Pins (left to right):
Pin 1 (VCC)  → 5V
Pin 2 (Data) → Pin 2 (with 10kΩ pull-up to 5V)
Pin 3 (NC)   → Not connected
Pin 4 (GND)  → GND

Code (requires DHT library)

#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
  float humidity = dht.readHumidity();
  float tempC = dht.readTemperature();
  
  if (isnan(humidity) || isnan(tempC)) {
    Serial.println("Failed to read from DHT");
    return;
  }
  
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print("%  Temperature: ");
  Serial.print(tempC);
  Serial.println("°C");
  
  delay(2000);
}
Always check sensor datasheets for voltage requirements! Some sensors are 3.3V, using 5V will fry them. Use level shifters when needed.
Post #19

Reading Sensor Data on Serial Monitor - Debug Like a Pro

What's the Serial Monitor?

The Serial Monitor is Arduino IDE's built-in terminal that lets you see data sent from your Arduino in real-time. Essential for debugging and monitoring sensor values.

Basic Serial Commands

// Initialize serial communication (in setup)
Serial.begin(9600); // 9600 baud rate

// Print without newline
Serial.print("Temperature: ");
Serial.print(temp);

// Print with newline
Serial.println(" °C");

// Print in different formats
Serial.print(value, DEC); // Decimal (default)
Serial.print(value, HEX); // Hexadecimal
Serial.print(value, BIN); // Binary
Serial.print(value, 2);   // 2 decimal places for floats

Complete Example - Multi-Sensor Monitoring

int tempPin = A0;
int lightPin = A1;
int buttonPin = 2;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.println("=== Arduino Sensor Monitor ===");
}

void loop() {
  // Read sensors
  int tempRaw = analogRead(tempPin);
  int lightRaw = analogRead(lightPin);
  int buttonState = digitalRead(buttonPin);
  
  // Convert temperature
  float voltage = tempRaw * (5.0 / 1023.0);
  float tempC = voltage * 100;
  
  // Format output
  Serial.println("====================");
  Serial.print("Temp: ");
  Serial.print(tempC, 1);
  Serial.print("°C | Light: ");
  Serial.print(lightRaw);
  Serial.print(" | Button: ");
  Serial.println(buttonState == LOW ? "PRESSED" : "RELEASED");
  
  delay(1000);
}

Advanced Serial Techniques

CSV Format (Easy to import to Excel)

void loop() {
  Serial.print(millis());
  Serial.print(",");
  Serial.print(temp);
  Serial.print(",");
  Serial.print(humidity);
  Serial.print(",");
  Serial.println(pressure);
  delay(1000);
}
// Output: 1000,25.3,65.2,1013.25

Reading Serial Input (Interactive)

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Type 'ON' or 'OFF':");
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim();
    
    if (command == "ON") {
      digitalWrite(LED_BUILTIN, HIGH);
      Serial.println("LED turned ON");
    } else if (command == "OFF") {
      digitalWrite(LED_BUILTIN, LOW);
      Serial.println("LED turned OFF");
    }
  }
}
Pro Tip: Match baud rates! Arduino's Serial.begin(9600) must match the Serial Monitor's dropdown setting (bottom-right corner of Arduino IDE).

Common Serial Monitor Shortcuts

Action Shortcut
Open Serial Monitor Ctrl + Shift + M
Clear Output Click "Clear output" button
Autoscroll Check "Autoscroll" box
Send Data Type in input box + Enter
Post #20

Introduction to Actuators - Making Things Move

What's an Actuator?

If sensors are Arduino's INPUT devices (reading the world), actuators are OUTPUT devices that make things happen in the physical world. They convert electrical signals into physical action.

Think of it this way: Sensors = Eyes & Ears | Actuators = Hands & Mouth

Common Types of Actuators

LEDs (Light Emitting Diodes)