Md. Khairul Alam
Published © CC BY

Complete Motor guide for Robotics

Robot is an electromechanical device which is capable of reacting in some way to its environment, and take autonomous decisions or action...

BeginnerFull instructions provided59,701
Complete Motor guide for Robotics

Story

Read more

Code

Code snippet #1

Plain text
    /*
    Single transistor DC Motor control
    */
     
     
    int motorPin = 3;
    int speed = 100;
     
    void setup() 
    { 
      pinMode(motorPin, OUTPUT);     
    } 
     
    void loop() 
    { 
     // analogWrite() function is used to generate PWM signal.
     // speed define the duty cycle of the PWM.
     // if the speed = 0 means duty cycle is 0 and motor is off
     // the maximum value of speed can be 255, then motor will run with maximum speed
     analogWrite(motorPin, speed); 
     delay(1000);  // wait 1 sec
     analogWrite(motorPin, 175);
     delay(1000);
     analogWrite(motorPin, 255); // maximum speed
     delay(1000); 
    } 

Code snippet #2

Plain text
const int switchPin = 2;    // switch input
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin

//In the setup(), set all the pins for the H-bridge as outputs, 
//and the pin for the switch as an input. The set the enable pin high 
//so the H-bridge can turn the motor on.

void setup() {
    // set the switch as an input:
    pinMode(switchPin, INPUT); 
 
    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    pinMode(ledPin, OUTPUT);
 
    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
  }

//In the main loop() read the switch. If it’s high, 
//turn the motor one way by taking one H-bridge pin high and the other low.
// If the switch is low, reverse the direction by reversing the states of
// the two H-bridge pins.
void loop() {
    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
    }
  }

Code snippet #3

Plain text
/*
 * created by Md. Khairul Alam
 * Control 2 DC motors with arduino
 * 2015
 */
int motor1Pin1 = 3; // pin 2 on L293D IC
int motor1Pin2 = 4; // pin 7 on L293D IC
int motor1EnablePin = 6; // pin 1 on L293D IC
int motor2Pin1 = 8; // pin 10 on L293D IC
int motor2Pin2 = 9; // pin 15 on L293D IC
int motor2EnablePin = 11; // pin 9 on L293D IC

int Speed = 100; 

void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor1EnablePin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(motor2EnablePin, OUTPUT);
    // sets enable1Pin and enable2Pin high so that motor can turn on:
    //digitalWrite(Motor1EnablePin, HIGH);
    //digitalWrite(Motor2EnablePin, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}

void loop() {
    //write your code here
}

void forword(){ // run two motor in forward direction
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    analogWrite(motor1EnablePin, Speed);
    analogWrite(motor2EnablePin, Speed);
    //Serial.println("Go Forward!");
}

void backword(){ // run two motor in reverse direction
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH); 
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    analogWrite(motor1EnablePin, Speed);
    analogWrite(motor2EnablePin, Speed);
    //Serial.println("Go Reverse!");

}

void turnRight(){ //  motor 1 off, motor 2 forward
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    analogWrite(motor1EnablePin, Speed);
    analogWrite(motor2EnablePin, Speed);
    //Serial.println("Turn Right");

}

void turnLeft(){ // motor 2 off, motor 1 forward
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
    analogWrite(motor1EnablePin, Speed);
    analogWrite(motor2EnablePin, Speed);
    //Serial.println("Turn Left");

}

void Stop(){ two motor off
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
    analogWrite(motor1EnablePin, Speed);
    analogWrite(motor2EnablePin, Speed);
    //Serial.println("Stop");

}

Code snippet #4

Plain text
// For calibrating you may use the following code snipet
#include <Servo.h>
#define MAX_SIGNAL 2000
#define MIN_SIGNAL 700
#define MOTOR_PIN 9

Servo motor;

void setup() {
  Serial.begin(9600);
  Serial.println("Program begin...");
  Serial.println("This program will calibrate the ESC.");

  motor.attach(MOTOR_PIN);

  Serial.println("Now writing maximum output.");
  Serial.println("Turn on power source, then wait 2 seconds and press any key.");
  motor.writeMicroseconds(MAX_SIGNAL);

  // Wait for input
  while (!Serial.available());
  Serial.read();

  // Send min output
  Serial.println("Sending minimum output");
  motor.writeMicroseconds(MIN_SIGNAL);

}

void loop() {  

}

// For controlling you may use the following code

#include <Servo.h>  // include servo library
Servo esc;
int throttlePin = 0;
 
void setup()
{
esc.attach(9);
}
 
void loop()
{
int throttle = analogRead(throttlePin); // read from pot
throttle = map(throttle, 0, 1023, 0, 179);
esc.write(throttle); // throttle value define the speed of esc
}

Code snippet #5

Plain text
#include <Servo.h>

Servo myServo;  // create servo object to control a servo
                // twelve servo objects can be created on most boards

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

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

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

Code snippet #6

Plain text
// Global variables
int timeDelay = 3000;

void setup(){
  // declaring the four pins to be outputs
  pinMode(34, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(28, OUTPUT);
  // setting the inital state of the electromagnets
  digitalWrite(34, HIGH);
  digitalWrite(32, LOW);
  digitalWrite(30, LOW);
  digitalWrite(28, HIGH);
  delay(10); // a small time delay to allow the motor to move
}// end of setup

void loop(){
  for(int i=0; i<202; i++){ // looping through this chunk of code for ~ a full rotation
    digitalWrite(30, LOW);
    digitalWrite(34, HIGH);
    delayMicroseconds(timeDelay);
    digitalWrite(28, LOW);
    digitalWrite(32, HIGH);
    delayMicroseconds(timeDelay);
    digitalWrite(34, LOW);
    digitalWrite(30, HIGH);
    delayMicroseconds(timeDelay);
    digitalWrite(32, LOW);
    digitalWrite(28, HIGH);
    delayMicroseconds(timeDelay);
  }// end of looping
  // preparing the electromagnets to go the other direction
  digitalWrite(32, HIGH);
  digitalWrite(28, LOW);
  delayMicroseconds(timeDelay*500);
  
  
  for(int j=0; j<204; j++){ // looping through this chunk of code for ~ a full rotation in the other direction
    digitalWrite(30, LOW);
    digitalWrite(34, HIGH);
    delayMicroseconds(timeDelay);
    digitalWrite(28, HIGH);
    digitalWrite(32, LOW);
    delayMicroseconds(timeDelay);
    digitalWrite(34, LOW);
    digitalWrite(30, HIGH);
    delayMicroseconds(timeDelay);
    digitalWrite(32, HIGH);
    digitalWrite(28, LOW);
    delayMicroseconds(timeDelay);
  }// end of looping
  // preparing the electromagnets to go the other direction
  digitalWrite(32, LOW);
  digitalWrite(28, HIGH); 
  delayMicroseconds(timeDelay*500);
  
}//end of loop

Credits

Md. Khairul Alam

Md. Khairul Alam

68 projects • 585 followers
Developer, Maker & Hardware Hacker. Currently working as a faculty at the University of Asia Pacific, Dhaka, Bangladesh.

Comments