Arduino Bluetooth DC motor speed

Control the speed of a DC motor via Bluetooth

Arduino Bluetooth DC motor speed

This page explains how to remote control the DC motor speed via Bluetooth using Arduino and Android phone with RoboRemo app.

Materials needed:
- Arduino UNO (or similar) + USB cable for it
- DC adapter for Arduino
- BlueSMiRF module
- DC motor
- NPN transistor (D882 or similar)
- resistor of 220 Ohm (or 100 or 470)
- wires
- Android phone with RoboRemo app installed

Introduction

DC motor runs from DC power supply. You connect the power and the motor starts running at constant speed. You remove power and the motor stops. To control the speed we must periodically connect and remove power and do this very fast, so the motor "thinks" it is always powered, but with less power, according to the ON/OFF time ratio. A NPN transistor is used as the ON/OFF switch, and it is controlled by an Arduino.

Steps:

1.
Upload the code to your Arduino.

// Control dc motor speed via Bluetooth
// using RoboRemo app
// www.roboremo.com

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1


//                    Vin
//    |^^^^^^^^^^^|___|
//  ==|  dc motor |___
//    |___________|   |
//                    | C
//         ___   B  |/
//  pin9--|___|-----|    NPN
//        220 Ohm   |->.
//                     | E
//                     |
//                     GND


#define bluetooth Serial

int motorSpeedPin = 9;  // pin 9 (PWM) to contorl motor speed

char cmd[100];
int cmdIndex;

void exeCmd() {
  
  if( cmd[0]=='s' &&
      cmd[1]=='p' &&
      cmd[2]=='e' &&
      cmd[3]=='e' &&
      cmd[4]=='d' &&
      cmd[5]==' ' ) {
        
       int val = 0;
       for(int i=6; cmd[i]!=0; i++) { // number begins at cmd[6]
         val = val*10 + (cmd[i]-'0');
       }
       // if cmd is "speed 100", val will be 100        
       analogWrite(motorSpeedPin, val);
  } 
}

void setup() {
  
  delay(500); // wait for bluetooth module to start

  bluetooth.begin(115200); // Bluetooth default baud is 115200
  
  pinMode(motorSpeedPin, OUTPUT);
  analogWrite(motorSpeedPin, 0);
  
  cmdIndex = 0;
}

void loop() {
  
  if(bluetooth.available()) {
    
    char c = (char)bluetooth.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    }
  }
}

2.
Build the circuit according to the schematic:

arduino-bluetooth-dc-motor-speed-schematic.jpg


Update 2015-05-28: For bigger motor, use 2 NPN transistors and separate power supply:

schematic2.jpg

3.
Connect your Arduino to the DC power supply.

4.
Configure the RoboRemo interface and enjoy :)