Pages: [1] 2   Go Down
Author Topic: Running a DC motor with PWM  (Read 3242 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. I am really new to this and just trying to figure out some things so that I can have some fun.  I have been messing around with the PWM and have been wanting to get a DC motor to ramp up slowly and stay at Full speed until I tell it to Slow down.  Kinda like a soft start up.  I can get it to ramp up but then when it goes through the loop again it starts at 0 and comes back up to 255 over and over.  I want it to stop at 255 until I tell it to slow down. 
Logged

Offline Offline
Faraday Member
**
Karma: 67
Posts: 2699
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do a digital write from 0 to 255 with some add delay() then repeat.
EX:

loop()
for (int x=1; x<256;x++)
{
// look at the digital write code & place here
// add some delay for the motor to move
}
// maybe some more delay to stop at 255 for a bit
Logged

The way you have it in your schematic isn't the same as how you have it wired up!

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't want it do go down on its own.  I want it to go up and then stay up
Logged

Offline Offline
Faraday Member
**
Karma: 67
Posts: 2699
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
loop()
for (int x=1; x<256;x++)
{
// look at the digital write code & place here
// add some delay for the motor to move
}
Place the above in setup()
In loop() look at a digital i/p switch use an 'if' statement, then run a 'for' loop to slow down your motor.
Logged

The way you have it in your schematic isn't the same as how you have it wired up!

The Netherlands
Online Online
Edison Member
*
Karma: 50
Posts: 1672
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Jason2548.

The for.. loop will count until a set value.
If you are in a loop (like void loop), you will always start at the initial value (here 1) and end up with the final value (255), and do that over and over again.

So to get rid of that, you can set the initial value for X in void setup.
In void loop you will increase X every time, until you reach your end value.
In that case you will not increase, because increasing will get to 0 again (an overflow will occur).

By the way:
You'll see that your motor will not start running immediatly.
It might start at 20 % or later.
And it might also reach its maximum speed before it's at 255, but that will be hard to find out.
So it would be nice for your experiment to keep track of where you are when the motor starts running.
You can do that using a display or using serial and a terminal to see the output.
You would also need to slow down the update rate of your PWM, because else you will hardly see the speed ramping up.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the input.  I am going to try some stuff and see what happens.  If your wondering this is for a little HO train.  I am going to have a pro mini in the train with a motor driver.  Its going to be controlled by infrared lights that tell it when to stop and when to go.  getting the motor to ramp up and stay up is my only problem.  I will try somethings today.  Thank you for your help.
Logged

The Netherlands
Online Online
Edison Member
*
Karma: 50
Posts: 1672
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice application.
For this use, you will for sure need to find out the startspeed.
And you'll probably want to slow down the steps (as i mentioned) to get a less steep ramp.
Else your train (mass, try different configurations/number of cars) might interfere with the slowing down or speeding up.

Do you also plan on a failsafe, in case your optical communication fails ?

Keep us informed on your progress please.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Beijing
Offline Offline
Full Member
***
Karma: 3
Posts: 193
Skype name habib.derbyshire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This sounds like a nice project. What type of driver are you using? A driver IC, or MOSFETs, or a transistor circuit?
Logged

What is man's best friend? The breadboard!

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its going to be a little motor driver from sparkfun.
Here is my plan.  I am going to supply 12vdc to the track and use that to control the trains that have a ProMini inside them with the little 2amp motor driver. 
On the front of each train is going to be the IR RX. Then is going to let the train stop and start were I want it. 
All of the track switches are going to be controlled with a servo. 
Then under the track is going to be some IR sensors that will let me know were the trains are and by the programming would not let them be on the same track at the same time. The failsafe I was thinking that if two trains are on the same track and the sensors see that then it would just shut off power to the whole track so that everything stops. 
I also am thinking about using just regualar leds to see were the trains are too.  I read this the other day and thought it would make it easier and actaully really neat.  The only thing that is still throwing a fit is the motor control for the train.  I need to rebuild the program and see what I can do.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also.  The Arduino is very new to me and so is programming.  There is alot of stuff I do not understand.  But for really simple stuff I can make it work.  I have made some light controllers.  That is all I have done.  Also I made a submarine with the uno that worked pretty well except for the design.  Thats why with the motor control I really don't understand what I am doing yet.  I can get the motor to run but it ramps up and the stops and then ramps up again.  just the cycling.
Logged

Beijing
Offline Offline
Full Member
***
Karma: 3
Posts: 193
Skype name habib.derbyshire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you need to change the dirrection of the motor? If not, just use a MOSFET to drive it. Just Google "motor driver circuit"
Logged

What is man's best friend? The breadboard!

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what I am using but still not working.  I can't get it to stop once it reaches the 255


int PWMpin = 13;
void setup()
{
}
void loop()
{
   for (int i=0; i <= 255; i++){
      analogWrite(PWMpin, i);
      delay(10);
   }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some time I would like to control the direction.  I thought about using the Mosphet but it would simple to buy the chip.  It works great just can't get it to do what I want.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This actually does what I would like.  Just seems more complicated than it should be

int PWMpin = 13;

void setup()
{

}

void loop()
{
   int x = 1;
   for (int i = 0; i > -1; i = i + x){
      analogWrite(PWMpin, i);
      if (i == 255) x = 0;             
      delay(10);
   }
}
Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bro! Dem Mosphets is phat! Braaaaap!
Sorry, the temptation was too great! smiley-wink

You should be up and running now...

Have fun!
Logged

For whom does the clock pulse? It pulses for you!

Pages: [1] 2   Go Up
Jump to: