Arduino Projects For Dummies
Book image
Explore Book Buy On Amazon

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.

<b>int</b> motorPin = 9;
void setup(){
 <b>pinMode</b>(motorPin, <b>OUTPUT</b>);
}
void loop() {
 <b>for</b>(<b>int</b> motorValue = 0 ; motorValue <= 255; motorValue +=5){
 <b>analogWrite</b>(motorPin, motorValue); 
 <b>delay</b>(30);      
 }
 <b>for</b>(<b>int</b> motorValue = 255 ; motorValue >= 0; motorValue -=5){
 <b>analogWrite</b>(motorPin, motorValue); 
 <b>delay</b>(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.

<b>int</b> motorPin = 9;

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

void setup() {
 <b>pinMode</b>(motorPin, <b>OUTPUT</b>);
}

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() {
 <b>for</b>(<b>int</b> motorValue = 0 ; motorValue <= 255; motorValue +=5){
 <b>analogWrite</b>(motorPin, motorValue); 
 <b>delay</b>(30);      
 }
 <b>for</b>(<b>int</b> motorValue = 255 ; motorValue >= 0; motorValue -=5){
 <b>analogWrite</b>(motorPin, motorValue); 
 <b>delay</b>(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.

About This Article

This article can be found in the category: