Assignment 3
Assignment 3
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
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); }
}
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.