Joystick Controls DC Motor

From Microduino Wiki
Revision as of 02:59, 2 September 2015 by 1304410487@qq.com (talk) (Created page with "{{Language| Joystick Controls DC Motor}} {| style="width: 800px;" |- | ==Objective== To control rotation of DC motor by Joystick. ==Principle== By detecting Joystick analog v...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Language: English  • 中文

Objective

To control rotation of DC motor by Joystick.

Principle

By detecting Joystick analog values in X- and Y-axes, you can control the rotation and direction of the DC motor.

Equipment

Module Number Function
Microduino-CoreUSB 1 Core board
Microduino-Sensorhub 1 Sensor pinboard
Microduino-Joystick 1 Joystick sensor
Microduino-Robot 1 Robot control board
Microduino-Motor 1 DC motor drive board
Motor 1

Hardware Buildup

  • Setup 1:Stack CoreUSB, Robot, Motor and Sensorhub.
  • Setup 2:Connect Joystick to Sensorhub to A0-A1 pin of Sensorhub and motor to OUTA-OUT1B pin of Stepper.
Microduino-sensorhub Joystick.JPG

Software Debugging

  • Define pin of Joystick and motor control.
#define PI 3.1415926
#define JoystickX_PIN A1   //Joystick X-axis output 
#define JoystickY_PIN A0   //Joystick Y-axis output 
#define PIN_MOTORA 5    //Motor output
#define PIN_MOTORB 6    // Motor output pin
  • Calculate the angle where Joystick lies, according to which you can control rotation speed and decide rotation direction based on positive or negative value of the angle.
    valueX = (float)(analogRead(JoystickX_PIN)-512);    
    valueY = (float)(analogRead(JoystickY_PIN)-512);
    valueL = sqrt(sq(valueX)+sq(valueY));
    angle = asin(valueX/valueL)*180/PI;    // Calculate the angle of Joystick by inverse trigonometric function 
    motorSpeed = (int)(angle*255/180);    // Calculate rotation speed according to the angle. 
    if(motorSpeed > 0)                    // If the angle is positive, then the motor rotates clockwise. 
    {
      digitalWrite(PIN_MOTORA, LOW);
      analogWrite(PIN_MOTORB, motorSpeed);
    }
    else if(motorSpeed < 0)               //If the angle is negative, then the motor rotates anti-clockwise. 
    {
      digitalWrite(PIN_MOTORB, LOW);
      analogWrite(PIN_MOTORA, abs(motorSpeed));
    }
    else                                  //If the angle is zero, then the motor stops rotation. 
    {
      digitalWrite(PIN_MOTORA, LOW);
      digitalWrite(PIN_MOTORB, LOW);
    }
    delay(500);

Program

[MicroduinoJoystickMotor]

Result

Push Joystick to different angle, you can see the rotation speed and direction will change accordingly.

Video