1 System design: thinking in blocks
Professional embedded code starts on paper, not in the IDE. Before writing a single line, draw a block diagram showing every input, output, and the logic that connects them.
Our smart fan has four blocks:
- Speed input — a potentiometer on
A0produces a raw value 0–1023. - Direction input — a push-button on
pin 2(INPUT_PULLUP) toggles forward / reverse. - Actuator — an L298N H-bridge drives a DC motor;
ENA → pin 6(PWM),IN1 → pin 4,IN2 → pin 5. - Status output — a green LED on
pin 13glows while the motor is running;Serialstreams speed and direction every 500 ms.
This separation matters: each block can be coded, tested, and debugged independently before you wire them together.
Why an H-bridge? A microcontroller pin can only source a few milliamps — far too little for a motor. The L298N is a power driver: it takes logic-level signals (IN1, IN2) and switches up to 2 A from a separate 12 V supply. Setting IN1 HIGH, IN2 LOW spins the motor one way; reversing them reverses it. ENA is the PWM speed pin — higher duty cycle = more average voltage = faster spin.