Lesson 4 / 8
Module 2Lesson 4⏱ 50 min study

Analog Sensing, Pulse-Width Modulation (PWM) & Actuator Control

Deconstruct the 10-bit Analog-to-Digital Converter (ADC), build voltage divider sensor circuits, synthesize analog outputs via PWM, and drive DC motors with H-Bridge circuitry.

✦ Key Architectural Takeaways
  • •The ATmega328P incorporates a 10-bit successive approximation ADC that maps 0V to 5V inputs into discrete integer values from 0 to 1023
  • •Analog resistive sensors (photoresistors, thermistors) require a voltage divider circuit to convert changing resistance into readable changing voltage
  • •Microcontrollers cannot output true variable analog voltage; Pulse-Width Modulation (PWM) rapidly switches 5V on and off to simulate analog voltage via average duty cycle
  • •Inductive loads (motors, solenoids) draw massive current and produce reverse voltage spikes (back-EMF); they must be isolated using H-Bridge drivers and flyback diodes

## Continuous Measurements & Analog Synthesis

The physical universe is analog: light intensity, temperature, air pressure, and sound vary continuously across infinite gradients. Microcontrollers, however, are fundamentally binary.

To bridge this gap, microcontrollers utilize two core technologies:

  1. 1Analog-to-Digital Converters (ADC): Quantize continuous voltage into numerical values.
  2. 2Pulse-Width Modulation (PWM): Synthesize variable power output through high-frequency digital pulsing.
§

1. The 10-Bit ADC & Voltage Divider Physics

The Arduino Uno features 6 analog input pins (A0 to A5) multiplexed into a single 10-bit Successive Approximation ADC:

∑Mathematical Formulation & Principle
THEORETICAL MODEL
ext{ADC Resolution} = 2^{10} = 1024 discrete quantization steps (0 to 1023)
∑Mathematical Formulation & Principle
THEORETICAL MODEL
ext{Voltage Resolution} = rac{V_ref}{1024} = (5.0 V / 1024) approx 0.0048828 V (4.88 mV per step)
∑Mathematical Formulation & Principle
THEORETICAL MODEL
ext{Input Voltage } V_in = ADC Value imes left((5.0 V / 1023.0) ight)
TEXTSYSTEM SCHEMATIC
  VOLTAGE DIVIDER SENSOR CIRCUIT (For LDR Photoresistor / Thermistor):
  
          +5V VCC
             |
             +---------[ Variable Sensor: R_sensor (e.g., LDR) ]
             |
             +---------> Analog Pin A0 (Measures V_out)
             |
             +---------[ Fixed Reference Resistor: R_fixed (10k Ohm) ]
             |
            GND
  
  FORMULA: V_out = VCC * [ R_fixed / (R_sensor + R_fixed) ]
§

2. Pulse-Width Modulation (PWM) Demystified

Microcontroller pins can only output 0V or 5V. To control the brightness of an LED or the speed of a DC motor, PWM rapidly switches the digital pin between 0V and 5V at approximately 490 Hz or 980 Hz:

TEXTSYSTEM SCHEMATIC
  PWM DUTY CYCLES & EQUIVALENT ANALOG VOLTAGE:
  
  analogWrite(pin, 0)     [ 0% Duty Cycle ]:
  0V ---------------------------------------------------------------- (0.0 V)

  analogWrite(pin, 64)    [ 25% Duty Cycle ]:
  5V |--|      |--|      |--|      |--|
  0V |  |------|  |------|  |------|  |------------------------------ (1.25 V)

  analogWrite(pin, 128)   [ 50% Duty Cycle ]:
  5V |----|    |----|    |----|    |----|
  0V |    |----|    |----|    |----|    |---------------------------- (2.50 V)

  analogWrite(pin, 255)   [ 100% Duty Cycle ]:
  5V ================================================================ (5.0 V)
§

3. Actuator Control: The H-Bridge Architecture

DC motors draw hundreds of milliamperes of current—far exceeding the 40 mA limit of an Arduino pin. Furthermore, reversing a motor requires reversing the electrical polarity across its terminals.

An H-Bridge uses four electronic switches (transistors/MOSFETs) configured in an "H" shape:

TEXTSYSTEM SCHEMATIC
                     + V_Motor (12V Battery)
                                |
                   +------------+------------+
                   |                         |
              [ Switch S1 ]             [ Switch S2 ]
                   |                         |
                   +---------[ MOTOR ]-------+
                   |                         |
              [ Switch S3 ]             [ Switch S4 ]
                   |                         |
                   +------------+------------+
                                |
                               GND

  FORWARD ROTATION: Close S1 and S4 (Current flows Left-to-Right through motor)
  REVERSE ROTATION: Close S2 and S3 (Current flows Right-to-Left through motor)
  COAST / STOP:     Open all switches
  ACTIVE BRAKE:     Close S3 and S4 (Shorts motor terminals; back-EMF brakes rotor)

Complete Closed-Loop Sensor-to-Actuator Implementation

CPPSYSTEM SCHEMATIC
// =============================================================================
// CLOSED-LOOP SENSOR TO MOTOR SPEED CONTROLLER
// =============================================================================

const uint8_t SENSOR_PIN = A0;   // Potentiometer or Photoresistor
const uint8_t MOTOR_PWM = 9;     // Enables motor speed control (Timer1 PWM)
const uint8_t MOTOR_IN1 = 8;     // Direction Control 1
const uint8_t MOTOR_IN2 = 7;     // Direction Control 2

void setup() {
    pinMode(MOTOR_PWM, OUTPUT);
    pinMode(MOTOR_IN1, OUTPUT);
    pinMode(MOTOR_IN2, OUTPUT);

    // Set motor forward direction
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
}

void loop() {
    // 1. Read 10-bit analog voltage from sensor (0 to 1023)
    int sensorValue = analogRead(SENSOR_PIN);

    // 2. Map 10-bit ADC range down to 8-bit PWM range (0 to 255)
    int motorSpeed = map(sensorValue, 0, 1023, 0, 255);

    // 3. Write duty cycle to motor driver enable pin
    analogWrite(MOTOR_PWM, motorSpeed);

    delay(20); // 50 Hz control loop update rate
}