## 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:
- 1Analog-to-Digital Converters (ADC): Quantize continuous voltage into numerical values.
- 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:
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:
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:
+ 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
// =============================================================================
// 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
}