From Wikiversity
Jump to navigation Jump to search

Arduino Motor Control Tutorials[edit]

Servo Motors[edit]

Servo Motors can be positioned from 0-180 degrees Internal feedback circuitry & gearing takes care of the hard stuff. Easy three-wire PWM 5V interface. Servos are used for Roboticists, movie effects people, and puppeteers use them extensively any time you need controlled, repeatable motion. Can turn rotation into linear movement with clever mechanical levers. Come in all sizes from super-tiny to drive-your-car. But all have the same 3-wire interface.

Pulse width Modulation[edit]

More commonly known as PMW. This is used because computer cant use output analog signals so PWM is used to fake it. Using digital voltages and in th Arduinos case the two digital signals are 0 volts and 5 volts.

  • The way this works is you average the output voltage over a given amount of time on vs. off. Here is the equation for figuring out the final output voltage
    • output_voltage = (on_time / off_time) * max_voltage
    • ex. if you put power out 75% of the time and have it off 25% of the time you will come up with an output of 3.75 volt
    • On 50% Off 50% output 2.5 volts
    • On 20% Off 80% output 1.0 volts

How to Control a Servo With PWM[edit]

PWM freq is 50 Hz (i.e. every 20 millisecs)

  • Pulse width ranges from 1 to 2 millisecs
    • 1 millisec = full anti-clockwise position
    • 2 millisec = full clockwise position

To position, send a pulse train from 1 to 2 ms

  • To hold a position, pulses must repeat
  • Takes time to rotate, so pulse too fast & it won’t move
int servopin = 4; //These first four lines are where you define your scope this 

//first line defines which digital pin you are using on your arduino in this case its four because thats the pin I used but you 

//could use any pin from 1-12
int pulse = 2500; // This defines your Output in microseconds and determines what position you motor will go to the

 //word pulse can be replaced with anything because its just a variable being defined as 2500 in this case

void setup() //These next three lines setup the program to begin you will need this in all your programs in one form or another
  pinMode(servopin, OUTPUT);

void loop() // The void loop function tells the motor to keep running through this program on a constant loop
  digitalWrite(servopin, HIGH);// this is where the action is tells the motor to go on
  delayMicroseconds(pulse); //this is where the PULSE that we defined in the second line earlier this i counted in micro seconds
  digitalWrite(servopin, LOW);

Adjusting the Code To Do What you Want[edit]

The code that I posted has the motor go the its full range of 180 degree motion this is done in the first line of the code where I define the number of micro seconds as 2500 this sets the motor to go to 180 degree as seen in this [1]

  • Then I change the value to 1500 in the second line of the code to read int Pulse=1500 this sets the motor to the 90 degree position


For this I used the most simple set up connecting using the servo motor and the arduino motor.

  • Black wire goes to GRD
  • Orange to digital pin 4
  • Red to the 5V

Pictured below

Add caption here