VEEROBOT Docs
StoreBlogWiki
  • 🌐Introduction
  • ROS Based Robots
    • 🐺Wolf Robot
      • ROS Introduction
      • Getting Started with ROS 2
      • Introduction to Wolf Robot
      • ROS 2 Setup
      • ROS Concepts
      • Using Wolf Examples
      • Creating a URDF File
      • Visualising in Gazebo
      • Controlling motors from ROS
      • LiDAR Basics & Control
      • Using a Camera with ROS2
      • ROS 2 Cleanup
      • ROS 2 Controller
      • Teleoperation
      • Slam Navigation
      • Wolf : Conclusion
  • Arduino Based Robots
    • 🚜Micro:Xbot
      • Preparations before First Use
      • Programming the Board
      • Powering the Board
      • Pin Mapping
      • Basic Programs
        • Blinking LED
        • Buzzer Program
        • Fading LED
        • RGB LED
        • Light Detection
        • Battery Voltage
        • Line Follower Module
        • Ultrasonic Sensor
        • Infrared Remote Control
        • Motor Control
        • Bluetooth Control
        • Display Test
        • Test Me
      • Robot Assembly
      • Advanced Programs
        • Bluetooth Controlled Robot
        • Obstacle Avoidance Robot
        • Line Follower Robot
  • ROS2 Repo
    • Introduction
    • Page 1
      • Sub Page 1
    • Page 2
Powered by GitBook
On this page
  1. Arduino Based Robots
  2. Micro:Xbot
  3. Basic Programs

Motor Control

Controlling motors using PWM signals

DC Motors

A direct current, or DC motor is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.

To control the direction of the spin of DC motor, without changing the way that the leads are connected, you can use a circuit called an H-Bridge. An H bridge is an electronic circuit that can drive the motor in both directions. H-bridges are used in many different applications and one of the most common being to control motors in robots. It is called an H-bridge because it uses four transistors connected in such a way that the schematic diagram looks like an "H."

In micro:Xbot we use a TB6612FNG motor controller which has four channels to control two DC motors. For a Four wheel drive robot, we can connect them in pairs where a single signal controls two motors.

The program below can be used to drive two motors and control their direction and speed. To test this program, connect two motors to the motor connectors on either side at the bottom of the board. Once the program is uploaded, the motors start to spin. Make sure they are held in place.

Motors require a lot of current to run. Before uploading the program, connect the battery to battery connector and slide the switch to turn on the board. Once this is done, connect FTDI connector and upload the program.

/*
  Motor Controller: Controls the direction and speed of two motors on either side

  At VEEROBOT, we invest time and resources providing this open source code,
  Please support VEEROBOT and open-source hardware by purchasing products
  from us @ http://veerobot.com
  -----------------------------------------------------------------------------
  You are free to redistribute it and/or modify it under the terms of the GNU
  Lesser General Public License as  published by the Free Software Foundation,
  either version 3 of the License, or (at your option) any later version.

  This Code is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  See <http://www.gnu.org/licenses/>
*/

#include "config.h"

void setup() {
  Serial.begin(9600);    // begin serial communication

  pinMode(EN1, OUTPUT);  // sets motor pins as output
  pinMode(EN2, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);  
  testMotors();          // run this function once
}

void loop() {
// we do not have anything to run in the loop
}


void testMotors() {
  LeftMotor(100, false);  // both motors forward
  RightMotor(100, false);
  delay(2500);

  LeftMotor(0, true);    // both motors stopped
  RightMotor(0, true);
  delay(500);

  LeftMotor(150, true);  // both motors reverse
  RightMotor(150, true);
  delay(2500);

  LeftMotor(0, true);    // both motors stopped
  RightMotor(0, true);
  delay(500);

  LeftMotor(125, true);  // Left forward and Right Backward
  RightMotor(200, false);
  delay(2500);

  LeftMotor(0, true);    // both motors stopped
  RightMotor(0, true);
  delay(500);

  LeftMotor(150, false);  // Left backward and Right forward
  RightMotor(75, true);
  delay(2500);

  LeftMotor(0, true);      // both motors stopped
  RightMotor(0, true);
  delay(500);
}

void LeftMotor(int pwm, boolean reverse) // left motor
{
  analogWrite(EN1, pwm); //set pwm control, 0 for stop, and 255 for maximum speed
  if (reverse)
  {
    digitalWrite(IN1, HIGH);
  }
  else
  {
    digitalWrite(IN1, LOW);
  }
}

void RightMotor(int pwm, boolean reverse) // right motor
{
  analogWrite(EN2, pwm);
  if (reverse)
  {
    digitalWrite(IN2, HIGH);
  }
  else
  {
    digitalWrite(IN2, LOW);
  }
}

The above program runs motors in different directions and different speeds. After each run, the motors are stopped and the direction is changed. This is a good practice so that there is not too much stress on the motors. When motors are reversed, the shaft produces reverse mechanical energy and the motors winding get a reverse electrical energy. So, we are fighting the inertia of the system. This produces a large current spike and also reduces the life of the motor.

Though the board and the controller is stable enough to overcome these large spikes, its a good practice to either slow down or stop the motor before reversing.

PreviousInfrared Remote ControlNextBluetooth Control

Last updated 2 years ago

🚜