ASSIGNMENT – 03
Report
Name:
 1.Nitin Kalsi – 230102063
 2.Adarsh Chandresh Choudhary – 230102002
 3.Adarsh Kumar – 230102003
 4.Abhinav Soni - 230102001
Objective
To study and implement Pulse Width Modulation (PWM) using
Arduino. Tasks include generating PWM signals manually,
controlling LED brightness, using potentiometer input to vary duty
cycle, and controlling a servo motor.
Components Required
  1. Arduino UNO Board
  2. LEDs
  3. Potentiometer (10kΩ)
  4. Servo Motor (SG90 or equivalent)
  5. Resistors (220Ω – 330Ω for LED current limiting)
  6. Breadboard
  7. Jumper wires
  8. Oscilloscope (for PWM signal visualization)
Procedure
Task 1 – Generate a PWM signal without using library/in-built functions
      ●    Used digitalWrite and delayMicroseconds to manually toggle pin HIGH/LOW.
      ●    Observed PWM waveform on oscilloscope.
      ●    Verified duty cycle by adjusting ON/OFF times.
           CODE 1:
const int ledPin = 9;
float frequency = 50;
float duty=25;
void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop() {
 generatePWM(ledPin, duty, frequency, 10);
void generatePWM(int pin, float dutyCycle, float freq, int cycles) {
 float period = 1000 / freq;
 float highTime = (period * dutyCycle) / 100;
 float lowTime = period - highTime;
    for (int i = 0; i < cycles; i++) {
      digitalWrite(pin, HIGH);
      delay(highTime);
      digitalWrite(pin, LOW);
      delay(lowTime);
    }
}
CIRCUIT:
Task 2:
Control LED Brightness using generated PWM
Connected an LED to Arduino PWM output through a current
limiting resistor.
Modified duty cycle from 0% to 100% in steps to change
brightness.
Observed smooth transition in brightness.
Code:
const int ledPin = 6;
float frequency = 500;
void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop() {
  for (float duty = 0; duty <= 100; duty++) {
    generatePWM(ledPin, duty, frequency, 1);
  }
}
void generatePWM(int pin, int dutyCycle, float freq, int cycles) {
 float period = 1000 / freq;
 float highTime = (period * dutyCycle) / 100;
 float lowTime = period - highTime;
    for (int i = 0; i < cycles; i++) {
      digitalWrite(pin, HIGH);
      delay(highTime);
      digitalWrite(pin, LOW);
      delay(lowTime);
    }
}
Circuit:
Task 3:
Use potentiometer to vary duty cycle and LED brightness
Connected potentiometer to analog input (A0).
Read analog value (0–1023) and mapped it to duty cycle
(0–100%).
Used PWM output to control LED brightness dynamically.
Code:
const int potPin = A0;
const int ledPin = 9;
float frequency = 50;
void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop() {
  float potValue = analogRead(potPin);
  float duty = map(potValue, 0, 1023, 0, 100);
  generatePWM(ledPin, duty, frequency, 5);
}
void generatePWM(int pin, float dutyCycle, float freq, int cycles) {
 float period = 1000 / freq;
 float highTime = (period * dutyCycle) / 100;
 float lowTime = period - highTime;
    for (int i = 0; i < cycles; i++) {
      digitalWrite(pin, HIGH);
      delay(highTime);
      digitalWrite(pin, LOW);
      delay(lowTime);
    }
}
Circuit:
Task 4:
Use PWM pins and analogWrite() to carry out Task 3
Used built-in Arduino PWM (analogWrite) instead of manual
delay-based PWM.
LED brightness changed smoothly based on potentiometer
rotation.
Code:
const uint8_t LED_PWM_PIN = 9;
const uint8_t POT_PIN = A0;
void setup() {
  pinMode(LED_PWM_PIN, OUTPUT);
}
void loop() {
  int raw = analogRead(POT_PIN);
  int pwm = map(raw, 0, 1023, 0, 255);
  analogWrite(LED_PWM_PIN, pwm);
}
Circuit:
Task 5:
Control a servo motor using PWM
Connected servo motor to PWM pin.
Implemented two methods:
(i) Manually generated PWM with duty cycle between 1–2 ms
at 50 Hz.
(ii) Used Servo library functions for easy angle control.
Verified servo rotation between 0° and 180°.
Code:
#include <Servo.h> // For Servo library method
Servo myServo; // Create servo object
int potPin = A0; // Potentiometer pin
int servoPin = 9;
void setup() {
 Serial.begin(9600);
    //myServo.attach(servoPin);
}
void loop() {
 int potValue = analogRead(potPin);
 Serial.print("Potentiometer: ");
 Serial.println(potValue);
    //using Servo.h
 /*
 int angle = map(potValue, 0, 1023, 0, 180);
 myServo.write(angle);
 delay(15);
*/
    // Manual PWM
    int pwmValue = map(potValue, 0, 1023, 544, 2400);
    for (int i = 0; i < 50; i++) {
      digitalWrite(servoPin, HIGH);
      delayMicroseconds(pwmValue);
      digitalWrite(servoPin, LOW);
      delayMicroseconds(20000 - pwmValue);
    }
}
Circuit:
Observations:
1. PWM waveform successfully generated and visualized on
oscilloscope.
2. LED brightness control verified by varying duty cycle.
3. Potentiometer provided smooth real-time adjustment of
brightness.
4. Arduino’s built-in analogWrite provided stable PWM compared
to manual generation.
5. Servo motor controlled effectively using PWM pulse width
variation.
Conclusion:
This experiment demonstrated the principle and
applications of PWM. Manual PWM generation
helped understand timing and duty cycle
concepts, while hardware PWM (analogWrite)
simplified implementation. PWM proved
effective in controlling LED brightness and
servo motor position.