Experiment No 5
Experiment No 5
1.Aim / Objective
3.Theory
Although the Arduino lacks a true Digital-to-Analog Converter (DAC) for generating variable
voltages, it can simulate analog output using Pulse Width Modulation (PWM). PWM involves
toggling a digital pin between HIGH and LOW at a high frequency. By adjusting the duty cycle
(the percentage of time the pin remains HIGH), an average voltage can be created. For
example, a 50% duty cycle on a 5V Arduino pin results in an effective 2.5V signal.
Pin Configuration
PWM Pins: The Arduino Uno offers PWM on digital pins marked with a tilde (~), such as pins 3,
5, 6, 9, 10, and 11.
The analogWrite() function generates PWM signals on these pins, accepting values from 0 (0%
duty cycle) to 255 (100% duty cycle).
4 . Code
The following C code demonstrates how to control an LED's brightness using PWM on an
Arduino pin:
#define pwmPin 9 // Define PWM pin (9) for the LED
void setup() {
pinMode(pwmPin, OUTPUT); // Set the PWM pin as output
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) { // Increase brightness
analogWrite(pwmPin, brightness);
delay(10); // Small delay to observe the gradual change
}
1. Pin Setup: The PWM pin 9 is defined as the output for the LED.
2. analogWrite() Function: This function generates a PWM signal on the specified pin. Values
from 0 to 255 control the duty cycle, creating an analog-like effect.
3. Brightness Control: A loop is used to gradually increase and decrease the brightness by
adjusting the PWM value.
5. Result
The LED's brightness was successfully controlled using PWM. The LED smoothly transitioned
from off to full brightness and back, demonstrating the analog-like output.
6. Applications
Audio Signal Generation: Simulate sound waveforms by adjusting PWM frequency and duty
cycle.