Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

session 7arduino -

The document provides an overview of Pulse Width Modulation (PWM) and its applications in controlling LED brightness and DC motor speed using Arduino. It explains key terms related to PWM, the Arduino functions for generating PWM signals, and the use of motor drivers like L293D and L298N for controlling DC motors. Additionally, it includes example code for implementing these controls in Arduino projects.

Uploaded by

anasmostafa801
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

session 7arduino -

The document provides an overview of Pulse Width Modulation (PWM) and its applications in controlling LED brightness and DC motor speed using Arduino. It explains key terms related to PWM, the Arduino functions for generating PWM signals, and the use of motor drivers like L293D and L298N for controlling DC motors. Additionally, it includes example code for implementing these controls in Arduino projects.

Uploaded by

anasmostafa801
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Prepared by:

Esraa taha ali


PWM
PWM
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 .
It is used as an alternative to DAC.
PWM

The Arduino digital pins can gives us 5V or 0V and the output is


can be 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.
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. •
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.
Duty Cycle
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 90Hz (90 times ON and OFF per
second), then the led will be seen glowing at half brightness by
the human eye.
Duty Cycle
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.
Arduino and PWM

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.
Arduino and PWM
On Arduino Uno, the PWM pins are 3, 5, 6, 9, 10 and
11. The frequency of PWM signal on pins 5 and 6
will be about 980Hz and on other pins will be 490Hz.
The PWM pins are labeled with ~ sign.
Example

Controlling Brightness of LED through


Code
Code
//Initializing LED Pin
int led_pin = 6;
void setup() {
//Declaring LED pin as output
pinMode(led_pin, OUTPUT);
}
void loop() {
//Fading the LED
for(int i=0; i<255; i++){
analogWrite(led_pin, i);
delay(5);
}
for(int i=255; i>0; i--){
analogWrite(led_pin, i);
delay(5);
}
}
DC motor
DC motors
works on the principal, when a motor This DC or direct current
current carrying conductor is placed in a magnetic field, it
experiences a torque and has a tendency to move
DC motor speed control

important note
We can use PWM signal to control the speed of
the DC Motor (when the duty cycle of PWM signal
increases then the speed increases).
.
DC Motor

We can’t connect a DC Motor directly


to a microcontroller?
1-A microcontroller can’t supply the current required for the §
working of DC Motor.
arduino can source or sink currents up to 40mA but a DC §
Motor needs current very much more than that.
2-The negative voltages created due to the back emf of the •
motor may affect the proper functioning of the
microcontroller.
4-The operating voltage of the DC Motor may be much higher than
the operating voltage of the microcontroller.
Relay
Relay types
Relay connections
code
#define Relay 10
void setup() {

pinMode(Relay,OUTPUT);
}

void loop() {
digitalWrite(Relay,HIGH);}
Proteus design
H-Bridge
It is used to control the direction of motor.
H-Bridge
code:
To control direction using H-bridge.

int motor1pin1 = 10;


int motor1pin2 = 11;
void setup() {
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);

}
void loop() {
// put your main code here, to run repeatedly:

digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
delay(1000);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
delay(1000);
}
Motor Driver
Instead of using previous circuit you may use transistorized H
Bridge in which freewheeling diodes are used to avoid
problems due to back emf. Thus it requires minimum four
transistors, diodes and resistors for each motor. It is better to
use readymade ICs such as L293D or L293 and L298N motor
driver instead of making your own H Bridge, which simplifies
your project.
L293D can provide up to 600mA current, in the voltage ranging
from 4.5 to 36v.
L293D
The L293D Motor Driver is a controller that uses an
H-Bridge to easily control the direction and speed of
up to 2 DC motors
L298N MOTOR driver
L298N PINOUT
L298N PINOUT
IN1 and IN2 control the direction of the motor
connected to OUT1 and OUT2.
IN3 and IN4 control the direction of the motor
connected to OUT3 and OUT4.
Input EN1 is used to control speed of motor 1and
EN2 for motor 2.
L298N
You can power the L298N with up to 12V by
plugging your power source into the pin on the
L298N labelled "12V". The pin labelled "5V" is a 5V
output that you can use to power your Arduino
code :

To control speed and direction of two


motors
Code
int motor1pin1 = 2;
int motor1pin2 = 3;
int speed_motor1=6;
int motor2pin1 = 4;
int motor2pin2 = 5;
int speed_motor2=9;
void setup() {
// put your setup code here, to run once:
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(speed_motor2, OUTPUT);
pinMode(speed_motor1, OUTPUT);
}
Code
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
analogWrite(speed_motor1,127);

digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(speed_motor2,127);
delay(1000);

digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
analogWrite(speed_motor1,127);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
analogWrite(speed_motor2,127);
delay(1000);
}
Proteus design

You might also like