GSM Based DC Motor Speed Control

In this post we are going to make a DC motor control circuit whose speed and rotational direction can be set / controlled via SMS commands. By sending appropriate SMS commands to the circuit, you can control a brushed DC motor of 6V to 12V rated up to 2A. We can set 255 different levels of speed in both the directions.

We will see:

  • Block Diagram.
  • Full Circuit Diagram.
  • What is L298N Motor Driver?
  • What is PWM control?
  • Program code and Its Explanation.
  • How to Operate This Circuit Properly.

Overview:

Motors are more frequently used in our daily life than you might think. From irrigation / water pump motor to vibration motor in your smartphone, from electric vehicles to gasoline / diesel ignition motor and from electric saw to electric tooth driller.

Electric motors did a great help to human kind and pushed us light years ahead from an era where man power decided health of an economy to an automated zero man economy, of course with a smart electronic brain.

Turning ON and OFF motor is not enough for many applications; we need to precisely control the rotational speed and the direction of motors. To do this we are using a method called PWM which we will learn in detail.

For many applications we might need to start or stop / control speed of motors remotely, so we introduced GSM in the circuit so that we can control the DC motor anywhere in the world.

Block Diagram:

Block Diagram of GSM DC Motor Speed Control
Block Diagram of GSM DC Motor Speed Control

Full Circuit Diagram:

GSM based DC motor speed control circuit
GSM based DC motor speed control circuit

The GSM based motor control circuit consists of a GSM modem for sending and receiving SMS, an Arduino board will interpret the received SMS command and gives out control signal to L298N DC motor driver which drives the motor at the desired speed and direction.

The 12V power supply is shared by L298N driver and GSM module SIM 800 / 900. 5V for Arduino is derived from L298N driver (you can spot this on block diagram) which has built-in 5V regulator which is fed to VIN pin of Arduino.

The Tx of the GSM is connected to Rx of Arduino and Rx of the GSM is connected to Tx of Arduino. Pin #9 is the speed control pin which is connected to ENA of the L298N and pins 7 and 6 are direction control which is connected to IN1 and IN2 of L298N.

You need to insert a valid SIM card with a working SMS plan.

NOTE: Do NOT use JIO SIM cards with GSM module; it is not 2G backward compatible. 

Detailed L298N Pin configuration:

L298N
L298N
L298N
L298N

What is L298N Module?

L298N is a dual H-bridge DC motor driver module, meaning this module can control two DC motors simultaneously, the main IC in the module has two H-bridges for controlling two DC motors independently.

It has control pins for each motor, through which we can control speed and direction of each motor independently. The L298N can operate from 6V to 12V and can control two motors of 6V to 12V. L298N module can handle motors rated up to 2A. We are going to use only one motor in this project.

Diagram representation:      

L298N
L298N

Functions of each pins in L298N:

  • There are two motor outputs on two sides of the module, OUT1 and OUT2 controls one motor and OUT3 and OUT4 control another motor.
  •  There is a +12V input, a ground and a +5V output (Yes, 5V output) terminals. We need to connect 6-12V input to +12V and GND terminals. 5V output terminal can be used to power Arduino and other 5V devices if needed. 
  • Beside these three terminals there are bunch of male connectors which controls the direction and speed of the motors. By connecting ENA to 5V this will enable the motor at OUT1 and OUT2 and by connecting ENA to GND, OUT1 and OUT2 will be disabled, similarly for ENB which is associated with OUT3 and OUT4.
  • IN1 and IN2 are the direction control for motor at left hand side (out1, out2). By applying +Ve at IN1 and –Ve at IN2 the motor rotates in a direction, by reversing the applied signal polarity at IN1 and IN2 will reverse the rotating direction of the motor at OUT1 and OUT2.
  • Similarly for IN3 and IN4 which is associated with OUT3 and OUT 4.

How to control the speed and direction of the motor:

Controlling the Speed:

We can control the speed of the motor by applying PWM signal to ENA or ENB depending on at which output your motors are at, at the same time we should keep the direction signal at a constant voltage and polarity. Some of you may or may not know what PWM is, so here is the brief explanation on PWM or Pulse Width Modulation and its power delivery.

PWM is a modulation technique used for delivering constant power output for loads like brushed DC motors. PWM is modulated at a constant frequency and voltage, in this circuit the frequency is 490Hz at a constant voltage you applied to L298N.

In PWM we are changing the duty cycle of the frequency, that is ON and OFF time in a cycle. If we keep 50% ON and 50% OFF in a cycle to a DC motor, it will rotate half of its full speed. If we keep the duty cycle at 25% that is 25% ON and 75% OFF, the motor will rotate 25% of its maximum speed. If you keep the pulse at 100% of the time ON (the pulse will be straight line on graph) the motor will rotate at 100% of its speed.

So you got the point, right?

PWM looks like this on graph:

PWM Duty Cycle
PWM Duty Cycle

The DC motor will average out the pulsating power in to a constant rotational motion. So by simply changing the pulse width we can control the power delivery to a DC motor thus its rotational speed.

Direction Control:

Direction control in DC motor is very simple; by just reversing the polarity across the terminal will change rotational direction of the motor. This is done by pin 7 and 6 of the Arduino which is applied to IN1 and IN2 of L298N.

Program code:

//----------------(c)Electronics Project Hub-------------//
const int IN1 = 6;
const int IN2 = 7;
int pwmPin = 9;
boolean received = false;
char str[15];	
int i = 0;
int value = 0;
int PWM_value = 0;
//---------------------------//
char number[] = "+91xxxxxxxxxx"; 
//---------------------------//
void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  analogWrite(pwmPin, 0);
  delay(20000);
  delay(20000);
  delay(20000);
  Serial.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
  Serial.println("AT+CMGF=1");
  delay(1000);
  Serial.print("AT+CMGS=");
  Serial.print("\"");
  Serial.print(number);
  Serial.println("\"");
  delay(1000);
  Serial.println("System is Ready.");
  delay(1000);
  Serial.println((char)26);
  delay(1000);
}

void loop()
{
  if (received == true)
  {
    if (str[0] == 'r' || str[0] == 'R')
    {
      reverse();
    }
    if (str[0] == 'n' || str[0] == 'N')
    {
      normal();
    }
    value = (str[1] - 0x30) * 100 + (str[2] - 0x30) * 10 + (str[3] - 0x30);
    analogWrite(pwmPin, value);
  }
  received = false;
  i = 0;
}

void serialEvent()
{
  while (Serial.available())
  {
    if (Serial.find("/"))
    {
      delay(1000);
      while (Serial.available())
      {
        char Buffer = Serial.read();
        str[i++] = Buffer;
        if (Buffer == '/')
        {
          received = true;
          return;
        }
      }
    }
  }
}

void normal()
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
}

void reverse()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
}
//----------------(c)Electronics Project Hub-------------//

NOTE 1: Change “xxxxxxxxx” with a phone number from which you are going to send SMS commands to GSM module:

char number[] = “+91xxxxxxxxxx”;

NOTE 2: +91 is country code for India; please change it with your country code.

NOTE 3: Remove Tx and Rx pins before uploading the code to Arduino otherwise you will get upload error

Explanation for Program code:

First things first, assign variables and pin numbers you wish to use:

const int IN1 = 6;
const int IN2 = 7;
int pwmPin = 9;
boolean received = false;
char str[15];
int i = 0;
int value = 0;
int PWM_value = 0;
//---------------------------//
char number[] = "+91xxxxxxxxxx";  // Replace xxxx with your phone number
//---------------------------//

Second things second: We need to set a serial communication baud rate and set the assigned pin numbers as inputs and outputs.

void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(pwmPin, OUTPUT);

Make the PWM pin initially to zero:

analogWrite(pwmPin, 0);

Delay of 40 second is given so that GSM modem will latch to a network:

  delay(20000);
  delay(20000);
  delay(20000);

AT command to receive live SMS:

  Serial.println("AT+CMGF=1");
  delay(1000);

Set GSM module to SMS text mode:

