Overview
This lecture provides a beginner-friendly introduction to Arduino, covering its hardware and software, programming basics, input/output handling, variables, functions, and building simple interactive LED projects.
Introduction to Arduino
- Arduino is an open-source hardware and software platform for building electronic projects.
- The Arduino board is a circuit board with a microcontroller (mainly Atmega328p) as its core.
- Arduino IDE is the official free software for writing, compiling, and uploading code to Arduino boards.
Arduino Board and Pins
- Microcontroller acts as the brain, running code and controlling hardware via pins.
- Pins are grouped into digital pins, analog input pins, and power pins.
- Digital pins (13 on Uno) act as on/off switches (0V for off, 5V for on).
- Analog pins read varying voltage values (0-5V), useful for sensors.
- PWM pins (marked with ~) simulate analog output using digital signals.
Powering Arduino
- Arduino can be powered via USB or an external power supply (recommended 12V via DC Barrel Jack).
Setting up Arduino IDE
- Download and install Arduino IDE 1.8.
- Adjust preferences for readability (font size, line numbers).
- Select appropriate board and COM port before uploading code.
Basic Arduino Programming Structure
- An Arduino program (sketch) uses C/C++ and consists of setup() and loop() functions.
- setup() runs once; loop() runs repeatedly.
- Functions must be correctly named (case-sensitive) and use curly brackets for their body.
- Comments (// for single-line, /* */ for multi-line) help document code.
Controlling LEDs and Using Functions
- pinMode() configures pins as INPUT or OUTPUT.
- digitalWrite(pin, HIGH/LOW) sends digital signals to pins.
- delay(ms) pauses program execution.
- Use functions to organize repetitive code, making programs clearer and easier to manage.
Serial Communication and Monitoring
- Serial Monitor allows real-time data viewing and interaction between Arduino and computer.
- serial.begin(9600) starts communication; serial.println() sends data to the monitor.
Variables and Data Types
- Variables are containers for data, defined by data type and name (e.g., int, float, boolean).
- Constants (const keyword) store unchangeable values, like pin numbers.
- Variable scope determines where a variable can be accessed (local or global).
- Main data types: boolean (true/false), byte (0-255), int (whole numbers), long (large numbers), float/double (decimals), char (single characters).
- Use the correct data type to avoid errors like garbage values.
Operators in Arduino
- Arithmetic operators: +, -, *, /, % for basic calculations.
- Increment (++) and decrement (--) operators adjust variable values.
- Relational operators (>, <, >=, <=, ==, !=) compare values and return true/false.*
Control Structures: Loops and If Statements
- for loops repeat code a specific number of times with initialization, condition, and increment.
- if and if-else statements make decisions based on conditions.
Working with Digital and Analog Inputs/Outputs
- digitalRead(pin) reads if a pin is HIGH or LOW (e.g., button presses).
- analogRead(pin) reads analog values (0-1023) from sensors.
- analogWrite(pin, value) uses PWM to control outputs like LED brightness (value 0-255).
- Use map() to scale analog readings to PWM range.
Sample Projects
- Blink an LED using digitalWrite and delay.
- Create LED patterns and chasers using functions and loops.
- Use PWM and potentiometer to control LED brightness smoothly.
- Read button states and control LEDs using if statements.
- Use the Serial Monitor to display sensor values and debug.
Key Terms & Definitions
- Microcontroller — A small computer on a single chip that runs Arduino code.
- IDE (Integrated Development Environment) — Software to write and upload code to Arduino.
- Digital Pin — Pin handling on/off (0/5V) signals.
- Analog Pin — Pin reading continuous voltage values (0-5V).
- PWM (Pulse Width Modulation) — Technique for simulating analog output using digital pins.
- Serial Monitor — Tool for exchanging data between Arduino and computer.
- Variable — Named storage for data.
- Constant — Read-only variable whose value cannot change.
- Scope — Part of code where a variable is accessible.
- Function — A reusable block of code with a specific task.
- For Loop — Control structure for repeating code a set number of times.
- If Statement — Control structure for decision making.
Action Items / Next Steps
- Download and install Arduino IDE 1.8.
- Set up your Arduino board and connect via USB.
- Complete example projects: LED blink, LED chaser, potentiometer-controlled LED.
- Explore Serial Monitor and variables in code.
- Practice writing and modifying basic sketches using loops, conditionals, and functions.