2019/05/04

A Pan Tilt Gimbal w/ Joystick




In this Arduino tutorial, I will show you how to make A Pan Titl Gimbal with Joystick; plus speed control.  It's quite simple.

This Gimbal base is controlled by the joystick and the potentiometer is used to control how fast the SG90 servo would move.

3D printed Gimbal




Schematic:




In the video, as I turn the potentiometer, the Serial port shows the speed is increased to 9.  You will see the Gimbal base is moving must faster. Also, this video shows you how fast the Gimbal is moving based on the potentiometer value.

Watch the Video:



 Components: 

  •  One Arduino Nano 
  •  One Joystick module 
  •  One Potentiometer 
  •  Two SG90 servo 
  •  3D printed Gimbal base

Wire Connections:

  •  Connect joystick GND, 5V, VRX to A4, VRX to A3 and SW to D6 pins to Arduino.
  •  Connect 1st SG90 servo VCC to 5V, D2, and GND to Arduino 
  •  Connect 2nd SG90 servo VCC to 5V, D4, and GND to Arduino 
  •  Connect potentiometer VCC to 5V, A0, and GND to Arduino 

That's it! I hope you enjoy this tutorial, Have fun!
I'm thinking to extend this project to .... Light tracing Gimbal base, good idea?

Maybe you will be interested in my other projects:


References:


 Source codes:


/**
  2019-04-07 V0.4 by Stonez56

  Code from: https://stonez56.blogspot.com

  v0.4 Fix a bug to allow servos to reach the max limited angle
  v0.3 Added potentiometer to adjust speed (2019-04-28)
  v0.2 Made SG90 server stays there once hand released from Joystick (2019-04-28)
  v0.1 This Gimbal Servo code  moves SG90 servers to position (2019-04-07)



  Hint:  If x or y > 512 move to the one side, or x or y <= 512 move to the other side
*/

#include <Servo.h>
Servo servo1_x;
Servo servo2_y;

//Pin definitions
const  int potPIN = A0;  //potentiometer pin14
const int servo1_x_pin = 2;
const int servo2_y_pin = 4;
const int joystick_s_pin = 6;
const int joystick_x_pin = A3;
const int joystick_y_pin = A4;
const uint8_t  joystick_tolerance = 5;
uint8_t  joystick_center = 520;

//User define variables
uint8_t originalAngle = 92;
uint8_t x_last = originalAngle;  // X last postion
uint8_t y_last = originalAngle; // Y last postion
uint8_t moveSpeed = 10;  //How fast show this Servo move
uint8_t maxSpeed = 50; //Max speed
uint8_t minSpeed = 1;

uint8_t y_minAngle = 1;  //Mimum angle
uint8_t y_maxAngle = 180;  //Maximum angle

uint8_t x_minAngle = 90;  //Mimum angle
uint8_t x_maxAngle = 180;  //Maximum angle



void setup() {
  Serial.begin(57600);
  servo1_x.attach(servo1_x_pin);
  servo1_x.write(originalAngle); //place it servo in the middle
  servo2_y.attach(servo2_y_pin);
  servo2_y.write(originalAngle); //place it servo in the

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(joystick_x_pin, INPUT);
  pinMode(joystick_y_pin, INPUT);
  pinMode(joystick_s_pin, INPUT_PULLUP);

}

