Professional Documents
Culture Documents
Zaynsh L#2
Zaynsh L#2
Zaynsh L#2
Date:17.11.2023
Date : 17.11.2023 1 / 11
LAB#2: PWM in Arduino
1. INTRODUCTION
In this experiment we will learn about the PWM(pulse width modulator) and
duty cycle. PWM is a technique for controlling the power or intensity of
electrical signals. We going to use the function analogWrite(pin#,value) to
control he PWM signals on the pins(~3, ~5, ~6, ~9, ~10, ~11). The value that
the function takes is 255*duty cycle.
And we will learn about how to let the button activate in some point one
released or on click. (from high to low or low to high).
2. OBJECTIVES
1. To get to know how can we control the power or intensity of electrical signals using the arduino.
2. To know how to activate the button in same action.
3. COMPONENTS REQUIRED:
ID Component name Amount
1 Arduino 1
2 Resistances 2
3 Push button 1
4 Bread board 1
5 Wires As needed
6 USB 1
7 LEDS 1
8 computer 1
Date : 17.11.2023 2 / 11
LAB#2: PWM in Arduino
4. EXAMPLES:(IN CLASS)
//
INT BTN P IN = 8;
INT LED P IN = 5;
INT BTN O LD STATE =LOW;
VOID SETUP ()
{
PINMODE (LEDPIN, OUTPUT);
PIN M ODE ( BTN P IN , INPUT);
DIGITALWRITE ( LED P IN ,HIGH);
}
VOID LOOP ()
{
INT STATE = DIGITAL R EAD ( BTN P IN );
IF ( STATE == HIGH && BTN O LD S TATE == LOW && DIGITAL R EAD ( LED P IN ) ==HIGH)
{
DIGITAL W RITE ( LED P IN ,LOW);
}
ELSE IF ( STATE == HIGH && BTN O LD STATE ==LOW && DIGITAL R EAD ( LED P IN ) ==LOW )
{
DIGITAL W RITE ( LED P IN ,HIGH);
Date : 17.11.2023 3 / 11
LAB#2: PWM in Arduino
EX 2: change the brigntnes of a LED from 255 to 127 to 64 with deley 3 sec between each change.
// C++ CODE
//
VOID SETUP ()
{
ANALOG W RITE (9,255);
DELEY (3000);
DELEY (3000);
VOID LOOP ()
Date : 17.11.2023 4 / 11
LAB#2: PWM in Arduino
5. TASKS
TASK1: Connect two LEDs and a button to Arduino borad. One LED is on and the other is off. Each
time the button is released, the state of the LEDs is swiched.
INT PIN0 = 0;
INT PIN1 = 1;
INT PINI NPUT = 2;
INT PAST STATE = LOW;
VOID SETUP ()
{
PINM ODE(PIN0, OUTPUT);
PINM ODE(PIN1, OUTPUT);
PINM ODE(PINI NPUT , INPUT);
DIGITAL WRITE(PIN0, HIGH);
DIGITAL WRITE(PIN1, LOW);
}
VOID LOOP ()
{
INT STATE = DIGITALREAD(PININPUT);
{
DIGITAL WRITE(PIN0, LOW);
DIGITAL WRITE(PIN1, HIGH);
}
}
Date : 17.11.2023 5 / 11
LAB#2: PWM in Arduino
TASK 2: change the brigntness of a LED smoothly (30 level within a loop) from maximum to minimum
within 4 seconds.
int pinOutput=3;
void setup()
{
analogWrite(pinOutput,255);
}
void loop()
{
for (int i = 255; i >= 0; i--) {
analogWrite(pinOutput, i);
delay(4000 / 255);
}
delay(3000);
Date : 17.11.2023 6 / 11
LAB#2: PWM in Arduino
6. FLOWCHARTS
Ex 1
Ex 2
Date : 17.11.2023 7 / 11
LAB#2: PWM in Arduino
Task2
Task1
Date : 17.11.2023 8 / 11
LAB#2: PWM in Arduino
7. BLOCK DIAGRAM
Ex 1
Ex 2
Date : 17.11.2023 9 / 11
LAB#2: PWM in Arduino
Task 1
Task 2
Date : 17.11.2023 10 / 11
LAB#2: PWM in Arduino
8. PROCEDURES
1. First, we wrote the programs in task1.
2. We connected the breadboard with the Arduino VSS and GND.
3. We connected the led and resistance on the breadboard.
4. We connected the leds with pin0 and pin1 and push button with pin2.
5. we ran the program and saw the results directly.
6. we wrote the programs in task2.
7. We connected the led with pin3 (~3).
8. We ran the program then we run it and saw the result.
Date : 17.11.2023 11 / 11