eliott3005
Published © GPL3+

PWM and direction control of a DC motor via Bluetooth

Control a DC motor's speed and direction via Bluetooth through a mobile app.

BeginnerFull instructions provided22,741
PWM and direction control of a DC motor via Bluetooth

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-06 Bluetooth Module
HC-05 Bluetooth Module will also work.
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
DC Motor, 12 V
DC Motor, 12 V
×1

Software apps and online services

Bluetooth Electronics
Found this app on the Play Store, but feel free to adapt code to use other apps, the customization options on this one are the reason we chose to use it.
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Electrical schematic

circuit diagram

Code

Speed and direction control prgm

C/C++
#include<SoftwareSerial.h>
SoftwareSerial bt_ser(4,5); //connected to RX and TX pins for serial data communication
char c[6];
int i=0,speed_value=0,send_value;

#define pwm1     9   //input 2
#define pwm2    10   //input 1
 
boolean motor_dir = 0;

void setup()
{
  Serial.begin(9600);
  bt_ser.begin(9600);
  pinMode(pwm1,   OUTPUT);
  pinMode(pwm2,   OUTPUT);
}

void loop()
{
  
   while(bt_ser.available())   //when data is transmitted
     {
       if(bt_ser.available()>0)
       {
         c[i] = bt_ser.read();   //reading the string sent from master device
         Serial.print(c[i]); 
         i++;  
       }
       if(c[i-1]=='N')    //if button is pressed
        {
          motor_dir = !motor_dir;     //toggle direction variable
          if(motor_dir)               //setting direction, pwm1 and pwm2 are opposites
            digitalWrite(pwm2, 0);
          else
            digitalWrite(pwm1, 0); 
        }
      }
       
       speed_value = (c[1]-48)*100+(c[2]-48)*10+(c[3]-48)*1;  //interpreting speed from string
       if(motor_dir)   //for a given direction
          {
            if(c[i-1]=='#'){   //if data has been transmitted from slider
              
              analogWrite(pwm1, speed_value-100);  //-100 so that when slider is on "0" speed is 0
              i=0;
            }
          }
        else{       //for opposite direction
          if(c[i-1]=='#'){
            
            analogWrite(pwm2, speed_value-100);
            i=0;
          }
          
        }
            
                
}   

Credits

eliott3005

eliott3005

0 projects • 7 followers

Comments