🤖 Arduino Programming
Master microcontroller programming and embedded systems - from basic circuits to IoT and advanced sensors.
01 What is Arduino?
Introduction to the Arduino platform and its vast potential for makers.
02 Setup & Components
Install the IDE and understand basic hardware components like resistors and LEDs.
03 Digital I/O
Learn how to control digital pins and read inputs from buttons.
04 Analog I/O
Read analog voltages and use PWM to dim LEDs or control motors.
05 Serial Communication
Talk to your computer and debug your code using the Serial Monitor.
06 Sensors & Input
Interface with real-world sensors like ultrasonic, PIR, and LDR.
07 Libraries
Extend Arduino functionality by using pre-written community libraries.
08 Simple Projects
Build complete projects like traffic light systems and smart alarms.
📑 Table of Contents
What is Arduino?
Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's perfect for beginners to learn electronics and IoT, and powerful enough for advanced projects.
Why Arduino?
- Easy to Learn: Simple programming language and clear documentation
- Affordable: Low-cost boards and sensors
- Open Source: Community-driven with thousands of libraries
- Versatile: Can control lights, motors, sensors, and more
- IoT Ready: Connect to WiFi and the internet
Setup & Installation
Hardware Needed:
- Arduino Board (Uno, Mega, Nano, etc.)
- USB Cable (to connect to computer)
- Breadboard and jumper wires
- LEDs, resistors, sensors (for projects)
Software Installation:
- Download Arduino IDE from arduino.cc
- Install the IDE for your operating system
- Connect your Arduino board via USB
- Select board type: Tools → Board → Arduino Uno
- Select COM port: Tools → Port → COM3 (or your port)
Basic Syntax
void setup() {
// Code here runs once when Arduino starts
Serial.begin(9600); // Initialize serial communication
pinMode(13, OUTPUT); // Set pin 13 as output
}
void loop() {
// Code here runs repeatedly
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait 1 second
// Print to serial monitor
Serial.println("LED toggled");
}
Key Functions:
pinMode(pin, mode); // Set pin as INPUT or OUTPUT
digitalWrite(pin, value); // Set digital pin HIGH or LOW
digitalRead(pin); // Read digital pin (HIGH/LOW)
delay(milliseconds); // Wait for specified time
millis(); // Get milliseconds since startup
Digital I/O
Controlling an LED:
const int LED_PIN = 13;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // Turn on
delay(500);
digitalWrite(LED_PIN, LOW); // Turn off
delay(500);
}
Reading a Button:
const int BUTTON_PIN = 2;
const int LED_PIN = 13;
void setup() {
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int button_state = digitalRead(BUTTON_PIN);
if (button_state == HIGH) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
Analog I/O
Reading Analog Values:
const int SENSOR_PIN = A0; // Analog pin
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor_value = analogRead(SENSOR_PIN); // 0-1023
float voltage = sensor_value * (5.0 / 1023.0);
Serial.print("Raw value: ");
Serial.println(sensor_value);
Serial.print("Voltage: ");
Serial.println(voltage);
delay(500);
}
PWM (Pulse Width Modulation):
const int LED_PIN = 3; // Must be PWM pin (3, 5, 6, 9, 10, 11)
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(LED_PIN, brightness);
delay(10);
}
}
Sensors & Modules
Popular Sensors:
Temperature Sensor (DHT11)
Measures temperature and humidity
Ultrasonic Sensor
Measures distance using sound waves
Motion Sensor (PIR)
Detects movement
Light Sensor (LDR)
Measures light intensity
Communication
Serial Communication:
void setup() {
Serial.begin(9600); // 9600 baud rate
}
void loop() {
// Send data
Serial.print("Value: ");
Serial.println(42); // With newline
// Receive data
if (Serial.available() > 0) {
char data = Serial.read();
Serial.print("Received: ");
Serial.println(data);
}
delay(1000);
}
I2C Communication:
#include
void setup() {
Wire.begin(); // Join I2C bus
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(0x68); // Address
Wire.write(0x00); // Register
Wire.endTransmission();
delay(100);
}
Libraries
Installing Libraries:
- Open Arduino IDE
- Go to Sketch → Include Library → Manage Libraries
- Search for desired library
- Click Install
Popular Libraries:
Wire // I2C communication
SoftwareSerial // Software serial
Servo // Servo motor control
LiquidCrystal // LCD display
DHT // Temperature/humidity sensors
NewPing // Ultrasonic sensors
RF24 // Wireless communication
Troubleshooting
- Port not showing: Install drivers for your board, check USB cable
- Upload fails: Select correct board and COM port, check cable connection
- No serial output: Check baud rate matches (usually 9600), check Serial Monitor
- Incorrect readings: Verify wiring, check sensor specifications, add delay
- Arduino not responding: Reset board (press reset button), reupload code
Projects & Practice
Beginner Projects:
- Blinking LED
- Button-controlled LED
- Traffic Light System
- Temperature Monitor
- Simple Alarm System
Intermediate Projects:
- Smart Home Automation
- Weather Station
- Line-following Robot
- Obstacle Avoidance Robot
- Plant Watering System
Advanced Projects:
- IoT Smart House
- Quadcopter Control
- Wireless Sensor Network
- Industrial Automation System
- Environmental Monitoring System