Thursday, 14 May 2015

Control speed of a dc motor with pwm in Arduino


Right, in this post I'm going to show you what "PWM" is and how it works?

Ok, the pwm stands for Pulse Width Modulation. What it means is that you kind of cntrol the power by switching the power on and off. And you control the duration of the power you putting in and the duration of the power that you are not giving.

So, let me give you an example, as you can see in the picture above, there are three examples in that picture. Every example's got same input power which is 5volts, but the output power is different.


Why?
Well, that's because in the first example you can see that the duration of the power is 75% then 25% no power then 75% power again so if you continue like this then the output power will be 3.75 volts.

Let's have a look at the second example. The input power is 5volts and the output is 2.5volts. That's because we can see in this example that the duration of power is 50% and none duration of power is 50% too. So we getting 2.5volts in result.

Ok, Let me give you a real example which I've done with Arduino, please watch the video and see how easily you control speed of a dc motor without any potentiometer.




There are three sketches, please upload one sketch at a time.

// sketch 1. high speed
int motor = 2;

// the setup routine runs once when you press reset:
void setup() { // initialize the digital pin as an output.
pinMode(motor, OUTPUT);


}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(motor, LOW);
delay(50);
digitalWrite(motor, HIGH);
delay(100);
}

// Sketch 2. medume speed
int motor = 2;

// the setup routine runs once when you press reset:
void setup() { // initialize the digital pin as an output.
pinMode(motor, OUTPUT);

}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(motor, LOW);
delay(25);
digitalWrite(motor, HIGH); //Motor on
delay(100);
}

// Sketch 3.low speed
int motor = 2;

// the setup routine runs once when you press reset:
void setup() { // initialize the digital pin as an output.
pinMode(motor, OUTPUT);

}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(motor, LOW);
delay(15);
digitalWrite(motor, HIGH);
delay(70);
}

Note: You can change the "delay" numbers according to your needs.

Circuit diagram: 


4 comments:

  1. Useful information. Fortunate me I found your web site accidentally, and
    I'm surprised why this coincidence did not came about earlier!
    I bookmarked it.

    my webpage ...

    ReplyDelete
    Replies
    1. Thanks my friend for bookmarking my page and I'm very happy hear that you've found it useful. :)

      Delete
  2. How to combine all three of the coding?
    What happen if i use 'if' statement coding in arduino?

    ReplyDelete
    Replies
    1. I've tried to combine all three codes but couldn't make it work. And the "if" statements might not work, because it keeps looping. So if you make a swicth for example, it will turn on the motor and then turn it off permanently until you click the switch again.

      Delete