Course Content
📘 MODULE 11 – Edge Avoiding Robot
📦 MODULE 12 – Smart Multi-Function Robot (Mega Project)
Arduino Hands-On Programming and Robotics Course

📘 Lesson P14 – Analog Input and PWM Programming

🎯 Learning Objectives

After completing this lesson, students will be able to:

✅ Understand Analog Signals

✅ Understand Analog Input Pins

✅ Use analogRead()

✅ Read Potentiometer Values

✅ Read Analog Sensors

✅ Understand ADC (Analog to Digital Conversion)

✅ Understand PWM Output

✅ Use analogWrite()

✅ Control LED Brightness

✅ Control Motor Speed


1. Introduction

In the previous lesson, we learned about Digital Input and Output.

Digital signals have only two states:

 
LOW = 0V
HIGH = 5V
 

But many sensors do not provide only ON or OFF values.

Examples:

  • Potentiometer
  • LDR
  • MQ Gas Sensor
  • Sound Sensor
  • Temperature Sensors

These sensors provide continuously changing values.

Such signals are called:

Analog Signals


2. What is an Analog Signal?

An Analog Signal can have many values between minimum and maximum.

Example:

Temperature:

 
20°C
21°C
22°C
23°C
24°C
 

Not just ON or OFF.


Real-Life Example

Think about a water tap.

Digital:

 
OFF
ON
 

Only two states.


Analog:

 
Slow Flow
Medium Flow
Fast Flow
 

Many levels.

This is similar to analog signals.


3. Analog Pins on Arduino UNO

Arduino UNO contains:

 
A0
A1
A2
A3
A4
A5
 

These are called:

Analog Input Pins

Used to read analog sensors.


4. What is ADC?

Arduino cannot directly understand analog signals.

Arduino understands only digital data.

To solve this problem Arduino contains:

ADC

Analog to Digital Converter


Working

Sensor

Analog Voltage

ADC

Digital Number

Arduino


5. ADC Resolution

Arduino UNO uses:

10-bit ADC

This means:

 
0 to 1023
 

Total:

 
1024 Levels
 

Voltage Conversion

Voltage ADC Value
0V 0
1V 205
2V 410
3V 615
4V 820
5V 1023

6. Reading Analog Values

Arduino uses:

 
analogRead(pin);
 

Syntax:

 
analogRead(A0);
 

Returns:

 
0 to 1023
 

Example

 
int sensorValue;

void setup()
{
Serial.begin(9600);
}

void loop()
{
sensorValue = analogRead(A0);

Serial.println(sensorValue);

delay(500);
}
 

Output

 
150
320
500
700
900
 

Values change depending on sensor input.


7. Potentiometer Example

A potentiometer acts like a variable resistor.

Connection:

 
5V
|
Potentiometer
|
A0
|
GND
 

Program

 
void setup()
{
Serial.begin(9600);
}

void loop()
{
int value;

value = analogRead(A0);

Serial.println(value);

delay(200);
}
 

Result

Rotating knob changes values from:

 
0 to 1023
 

8. LDR Example

LDR:

Light Dependent Resistor

Used to measure light intensity.


Possible Readings:

Bright Light:

 
850
 

Dark Room:

 
200
 

Applications:

  • Automatic Street Lights
  • Smart Homes
  • Light Monitoring

9. MQ Gas Sensor Example

MQ sensors provide analog output.

Example:

 
int gasValue;

gasValue = analogRead(A0);
 

Output:

 
100
250
400
700
 

Higher value indicates higher gas concentration.


10. Understanding PWM

Many beginners think:

 
analogWrite()
 

creates analog voltage.

Actually:

❌ Not True Analog Output

Arduino UNO does not have a DAC.

Instead it uses:

PWM

Pulse Width Modulation


11. What is PWM?

PWM rapidly switches a pin:

 
ON
OFF
ON
OFF
ON
OFF
 

very quickly.

This creates the effect of analog voltage.


Example

50% Duty Cycle:

 
ON OFF ON OFF ON OFF
 

Average output appears:

 
2.5V
 

12. PWM Pins on Arduino UNO

Only specific pins support PWM:

Pin
3
5
6
9
10
11

Marked with:

 
~
 

symbol.


13. analogWrite()

Used for PWM output.

Syntax:

 
analogWrite(pin,value);
 

Example:

 
analogWrite(9,128);
 

PWM Range:

 
0 to 255
 

Output Levels

Value Output
0 OFF
64 25%
128 50%
192 75%
255 Full ON

