Arduino
Programming
Level-Beginner
PARTICLE AI
SPACE FOR INNOVATION
HANDS ON LEARNING WITH INTEGRATED
HARDWARE & SOFTWARE TOOLS
Index
1. Human vs. Machine Analogy
Explanation of the analogy
Components:
Brain (Microcontroller)
Senses (Sensors)
Muscles (Actuators)
2. Introduction to Physical Computing
Definition and purpose
Components:
Sensors
Microcontrollers
Actuators
3. Components of an Interactive System
Explanation of the three main elements:
Sensors
Microcontroller
Actuators
Real-world example: Automatic door system
4. Arduino Basics
Overview of Arduino as a microcontroller
Pin description:
Power supply pins
Digital pins
Analog pins
5. Arduino IDE and Basic Programming
Introduction to Arduino IDE
Main sections:
setup()
loop()
Basic instructions:
pinMode()
digitalWrite()
analogWrite()
delay()
6. Hands-on Activities with LEDs
Activity 1: Turning on an LED
Activity 2: Blinking an LED
Expanding to more LEDs (e.g., controlling 2, 3, or 13 LEDs)
7. Using Analog Output for LED Control
Introduction to analogWrite()
Activity 1: LED Brightness Control
Activity 2: Multiple LEDs Brightness Control
8.RGB LED Control
Introduction to RGB LEDs and color mixing
Activity 1: Controlling RGB LED with digitalWrite()
Activity 2: Color Mixing with digitalWrite()
Activity 3: Analog Color Mixing with analogWrite()
9. Introduction to Serial Monitor
Purpose of Serial Monitor in Arduino
How to open and use the Serial Monitor
Serial Communication Basics
Setting up Serial communication with Serial.begin()
Printing data using Serial.print() and Serial.println()
10.Variables and Data Types
Declaring variables
Data types:
int
float
char
String
bool
Using Serial Monitor to Display Variables
Printing variable values
Formatting output for clarity
11. Introduction to Arithmetic Operators
Overview of arithmetic operations:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
Using Arithmetic Operators in Arduino
Performing calculations with variables
Storing results in separate variables
Example Code Implementation
Full code for performing arithmetic operations
Explanation of each part of the code
Expected Output in Serial Monitor
Example output from the Serial Monitor
Interpretation of results
1.Human vs Machine
Analogy
The analogy between humans and machines helps simplify how physical
computing systems work. Both involve sensing, processing, and reacting to their
surroundings, but in different forms.
Human Body as a System:
1. Brain (Microcontroller):
Just as the brain interprets and processes signals from the body’s senses,
the microcontroller processes data from sensors. It decides what action to
take, just like the brain decides how to react to stimuli.
2. Senses (Sensors):
Our senses, like eyes, ears, and skin, detect information from the
environment (light, sound, touch). In machines, sensors such as
temperature detectors, light sensors, and motion detectors perform the
same task—gathering information to be processed.
3. Muscles (Actuators):
The brain sends signals to muscles to perform actions, like moving a hand
or turning the head. Similarly, a microcontroller sends signals to actuators
(motors, LEDs, or speakers) to perform tasks, like moving a robot arm or
lighting up a display.
Conclusion:
This analogy illustrates how machines can "sense," "think," and "act" similarly to
living organisms. By understanding the parallels, we can better grasp the role of
physical computing systems in everyday applications.
2.Introduction to Physical
Computing
Physical computing refers to building systems that interact with the real world by
combining hardware and software. It allows machines to sense and respond to
their environment, creating interactive experiences.
Components of Physical Computing:
1. Sensors:
Sensors are devices that detect changes in the physical environment, such
as light, temperature, or distance. They collect real-time data and send it to
the microcontroller.
Examples:
Light sensor (detects brightness)
Temperature sensor (measures heat)
Ultrasonic sensor (measures distance)
2. Microcontrollers:
A microcontroller acts as the brain of the system, processing data from
sensors. Based on this data, it makes decisions and sends instructions to
actuators. It runs a program that controls how the system behaves.
Examples: Arduino, Raspberry Pi Pico
3. Actuators:
Actuators are devices that perform actions, such as moving a motor,
turning on an LED, or playing a sound, based on the microcontroller’s
instructions.
Examples:
Motors (for movement)
LEDs (for visual output)
Speakers (for sound output)
In Summary: Physical computing brings together sensors, microcontrollers, and
actuators to create systems that interact with the world, allowing machines to
observe, process, and act in real time.
3.Components of an
Interactive System
Interactive systems are designed to respond to inputs from the environment, making
them dynamic and responsive. These systems rely on three main components that
work together seamlessly:
1. Sensors:
Function: Sensors capture data from the environment and provide input to the
system.
Examples:
Motion Sensor: Detects movement in a given area.
Temperature Sensor: Measures the ambient temperature.
Light Sensor: Monitors light levels to determine brightness.
2. Microcontroller:
Function: The microcontroller acts as the brain of the system, processing the data
received from sensors. It analyzes this input and makes decisions based on pre-
programmed logic.
Examples:
Arduino: A popular microcontroller used for a variety of projects.
Raspberry Pi: A more powerful option that can handle complex tasks.
3. Actuators:
Function: Actuators are responsible for carrying out the physical tasks based on
the decisions made by the microcontroller. They perform actions that affect the
environment.
Examples:
Motor: Moves parts of the system, such as opening a door.
LED: Provides visual feedback by lighting up.
Buzzer: Produces sound as a notification or alert.
Example: Automatic Door System
1. Sensor: A motion sensor detects when someone approaches the door (input).
2. Microcontroller: The microcontroller receives the signal from the motion sensor
and determines whether to open the door.
3. Actuator: A motor moves the door open based on the microcontroller's command
(output).
This simple interaction illustrates how the three components work together to create
a responsive system that reacts to user input in real time.
4.Arduino Basics
What is Arduino? Arduino is an open-source electronics platform that combines
hardware and software to create interactive projects. It consists of a
microcontroller, which is a small computer on a single chip, and an Integrated
Development Environment (IDE) for programming. Arduino is widely used in
education, prototyping, and hobbyist projects due to its simplicity, accessibility,
and extensive community support.
Why Use Arduino?
Ease of Use: Arduino provides a user-friendly interface, making it accessible
for beginners while still being powerful enough for experienced developers.
Versatility: It can be used in a wide range of applications, from simple LED
projects to complex robotics and IoT devices.
Community Support: A large community provides extensive resources,
tutorials, and libraries, facilitating learning and troubleshooting.
Role of the Microcontroller The microcontroller is the heart of the Arduino board.
It processes inputs from sensors, executes programmed instructions, and controls
outputs to actuators. The microcontroller interprets the code written in the
Arduino IDE and translates it into actions, making it essential for creating
interactive electronic systems.
Here are the key specifications of a typical Arduino board, specifically the Arduino
Uno, which is one of the most popular models:
Arduino Uno Specifications
1. Microcontroller:
ATmega328P
2. Operating Voltage:
5V
3. Input Voltage (recommended):
7-12V
4. Digital I/O Pins:
14 (of which 6 can be used as PWMor Analog outputs)
5. Analog Input Pins:
6 (A0 to A5)
6. DC Current per I/O Pin:
20 mA (maximum)
7. DC Current for 3.3V Pin:
50 mA (maximum)
8. Flash Memory:
32 KB (ATmega328P) of which 0.5 KB is used for the bootloader
9. SRAM:
2 KB (ATmega328P)
10. EEPROM:
1 KB (ATmega328P)
11. Clock Speed:
16 MHz
12. LED_BUILTIN:
Pin 13
13. Dimensions:
68.6 mm x 53.4 mm (2.7 in x 2.1 in)
14. Weight:
Approximately 25 g
15. USB Connection:
Standard USB-B connector for programming and power.
Additional Features
Programming Interface:
The board can be programmed via the Arduino IDE using a USB cable.
Power Supply Options:
Power can be supplied through the USB connection or via an external
power supply connected to the barrel jack.
Communication Interfaces:
UART (Serial), I2C, SPI for communication with other devices.
Shield Compatibility:
The Arduino Uno is compatible with a variety of shields that can be stacked
on top of it to expand its functionality (e.g., motor control, Wi-Fi, etc.).
Pin Description:
1. Power Supply Pins:
5V: This pin provides power to most components connected to the
Arduino, such as sensors and actuators.
GND: Ground pin connects all circuits and serves as the reference point for
the voltage levels in the system.
Vin: This pin receives input voltage from an external power source (like
batteries or adapters). It typically accepts voltages between 7V and 12V.
1. Digital Pins (0-13):
These pins can be configured as either input or output.
Input: Read digital signals (HIGH/LOW) from sensors (e.g., buttons).
Output: Send digital signals to actuators (e.g., turning an LED on/off).
Digital Pins 0 and 1:
These pins are primarily used for serial communication (TX and RX). They
send and receive data to and from other devices (e.g.,Bluetooth).
3.Analog Pins:
Analog Output Pins (3, 5, 6, 9, 10, 11): These pins can generate PWM (Pulse
Width Modulation) signals, allowing for analog-like control of devices (e.g.,
dimming an LED).
Analog Input Pins (A0-A5): These pins read analog signals from sensors,
providing a range of values rather than just HIGH or LOW (e.g., reading the
voltage from a temperature sensor).
4.Reset Pin:
The reset pin is used to reset the Arduino board. By connecting this pin to
ground, you can restart the program running on the microcontroller, which is
useful for troubleshooting or restarting projects.
Summary
Understanding the pin configuration of the Arduino board is crucial for effectively
designing and implementing physical computing projects. With the right
connections, you can control a wide range of components and create interactive
systems that respond to the environment.
5.Arduino IDE and Basic
Programming
The Arduino IDE (Integrated Development Environment) is the software platform
used to write, compile, and upload programs (called sketches) to the Arduino
board. It provides an easy-to-use interface for developing projects in physical
computing.
Main Sections of an Arduino Sketch:
1. setup():
This function runs once when the program starts. It’s where you initialize
settings, such as configuring pin modes or starting communication with
sensors.
Example:
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as an output
}
2. loop():
The code in this section runs continuously, allowing the Arduino to respond to
inputs and perform actions over time. This is where the main functionality of
your program resides.
Example:
void loop()
{
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(LED_BUILTIN, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
Basic Instructions:
pinMode(pin, INPUT/OUTPUT):
This function defines the behavior of a pin. You specify whether a pin will
receive inputs (from sensors) or send outputs (to actuators).
Example:
pinMode(2, INPUT); // Set pin 2 as an input
digitalWrite(pin, HIGH/LOW):
This command sends a digital signal to an output pin. HIGH turns the pin
on, while LOW turns it off.
Example:
digitalWrite(13, HIGH); // Turn on the LED connected to pin 13
analogWrite(pin, value):
This function generates a PWM signal on the specified pin, allowing for
analog-like control of devices. The value can range from 0 (off) to 255 (fully
on).
Example:
analogWrite(9, 128); // Set pin 9 to half brightness
delay(time):
This pauses the execution of the program for a specified number of
milliseconds. It’s useful for timing events in your sketch.
Example:
delay(500); // Wait for half a second
6.Hands-on Activities with
LEDs
These hands-on activities help students understand basic programming concepts
using LEDs, which are great for visual feedback and learning about digital outputs.
Activity 1: Turning on an LED
Objective: Turn on an LED and keep it lit for a specified duration.
Materials Needed:
1 LED
1 220Ω resistor
Breadboard and jumper wires
Arduino board
Steps:
1. Wiring:
Connect the longer leg (anode) of the LED to a digital pin on the Arduino
(e.g., pin 9).
Connect the shorter leg (cathode) of the LED to one end of the resistor.
Connect the other end of the resistor to GND on the Arduino.
1. Code:
void setup()
{
pinMode(9, OUTPUT); // Set pin 9 as output
}
void loop()
{
digitalWrite(9, HIGH); // Turn the LED on
delay(2000); // Keep it on for 2 seconds
}
Activity 2: Blinking an LED
Objective: Make the LED blink on and off at regular intervals.
Materials Needed: Same as Activity 1.
Steps:
1. Wiring: Use the same setup as in Activity 1.
2. Code:
void setup()
{
pinMode(9, OUTPUT); // Set pin 9 as output
}
void loop()
{
digitalWrite(9, HIGH); // Turn the LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(9, LOW); // Turn the LED off
delay(500); // Wait for 500 milliseconds
}
7.Using Analog Output for
LED Control
The analogWrite function allows you to control the brightness of LEDs by
providing a value between 0 (off) and 255 (fully on). This is useful for creating
dynamic lighting effects and smooth transitions.
Activity 1: LED Brightness Control
Objective: Adjust the brightness of a single LED using analogWrite.
Materials Needed:
1 LED
1 220Ω resistor
Breadboard and jumper wires
Arduino board
Steps:
1. Wiring:
Connect the longer leg (anode) of the LED to a Analog output-capable
digital pin on the Arduino (e.g., pin 9).
Connect the shorter leg (cathode) to one end of the resistor, and connect
the other end of the resistor to GND.
1. Code:
void setup() {
pinMode(9, OUTPUT); // Set pin 9 as output
}
void loop() {
analogWrite(9, 128); // Set LED brightness to medium (50%)
delay(2000); // Keep it at this brightness for 2 seconds
analogWrite(9, 255); // Set LED brightness to maximum (100%)
delay(2000); // Keep it at this brightness for 2 seconds
analogWrite(9, 0); // Turn off the LED
delay(2000); // Keep it off for 2 seconds
}
Activity 2: Multiple LEDs Brightness Control
Objective: Control the brightness of multiple LEDs to create dynamic effects like
“rise and fall.”
Materials Needed:
3 LEDs
3 220Ω resistors
Breadboard and jumper wires
Arduino board
Steps:
1. Wiring:
Connect the anodes of each LED to different PWM-capable digital pins on
the Arduino (e.g., pins 9, 10, and 11).
Connect the cathodes of each LED to a resistor, then to GND.
Code:
void setup()
{
pinMode(9, OUTPUT); // LED 1
pinMode(10, OUTPUT); // LED 2
pinMode(11, OUTPUT); // LED 3
}
void loop()
{
analogWrite(9, 0); // LED 1 off
analogWrite(10, 128); // LED 2 medium brightness
analogWrite(11, 255); // LED 3 fully on
delay(2000); // Keep this configuration for 2 seconds
analogWrite(9, 255); // LED 1 fully on
analogWrite(10, 0); // LED 2 off
analogWrite(11, 128); // LED 3 medium brightness
delay(2000); // Keep this configuration for 2 seconds
analogWrite(9, 128); // LED 1 medium brightness
analogWrite(10, 255); // LED 2 fully on
analogWrite(11, 0); // LED 3 off
delay(2000); // Keep this configuration for 2 seconds
}
8.RGB LED Control
RGB LEDs allow you to create a wide range of colors by adjusting the intensity of
the red, green, and blue components. Here are three activities to explore RGB LED
control without using conditional or loop statements.
Activity 1: Controlling RGB LED
Objective: Turn each color (red, green, blue) on and off using digitalWrite.
Materials Needed:
1 RGB LED
3 220Ω resistors
Breadboard and jumper wires
Arduino board
Wiring:
1. Connect the anode (common positive) of the RGB LED to the 5V pin on the
Arduino.
2. Connect the red leg to a digital pin (e.g., pin 9) through a resistor.
3. Connect the green leg to another digital pin (e.g., pin 10) through a resistor.
4. Connect the blue leg to another digital pin (e.g., pin 11) through a resistor.
Code:
void setup() {
pinMode(9, OUTPUT); // Red
pinMode(10, OUTPUT); // Green
pinMode(11, OUTPUT); // Blue
}
void loop()
{
digitalWrite(9, HIGH); // Turn on Red
delay(2000); // Keep Red on for 2 seconds
digitalWrite(9, LOW); // Turn off Red
digitalWrite(10, HIGH); // Turn on Green
delay(2000); // Keep Green on for 2 seconds
digitalWrite(10, LOW); // Turn off Green
digitalWrite(11, HIGH); // Turn on Blue
delay(2000); // Keep Blue on for 2 seconds
digitalWrite(11, LOW); // Turn off Blue
}
Activity 2: Color Mixing
Objective: Create new colors by mixing two colors.
Materials Needed: Same as Activity 1.
Wiring: Same as Activity 1.
Code:
void setup() {
pinMode(9, OUTPUT); // Red
pinMode(10, OUTPUT); // Green
pinMode(11, OUTPUT); // Blue
}
void loop()
{
digitalWrite(9, HIGH); // Turn on Red
digitalWrite(10, HIGH); // Turn on Green
delay(2000); // Keep both on for 2 seconds
digitalWrite(9, LOW); // Turn off Red
digitalWrite(10, LOW); // Turn off Green
digitalWrite(9, HIGH); // Turn on Red
digitalWrite(11, HIGH); // Turn on Blue
delay(2000); // Keep both on for 2 seconds
digitalWrite(9, LOW); // Turn off Red
digitalWrite(11, LOW); // Turn off Blue
digitalWrite(10, HIGH); // Turn on Green
digitalWrite(11, HIGH); // Turn on Blue
delay(2000); // Keep both on for 2 seconds
digitalWrite(10, LOW); // Turn off Green
digitalWrite(11, LOW); // Turn off Blue
}
Activity 3: Analog Color Mixing
Objective: Generate specific colors by adjusting the intensity of red,
green, and blue components using analogWrite.
Materials Needed: Same as Activity 1.
Wiring: Same as Activity 1.
Code:
void setup() {
pinMode(9, OUTPUT); // Red
pinMode(10, OUTPUT); // Green
pinMode(11, OUTPUT); // Blue
}
void loop() {
// Display Yellow (Red + Green)
analogWrite(9, 255); // Full Red
analogWrite(10, 255); // Full Green
analogWrite(11, 0); // No Blue
delay(2000); // Display Yellow for 2 seconds
// Display Cyan (Green + Blue)
analogWrite(9, 0); // No Red
analogWrite(10, 255); // Full Green
analogWrite(11, 255); // Full Blue
delay(2000); // Display Cyan for 2 seconds
// Display Magenta (Red + Blue)
analogWrite(9, 255); // Full Red
analogWrite(10, 0); // No Green
analogWrite(11, 255); // Full Blue
delay(2000); // Display Magenta for 2 seconds
// Display White (Red + Green + Blue)
analogWrite(9, 255); // Full Red
analogWrite(10, 255); // Full Green
analogWrite(11, 255); // Full Blue
delay(2000); // Display White for 2 seconds
// Turn off all colors
analogWrite(9, 0); // No Red
analogWrite(10, 0); // No Green
analogWrite(11, 0); // No Blue
delay(2000); // Keep off for 2 seconds
}
9.What is the Serial
Monitor?
The Serial Monitor is a feature in the Arduino IDE that allows you to send and
receive data between your computer and an Arduino board. It provides a way to
communicate with the board, enabling you to monitor its output and send input in
real-time. This is especially useful for debugging, as it helps you understand how
your program is functioning inside the microcontroller.
Why is the Serial Monitor Useful?
1. Debugging: You can print debug messages to understand the flow of your
program and identify where things might be going wrong.
2. Monitoring Variables: By printing variable values, you can track changes and
ensure they are behaving as expected.
3. User Input: You can send commands or data from your computer to the
Arduino for real-time interaction.
4. Data Logging: It can be used to log data to monitor sensor outputs or other
measurements.
How to Use the Serial Monitor in Arduino Programming
1. Initialize Serial Communication: In the setup() function, initialize the serial
communication with Serial.begin(baud_rate);, where baud_rate is typically set
to 9600 bps (bits per second).
2. Print Messages: Use Serial.print() or Serial.println() to send messages or
variable values to the Serial Monitor.
3. Open the Serial Monitor: Click on the magnifying glass icon in the Arduino IDE
or press Ctrl + Shift + M to open the Serial Monitor.
4. Select Baud Rate: Ensure the baud rate in the Serial Monitor matches the one
set in your code.
Example Code to Print a Message Using the Serial Monitor
Here’s a simple example that initializes serial communication and prints a message
to the Serial Monitor.
void setup()
{
Serial.begin(9600); // Initialize serial communication at 9600
bps
}
void loop()
{
Serial.println("Hello, Arduino!"); // Print a message to the Serial
Monitor
}
print() vs println() in Arduino
In Arduino programming, Serial.print() and Serial.println() are used to send data
from the Arduino board to the Serial Monitor. Both functions are essential for
debugging and monitoring the status of your program.
1. Serial.print()
Purpose: Sends data to the Serial Monitor without adding a new line or space
at the end.
Usage: Useful when you want to print multiple pieces of data on the same line.
Example:
void setup()
{
Serial.begin(9600); // Initialize serial communication
}
void loop()
{
Serial.print("Temperature: "); // Print label without newline
Serial.print(25.5); // Print temperature value without newline
delay(1000); // Wait for 1 second
}
Output in Serial Monitor:
Temperature: 25.5
This output appears as a single line, and the next message would be on the
same line unless println() is used afterward.
2. Serial.println()
Purpose: Sends data to the Serial Monitor and adds a new line (carriage return) after
the data.
Usage: Ideal for printing values when you want each output to appear on a new line.
Example:
void setup()
{
Serial.begin(9600); // Initialize serial communication
}
void loop()
{
Serial.println("Hello, Arduino!"); // Print message with newline
delay(1000); // Wait for 1 second
}
Output in Serial Monitor:
Each message appears on a new line in the Serial Monitor, making it easier to read
multiple outputs.
Key Differences
Output Formatting:
Serial.print(): Continues on the same line.
Serial.println(): Moves to the next line after printing.
Use Case:
Use print() when you want to format data on the same line.
Use println() when you want clearer, line-separated outputs.
Combined Example
Here’s a combined example that demonstrates both print() and println():
void setup()
{
Serial.begin(9600); // Initialize serial communication
}
void loop()
{
int temperature = 25; // Example temperature
int humidity = 60; // Example humidity
Serial.print("Temperature: "); // Print label without newline
Serial.print(temperature); // Print temperature value
Serial.print(" °C, "); // Print unit without newline
Serial.print("Humidity: "); // Print label without newline
Serial.print(humidity); // Print humidity value
Serial.println(" %"); // Print unit with newline
delay(2000); // Wait for 2 seconds before repeating
}
Output in Serial Monitor:
10.Variables and Data Types
in Arduino Programming
In Arduino programming, variables are used to store data that can be changed during
the execution of a program. Understanding how to use variables and the different data
types available is crucial for effective programming.
1. What is a Variable?
A variable is a named storage location in memory that holds a value. Each variable has
a type, which determines what kind of data it can store.
2. Common Data Types in Arduino
Arduino supports several data types, each with its specific use cases. Here are
some of the most commonly used data types:
int:
Description: Stores integer values (whole numbers).
Size: Typically 2 bytes (16 bits) on most Arduino boards, can hold values from
-32,768 to 32,767.
Example: int temperature = 25;
float:
Description: Stores floating-point numbers (decimal values).
Size: 4 bytes (32 bits), allowing for more precise calculations.
Example: float temperature = 25.5;
char:
Description: Stores a single character.
Size: 1 byte (8 bits).
Example: char grade = 'A';
boolean:
Description: Stores a true or false value.
Size: 1 byte, but only uses 1 bit for data.
Example: boolean isActive = true;
String:
Description: Stores a sequence of characters (a string of text).
Size: Varies depending on the string length.
Example: String message = "Hello, World!";
3. Declaring and Initializing Variables
To declare a variable, you specify its type followed by its name. You can also
initialize it with a value.
Syntax:
data_type variable_name = value;
Example:
int speed = 60; // Declare and initialize an integer
variable
float temperature = 25.5; // Declare and initialize a float
variable
char letter = 'A'; // Declare and initialize a char
variable
boolean isActive = false; // Declare and initialize a boolean
variable
String message = "Robotics & AI"; // Declare and initialize a String
variable
Printing variable data in the Serial Monitor is a common practice in Arduino
programming for debugging and monitoring the state of variables. Here's how to do
it effectively:
Steps to Print Variable Data in the Serial Monitor
1. Initialize Serial Communication: In the setup() function, use
Serial.begin(baud_rate); to start the serial communication.
2. Declare and Initialize Variables: Define the variables you want to monitor.
3. Print Variables: Use Serial.print() or Serial.println() to output the values of the
variables to the Serial Monitor.
4. Open the Serial Monitor: Use the magnifying glass icon in the Arduino IDE to
view the printed data.
Example Code
Here's a simple example that demonstrates how to print different types of
variables:
void setup()
{
Serial.begin(9600); // Initialize serial communication at 9600 bps
}
void loop()
{
int temperature = 25; // Example integer variable
float voltage = 3.3; // Example float variable
char status = 'A'; // Example char variable
boolean isActive = true; // Example boolean variable
// Print integer variable
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C"); // Print with newline
// Print float variable
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println(" V"); // Print with newline
// Print char variable
Serial.print("Status: ");
Serial.println(status); // Print with newline
// Print boolean variable
Serial.print("Is Active: ");
Serial.println(isActive ? "True" : "False"); // Print True/False
delay(2000); // Wait for 2 seconds before repeating
}
Output in the Serial Monitor
When you upload this code to your Arduino and open the Serial Monitor, you
should see something like this:
11.Introduction to Arithmetic
Operators in Arduino
Arithmetic operators are fundamental in programming, allowing you to perform basic
mathematical calculations. In Arduino, these operators can be used to manipulate
variables and display results on the Serial Monitor.
Arithmetic Operators Overview
1. Addition (+): Adds two operands.
2. Subtraction (-): Subtracts the second operand from the first.
3. Multiplication (*): Multiplies two operands.
4. Division (/): Divides the first operand by the second.
5. Modulus (%): Returns the remainder of division.
Arithmetic Operators Overview
1.Addition (+):
Description: Adds two operands together.
Example: If you have int sum = 5 + 3;, then sum will be 8.
2.Subtraction (-):
Description: Subtracts the second operand from the first.
Example: If you write int difference = 10 - 4;, then difference will be 6.
3.Multiplication (*):
Description: Multiplies two operands.
Example: Using int product = 7 * 3; will result in product being 21.
4.Division (/):
Description: Divides the first operand by the second. Note that if both operands are
integers, the result will also be an integer (any remainder is discarded).
Example: If you have int quotient = 20 / 4;, then quotient will be 5.
5.Modulus (%):
Description: Returns the remainder of the division of the first operand by the second.
Example: With int remainder = 10 % 3;, the remainder will be 1.
Using Arithmetic Operators in Arduino Code
Here’s a simple example that demonstrates the use of arithmetic operators in an
Arduino program. This program performs some calculations and prints the results
to the Serial Monitor:
void setup()
{
Serial.begin(9600); // Initialize serial communication
}
void loop()
{
int a = 10;
int b = 5;
// Declare result variables
int sum;
int difference;
int product;
float quotient;
int remainder;
// Perform calculations
sum = a + b; // Addition
difference = a - b; // Subtraction
product = a * b; // Multiplication
quotient = (float)a / b; // Division
remainder = a % b; // Modulus
// Print results to Serial Monitor
Serial.print("Sum: ");
Serial.println(sum);
Serial.print("Difference: ");
Serial.println(difference);
Serial.print("Product: ");
Serial.println(product);
Serial.print("Quotient: ");
Serial.println(quotient);
Serial.print("Remainder: ");
Serial.println(remainder);