Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Assignment 3

Name: Bhakti Kalyankasture


Roll. No.: 210108019

1. Generate a PWM signal without using library/ in-


built function. Use Oscilloscope to visualize output.
2. Control LED Brightness using generated PWM
Signal. (Cycle through low to high brightness)

Arduino Code:
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the
LED by
void setup() {
pinMode(led, OUTPUT); // declare pwm pin to be an
output:
}

void loop() {
analogWrite(led, brightness); // set the brightness of
led

// change the brightness for next time through the


loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of


the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(300); // wait for 30 milliseconds to see the
dimming effect
}
Link to Tinkercad Simulation:
https://www.tinkercad.com/things/edEC9NCtjC3-copy-
of-ee-312-exp-3-part-12/editel?tenant=circuits
Description:
We control the brightness of an LED using pulse-width
modulation (PWM) on pin 9. The LED's brightness is
gradually changed in a fading effect, increasing or
decreasing its intensity in small steps. The fade
direction is reversed when the brightness reaches the
lower or upper limit (0 or 255) to create a looping fade
effect.
3. Use a potentiometer to change the duty cycle of
PWM Signal and control brightness of LED. Rotating
the potentiometer should dim or brighten up the LED.
Arduino Code:
int ledPin = 9; // LED connected to digital pin 9
int analogPin = A0; // potentiometer connected to
analog pin 3
int val = 0; // variable to store the read value

void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4);// analogRead values go
from 0 to 1023, analogWrite values from 0 to 255
}
Link to Tinkercad Simulation:
https://www.tinkercad.com/things/2SSIhO8SYTT-copy-
of-ee-312-exp3-part-3/editel?tenant=circuits
Description:
As you turn the potentiometer, the analog value read
from it changes, and this change is reflected in the
LED's brightness. The division by 4 ensures that the
LED's brightness changes smoothly and visibly in
response to the potentiometer's movement.
In summary, this code creates an interactive setup
where the LED's brightness is controlled by the position
of a potentiometer, providing a simple demonstration
of analog input and PWM output in Arduino
programming.
4. Use PWM pins and analogWrite() to carry out task
(c).
Arduino Code:
int bright = 0;

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

void loop()
{
for (bright = 0; bright <= 255; bright += 5) {
analogWrite(5, bright);
delay(100);
}
for (bright = 255; bright >= 0; bright -= 5) {
analogWrite(5, bright);
delay(100);
}
}
Link to Tinkercad Simulation:
https://www.tinkercad.com/things/dGimOzvK7Ma-
copy-of-task-4/editel?tenant=circuits
Description:
This combination of the two loops creates a continuous
loop where the LED gradually fades in and then fades
out repeatedly. The delay() functions control the pace
of the fade-in and fade-out transitions.
In essence, this code demonstrates a simple animation
effect using PWM to smoothly adjust the LED's
brightness, resulting in a visually appealing fading
pattern.
5. Control a servo motor using (i) generated PWM (ii)
Servo.h library
(i)

Arduino Code:
int servo = 9;

int angle;
int pwm;

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

void loop ()
{
for (angle = 0; angle <= 140; angle += 5) {
servoPulse(servo, angle); }
for (angle = 140; angle >= 0; angle -= 5) {
servoPulse(servo, angle); }
}

void servoPulse (int servo, int angle)


{
pwm = (angle*11) + 500; // Convert angle to
microseconds
digitalWrite(servo, HIGH);
delayMicroseconds(pwm);
digitalWrite(servo, LOW);
delay(50); // Refresh cycle of servo
}
Link to Tinkercad Simulation:
https://www.tinkercad.com/things/73XVVhCwgf3-
copy-of-ee312-exp-3-part-5servo-control-without-
library/editel?tenant=circuits
Description:

It controls a servo motor, which is a device capable of


moving to specific angles, typically between 0 and 180
degrees. The servo's movement is controlled by
sending it PWM (Pulse Width Modulation) signals that
dictate the angle it should move to.
the code demonstrates the controlled movement of a
servo motor by sending it specific PWM signals that
dictate the angle it should move to. The loops cause
the servo to smoothly move back and forth between 0
and 140 degrees, creating a repetitive motion.
(ii) Using Servo:

Arduino Code:
#include <Servo.h>

int pos = 0;

Servo servo_9;

void setup()
{
servo_9.attach(9);
}

