Tag Archives | joystick

DC Motor with Variable Speed Control by Joystick

This project makes use of a thumb joystick to control the direction of shaft rotation on a DC motor. The joystick also controls the rotational speed of the motor shaft by how far it is moved horizontally (y-axis). With the joystick on center and at rest, the DC motor doesn’t rotate. However, gradually moving the joystick horizontally to the right increases the speed of the motor until a maximum value permitted by a L293 motor controller. The same method of variable speed control is achieved in reverse with its negative limit to available speed in the opposite rotational direction.

Joystick, DC Motor and motor controller with Arduino Uno R3.

Further details that correspond to this photo are given in the schematic diagram below. The schematic diagram provides specific connection details to indicate where system components are placed and terminated.

Project hook up details further explained in schematic below.

Project Schematic:

Schematic diagram of project to illustrate how the joystick, DC motor, and motor controller interface with each other and the Arduino Uno R3.

Motor Speed Formulas:

To continuously update the motor speed as a function of gradual joystick movement, it is necessary to read updated joystick values read from memory. As an assigned analog GPIO pin (A1) reads the VRy levels at the joystick, continuous updates are made to memory in order to change motor speed and direction.

Forward and reverse formulas derived for insertion into controller code. To originate continuously changing motor speeds from 0 to 255 or 0 to -255 as read and interpreted analog joystick values between 0 and 1023 (with 512 on center and at rest).

Continuously changing joystick values read into memory becomes calculated for increased or decreased speed adjustment either in a forward or reverse direction. The positive y-axis movement rotates the motor shaft clockwise while the negative y-axis joystick movement rotates the motor shaft counter-clockwise. Both at continuously variable speeds depending upon the joystick’s position off-center from its physically at-rest state.

Thumb joystick and DC Motor suitable for microcontroller use.

Project Review:

It is necessary to pump the joystick to effect charge momentum for break-through motion and reversal. As demonstrated here, the positive y-axis joystick movement rotates the motor shaft clockwise while horizontally negative joystick movement rotates the shaft counterclockwise. The thumb joystick is inverted in this video, so the opposite lateral motion appears counter-intuitive. Reorient the joystick 180 degrees and its motion directly corresponds to the positive and negative movement for positive and negative rotation.


Code Example:

int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed;
int jPin=A1;
int jVal;

void setup()
{
pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(jPin,INPUT);
Serial.begin(9600);
}

void loop()
{
jVal=analogRead(jPin);
Serial.println(jVal);

if (jVal<505)
{
delay(25);
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
mSpeed=-255./512.jVal+255.; //derived equation
delay(100);
analogWrite(speedPin,mSpeed);
}

if (jVal>=505)
{
delay(25);
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
mSpeed=(255./512.)jVal-255.; //derived equation
delay(100);
analogWrite(speedPin,mSpeed);
}
}

Servo Motors with Two-Axis Joystick Control

This project makes use of two servos (Tiankongrc MG995), servo mounting brackets a 2-axis thumb joystick, an active buzzer, and an Arduino Uno R3. To operate the two servos together it is necessary to mount them perpendicular to each other. Where one servo moves along 180 degrees horizontally (x-axis) and the other moves 180 degrees vertically (y-axis). While the joystick VRx, VRy, and SW terminals are read by the Arduino through its GPIO pins, their coordinates are interpreted and sent to each servo for positioning. Either by a single manual motion event or as movement continuously read at the servos to rotate from one position to the next.

Two servos, 2-axis thumb joystick, active buzzer, and an Arduino Uno R3.

The active buzzer sounds when the joystick is depressed. It acts as a toggle to represent a select function that corresponds to typical X,Y positioning at a specific point along its movement.

Rotational Movement Control with the Thumb Joystick.

Project Schematic:

Through pulse width modulation pin connections on the Arduino, simultaneously movement control is achieved at both servos. Along with voltage and ground connections shared with the joystick, separate analog connections are terminated for analogRead instructions at the Arduino controller.

Dual X & Y servos with a 2-axis thumb joystick makes use of both PWM and analog input pins.

Project Review:

This video demonstrates the servo motor’s range of motion and precise positioning as the joystick is controlled with its push-button switch.


Code Example:

#include <Servo.h>
Servo Xservo;
Servo Yservo;

int Xpin=A0;
int Ypin=A1;
int Spin=2;
int XSpin=9;
int YSpin=10;
int buzzPin=7;
int WVx;
int WVy;
int Xval;
int Yval;
int Sval;
int dt=200;

void setup()
{
Serial.begin(9600);
pinMode(Xpin,INPUT);
pinMode(Ypin,INPUT);
pinMode(Spin,INPUT);
pinMode(XSpin,OUTPUT);
pinMode(YSpin,OUTPUT);
pinMode(buzzPin,OUTPUT);

Xservo.attach(XSpin);
Yservo.attach(YSpin);

digitalWrite(Spin,HIGH);
}

void loop() {
Xval=analogRead(Xpin);
WVx=(180./1023.)Xval; Yval=analogRead(Ypin); WVy=(180./1023.)Yval;
Sval=digitalRead(Spin);

Xservo.write(WVx);
Yservo.write(WVy);

if (Sval==0)
{
digitalWrite(buzzPin, HIGH);
}
else
{
digitalWrite(buzzPin, LOW);
}

delay(dt);
Serial.print(“X Value = “);
Serial.print(Xval);
Serial.print(” Y Value = “);
Serial.print(Yval);
Serial.print(” Switch State is “);
Serial.println(Sval);
}