Arduino Programming — Digital and Analog I/O, Sensors, and Serial Communication Explained
Arduino is an open-source electronics platform based on easy-to-use hardware and software, designed for building interactive electronic projects with a microcontroller that reads inputs from sensors and controls outputs like motors and LEDs.
Why Arduino Matters
Arduino is the most popular entry point for embedded programming. Over 30 million Arduino boards have been sold worldwide. It powers millions of DIY projects — from robotic arms to weather stations to automated plant waterers. In industry, Arduino-compatible boards are used for rapid prototyping of IoT devices before moving to custom PCBs. Understanding Arduino teaches the fundamentals of microcontrollers: GPIO, ADC, PWM, and serial communication — skills that transfer directly to professional platforms like ESP32 and STM32.
Plain-Language Explanation
Think of a human body. Your eyes are sensors — they detect light. Your brain is the microcontroller — it processes input and decides what to do. Your hand is an actuator — it performs an action.
An Arduino works the same way. You connect a sensor (like a temperature sensor) to an input pin. The Arduino reads the voltage, converts it to a temperature value using code. Then it decides what to do — maybe turn on an LED (actuator) if the temperature is too high. All this happens in a loop that runs thousands of times per second.
graph TD
Computer[Computer
USB Programming] -->|Upload Code| Arduino[Arduino Board]
Arduino -->|Digital Read| Button[Push Button]
Arduino -->|Analog Read| Sensor[Temperature Sensor
LM35 / DHT11]
Arduino -->|Digital Write| LED[LED]
Arduino -->|PWM| Motor[Servo Motor]
Arduino -->|Serial| Monitor[Serial Monitor
Display Data]
style Arduino fill:#00979d,color:#fff
style Sensor fill:#3498db,color:#fff
style LED fill:#e74c3c,color:#fff
Board Overview
The Arduino Uno (most common model) features:
- 14 digital I/O pins (6 support PWM)
- 6 analog input pins (10-bit ADC, 0-1023)
- 16 MHz clock speed
- 32KB flash memory
- 2KB SRAM
LED Blink — The “Hello World” of Hardware
Components: 1x LED, 1x 220Ω resistor, 2x jumper wires.
Wiring: Connect LED anode (+) to pin 13. Connect LED cathode (-) through resistor to GND.
// Blink.ino — Blinks an LED every second
const int LED_PIN = 13; // Built-in LED on pin 13
void setup() {
pinMode(LED_PIN, OUTPUT); // Configure pin 13 as output
Serial.begin(9600); // Initialize serial communication
Serial.println("Blink started!");
}
void loop() {
digitalWrite(LED_PIN, HIGH); // Turn LED on (5V)
Serial.println("LED ON");
delay(1000); // Wait 1 second
digitalWrite(LED_PIN, LOW); // Turn LED off (0V)
Serial.println("LED OFF");
delay(1000); // Wait 1 second
}Expected Serial Monitor output:
Blink started!
LED ON
LED OFF
LED ON
LED OFF
...Temperature Sensor (LM35)
Components: 1x LM35 temperature sensor, breadboard, jumper wires.
Wiring:
- LM35 pin 1 (left) → 5V
- LM35 pin 2 (middle) → A0 (analog input)
- LM35 pin 3 (right) → GND
// Temperature.ino — Reads LM35 temperature sensor
const int SENSOR_PIN = A0; // Analog input pin
void setup() {
Serial.begin(9600);
Serial.println("Temperature Sensor Started");
}
void loop() {
int raw = analogRead(SENSOR_PIN); // Read 0-1023
float voltage = raw * (5.0 / 1023.0); // Convert to volts
float temperature = voltage * 100.0; // LM35: 10mV per °C
Serial.print("Temperature: ");
Serial.print(temperature, 1); // One decimal place
Serial.println(" °C");
if (temperature > 30.0) {
Serial.println("WARNING: High temperature!");
}
delay(2000); // Read every 2 seconds
}Expected Serial Monitor output:
Temperature Sensor Started
Temperature: 24.5 °C
Temperature: 24.7 °C
Temperature: 25.1 °C
Temperature: 31.2 °C
WARNING: High temperature!Analog vs Digital I/O
Digital: Two states — HIGH (5V) or LOW (0V). Used for buttons, LEDs, relays.
Analog Input: Reads 0-1023 (10-bit) representing 0-5V. Used for sensors with variable output (temperature, light, potentiometer).
PWM (Analog Output): Simulates analog output by rapidly switching HIGH/LOW. analogWrite(pin, value) where value is 0-255. Used for dimming LEDs, controlling servo motors.
// Fade.ino — Fades an LED using PWM
const int LED_PIN = 9; // PWM-capable pin (marked with ~)
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Fade in (brighter)
for (int brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(LED_PIN, brightness);
Serial.print("Brightness: ");
Serial.println(brightness);
delay(30);
}
// Fade out (dimmer)
for (int brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(LED_PIN, brightness);
delay(30);
}
}Common Mistakes
No current-limiting resistor with LED: Connecting an LED directly between a pin and ground without a resistor will destroy the LED (and possibly the pin) within seconds. Always use a 220Ω-330Ω resistor in series.
Wrong pin mode: Using
digitalWriteon a pin without setting it toOUTPUTfirst doesn’t work. Always callpinMode(pin, OUTPUT)insetup().Floating input pins: An unconnected digital input pin reads random values. Use
pinMode(pin, INPUT_PULLUP)to enable the internal pull-up resistor.Blocking delays:
delay()stops all execution. For time-sensitive projects (reading multiple sensors simultaneously), usemillis()instead.Ignoring power limits: Each Arduino pin can supply only 40mA max. The total across all pins is 200mA. Driving a motor directly from a pin will damage the board. Use a transistor or motor driver.
Practice Questions
What is the difference between digitalWrite and analogWrite?
digitalWritesets a pin to HIGH (5V) or LOW (0V).analogWriteuses PWM to simulate a voltage between 0-5V based on a 0-255 value.How does the analogRead function work? It reads the voltage on an analog pin and returns a 10-bit value (0-1023) representing 0-5V. 0 = 0V, 1023 = 5V.
What is the purpose of the setup function?
setup()runs once when the Arduino powers on or resets. Used to initialize pins, serial communication, and set initial state.Why use INPUT_PULLUP instead of INPUT?
INPUT_PULLUPenables the internal 20kΩ pull-up resistor, preventing a floating pin state when no button is pressed. The pin reads HIGH and goes LOW when the button connects to ground.What limits how many LEDs you can blink simultaneously? Each pin’s 40mA current limit and the board’s 200mA total limit. Also, the
delay()approach blocks other code — usemillis()for concurrent timing.
Mini Project
Build a temperature-triggered LED indicator:
Components: Arduino Uno, LM35 temperature sensor, red LED, green LED, 2x 220Ω resistors.
Wiring: LM35 to A0. Green LED to pin 9 (through resistor). Red LED to pin 10 (through resistor).
// TemperatureIndicator.ino
const int SENSOR_PIN = A0;
const int GREEN_LED = 9;
const int RED_LED = 10;
void setup() {
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
Serial.begin(9600);
Serial.println("Temperature Monitor Started");
}
void loop() {
int raw = analogRead(SENSOR_PIN);
float voltage = raw * (5.0 / 1023.0);
float temp = voltage * 100.0;
Serial.print(temp, 1);
Serial.println(" °C");
if (temp <= 28.0) {
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
Serial.println("Status: COMFORTABLE");
} else {
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
Serial.println("Status: HOT - Alert!");
}
delay(2000);
}Expected output:
Temperature Monitor Started
24.3 °C
Status: COMFORTABLE
29.1 °C
Status: HOT - Alert!Cross-References
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro