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.
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.
Full Circuit Diagram:
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:
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.
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:
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 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.
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:
Second things second: We need to set a serial communication baud rate and set the assigned pin numbers as inputs and outputs.
Make the PWM pin initially to zero:
Delay of 40 second is given so that GSM modem will latch to a network:
AT command to receive live SMS:
Set GSM module to SMS text mode:
Sending ready SMS to the number entered in the code:
(char)26, which is equivalent to ctrl + Z, SMS will be send only if this command is send to GSM modem:
Here is the Serial interrupt function serialEvent(), if there is any activity on serial input pin, below loop get executed:
The below code will search for this character staring with “/”:
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”.
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.
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:
The PWM values received via SMS is segregated here as three integer values:
Assigning “received” Boolean variable as false, so that it can detect another SMS as per preformatted command:
Below two user defined functions set pin number 7 and 6 as high and low and vice versa to change rotational direction:
How to send SMS to Circuit:
SMS Data Format: /R000/ or /r000/ or /N000/ or /n000/
- You have to send SMS command by starting with ‘/’ and end with ‘/’.
- There must be four characters between ‘/’ and ‘/’.
- ‘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.
- ‘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:
- 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.
- 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.
- You will receive a SMS to your phone number which you entered in the code; saying – System is ready.
- 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.