Arduino_Programming_Notes1
Arduino_Programming_Notes1
Settings Configuration
1. Serial Port Setup
- Communication via USB-Serial adapter
- Requires proper driver installation
- Found under Tools -> Serial Port
2. Board Selection
- Must select correct board type
- Located under Tools -> Board menu
Core Concepts
INPUT vs OUTPUT
1. Input
- Signals/information entering the board - Examples:
- Buttons
- Switches
- Light Sensors
- Flex Sensors
- Humidity Sensors
- Temperature Sensors
2. Output
- Signals exiting the board - Examples:
- LEDs
- DC motors
- Servo motors
- Piezo buzzers
- Relays
- RGB LEDs
Analog vs Digital
- Microcontrollers are digital devices (ON/OFF, discrete)
- Digital: Binary states (0V or 5V)
- Analog: Can have full range of values
Programming Fundamentals
Digital I/O Operations
1. Pin Mode Configuration pinMode(pin, mode); // Sets pin to INPUT
or OUTPUT
2. Digital Operations digitalRead(pin); // Reads HIGH or LOW
digitalWrite(pin, value); // Writes HIGH or LOW
3. Electronic Specifications
- Output pins: 40mA current capacity
- Input pins with HIGH: 20KOhm pull-up installed
Timing Functions
- delay(ms): Pauses for milliseconds
- delayMicroseconds(us): Pauses for microseconds
Program Structure
1. Basic Sections: void setup() {
// Setup motors, sensors, etc.
}
void loop() {
// Get information from sensors
// Send commands to motors
}
2. Setup Section
- Used for I/O assignments
- Device configuration (OUTPUT/INPUT) - Uses pinMode
command pinMode(9, OUTPUT); // Example
3. Loop Section void loop() {
digitalWrite(9, HIGH);
delay(1000); digitalWrite(9,
LOW); delay(1000);
}
Variables
- Act as temporary storage ("buckets") - Declaration Format: int val = 5;
// Type, name, assignment, value
Variable Types
- 8-bit: byte, char
- 16-bit: int, unsigned int
- 32-bit: long, unsigned long, float
Variable Scope
- Global
- Function-level
Comments
// Single line comment
/* Multi-line comment
block */
Programming Concepts
Digital Input
- Uses pins 0-13 (0 & 1 also used for programming) - Configuration:
pinMode(pinNumber, INPUT); // Setup int buttonState =
digitalRead(pinNumber); // Reading
Digital Sensors
- Binary states only (ON/OFF)
- HIGH ~ 5V (Uno)
- LOW = 0V
Conditional Statements
void loop() { int buttonState = digitalRead(5);
if (buttonState == LOW) {
// action for LOW
} else {
// action for HIGH
}}
Boolean Operators
- == : Equal to
- != : Not equal to
- > : Greater than
- >= : Greater than or equal
- < : Less than
- <= : Less than or equal
Example Projects
Project 1: Basic Blink
Pseudocode Flow:
1. Turn LED ON
2. Wait
3. Turn LED OFF
4. Wait
5. Repeat