Arduino Pan Tilt Servo Motor Control via Bluetooth Using Android App



Hardware Required:

 - Arduino UNO R3
- HC-06 Bluetooth Module- Micro Servo Motor SG90 x2- Micro PAN&Tilt Kits- Breadboard- Jumper Wires- Battery- Android Device

Schematic diagram


Connections:

- The external battery VCC/ GND connect to the Breadboard
- The Arduino GND connect to the Breadboard's GND input

Micro Servo SG90 wires color
Orange Input - Signal
Red Input - Power or VCC
Brown Input - Ground or GND

- The Servo1 ( Pan ) VCC and GND connect to the Breadboard VCC/ GND inputs ( Power rails Battery )
- The Servo2 ( Tilt ) VCC and GND connect to the Breadboard VCC/ GND inputs ( Power rails Battery )

- The Servo1( Pan ) Signal connect to the Arduino Digital Pin 8
- The Servo2( Tilt ) Signal connect to the Arduino Digital Pin 9

- The Bluetooth module HC-06 attach to the Bradboard
- The Bluetooth VCC connect to the Arduino 3.3V
- The Bluetooth GND connect to the Arduino GND
- The Bluetooth TX connect to the Arduino RX
- The Bluetooth RX connect to the Arduino TX


Arduino Sketch


#include <Servo.h>
#include <LedControl.h>

/*
 Now we need a LedControl to work with.
 // Customized for RoboJax.com on Feb 26, 2017 in Ajax, Ontario, Canada.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to CS
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);


byte right[] = {B00011000,B00111100,B01111110,B11111111,B00011000,B00011000,B00011000,B00011000};
byte left[] = {B00011000,B00011000,B00011000,B00011000,B11111111,B01111110,B00111100,B00011000};

byte up[] = {B00010000,B00110000,B01110000,B11111111,B11111111,B01110000,B00110000,B00010000};
byte down[] = {B00001000,B00001100,B00001110,B11111111,B11111111,B00001110,B00001100,B00001000};
byte cross[] = {B10000001,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B10000001};
/* we always wait a bit between updates of the display */
unsigned long delaytime=1000;

Servo servo_pan;
Servo servo_tilt;

#define UP 102
#define DOWN 98
#define LEFT 108
#define RIGHT 114
int val;

int pan_pos = 0;
int tilt_pos = 0;

void setup() {

   /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,1);
  /* and clear the display */
  lc.clearDisplay(0);

  servo_pan.attach(8);
  servo_tilt.attach(9);

  servo_pan.write( pan_pos);
  servo_tilt.write( tilt_pos);
  delay(100);
  display(cross);
 Serial.begin(9600); // Default connection rate for my BT module
}

void loop() {

   if( Serial.available()){
       val = Serial.read();
       Serial.println(val);

       switch( val){
       case LEFT :{ Serial.println("LEFT");
                     display( left );
                     pan_pos-=2;
                     Serial.println( pan_pos );
                     if( pan_pos < 0) pan_pos = 0;
                     servo_pan.write( pan_pos);
                     delay(15);
                    }   
       break;
       case RIGHT :{ Serial.println("RIGHT");
                     display( right );
                     pan_pos+=2;
                     Serial.println( pan_pos );
                     if( pan_pos > 180) pan_pos = 180;
                     servo_pan.write( pan_pos);
                     delay(15);
                    }   
       break;

       case UP :{ Serial.println("UP");
                  display( up );
                     tilt_pos-=2;
                     Serial.println( tilt_pos );
                     if( tilt_pos < 0) tilt_pos = 0;
                     servo_tilt.write( tilt_pos);
                     delay(15);
                    }   
       break;
       case DOWN :{ Serial.println("DOWN");
                    display( down ); 
                     tilt_pos+=2;
                     Serial.println( tilt_pos );
                     if( tilt_pos > 180) tilt_pos = 180;
                     servo_tilt.write( tilt_pos);
                     delay(15);
                    }   
       break;
       }
   }

}
void display( byte Digits[] ){
    for (int i=0; i<8; i++){
  lc.setRow(0,i,Digits[i]);

  }
}
void clearLED(){
  
  for(int i=0; i<8; i++){
      lc.setRow(0,i,0);// this is for blank
  } 
}

ความคิดเห็น