Arduino Joystick Serial Issues


I am working on a project using a joystick from a wheelchair connected to an Arduino Uno which I am using to communicate with a Sabretooth 2x60 to drive two wheelchair motors. I am having issues with the motors having a twitching sound when the joystick is in the neutral position.

I have used this link as a reference.

// Code to read contactless joystick outputs via Arduino.
int ForeAft_Pin = 1; //Fore/Aft input is Arduino pin A1
int LeftRight_Pin = 0; //Left/Right input is Arduino pin A0

float ForeAft_Input ; // Expected Range 220-800
float LeftRight_Input ; // Expected Range 220-800

void setup() // Main Program Initialization
Serial.begin(9600); // Prepare debug information

void loop()
ForeAft_Input = analogRead(ForeAft_Pin) ;
LeftRight_Input = analogRead(LeftRight_Pin) ;


delay( 300 ); // Just to slow down the output for debug

I believe the problem is related to my joystick readings. When I use the code above to read my serial values, I am seeing a fluctuation of the values when the joystick is in the neutral position. The readings switch between (505, 506), (504, 506), (506,506), (506,506), (506,506), (504,507), (505,507)

My code is below:

/ Software Serial Sample for Packet Serial
// Copyright (c) 2012 Dimension Engineering LLC
// Edited by Mark J. Hughes for AllAboutCircuits
// See license.txt for license details.

#include <SoftwareSerial.h>
#include <Sabertooth.h>

// Decide where you are going to plug the joystick into the circuitboard.
int ForeAft_Pin = 0; // Plug Joystick Fore/Aft into Analog pin 1
int LeftRight_Pin = 1; // Plug Joystick Left/Right into Analog pin 0

//Decide where you are going to plug in the Arduino
int Tx_pin = 2; // Arduino Transmit Pin to Sabertooth S1

// Create variables to read joystick values
float ForeAft_Input ; // Variable to store data for Fore/Aft input from joystick
float LeftRight_Input ; // Variable to store data for Left/Right input from joystick

// These variables allow for math conversions and later error checking as the program evolves.
int Fore_Limit = 555; // High ADC Range of Joystick ForeAft
int Aft_Limit = 457; // Low ADC Range of Joystick ForeAft
int Right_Limit = 551; // High ADC Range of Joystick LeftRight
int Left_Limit = 463; // Low ADC Range of Joystick LeftRight

// Create variables for Sabertooth
int ForwardReverse_power = 0; // -127 for full reverse, 0 for stop, +127 for full forward.
int LeftRight_power = 0; // -127 for full CounterClockwise, 0 for stop, +127 for full Clockwise

Sabertooth ST(128); // Address 128, and use SWSerial as the serial port.

/* Sabertooth can be used in independent or mixed mode. We're using mixed mode which controls the motion
of both motors with a single command. Independent mode controls one motor with one command */

void setup()
{; // Send command to stop transldational motion
ST.turn(0); // Send command to stop rotational motion
SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth packet serial.

void loop() // Main program loop
// Read the joystick values
ForeAft_Input = analogRead(ForeAft_Pin) ; // Read the Fore/Aft joystick value
LeftRight_Input = analogRead(LeftRight_Pin) ; // Read the Left/Right joystick value

// Convert the joystick values to Sabertooth values
ForwardReverse_power=map(ForeAft_Input,Fore_Limit, Aft_Limit, 127,-127); // Translate and scale joystick values to Sabertooth values
LeftRight_power=map(LeftRight_Input,Left_Limit, Right_Limit, 127,-127); // Translate and scale joystick values to Sabertooth values

// Command the Sabertooth to drive the motors.; //Command the Sabertooth translational motion
ST.turn(LeftRight_power); //Command the Sabertooth rotational motion

// Create debug information

Serial.print(ForwardReverse_power); Serial.print("\t"); Serial.println(LeftRight_power); //Debug


When I run the code, it seemingly works, but with the twitching/electrical noise when the motors are stopped. I have toggled with the joystick initial values but changing them seems to make the motors move when the joystick is in the neutral position—the current settings are the only ones I have been able to use and not experience movement in neutral.

I have also run Sabretooth’s sample code from Sabretooth (below) and found no issues when the motors are stopped. The twitch/electrical noise is only when I use the code with the joystick.

I have included a link to some photos of the setup as well as videos where I tried to capture the twitching noise. The 10 second video is the motors running using the Sabretooth sample code and the 20 second video is with me using the joystick code.

Any advice is greatly appreciated! I am very new to electrical/dc motors/Arduino/sabretooth, so please let me know if any clarification is needed. I have searched some other posts, but not been able to find anything to solve this issue yet.



Try to add a dead band around the 0 point. Experiment with appropriate values.

if (ForwardReverse_power >= 5 or ForwardReverse_power <= -5); //Command the Sabertooth translational motion

if (LeftRight_power >= 5 or LeftRight_power <= -5)
  ST.turn(LeftRight_power); //Command the Sabertooth rotational motion
else ST.turn(0);
1 Like

Thank you for the help! Unfortunately, this didn't seem to work. I tried adding the dead band a few different ways, but no luck.

I apologize. This actually did work! I mistakenly thought I had commented out my previous /st.turn() commands, but I hadn't!

Thank you so much for your help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.