Serial.println("AT+CMGF=1");
delay(1000);

Sending ready SMS to the number entered in the code:

Serial.print("AT+CMGS=");
Serial.print("\"");
Serial.print(number);
Serial.println("\"");
delay(1000);
Serial.println("System is Ready.");
delay(1000);

(char)26, which is equivalent to ctrl + Z, SMS will be send only if this command is send to GSM modem:

  Serial.println((char)26);
  delay(1000);
}

Here is the Serial interrupt function serialEvent(), if there is any activity on serial input pin, below loop get executed:

void serialEvent()
{	
  while (Serial.available())
  {

The below code will search for this character staring with “/”:

    if (Serial.find("/"))
    {
      delay(1000);

The below lines will search for another “/“, if another forward slash is found then the characters between two forward slashes is stored in a string “str”.    

while (Serial.available())
      {
        char Buffer = Serial.read();
        str[i++] = Buffer;
        if (Buffer == '/')
        {

The “receive” Boolean variable will be assigned true if the above explanation occurred. When receive variable become true, the characters between two slashes are checked in void loop() function.

received = true;
          return;
        }
      }
    }
  }
}

The characters between two slashes are check in this loop, if the data is as per the pre-set format the data gets accepted and motor is controlled, otherwise data gets ignored: 

void loop()
{
  if (received == true)  //checking if the received variable is true or not.
  {
    if (str[0] == 'r' || str[0] == 'R')
    {
      reverse(); // Reversing motor polarity. 
    }
    if (str[0] == 'n' || str[0] == 'N')
    {
      normal(); // Normal motor polarity.
    }

The PWM values received via SMS is segregated here as three integer values:

    value = (str[1] - 0x30) * 100 + (str[2] - 0x30) * 10 + (str[3] - 0x30);
    analogWrite(pwmPin, value); // Writing PWM value to the pin, which controls the speed of the motor. 
  }

Assigning “received” Boolean variable as false, so that it can detect another SMS as per preformatted command:

 received = false; 
  i = 0;
}

Below two user defined functions set pin number 7 and 6 as high and low and vice versa to change rotational direction:

void normal()
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
}

void reverse()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
}

How to send SMS to Circuit:

SMS Data Format: /R000/ or /r000/ or /N000/ or /n000/

  1. You have to send SMS command by starting with ‘/’ and end with ‘/’.
  2. There must be four characters between ‘/’ and ‘/’.
  3. ‘N’ or ‘n’ represent rotational direction of shaft in one direction and ‘R’ or ‘r’ represents reverse rotational direction. We cannot say in which direction the shaft will rotate for ‘N’ and ‘R’; it is depend on how you are going to connect the motor terminals across OUT1 and OUT2 of L298N.
  4. ‘000’ mentioned here is PWM level, you can set from 000 which is motor is not rotating to 255 motor rotating at full speed. You can only set from 000 to 255.

Examples: /n127/ will rotate the motor in one direction at half of the maximum speed. /r127/ will rotate the motor in the opposite direction at half of the maximum speed, since 255/2 = 127.5 ~ 127.  /n000/ or /r000/ will stop the motor.

For two digits PWM values like 56 or 10 etc. must be with zero before the digits like this: /n056/ or /r010/

In conclusion you can set 255 different speed levels in clockwise and anticlockwise directions.  You can send SMS commands one after the other to start / stop or adjust the speed and direction. 

How to Operate this Speed Control Circuit Properly:

  1. With completed hardware, upload the program code with the phone number from which you are going to send the SMS commands to the GSM modem.
  2. Switch ON the circuit, within 40 seconds the GSM module would have been connected to a mobile network (with SIM inserted), the network LED on GSM modem must blink once every 3 seconds.
  3. You will receive a SMS to your phone number which you entered in the code; saying – System is ready.
  4. Now you can send SMS command to the SIM (phone number) which you inserted to GSM modem. Send /n200/ (say) the motor starts rotating; now try /r200/, now motor starts rotating in opposite direction.

If you have any question regarding this project, feel free to comment, you can anticipate a guaranteed reply from us.