Past exam question: Assignment 3, Q10
Open in the Padhaiko appExplain Atmega328 application with interfacing example.
Solution
Key Applications of ATmega328 in Instrumentation: Because it integrates a 10-bit ADC, hardware timers/PWM, USART/I²C/SPI buses, and 23 GPIO pins in a single low-cost 28-pin IC, the ATmega328 is widely used in:
- Sensor Data Acquisition & Telemetry: Reading analog transducers (temperature, humidity, light, pressure) and transmitting readings over UART/I²C/SPI.
- Closed-Loop Motor & Heater Control: Generating PWM waveforms to regulate DC motor speed, servo position, or heating power.
- Industrial & Smart Home Automation: Relay switching, keypad/LCD user interfaces, and smart energy monitoring.
- Portable Battery-Operated Loggers: Leveraging low-power sleep modes for periodic environmental sampling.
Interfacing Example: Automated Temperature-Controlled Fan (LM35 + DC Fan via Transistor):
- Sensor Input (
PC0 / ADC0): An LM35 temperature sensor () outputs an analog voltage to analog channelA0(PC0). - Actuator Output (
PD3 / OC2BPWM): Digital PWM pin3(PD3) drives the base of an NPN switching transistor (e.g., 2N2222 / TIP122) through a resistor to control a 12 V DC fan (with a 1N4007 flyback diode across the motor). - Status LED (
PB5): Digital pin13(PB5) lights an over-temperature warning LED when .
+5V
|
+-------+-------+
| 1:Vcc | +12V
[LM35] | |
| 2:Vout -----> PC0 (ADC0) [DC Fan]===+ (1N4007 Flyback Diode)
| 3:GND | | |
GND ATmega328P +-------+
| | Collector
PD3 (OC2B) --[1k]--|(B) NPN Transistor (TIP120/2N2222)
| | Emitter
PB5 (D13) --[330]--|>|--GNDInterfacing Program (C / Arduino Sketch): The 10-bit ADC maps to :
const int TEMP_PIN = A0; // PC0 (ADC0)
const int FAN_PWM = 3; // PD3 (OC2B)
const int LED_PIN = 13; // PB5
void setup() {
pinMode(FAN_PWM, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int raw = analogRead(TEMP_PIN);
float tempC = (raw * 5.0 / 1024.0) * 100.0;
if (tempC < 25.0) {
analogWrite(FAN_PWM, 0); // Fan OFF
digitalWrite(LED_PIN, LOW);
} else if (tempC < 35.0) {
analogWrite(FAN_PWM, 140); // Medium speed (~55% duty)
digitalWrite(LED_PIN, LOW);
} else {
analogWrite(FAN_PWM, 255); // Full speed (100% duty)
digitalWrite(LED_PIN, HIGH); // High-temperature alert
}
Serial.println(tempC);
delay(500);
}The ATmega328 is widely applied in sensor telemetry, closed-loop control, and automation. In a typical temperature-controlled fan interface, an LM35 analog sensor connects to ADC0 (PC0), the 10-bit ADC computes temperature , and PWM pin PD3 (OC2B) modulates a transistor driver (with flyback diode) to regulate fan speed while PB5 signals over-temperature.