UNIT III:
PROGRAMMING WITH ARDUINO UNO
Prepared and Presented by:
Juhi Kaneria
ARDUINO UNO BOARD
Arduino Uno Pin Diagram detail
SKETCH STRUCTURE
An Arduino program is called a sketch.
A sketch consists of two essential functions: setup() and loop().
The setup() function runs only once when the program starts. It is
used to initialize variables and configure pins.
The loop() function runs repeatedly over and over again. This is
where you write the code that controls the behavior of your
program.
DATA TYPES & BUILT-IN CONSTANTS
Data types define the type of data a variable can hold.
Common data types in Arduino include:
int: Stores integers (whole numbers)
float: Stores decimals
char: Stores single characters
boolean: Stores true or false values
byte: Similar to int but uses less memory (0 to 255)
Built-in constants represent fixed values, such as HIGH (5V) and
LOW (0V) for digital pins.
OPERATORS
Operators perform operations on data.
Arduino supports various operators, including:
Arithmetic operators: Perform mathematical calculations (+, -, *, /)
Bitwise operators: Manipulate bits within a variable
Compound assignment operators: Combine assignment and
operation (+=, -=, *=, /=)
Comparison operators: Compare values (==, !=, <, >, <=, >=)
Boolean operators: Perform logical operations (&&, ||, !)
CONTROL STATEMENTS AND LOOPS
Control statements dictate the flow of execution in your program.
Common control statements include:
if statements: Execute code conditionally based on a boolean
expression.
else if statements: Provide additional conditions to check after an if
statement.
else statements: Execute code if none of the previous conditions are
true.
switch statements: Allow for multi-way branching based on a
variable's value.
Loops allow for repeated execution of a code block.
USER-DEFINED FUNCTIONS IN ARDUINO
User-defined functions are created by the programmer.
They allow you to break down complex programs into smaller,
more manageable pieces.
User-defined functions can take arguments and return values.
LIBRARY FUNCTIONS
Library functions are pre-written functions that come with
Arduino.
They provide access to hardware or software features.
There are many different Arduino libraries available, each
providing a set of functions for a specific purpose.
I/O FUNCTIONS
digitalRead(): Reads the value of a digital pin (HIGH or LOW).
digitalWrite(): Writes a HIGH or LOW value to a digital pin.
pinMode(): Sets a pin as an input or output.
analogRead(): Reads the value of an analog pin (0-1023).
analogWrite(): Writes an analog value (PWM) to a pin (0-255).
analogReference(): Sets the reference voltage for analog reads
(DEFAULT, INTERNAL, or EXTERNAL).
CHAR FUNCTIONS
isAlpha(): Checks if a character is alphabetic (a-z, A-Z).
isAlphaNumeric(): Checks if a character is alphanumeric (a-z, A-Z,
0-9).
isDigit(): Checks if a character is a digit (0-9).
isHexadecimalDigit(): Checks if a character is a hexadecimal digit
(0-9, A-F, a-f).
isSpace(): Checks if a character is a space character.
isWhitespace(): Checks if a character is a whitespace character
(space, tab, newline).
isUpperCase(): Checks if a character is uppercase (A-Z).
isLowerCase(): Checks if a character is lowercase (a-z).
MATH FUNCTIONS
Common math functions found in many libraries
abs: Calculates the absolute value of a number
constrain: Constrains a number to a specified range
max: Returns the larger of two numbers
min: Returns the smaller of two numbers
pow: Raises a number to a power
sqrt: Calculates the square root of a number
LED BLINKING USING ARDUINO
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second (1000 milliseconds)
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second (1000 milliseconds)
}
CODE EXPLANATION:
const int ledPin = 13;: This line declares a constant variable called ledPin and
assigns the value 13 to it. This variable stores the digital pin number connected to
the LED.
void setup() { ... }: The setup() function is executed only once when the program
starts. Inside this function, we use the pinMode(ledPin, OUTPUT) statement to
configure the ledPin as an output pin. This tells the Arduino that we will be using
this pin to control an external device (the LED).
void loop() { ... }: The loop() function is the core of the program and runs continuously
after the setup() function finishes. Inside the loop(), we have two main lines:
digitalWrite(ledPin, HIGH);: This line turns on the LED connected to the ledPin. The
HIGH value represents a voltage level that is high enough to light up the LED.
delay(1000);: This line pauses the program execution for 1 second (1000
milliseconds). This creates the blinking effect by giving the LED time to turn on and
off.
digitalWrite(ledPin, LOW);: This line turns off the LED connected to the ledPin. The
LOW value represents a voltage level that is not enough to light up the LED.
UPLOADING THE CODE:
Connect your Arduino board to your computer using a USB cable.
Open the Arduino IDE (Integrated Development Environment)
software.
Copy and paste the code into the Arduino IDE window.
Select the appropriate Arduino board type and serial port from the
IDE's tools menu.
Click the upload button to upload the code to your Arduino board.
SERIAL COMMUNICATION
Serial communication allows devices to talk to each other
Serial communication functions are used to send and receive data over a
serial port
Serial: Initializes serial communication
available: Checks if data is available to be read
begin: Sets the baud rate for serial communication
end: Ends serial communication
print: Prints data to the serial monitor
println: Prints data to the serial monitor followed by a newline
write: Writes a single byte of data to the serial port
read: Reads a single byte of data from the serial port
readBytes: Reads a specified number of bytes from the serial port
readString: Reads a string of characters from the serial port until a
newline character is received