[go: up one dir, main page]

0% found this document useful (0 votes)
18 views7 pages

Lab Manual Btech - Ardiuno

Ardiuno Manual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Lab Manual Btech - Ardiuno

Ardiuno Manual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

International Islamic University Islamabad

Faculty of Engineering and Technology


Department of Electrical Engineering

MICROPROCESSORS AND MICROCONTROLLER LAB

Lab 6 : PWM Signal Generation with Arduino

Name:

Reg. No:

Date of
Experiment:

OBE Rubrics Evaluation

a) PSYCHOMOTOR (To be judged in the field/lab during experiment)


Sr. Level 1 Level 2 Level 3 Level 4 Level 5 Marks
Criteria
No. (0%) (25%) (50%) (75%) (100%) Obtained
0 1.25 2.5 3.75 5
Practical
With several With few With some Without
Implementation/
1 critical errors, errors, errors, errors,
Arrangement of Absent
incomplete incomplete complete complete
Equipment
and not neat and not neat but not neat and neat
Use of 0 0.5 1 1.5 2
Equipment or
2 Limited Some Considerable
Simulation/ Absent Competence
competence competence competence
Programming Tool

(b) COGNITIVE (To be judged on the copy of experiment submitted)


Sr. Level 1 Level 2 Level 3 Level 4 Level 5 Marks
Criteria
No. (0%) (25%) (50%) (75%) (100%) Obtained
Algorithm Design 0 0.25 0.5 0.75 1
or Data Record, Complete
3 Complete with Complete
Analysis and Absent Incorrect with few
some errors and Accurate
Evaluation errors

(c) AFFECTIVE (To be judged in the field/lab during experiment)


Sr. Level 1 Level 2 Level 3 Level 4 Level 5 Marks
Criteria
No. (0%) (25%) (50%) (75%) (100%) Obtained
Level of 0 0.5 1 1.5 2
Participation &
Good Encouraging
4 Attitude to Achieve Rare sensible Some sensible
Absent sensible sensible
Individual/Group interaction interaction
interaction interaction
Goals

5 Total Marks Obtained (Out of 10):

Lab 6: PWM Signal Generation with Arduino Page 34


Objectives:
 What is PWM and how you can get the PWM output from the digital pins of Arduino
 To program and use the PWM feature of AVR
 To generate a square wave of different duty cycles using PWM feature of AVR
 To control the brightness of LED through programming and then we will control it
manually by adding the potentiometer.
Introduction:
PWM stands for Pulse Width Modulation and it is a technique used in controlling
the brightness of LED, speed control of DC motor, controlling a servo motor or
where you have to get analog output with digital means. The Arduino digital pins either
gives us 5V (when turned HIGH) or 0V (when turned LOW) and the output is a square
wave signal. So if we want to dim a LED, we cannot get the voltage between 0 and 5V
from the digital pin but we can change the ON and OFF time of the signal. If we will
change the ON and OFF time fast enough then the brightness of the led will be changed.
Before going further, let’s discuss some terms associated with PWM.
TON (On Time): It is the time when the signal is high.
TOFF (Off Time): It is the time when the signal is low.
Time Period: It is the sum of on time and off time.
Duty Cycle: It is the percentage of time when the signal was high during the time of
period.
So at 50% duty cycle and 1Hz frequency, the LED will be high for half a second and will
be low for the other half second. If we increase the frequency to 50Hz (50 times ON and
OFF per second), then the led will be seen glowing at half brightness by the human eye.

Lab 6: PWM Signal Generation with Arduino Page 35


Arduino and PWM:
The Arduino IDE has a built in function analogWrite() which can be used to generate
a PWM signal. The frequency of this generated signal for most pins will be about 490Hz
and we can give the value from 0-255 using this function. analogWrite(0) means a signal
of 0% duty cycle. analogWrite(127) means a signal of 50% duty cycle. analogWrite(255)
means a signal of 100% duty cycle. On Arduino UNO, the PWM pins are labeled with
~ sign.

BOARD PWM PINS PWM FREQUENCY

UNO, Nano, Mini 3, 5, 6, 9, 10, 11 490 Hz


(pins 5 and 6: 980 Hz)
Mega 2 - 13, 44 - 46 490 Hz
(pins 4 and 13: 980 Hz)
Leonardo, Micro, 3, 5, 6, 9, 10, 11, 490 Hz
Yún 13 (pins 3 and 11: 980 Hz)
Uno WiFi Rev.2 3, 5, 6, 9, 10 976 Hz

Controlling Brightness of LED through Code:


Connect the positive leg of LED which is the longer leg to the Pin No.11 of Arduino UNO.
Then connect the 220Ω resistor to the negative leg of LED and connect the other end of
resistor to the ground pin of Arduino as shown in Figure 1.

Lab 6: PWM Signal Generation with Arduino Page 36


Figure 1: Circuit Diagram to PWM Implementation

Now write the following code to change the brightness of the LED using PWM.
Arduino Code: PWD Generation
int led_pin = 11; // Initializing LED Pin
int i;
void setup() {
pinMode(led_pin, OUTPUT); // Declare LED pin as output
}
void loop()
{
for( i=0; i<255; i++) // Fading the LED
{
analogWrite(led_pin, i);
delay(10);
}
for( i=255; i>0; i--)
{
analogWrite(led_pin, i);
delay(10);
}
}

Arduino Code to manually control the Brightness of LED:


An addition to Figure 1, take a 10KΩ potentiometer and connect its left pin to GND
and right pin to 5V of Arduino. and then connect the center pin of potentiometer to the A0
Pin of Arduino as shown in figure 2.

Lab 6: PWM Signal Generation with Arduino Page 37


Figure 2: Manually Controlling Brightness of LED

Sketch:
Upload the code in the Arduino IDE and on moving the knob of the potentiometer, the
brightness of the LED will change.
int led_pin = 11; // Initializing LED Pin
int pot_pin = A0; // Initializing LED Pin
int data_10_bit, data_8_bit;
void setup() {
pinMode(led_pin, OUTPUT); // Declare LED pin as output
}
void loop()
{
data_10_bit = analogRead(pot_pin); // Reading from potentiometer
// Mapping the Values between 0 to 255 because we can give
// output from 0-255 using the analogwrite() funtion
data_8_bit = data_10_bit >> 2;
// data_8_bit = map(data_10_bit, 0, 1023, 0, 255);
analogWrite(led_pin, data_8_bit);
delay(10);
}

Lab 6: PWM Signal Generation with Arduino Page 38


Lab Task:
Generate a PWM Signal of frequency 490 Hz on Pin No.9 of Arduino UNO Board.
 Place an LED with 220 Ω resistor on Pin No.9.
 Connect Two Push Buttons on Pin No.2 and Pin No.4. and join their other ends
to Ground.
o Label one button as Up and Other button as Down.
o When we press the Up Button, it should increase the Duty Cycle of PWD
Signal
o When we press the Down Button, it should decrease the Duty Cycle of
PWD Signal.

Lab 6: PWM Signal Generation with Arduino Page 39


Lab 6 Task Solution:

Lab 6: PWM Signal Generation with Arduino Page 40

You might also like