Tuesday, August 28, 2012

0 Arduino Project 5 - Controlling a DC motor and a Servo Motor

Okay, so now I've shown you enough projects on LED's and it's time to move on! This project will show you how to control a DC motor and servo motor with an arduino. This is just a basic projetct in which you will learn how to rotate a servo back and forth, and to any position you want, speed up a DC motor and speed it down. So let's get it started!
Things you will need:

  • An Arduino Board
  • A DC motor
  • A servo motor
  • A breadboard
  • Some jumper wires
  • A transistor (NPN)
  • A diode
  • A capacitor (1 microFarad)
First up is controlling a DC motor - 
The schematic is shown here:

Now what the transistor does is, that it prevents the current from going into the motor if it's below a certain level. This means that we can control the speed of the motor by varying the amount of current that goes into the transistor (which of course is controlled via Arduino). The combination of the capacitor and the diode acts as a filter to prevent electrical noise. Now that you have the schematic, go ahead and wire up the circuit.

Now for the programming part, I will first give you the code with the explanation in the comments.

int MotorPin =9; // Tells the Arduino that motor is connected to pin 9

void setup()
{
pinMode (motorPin, OUTPUT); // Sets the motor as output
}

void loop()
{
for (int i=0; i<=255; i++)    // Creates a variable that starts the arduino at 0 speed and increments till 255
{
analogWrite(motorPin, i);   // Actually executes the previous line
delay(10);                         // Gives a delay of 10ms in each increment
}
delay(1000)                      // Holds it at top speed for a second
for (int i=255; i>=0; i--)    // Brings the motor back from top speed to stationary
{
analogWrite(motorPin, i);  // Executes previous
delay(10);
}
delay (1000)                   // Holds the motor at zero speed for a second
}

Note that the values only go up to 255. Why is that, you might ask? Well the answer is that what we are feeding to the motor, are analog values. Unlike digital values which have 2 states only, analog values can have 256 or even 1024 states. Since the motor is of 8 bit resolution, it can have 2^8= 256 states ( 0 - 255). If you did take the values above 255, the motor will show unpredictable behaviour. This is all for the lesson on DC motors.
You are welcome to come up with your version of the code and post it in the comments!

Now to the Servo motors! So what actually are servo motors? They are just motors with some modifications in them which allows them to turn at precise angles and unlike plain old motors, they can't do full revolutions.

Here to keep, the project simple, I will not use any addons with the servo. just connect the servo to the arduino and write a software that controls it. Though servos require some external power to work efficiently, for the purposes of this project the power supplied by the arduino is sufficient. Or one thing you can do is attach a 5V power source directly to the servo.
 If you notice, there will be three wires connected to the servo. The black is for ground and will go to the pin marked GND in the arduino. The red one is for the power and should go to the pin marked 5V. You can also connect the red and black wire to a 5V power source. DO NOT connect the servo to a source greater than 5V, otherwise you'll fry it! The last yellow one is the data wire that tell the servo what to do. This goes into any digital output pin, I'll use pin 9 for the purposes of this tutorial.
 Now for the coding part. Again I will just write the code and explain it in the comments.


#include <Servo.h>

Servo arushservo;                          // create servo object to control a servo
                                     

int i = 0;                                         // variable to store the servo position

void setup()
{
  arushservo.attach(9);                   // attaches the servo on pin 9 to the servo object
}


void loop()
{
  for(i = 0; i<= 180; i+20)              // goes from 0 degrees to 180 degrees
  {                                                 // in steps of 20 degree
    myservo.write(i);                       // tell servo to go to position in variable 'i'
    delay(1000);                             // waits 1 sec for the servo to reach the position
  }
  for(i = 180; i>=0; i-20)               // goes from 180 degrees to 0 degrees
  {                              
    myservo.write(pos);                  // tell servo to go to position in variable 'i'
    delay(1000);                             // waits 1 sec for the servo to reach the position
  }
}


 I think that should do the trick! Just upload the code to the board and watch the servo turn! It's exciting because you know you have told the servo to turn! There are many ways by which you can use an input to control the output of the servo. Some of which I will post at a later time! So that's all for now folks. See ya in the next post which will probably be about distance measurment!
Oh yeah! and if you've got any doubt whatsoever, just comment or mail me!

0 comments:

Post a Comment

Do Comment on Anything you like or dont like!

 

The Engineer's Spot! Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates