0% found this document useful (0 votes)
35 views11 pages

Arduino Programming Fundamentals

Uploaded by

Vishal Karande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views11 pages

Arduino Programming Fundamentals

Uploaded by

Vishal Karande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Arduino

Programming
Fundamentals
A Beginner's Guide to Coding with Arduino

This presentation provides a clear, instructional overview of the


core concepts, structure, and essential functions used in writing
programs (called "sketches") for Arduino microcontrollers.
The Structure of an Arduino Sketch
Arduino programming uses a simplified version of the widely-used C++ language. Every Arduino program is based on two mandatory functions: setup() and loop().
These two functions define the entire life cycle of your program, from initialization to continuous execution.

void setup() void loop()


This function runs only once when the Arduino board starts up or is reset. It After setup() completes, the loop() function runs continuously and
is dedicated to initialization tasks. repeatedly forever. This is the heart of your program.

• Set pin modes (INPUT or OUTPUT). • Contains the main logic and core functionality of the program.
• Initialize serial communication (for debugging or data transfer). • Reads inputs, processes data, and controls outputs.
• Configure libraries, sensors, and other hardware settings. • Allows the Arduino to react constantly to its environment.
C haracter Set, Variab les, and D ata Typ es

C haracter Set D ata Types


The characters you use in Arduino code are standard ASCII The data type specifies the kind of data the variable holds and how much memory it consumes:
characters. This includes the building blocks for all commands,
names, and literal values you write. Data Type Size Example Usage

• Letters: A-Z, a-z (Note: C++ is case-sensitive!)


int 2 bytes int x = 5; Whole Numbers (−32768
• Digits: 0-9 to 32767)
• Special Characters: +, -, *, /, =, (, ), {, }, ;, :, #, etc.
float 4 bytes float t = 23.7; Decimal numbers
Variables (floating point)

Variables are named locations in memory used to store data that char 1 byte char c = 'A'; Single character
can change during program execution. They must be declared
before use. boolean 1 byte bool flag = true; Logical True/False

Declaration Syntax: dataType variableName = String varies String name = Text data (a sequence of
initialValue; "Arduino"; characters)
Keywords and O p erators

Understanding keywords and operators is crucial for writing instructions the Arduino compiler can understand. Keywords are reserved, and operators define how values are manipulated.

Keywords (Reserved Words) O perators (Action Sym bols)


These words have special, defined meanings in the language and cannot be used as names for Operators perform calculations and comparisons on variables and values.
your variables or functions.
Type Operator Meaning Example
D ata Types
Arithmetic +, -, *, /, % Math operations c = a + b;
int, float, char, void
Assignment =, +=, -= Assigns or updates x += 2;
a value
Control Structures
Comparison ==, !=, <, > Tests relationship if (a == 5)
if, else, for, while, switch between values

Logical/Boolean &&, ||, ! Combines/negates if (a && b)


Constants & Qualifiers conditions

const, return, true, false, HIGH, LOW, INPUT, OUTPUT Manipulates


LBitwise ^ ~ << >> individual bits of y = 5 << 1
an integer.
Conditional Statements
Conditional statements give your program the ability to "make decisions." They execute specific blocks of code only if a given condition evaluates as true.

The if Statement The if-else Statement The if-else if-else Structure


Executes code only if the condition inside the Provides two paths: one executed if the
parentheses is satisfied. condition is true, and one executed if the Used to check multiple, sequential conditions,
condition is false. often used for setting different states or
if (temperature > 30) { // behaviors.
Turn on fan} if (lightLevel < 50) { // Turn
on lamp} else { // Turn off if (c1) { ... }else if (c2) {
lamp} ... }else { // default }
Loop Statements and Arrays
Loops are essential for tasks that need to be repeated many times, such as blinking an LED or reading a sensor continuously. Arrays allow you to group related data.

Loop statement Arrays


• for loop: Ideal when you know exactly how many times you want the code to run An array is a collection of variables of the same data type stored under a
(e.g., iterating through all pins 2 to 10). single name. Access to individual elements is done using an index,
• while loop: Runs as long as a specified condition remains true. Be careful of infinite which always starts at 0.

loops!
Example: int ledPins[] = {2, 3, 4, 5};
• do-while loop: Executes the block of code at least once before checking the
condition, ensuring initial execution. Here, ledPins[0] refers to the value 2.

for (int i = 0; i < 5; i++) { // code runs 5 times}


Functions and Pointers

Functions: The Building Blocks Pointers: Advanced Memory Access


A function is a reusable block of code that performs a single, specific task.
Using functions makes your code: A pointer is a variable that stores the memory address of another variable.
• More organized (Modular). While advanced, they are fundamental for:

• Easier to read and debug. • Efficient memory management.

• Reusable across different parts of the program. • Working with complex data structures.
• Passing large variables to functions without copying them.
Syntax: returnType functionName(dataType parameter1, ...)

Pointers rely on two key operators: The * (Dereference operator) is used to access the value at a memory address, and the & (Address-of operator) is used to get the memory address of a variable.
Essential Arduino Functions: D igital I/O
Digital Input/Output (I/O) functions are the basic commands for interacting with hardware components that operate in only two distinct states: ON (HIGH) or OFF (LOW).

pinMode(pin, mode)
Used in setup() to configure a specific digital pin as either an INPUT (to read sensors or buttons) or an OUTPUT (to control LEDs or motors).

digitalWrite(pin, state)
Sets the voltage of an OUTPUT pin to HIGH (5V) or LOW (0V). This is how you actively turn things on or off.

digitalRead(pin)
Checks the current state of an INPUT pin. It returns HIGH if the pin is at 5V, or LOW if the pin is at 0V.
Essential Arduino Functions: Analog I/O and Timing

Analog I/O allows interaction with components that have a range of values, not just two states. Timing functions are crucial for controlling when and how long actions occur.

Analog I/O Functions Timer Functions


delay(ms)
analogRead(pin)
Pauses the program for a specified duration in milliseconds (ms). WARNING: This is a
Reads the voltage on an Analog Input pin (A0-A5). Returns an integer value from 0 "blocking" function, meaning no other code runs while the delay is active.
(0V) to 1023 (5V), offering high resolution measurement.

millis()
analogWrite(pin, value) Returns the time, in milliseconds, since the program started. This is non-blocking and
preferred for complex timing tasks, allowing the program to continue executing other
Outputs a Pulse Width Modulation (PWM) signal on specific pins (marked with ~).
code.
This simulates analog output by setting a duty cycle from 0 (0%) to 255 (100%), used
for dimming LEDs or controlling motor speed.
micros()

Returns the time, in microseconds, since the program started. Used for highly precise,
very short timing requirements.
Math Functions: Numerical Utility
Arduino includes access to standard C++ math functions, which simplify common numerical tasks and calculations, ensuring your code remains concise and efficient.

abs() constrain() max() / min()


Returns the absolute (positive) value of a number, Limits a number to stay within a specified minimum Returns the larger (max) or smaller (min) of two input
discarding any negative sign. and maximum range. numbers.

sq() / pow() sqrt()


sq() returns the square (x*x). pow() returns a Calculates and returns the square root of a number.
number raised to an exponent.
Essential Arduino Functions: Serial Co m munication
Serial.begin(speed)
Initializes serial communication at the specified baud rate (bits per second). E.x. Serial.begin(9600);

Serial.print(data)
Sends data to the Serial Monitor without a new line. E.x. Serial.print("Hello");

Serial.println(data)
Sends data to the Serial Monitor with a new line. E.x. Serial.println("Hello");

Serial.available()
Returns the number of bytes available to read from the serial buffer. E.x. if (Serial.available() > 0) {...}

Serial.read()/Serial.write(data)
Reads the first byte of incoming serial data ex. char c = Serial.read(); /Sends binary data or bytes over serial ex. Serial.write(65);

You might also like