Module 1
Arduino basics, digital i/o, pwm and analog
interfacing
1. Introduction to arduino
what is arduino?
Arduino is an open-source electronics platform used to build embedded systems and IoT projects.
It consists of:
• hardware board (Arduino Uno, Nano, Mega etc.)
• software (Arduino IDE)
• microcontroller
• input/output pins
It is mainly used for:
• automation
• robotics
• sensor interfacing
• home automation
• embedded systems learning
2. Arduino uno r3 basics
Important components of arduino uno r3
component function
ATmega328P main microcontroller
USB port programming and power
DC power jack external power supply
digital pins digital input/output
analog pins analog input
reset button restarts program
crystal oscillator clock generation
voltage regulator maintains voltage
3. Microcontroller
A microcontroller is a compact integrated circuit designed to perform specific tasks in embedded
systems.
It contains:
• CPU
• memory
• input/output ports
• timers
• communication modules
examples
• ATmega328P
• ESP32
• PIC
• STM32
4. programming interfaces
Arduino ide
Arduino IDE is used to:
• write programs
• compile code
• upload programs to board
• monitor serial communication
Basic structure of arduino program
void setup()
{
}
void loop()
{
}
setup()
Runs only once.
Used for:
• pin configuration
• serial initialization
loop()
Runs continuously.
Used for:
• repeated tasks
• sensor reading
• controlling outputs
5. Input/output pins
GPIO
GPIO = General Purpose Input Output
Pins can work as:
• input
• output
pinMode() : Used to configure pin direction.
pinMode(13, OUTPUT);
pinMode(2, INPUT);
6. Digital outputs
controlling LED using digitalWrite()
program
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
explanation
statement purpose
pinMode() sets pin as output
digitalWrite(HIGH) turns LED ON
digitalWrite(LOW) turns LED OFF
delay() creates time delay
7. Digital inputs
reading push button
program
int button = 2;
int led = 13;
void setup()
{
pinMode(button, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{
int x = digitalRead(button);
if(x == HIGH)
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
}
8. Pull-down resistor
purpose
A pull-down resistor ensures the input pin reads LOW when switch is not pressed.
Without it:
• pin becomes floating
• unstable readings occur
Typical value:
• 10kΩ
9. Internal pull-up resistor
Arduino has built-in pull-up resistors.
using INPUT_PULLUP
pinMode(button, INPUT_PULLUP);
characteristics
state reading
button not pressed HIGH
button pressed LOW
10. Contact bounce
definition
When a switch is pressed, mechanical vibrations create multiple ON/OFF signals.
This is called bouncing.
debounce solution
delay(50);
or use software debounce logic.
11. RGB LED interfacing
RGB LED
Contains:
• red LED
• green LED
• blue LED
Different colors are produced by mixing intensities.
program
int r = 9;
int g = 10;
int b = 11;
void setup()
{
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
}
void loop()
{
analogWrite(r,255);
analogWrite(g,0);
analogWrite(b,0);
}
12. pulse width modulation (PWM)
PWM controls average voltage by varying pulse width.
Used for:
• LED brightness
• motor speed control
pwm pins in arduino uno
~3, ~5, ~6, ~9, ~10, ~11
analogWrite()
analogWrite(pin, value);
Range:
• 0 → OFF
• 255 → fully ON
duty cycle
𝑇𝑂𝑁
Duty Cycle = × 100%
𝑇
Where:
• 𝑇𝑂𝑁 = ON time
• 𝑇= total time period
13. Analog and digital signals
Analog signal
• continuous values
• example: temperature sensor
Digital signal
• discrete values
• only HIGH or LOW
14. Analog to digital conversion (ADC)
ADC
Converts analog voltage into digital value.
Arduino Uno uses:
• 10-bit ADC
resolution
𝑉𝑟𝑒𝑓
Resolution =
2𝑛
For Arduino:
• 𝑛 = 10
• values from 0 to 1023
If 𝑉𝑟𝑒𝑓 = 5𝑉:
5
≈ 4.88 mV per step
1024
15. Quantization
definition
Process of converting continuous analog values into discrete digital levels.
quantization error
Difference between actual analog value and converted digital value.
16. Reading analog sensors
analogRead()
int x = analogRead(A0);
Range:
• 0 to 1023
example – potentiometer
program
int sensor = A0;
void setup()
{
[Link](9600);
}
void loop()
{
int x = analogRead(sensor);
[Link](x);
delay(500);
}
17. Controlling analog output using analog input
example
Control LED brightness using potentiometer.
program
int pot = A0;
int led = 9;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
int x = analogRead(pot);
x = x / 4;
analogWrite(led, x);
}
explanation
step purpose
analogRead() reads sensor value
divide by 4 converts 0–1023 to 0–255
analogWrite() controls brightness
18. communication buses
UART
• serial communication
• TX and RX pins
I2C
• SDA and SCL lines
• multiple devices supported
SPI
• faster communication
• uses MOSI, MISO, SCK
19. bootloader
definition
A small program stored in memory that allows uploading code through USB without external
programmer.
20. firmware
definition
Permanent software stored inside hardware memory.
Controls hardware operation.
important functions summary
function purpose
pinMode() configure pin
digitalWrite() write digital output
digitalRead() read digital input
analogWrite() PWM output
analogRead() read analog input
delay() time delay
[Link]() start serial communication
[Link]() print data
important viva questions
basic questions
1. what is arduino?
2. what is microcontroller?
3. what is GPIO?
4. difference between analog and digital signals?
5. what is ADC?
6. what is PWM?
7. what is duty cycle?
8. what is quantization?
9. what is pull-up resistor?
10. what is contact bounce?
11. what is bootloader?
12. what is firmware?
13. what is resolution of arduino uno ADC?
14. why divide analog value by 4 before analogWrite()?
15. difference between digitalWrite() and analogWrite()?
short notes for exam
digitalWrite()
Used to set output HIGH or LOW.
analogWrite()
Generates PWM signal.
analogRead()
Reads analog voltage through ADC.
PWM
Technique to control average voltage using pulse width variation.
ADC resolution
10-bit in Arduino Uno → 1024 levels.
pull-up resistor
Keeps input HIGH when switch open.
debounce
Eliminates unwanted switching noise.
instead of summary give detailed topic wise notes please
module 1 – detailed topic wise notes
arduino basics
1. introduction to arduino
5
what is arduino?
Arduino is an open-source electronics development platform used to create embedded systems and
automation projects. It combines both hardware and software.
The platform is designed for:
• beginners
• students
• engineers
• hobbyists
Arduino makes interfacing sensors, LEDs, motors, and communication modules easier.
features of arduino
• low cost
• easy programming
• open-source hardware and software
• supports many sensors and modules
• cross-platform support (Windows, Linux, Mac)
applications of arduino
home automation
Used for automatic lights, fans, and security systems.
robotics
Controls motors and sensors in robots.
IoT systems
Used in smart devices connected to the internet.
industrial automation
Used in monitoring and control systems.
medical devices
Used in health monitoring equipment.
2. arduino uno r3
6
specifications of arduino uno r3
parameter value
microcontroller ATmega328P
operating voltage 5V
input voltage 7–12V
digital I/O pins 14
PWM pins 6
analog input pins 6
flash memory 32 KB
SRAM 2 KB
EEPROM 1 KB
clock speed 16 MHz
components of arduino uno
ATmega328P microcontroller
Acts as the brain of the board.
Functions:
• executes program
• processes data
• controls input/output devices
USB port
Used for:
• uploading programs
• serial communication
• supplying power
power jack
Used to provide external power supply.
Recommended voltage:
• 7V to 12V
voltage regulator
Maintains stable 5V supply to board components.
crystal oscillator
Provides clock pulses for timing operations.
Frequency:
• 16 MHz
reset button
Restarts the running program from the beginning.
digital pins
Pins 0–13 used for digital input/output.
analog pins
Pins A0–A5 used for analog input.
3. microcontroller
5
definition
A microcontroller is a small integrated circuit designed to perform specific control operations in
embedded systems.
It combines:
• CPU
• memory
• timers
• communication modules
• I/O ports
on a single chip.
functions of microcontroller
• reads input signals
• processes instructions
• controls output devices
• communicates with sensors and modules
types of memory in microcontroller
flash memory
Stores program permanently.
SRAM
Temporary memory during execution.
EEPROM
Stores small permanent data even after power OFF.
4. programming interfaces
4
arduino IDE
Arduino IDE is software used for:
• writing code
• compiling programs
• uploading programs
• debugging through serial monitor
steps in programming arduino
1. write code
2. compile code
3. connect board
4. upload code
5. execute program
structure of arduino program
void setup()
{
}
void loop()
{
}
setup()
Runs only once after powering ON or resetting.
Used for:
• pin initialization
• serial communication setup
Example:
void setup()
{
pinMode(13, OUTPUT);
}
loop()
Runs repeatedly forever.
Example:
void loop()
{
digitalWrite(13,HIGH);
}
5. GPIO (general purpose input output)
definition
GPIO pins are programmable pins that can act as:
• input
• output
configuring GPIO
Using pinMode() function.
Syntax:
pinMode(pin, mode);
Modes:
• INPUT
• OUTPUT
• INPUT_PULLUP
Example:
pinMode(13, OUTPUT);
pinMode(2, INPUT);
digital outputs
6. controlling LED using digitalWrite()
6
LED interfacing
An LED is connected with:
• resistor
• digital output pin
• ground
Typical resistor:
• 220Ω
digitalWrite()
Used to produce:
• HIGH → 5V
• LOW → 0V
Syntax:
digitalWrite(pin, value);
LED blinking program
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
working
HIGH
LED turns ON.
LOW
LED turns OFF.
delay()
Creates delay in milliseconds.
7. RGB LED interfacing
6
RGB LED
Contains:
• red LED
• green LED
• blue LED
Combining these colors produces multiple colors.
types of RGB LEDs
common cathode
All cathodes connected together.
common anode
All anodes connected together.
controlling RGB LED
Each color intensity is controlled using PWM.
example program
int r=9,g=10,b=11;
void setup()
{
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
}
void loop()
{
analogWrite(r,255);
analogWrite(g,0);
analogWrite(b,0);
}
8. pulse width modulation (PWM)
7
definition
PWM is a technique used to control average voltage by changing pulse width.
Applications:
• LED brightness control
• motor speed control
• audio generation
duty cycle
Percentage of ON time in one cycle.
𝑇𝑂𝑁
Duty Cycle = × 100%
𝑇
PWM pins in Arduino Uno
• 3
• 5
• 6
• 9
• 10
• 11
analogWrite()
Generates PWM signal.
Syntax:
analogWrite(pin,value);
Value range:
• 0 → OFF
• 255 → maximum brightness
example
analogWrite(9,128);
Produces nearly 50% brightness.
digital inputs
9. digital input using push button
6
digitalRead()
Reads digital input from pin.
Syntax:
digitalRead(pin);
Returns:
• HIGH
• LOW
push button interfacing
Button connected between:
• VCC
• input pin
Resistor connected to ground.
example program
int button=2;
int led=13;
void setup()
{
pinMode(button,INPUT);
pinMode(led,OUTPUT);
}
void loop()
{
int x=digitalRead(button);
if(x==HIGH)
digitalWrite(led,HIGH);
else
digitalWrite(led,LOW);
}
10. pull-down resistor
definition
A resistor connected between input pin and ground to ensure LOW state when switch is open.
need for pull-down resistor
Without resistor:
• input pin floats
• random values occur
Typical value:
• 10kΩ
11. internal pull-up resistor
INPUT_PULLUP mode
Arduino provides internal pull-up resistor.
Syntax:
pinMode(2,INPUT_PULLUP);
operation
switch state input
open HIGH
pressed LOW
12. contact bounce
6
definition
Mechanical switches generate multiple unwanted transitions when pressed or released.
This effect is called contact bounce.
effects
• false triggering
• unstable input reading
debouncing methods
hardware debounce
Using capacitor and resistor.
software debounce
Using delay.
Example:
delay(50);
interfacing analog sensors
13. analog and digital signals
6
analog signal
A continuously varying signal.
Examples:
• temperature
• sound
• light intensity
digital signal
A signal having discrete values.
Usually:
• HIGH
• LOW
comparison
analog digital
continuous discrete
noise sensitive less noise
infinite values limited values
14. analog to digital converter (ADC)
6
definition
ADC converts analog voltage into digital number.
ADC in Arduino Uno
• 10-bit ADC
• values from 0 to 1023
ADC resolution
𝑉𝑟𝑒𝑓
Resolution =
2𝑛
For Arduino:
5𝑉
= 4.88𝑚𝑉
1024
Meaning:
Each step represents 4.88 mV.
ADC conversion example
If input voltage = 2.5V
2.5
Digital Value = × 1023 ≈ 512
5
15. quantization
definition
Process of converting continuous analog values into finite digital levels.
quantization error
Difference between:
• actual analog value
• nearest digital value
16. analogRead()
syntax
analogRead(A0);
Reads analog voltage from analog pin.
return values
0 to 1023
potentiometer interfacing
6
example program
int sensor=A0;
void setup()
{
[Link](9600);
}
void loop()
{
int x=analogRead(sensor);
[Link](x);
delay(500);
}
serial monitor
Used to display sensor values on computer screen.
17. controlling analog output using analog input
concept
Sensor input controls PWM output.
Example:
• potentiometer controls LED brightness
example program
int pot=A0;
int led=9;
void setup()
{
pinMode(led,OUTPUT);
}
void loop()
{
int x=analogRead(pot);
x=x/4;
analogWrite(led,x);
}
why divide by 4?
Because:
• analogRead() → 0 to 1023
• analogWrite() → 0 to 255
So scaling is required.
communication buses
18. UART communication
7
UART
Universal Asynchronous Receiver Transmitter.
Uses:
• TX pin
• RX pin
Applications:
• serial monitor
• Bluetooth communication
19. I2C communication
7
features
Uses only:
• SDA
• SCL
Supports multiple slave devices.
20. SPI communication
4
SPI pins
pin function
MOSI master out slave in
MISO master in slave out
SCK clock
SS slave select
advantages
• very fast
• full duplex communication
21. bootloader
definition
Bootloader is a small program stored in flash memory that allows uploading sketches through USB
without external programmer.
advantages
• easy programming
• no separate hardware required
22. firmware
definition
Firmware is permanent software programmed into hardware memory.
It controls device operation.
examples
• BIOS
• router software
• embedded device control program
important differences
analogWrite() vs digitalWrite()
analogWrite() digitalWrite()
generates PWM generates HIGH/LOW
variable output fixed output
used for brightness control used for ON/OFF control
analog signal vs digital signal
analog digital
continuous discrete
more noise less noise
infinite levels finite levels