void loop()
{
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 180; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
for (pos = 180; pos >= 0; pos -= 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
Link to Tinkercad Simulation:
https://www.tinkercad.com/things/d00OTmcEKUN-
copy-of-ee312-exp-3-part-5servo-control-using-
library/editel?tenant=circuits
Description:
We do using the servo library.
6. What is the need of PWM? Why do we use it
instead of digital square waves of 50% duty cycle?
PWM, or Pulse Width Modulation, is a technique
widely used in electronics to control the intensity of
analog devices (such as motors, LEDs, and servos) using
digital signals. It involves rapidly toggling a digital signal
on and off with varying duty cycles (the ratio of time
the signal is high to the total time of one period). This
technique offers several advantages over using a simple
digital square wave with a fixed 50% duty cycle:
1. Variable Intensity Control: PWM allows for precise
control over the intensity or output level of analog
devices. By adjusting the duty cycle, you can
effectively change the average power delivered to
the device, thereby controlling its behavior. This is
crucial for applications where you need smooth
and gradual variations in output, such as
controlling the speed of a motor or the brightness
of an LED.
2. Efficiency: PWM is efficient because it controls
power delivery by changing the duty cycle. When
the digital signal is high, power is being supplied,
and when it's low, power consumption drops
significantly. This is particularly advantageous for
devices that need varying power levels, as you can
control them without wasting excess energy.
3. Linear Control Mapping: PWM provides a linear
mapping between duty cycle and output level. For
instance, if you increase the duty cycle by 10%, the
output level also increases by a corresponding
amount. This makes it easier to design systems
where the input-to-output relationship needs to be
proportional and predictable.
4. Ease of Implementation: PWM can be easily
generated using microcontrollers and other digital
circuitry. It requires minimal hardware compared
to generating true analog signals. Creating precise
digital square waves with varying duty cycles is
more straightforward and cost-effective than
generating true analog waveforms.
5. Compatibility with Digital Circuits: Many
microcontrollers and digital components work with
digital signals. PWM provides a way to interface
between digital control systems and analog devices
without complex analog-to-digital conversion
circuits.
6. Reduced Heat Dissipation: In applications where
you need to control the output level by dissipating
excess energy as heat (as in voltage regulation
circuits), PWM can be more efficient. By toggling
the output on and off rapidly, you can reduce the
average heat dissipation compared to keeping the
output always on.
In contrast, using a fixed 50% duty cycle digital square
wave would provide only two states (full-on and full-
off), making it unsuitable for smoothly controlling
analog devices. PWM's ability to provide variable
intensity control, efficiency, and linear mapping makes
it an essential tool in modern electronics for interfacing
digital control systems with analog devices effectively.
7. What are the Drawbacks of using generated
PWM signals instead of Arduino PWM hardware.
Processing Overhead: When generating PWM
signals in software, the microcontroller's processor
has to manage the timing and duty cycle of the
signal. This can introduce processing overhead and
reduce the available processing power for other
tasks, potentially leading to slower response times
or reduced overall system performance.

Limited Precision and Resolution: Software-


generated PWM might have limited precision and
resolution compared to dedicated hardware PWM.
Hardware PWM modules often have higher clock
speeds and specialized counters, allowing for finer
control over duty cycle and frequency. Software-
based PWM might be susceptible to timing jitter,
which can result in less accurate control.

Interrupt Latency: Software-based PWM might


require interrupt-based timing control. This
introduces latency, as the processor needs to
handle interrupts, context switching, and other
tasks before generating the PWM signal. This
latency can be problematic in applications where
precise timing is crucial.

Stability and Accuracy: Hardware PWM modules


are designed to generate accurate and stable PWM
signals even under varying processor loads.
Software-based PWM might suffer from timing
variations due to fluctuations in processing load,
leading to inconsistent PWM signal generation.

Complexity: Software-based PWM can be more


complex to implement and maintain compared to
using hardware PWM, especially for intricate
applications that require multiple PWM signals
with different frequencies and duty cycles. It might
involve extensive code and timing management.

Limited Channels: Some microcontrollers have a


limited number of hardware PWM channels, which
could be insufficient for projects requiring
numerous PWM outputs. Using software to
generate additional PWM signals might further
strain the processor and impact overall
performance.
Higher Power Consumption: Generating PWM
signals in software can lead to increased power
consumption compared to using dedicated
hardware. This is because software-based PWM
might require the processor to operate at higher
clock speeds or remain active for longer periods,
consuming more energy.

Difficulty in Multitasking: In complex projects that


involve multitasking, generating PWM signals in
software could lead to timing conflicts and
difficulties in coordinating tasks. Hardware PWM
modules often operate independently, allowing for
smoother multitasking.

In summary, while software-generated PWM can


be a viable option when hardware PWM is
unavailable, it comes with limitations that affect
precision, efficiency, and overall system
performance. Dedicated hardware PWM modules
are purpose-built for generating accurate and
stable PWM signals, making them the preferred
choice in applications where precise control and
efficiency are crucial.

You might also like