14. LED Brightness Control

Program:

 
void setup()
{
pinMode(9,OUTPUT);
}

void loop()
{
analogWrite(9,50);

delay(1000);

analogWrite(9,150);

delay(1000);

analogWrite(9,255);

delay(1000);
}
 

Result

LED brightness changes automatically.


15. Fading LED Project

A popular beginner project.

 
void setup()
{
pinMode(9,OUTPUT);
}

void loop()
{
for(int i=0;i<=255;i++)
{
analogWrite(9,i);

delay(10);
}

for(int i=255;i>=0;i--)
{
analogWrite(9,i);

delay(10);
}
}
 

Result

LED smoothly fades in and out.


16. Motor Speed Control

PWM controls motor speed.

Example:

 
analogWrite(ENA,100);
 

Motor runs slowly.


 
analogWrite(ENA,255);
 

Motor runs at maximum speed.


Applications:

  • Robots
  • Fans
  • Pumps
  • Conveyor Systems

17. Potentiometer Controlled LED

Goal:

Rotate Potentiometer

Control LED Brightness


Program:

 
int sensorValue;
int brightness;

void setup()
{
pinMode(9,OUTPUT);
}

void loop()
{
sensorValue = analogRead(A0);

brightness = map(sensorValue,0,1023,0,255);

analogWrite(9,brightness);
}
 

18. What is map()?

Used to convert one range into another.

Syntax:

 
map(value,
inputMin,
inputMax,
outputMin,
outputMax);
 

Example:

 
map(512,0,1023,0,255)
 

Output:

Approximately:

 
128
 

Why map() is Important

Many sensors give:

 
0–1023
 

But PWM requires:

 
0–255
 

map() converts values automatically.


19. Real Project Applications

Automatic Street Light

Input:

LDR

Output:

LED


Smart Fan

Input:

Temperature Sensor

Output:

Fan Speed


Gas Leakage Detector

Input:

MQ Sensor

Output:

Alarm Intensity


Water Quality Monitoring

Input:

Analog Sensor

Output:

Display Values


Robotics

Input:

Potentiometer

Output:

Motor Speed


20. Common Beginner Mistakes

Mistake 1

Using:

 
analogWrite(A0,100);
 

Wrong.

PWM pins only.


Mistake 2

Expecting:

 
analogRead()
 

to return:

 
0–255
 

Actual range:

 
0–1023
 

Mistake 3

Using PWM on non-PWM pins.


Mistake 4

Forgetting to map sensor values.


21. Best Practices

✅ Test sensors using Serial Monitor

✅ Use map() when required

✅ Verify PWM pins

✅ Label sensor connections

✅ Calibrate sensors before projects


📊 Summary

In this lesson, we learned:

✅ Analog Signals

✅ ADC

✅ Analog Pins

✅ analogRead()

✅ Potentiometer Reading

✅ LDR Reading

✅ MQ Sensor Reading

✅ PWM

✅ analogWrite()

✅ LED Brightness Control

✅ Motor Speed Control

Analog Input and PWM are extremely important because most real-world sensors provide analog data and many actuators require variable control.


📖 Key Terms

Analog Signal

Signal with continuously changing values.

ADC

Analog to Digital Converter.

Analog Pin

Pin used to read analog values.

analogRead()

Reads analog values.

PWM

Pulse Width Modulation.

analogWrite()

Generates PWM output.

Duty Cycle

Percentage of ON time in PWM.

map()

Converts one range into another.


🎯 Quiz

1. How many analog input pins are available on Arduino UNO?

A. 4

B. 5

C. 6 ✅

D. 8


2. What is the ADC resolution of Arduino UNO?

A. 8-bit

B. 10-bit ✅

C. 12-bit

D. 16-bit


3. What range does analogRead() return?

A. 0–255

B. 0–512

C. 0–1023 ✅

D. 0–4095


4. Which function generates PWM?

A. digitalWrite()

B. analogRead()

C. analogWrite() ✅

D. pinMode()


5. Which pins support PWM on Arduino UNO?

A. All Pins

B. A0–A5

C. 3, 5, 6, 9, 10, 11 ✅

D. 0–5


🏠 Assignment

Task 1

Read a potentiometer value and display it in Serial Monitor.

Task 2

Create a fading LED program using PWM.

Task 3

Control LED brightness using a potentiometer.

Task 4

Research how ADC works in Arduino.

Task 5

Create a table comparing Digital Signals and Analog Signals.

Scroll to Top