Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Change the Speed of a DC Motor with the Arduino

Sometimes you need to have greater control over the speed of your motor, which the Arduino allows you to do with the MotorSpeed sketch. The following shows you how to control the speed of your motor with the same circuit.

The MotorSpeed sketch

Open a new Arduino sketch, save it with a memorable name, such as myMotorSpeed, and then type the following code.

int motorPin = 9;
void setup(){
 pinMode(motorPin, OUTPUT);
}
void loop() {
 for(int motorValue = 0 ; motorValue <= 255; motorValue +=5){
 analogWrite(motorPin, motorValue); 
 delay(30);      
 }
 for(int motorValue = 255 ; motorValue >= 0; motorValue -=5){
 analogWrite(motorPin, motorValue); 
 delay(30);      
 }
}

After you’ve typed the sketch, save it and press the Compile button to check your code. The Arduino Environment should highlight any grammatical errors in the Message Area if they are discovered.

If the sketch compiles correctly, click Upload to upload the sketch to your board. When uploading is done, you should have a motor that spins very slowly to start with, speeds up to its fastest spin, spins back down to a stop, and then repeats. It can be difficult to see this, so you should fix something more visible, such as adhesive putty, to show you what’s going on.

You may find that at its slowest point, the motor just hums. If so, this is not a problem; it just means that the electromagnet doesn’t have enough voltage to spin the motor; it needs more voltage to generate the magnetism and gain momentum.

The MotorSpeed sketch breakdown

The pin you are using to control the motor circuit, digital pin 9, is declared.

int motorPin = 9;

Because it’s an output, you define it in setup.

void setup() {
 pinMode(motorPin, OUTPUT);
}

In the main loop, you use analogWrite to send a PWM value to pin 9. This is the same principle as in the Fade sketch, used to fade an LED. The first for loop sends a gradually increasing value to pin 9 until it reaches the maximum PWM value of 255. The second for loop gradually returns this value to 0; then the cycle repeats itself

void loop() {
 for(int motorValue = 0 ; motorValue <= 255; motorValue +=5){
 analogWrite(motorPin, motorValue); 
 delay(30);      
 }
 for(int motorValue = 255 ; motorValue >= 0; motorValue -=5){
 analogWrite(motorPin, motorValue); 
 delay(30);      
 }
}

This process could be likened to revving a car engine. If you push the pedal down, you accelerate to full speed. If you tap the gas pedal, the engine accelerates and then slows down. If you tap it at a constant rate before it slows, you will maintain some of the momentum of the spinning motor and achieve an average (if somewhat jerky) speed.

This is what the transistor is doing, but very quickly. The intervals between on and off and the momentum of the motor allow you to achieve analog behavior from a digital signal.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!