void loop() {
  uint8_t x = 0;
  uint8_t y = 0;
  uint8_t button = 0;
  print_joystick_info();

  //Get readings from Joystick
  x = analogRead(joystick_x_pin);
  y = analogRead(joystick_y_pin);
  button = digitalRead(joystick_s_pin);

  int potValue = analogRead(potPIN);
  moveSpeed = map(potValue, 0, 560, minSpeed, maxSpeed);
  //  Serial.print("potValue: ");
  //  Serial.println(potValue);
  Serial.print("moveSpeed: ");
  Serial.println(moveSpeed);

  //If the button is pushed, move servo back to X 90, Y 90
  //This should move servo back slowly, not suddenly
  if (button == 0) {
    digitalWrite(LED_BUILTIN, HIGH);
    //resetServoPosition();
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }

  //Check joystick X Asix input

  if ( x > joystick_center + joystick_tolerance) {
    Serial.print("X_Pos + : ");
    Serial.println(x_last);
    //ensure servo is in the limited range, or it will not move

    if (! (x_last + moveSpeed > x_maxAngle)) {
      x_last += moveSpeed;
      servo1_x.write(x_last);
    } else {
      //move to the X max allowed angle
      x_last = x_maxAngle;
      servo1_x.write(x_maxAngle);
    }
  } else if (x < joystick_center - joystick_tolerance) {
    Serial.print("X_Pos - : ");
    Serial.println(x_last);
    //ensure servo is in the limited range, or it will not move
    if (! (x_last - moveSpeed < x_minAngle)) {
      x_last -= moveSpeed;
      servo1_x.write(x_last);
    } else {
      //move to the X min allowed angle
      x_last = x_minAngle;
      servo1_x.write(x_minAngle);
    }
  }

  //Check Joystick  Y Asix input
  if ( y > joystick_center + joystick_tolerance) {
    Serial.print("Y_Pos + :");
    Serial.println(y_last);
    //ensure servo is in the limited range, or it will not move
    if (! (y_last + moveSpeed > y_maxAngle)) {
      y_last += moveSpeed;
      servo2_y.write(y_last);
    } else {
      //move to the Y min allowed angle
      y_last = y_maxAngle;
      servo2_y.write(y_last);
    }
  } else if (y < joystick_center - joystick_tolerance) {
    Serial.print("Y_Pos - :");
    Serial.println(y_last);
    //ensure servo is in the limited range, or it will not move
    if (! (y_last - moveSpeed < y_minAngle)) {
      y_last -= moveSpeed;
      servo2_y.write(y_last);
    } else {
      //move to the Y min allowed angle
      y_last = y_minAngle;
      servo2_y.write(y_last);
    }
  }

  //moveSpeed from 1 ~ 48
  //So, the delay is calculated to be from  5ms ~ 240ms
  uint8_t delayTime = (480 - (moveSpeed - 1) * 10) / 2;
  //  Serial.print(F("Delay time:"));
  //  Serial.println(delayTime);
  delay(delayTime);

}

//This function moves servo back to originalAngle
void resetServoPosition() {


  //Move X
  if (x_last >= 90) {
    for (int i = x_last; i <= originalAngle; i -= 5) {
      servo1_x.write(i);
      Serial.print(i);
    }
  } else {
    for (int i = x_last;  i > originalAngle; i += 5) {
      servo1_x.write(i);

    }
  }
  Serial.print("X_last - : ");
  Serial.println(x_last);
  //Move Y
  if (y_last >= 90) {
    for (int i = y_last; i <= originalAngle; i -= 5) {
      servo2_y.write(i);

    }
  } else {
    for (int i = y_last;  i > originalAngle; i += 5) {
      servo2_y.write(i);

    }
  }

  Serial.print("Y_last - :");
  Serial.println(y_last);
}

void print_joystick_info() {
  Serial.print("button: ");
  Serial.print(digitalRead(joystick_s_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(joystick_x_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(joystick_y_pin));
  Serial.print("\n");

}




The End.
=================================

Arduino 用搖桿控制的雙向轉軸的平台


今天的Arduino教學中,我將教你如何製作-用搖桿控制的雙向轉軸的平台
而且用可變電阻控制速度。

整個平台通過畫面右下方的搖桿進行控制,而且可以用可變電阻用來
控制SG90伺服器的移動速度。

3D 列印雙軸平台




線路示意圖:



在下方的影片裡,當我轉動可變電阻鈕時,Arduino 串列輸出視窗裡
顯示速度已增加  9, 你可以看見,隻軸平台的移動得快一些。

影片中,也會調整可變電阻來試試看不同的速度。

實際運作影片:



 所需零件: 

  • 一個 Arduino Nano
  • 一個搖桿模組
  • 一個可變電阻
  • 兩個SG90伺服器
  • 一個3D列印的雙向轉軸的平台 (謝謝好心FB網友幫忙列印!!)

連線方式:

  •  Connect joystick GND, 5V, VRX to A4, VRX to A3 and SW to D6 pins to Arduino.
  •  Connect 1st SG90 servo VCC to 5V, D2, and GND to Arduino 
  •  Connect 2nd SG90 servo VCC to 5V, D4, and GND to Arduino 
  •  Connect potentiometer VCC to 5V, A0, and GND to Arduino 

我正在考慮將這個玩具再增加一點功能....自動的光源追踪器。希望有一天能做好!
我希望你喜歡這個教學,玩得開心!

這裡還有一些我之前做過的東西:

式參考資料:


原始程式:

請參考上面:

全篇完