Arduino Servo Motor Control Using Joystick

In this post we will learn how to control servo motors using a joystick and Arduino. We will see overview about joystick, its pins, its construction and working. We will be extracting useful data from the joy stick which will be base for controlling the servo motors.

By Girish Radhakrishnan

The motto of this article is not just to control the servo motors but, to learn how to use a joystick for controlling many other peripheral devices.

Now let’s take a look at the joystick.

A joystick is an input device which consists of a lever, which can move in several directions in X and Y axes. The movement of the lever is used for controlling a motor or any peripherals electronic devices.

Joysticks are used from RC toys to Boing airplanes and perform similar functions. Additionally gaming and smaller joy sticks have a push button in Z axis which can be programmed to do many useful actions.

Illustration of Joystick:



Illustration of Joystick:




Joysticks are electronic devices in general so, we need to apply power. The movement of the lever produces voltage difference at output pins. The voltage levels are processed by a microcontroller to control the output device such as a motor.

The illustrated joystick is similar one, which can be found in PlayStation and Xbox controllers. You no need to break these controllers to salvage one. These modules are readily available at local electronic shops and E-commerce sites.

Now let’s see the construction of this joystick.

It has two 10 Kilo ohm potentiometer positioned in X and Y axes with springs so that, it returns to its original position when the user release force from the lever. It has a push to ON button on Z axis.

It has 5 pins, 5 volt Vcc, GND, variable X, variable Y, and SW (Z axis switch). When we apply voltage and left the joystick on its original lever position. The X and Y pins will produce half of the applied voltage.

When we move the lever the voltage varies in X and Y output pins. Now let’s practically interface the joystick to Arduino.

Schematic Diagram:











Arduino Servo Motor Control Using Joystick




The pin connection details are given beside the circuit. Connect the completed hardware setup and upload the code.

 Program:

//—————Program Developed by R.Girish————–//

int X_axis = A0;
int Y_axis = A1;
int Z_axis = 2;
int x = 0;
int y = 0;
int z = 0;
void setup()
{
Serial.begin(9600);
pinMode(X_axis, INPUT);
pinMode(Y_axis, INPUT);
pinMode(Z_axis, INPUT);
digitalWrite(Z_axis, HIGH);
}
void loop()
{
x = analogRead(X_axis);
y = analogRead(Y_axis);
z = digitalRead(Z_axis);
Serial.print(“X axis = “);
Serial.println(x);
Serial.print(“Y axis = “);
Serial.println(y);
Serial.print(“Z axis = “);
if(z == HIGH)
{
  Serial.println(“Button not Pressed”);
}
  else
  {
   Serial.println(“Button Pressed”);
  }
Serial.println(“—————————-“);
delay(500);
}
//—————Program Developed by R.Girish————–//

Open the Serial monitor you can see the voltage level at the X and Y axes pins and the status of the Z axis i.e. push button as illustrated below.

These X, Y, Z axes values are used to interpret the position of the lever. As you can see the values are from 0 to 1023.

That’s because Arduino has built in ADC converter which convert the voltage 0V – 5V to 0 to 1023 values.

You can witness from the serial monitor that when the lever is left untouched the lever stays at mid position of both X and Y axes and shows half value of 1023.

You can also see it is not exact half of the 1023 that’s because manufacturing these joysticks never been perfect.

By now, you would have got some technical knowledge about joysticks.

Now let’s see how to control two servo motors using one joystick.

Circuit Diagram:





Arduino Servo Motor Control circuit Using Joystick  and 9V battery




The two servo motors are controlled by one joystick; when you move the joystick along the X axis the servo connected at pin #7 moves Clockwise and Anti-clock wise depending on the lever position.

You can also hold the servo actuator at a position, if you hold the joystick level at a particular position.

Similar for servo motor connected at pin #6, you can move the lever along Y axis.

When you press the lever along the Z axis, the two motors will perform 180 degree sweep.

You can either connect the arduino to 9v battery or to computer. If you connect the Arduino to computer you can open serial monitor and see the angle of the servo actuators and voltage levels.

Program for servo motor control:

//—————Program Developed by R.Girish————–//

#include<Servo.h>
Servo servo_X;
Servo servo_Y;
int X_angleValue = 0;
int Y_angleValue = 0;
int X_axis = A0;
int Y_axis = A1;
int Z_axis = 2;
int x = 0;
int y = 0;
int z = 0;
int pos = 0;
int check1 = 0;
int check2 = 0;
int threshold = 10;
void setup()
{
Serial.begin(9600);
servo_X.attach(7);
servo_Y.attach(6);
pinMode(X_axis, INPUT);
pinMode(Y_axis, INPUT);
pinMode(Z_axis, INPUT);
digitalWrite(Z_axis, HIGH);
}
void loop()
{
x = analogRead(X_axis);
y = analogRead(Y_axis);
z = digitalRead(Z_axis);
if(z == LOW)
  {
   Serial.print(“Z axis status = “);
   Serial.println(“Button Pressed”);
   Serial.println(“Sweeping servo actuators”);
   for (pos = 0; pos <= 180; pos += 1)
   {
    servo_X.write(pos);            
    delay(10);                    
  }
  for (pos = 180; pos >= 0; pos -= 1)
  {
    servo_X.write(pos);            
    delay(15);                      
  }
  for (pos = 0; pos <= 180; pos += 1)
   {
    servo_Y.write(pos);            
    delay(10);                    
  }
 for (pos = 180; pos >= 0; pos -= 1)
  {
    servo_Y.write(pos);            
    delay(15);                      
  }
  Serial.println(“Done!!!”);
  }
if(x > check1 + threshold || x < check1 – threshold)
{
X_angleValue = map(x, 0, 1023, 0, 180);
servo_X.write(X_angleValue);
check1 = x;
Serial.print(“X axis voltage level = “);
Serial.println(x);
Serial.print(“X axis servo motor angle = “);
Serial.print(X_angleValue);
Serial.println(” degree”);
Serial.println(“——————————————“);
}
if(y > check2 + threshold || y < check2 – threshold)
{
Y_angleValue = map(y, 0, 1023, 0, 180);
servo_Y.write(Y_angleValue);
check2 = y;
Serial.print(“Y axis voltage level = “);
Serial.println(y);
Serial.print(“Y axis servo motor angle = “);
Serial.print(Y_angleValue);
Serial.println(” degree”);
Serial.println(“——————————————“);
}
}
//—————Program Developed by R.Girish————–//

If you have any specific question regarding this project, feel free to express in the comment section, you may receive a quick reply.

Digital Capacitance Meter Using Arduino

In this post we are going to construct a digital capacitance meter circuit using Arduino which can measure capacitance of capacitors ranging from 1 microfarad to 4000 microfarad with reasonable accuracy.

By Girish Radhakrishnan

We measure value of the capacitors when the values written on the capacitor’s body is not legible, or to find the value of the ageing capacitor in our circuit which need to be replaced soon or later and there are several other reasons to measure the capacitance.

To find the capacitance we can easily measure using a digital multimeter, but not all multimeters have capacitance measuring feature and only the expensive multimeters have this functionality.

So here is a circuit which can be constructed and used with ease.

We are focusing on capacitors with larger value from 1 microfarad to 4000 microfarad which are prone to lose its capacitance due to ageing especially electrolytic capacitors, which consist of liquid electrolyte.

Before we go into circuit details, let’s see how we can measure capacitance with Arduino.

Most Arduino capacitance meter relies on RC time constant property. So what is RC time constant?

The time constant of RC circuit can be defined as time taken for the capacitor to reach 63.2 % of the full charge. Zero volt is 0 % charge and 100% is capacitor’s full voltage charge.

The product of value of resistor in ohm and value of capacitor in farad gives Time constant.

T = R x C

T is the Time constant

By rearranging the above equation we get:

C = T/R

C is the unknown capacitance value.

T is the time constant of RC circuit which is 63.2 % of full charge capacitor.

R is a known resistance.

The Arduino can sense the voltage via analog pin and the known resistor value can be entered in the program manually.

By applying the equation C = T/R in the program we can find the unknown capacitance value.

By now you would have an idea how we can find the value of unknown capacitance.

In this post I have proposed two kinds of capacitance meter, one with LCD display and another using serial monitor.

If you are frequent user of this capacitance meter it is better go with LCD display design and if you are not frequent user better go with serial monitor design because it save you some bucks on LCD display.

Now let’s move on to circuit diagram.

Serial Monitor based capacitance meter:




As you can see the circuit is very simple just a couple of resistors are needed to find the unknown capacitance.

The 1K ohm is the known resistor value and the 220 ohm resistor utilized for discharging the capacitor while measurement process takes place.

The Arduino sense the rising and decreasing voltage on pin A0 which is connected between 1K ohm and 220 ohm resistors.

Please take care of the polarity if you are using polarized capacitors such as electrolytic.

Program:

//—————–Program developed by R.Girish——————//

const int analogPin = A0;

const int chargePin = 7 ;

const int dischargePin = 6;

float resistorValue = 1000 // Value of known resistor in ohm

unsigned long startTime;

unsigned long elapsedTime;

float microFarads;

void setup()

{

Serial.begin(9600);

pinMode(chargePin, OUTPUT);

digitalWrite(chargePin, LOW);

}

void loop()

{

digitalWrite(chargePin, HIGH);

startTime = millis();

while(analogRead(analogPin) < 648){}

elapsedTime = millis() – startTime;

microFarads = ((float)elapsedTime / resistorValue) * 1000;

if (microFarads > 1)

{

Serial.print(“Value = “);

Serial.print((long)microFarads);

Serial.println(” microFarads”);

Serial.print(“Elapsed Time = “);

Serial.print(elapsedTime);

Serial.println(“mS”);

Serial.println(“——————————–“);

}

else

{

Serial.println(“Please connect Capacitor!”);

delay(1000);

}

digitalWrite(chargePin, LOW);

pinMode(dischargePin, OUTPUT);

digitalWrite(dischargePin, LOW);

while(analogRead(analogPin) > 0) {}

pinMode(dischargePin, INPUT);

}

//—————–Program developed by R.Girish——————//

Upload the above code to Arduino with completed hardware setup, initially don’t connect the capacitor. Open the serial monitor; it says “Please connect capacitor”.

Now connect a capacitor, its capacitance will be displayed as illustrated below.

It also shows the time taken to reach 63.2% of the capacitor’s full charge voltage, which is shown as elapsed time. 

Digital Capacitance Meter Using Arduino

Circuit diagram for LCD based capacitance meter:

The above schematic is connection between LCD display and Arduino. The 10K potentiometer is provided for adjusting the contrast of the display. Rest of the connections are self-explanatory.

The above circuit is exactly same as serial monitor based design; you just need to connect LCD display.

Program for LCD based capacitance meter:

//—————–Program developed by R.Girish——————//

#include<LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);

const int analogPin = A0;

const int chargePin = 7 ;

const int dischargePin = 6;

float resistorValue = 1000; // Value of known resistor in ohm

unsigned long startTime;

unsigned long elapsedTime;

float microFarads;

void setup()

{

Serial.begin(9600);

lcd.begin(16,2);

pinMode(chargePin, OUTPUT);

digitalWrite(chargePin, LOW);

lcd.clear();

lcd.setCursor(0,0);

lcd.print(” CAPACITANCE”);

lcd.setCursor(0,1);

lcd.print(” METER”);

delay(1000);

}

void loop()

{

digitalWrite(chargePin, HIGH);

startTime = millis();

while(analogRead(analogPin) < 648){}

elapsedTime = millis() – startTime;

microFarads = ((float)elapsedTime / resistorValue) * 1000;

if (microFarads > 1)

{

lcd.clear();

lcd.setCursor(0,0);

lcd.print(“Value = “);

lcd.print((long)microFarads);

lcd.print(” uF”);

lcd.setCursor(0,1);

lcd.print(“Elapsed:”);

lcd.print(elapsedTime);

lcd.print(” mS”);

delay(100);

}

else

{

lcd.clear();

lcd.setCursor(0,0);

lcd.print(“Please connect”);

lcd.setCursor(0,1);

lcd.print(“capacitor !!!”);

delay(500);

}

digitalWrite(chargePin, LOW);

pinMode(dischargePin, OUTPUT);

digitalWrite(dischargePin, LOW);

while(analogRead(analogPin) > 0) {}

pinMode(dischargePin, INPUT);

}

//—————–Program developed by R.Girish——————//

With the completed hardware setup upload the above code. Initially don’t connect the capacitor. The display shows “Please connect capacitor!!!” now you connect the capacitor. The display will show the capacitor’s value and elapsed time taken to reach 63.2% of full charge capacitor.

Author’s Prototype:

Tachometer using Arduino

A tachometer is a device that measures the RPM or angular velocity of a rotating body. It differs from speedometer and odometer as these devices deal with linear or tangential velocity of the body while tachometer a.k.a. “tach” deals with more fundamental the RPM.

By Ankit Negi


Tachometer is composed of a counter and a timer both of these working together provides the RPM.

In our project we are going to do same, using our Arduino and some sensors we will setup both a counter and a timer and develop our handy and easy tach.

Perquisites

Counter is nothing but a device or setup that can count any certain regular occurring event like passing of a dot in disc while in rotation. Initially the counters were built using the mechanical arrangement and linkages like gears, ratchets, springs etc.

But now we are using counter having more sophisticated and highly precise sensors and electronics.

Timer is an electronic element that is able to measure the time interval between events or measure time. 

In our Arduino Uno there are timers that not only keep track of time but also maintain some of the important functions of Arduino. In Uno we have 3 timers named Timer0, Timer1 and Timer2. These timers have following functions-

• Timer0- For Uno functions like delay(), millis(), micros() or delaymicros().

• Timer1- For the working of servo library.

• Timer2- For functions like tone(), notone().

Along with these functions these 3 timers are also responsible for generating the PWM Output when analogWrite() command is used in the PMW designated pin.

Concept of Interrupts

In Arduino Uno a hidden tool is present which can give access to a whole lot of functioning to us known as Timer Interrupts. 

Interrupt is a set of events or instructions that are executed when called interrupting the current functioning of the device, i.e. no matter what codes your Uno was executing before but once an Interrupt is called Arduino execute the instruction mentioned in the Interrupt. 
magnet on motor shaft
tachometer circuit using Arduino
Now Interrupt can be called at certain condition defined by the user using an inbuilt Arduino Syntax.

We will be using this Interrupt in our project that makes our tachometer more resolute as well as more precise than the other Tachometer project present around the web.

Components required for this Tachometer project using Arduino

• Hall Effect Sensor (Fig.1)

hall effect sensor module

• Arduino Uno 

Arduino UNO board

• Small magnet 

small magnet

• Jumper wires

• Rotating Object (Motor shaft) 

DC motor

Setup

• The setup for creating is as follows-

• In the shaft whose rotation speed is to be measured is fitted with a small magnet using glue gun or electrical tape.

• Hall Effect sensor has a detector in front and 3 pins for connections.

• The Vcc and Gnd pins are connected to 5V and Gnd pin of Arduino respectively. The Output pin of the sensor is connected to the digital pin 2 of the Uno to provide the input signal.

• All components are fixed in a mount board and Hall detector is pointed out from the board.

Code

int sensor = 2; // Hall sensor at pin 2

volatile byte counts;

unsigned int rpm; //unsigned gives only positive values

unsigned long previoustime;

void count_function()

{ /*The ISR function

Called on Interrupt

Update counts*/

counts++;

}

void setup() {

Serial.begin(9600);

//Intiates Serial communications

attachInterrupt(0, count_function, RISING); //Interrupts are called on Rise of Input

pinMode(sensor, INPUT); //Sets sensor as input

counts= 0;

rpm = 0;

previoustime = 0; //Initialise the values

}

void loop()

{

delay(1000);//Update RPM every second

detachInterrupt(0); //Interrupts are disabled

rpm = 60*1000/(millis() – previoustime)*counts;

previoustime = millis(); //Resets the clock

counts= 0; //Resets the counter

Serial.print(“RPM=”);

Serial.println(rpm); //Calculated values are displayed

attachInterrupt(0, count_function, RISING); //Counter restarted

}

Upload the code.

Know the code

Our tachometer uses Hall Effect Sensor; Hall Effect sensor is based on Hall effect named after its discoverer Edwin Hall. 

Hall Effect is phenomenon of generation of voltage across a current carrying conductor when a magnetic field is introduced perpendicular to the flow of current. This voltage generated due this phenomenon help in Input signal generation.

As mentioned Interrupt will be used in this project, to call Interrupt we have to setup some condition. Arduino Uno has 2 conditions for calling for Interrupts-

RISING- When used this, Interrupt are called every time when the Input signal goes from LOW to HIGH.

FALING-When used this, Interrupt are called when signal goes from HIGH to LOW.

We have used the RISING, what happens is that when the magnet placed in the shaft or rotating object come close to Hall detector Input signal is generated and Interrupt are called in, Interrupt initiates the Interrupt Service Routine(ISR) function, which include increment in the counts value and thus count takes place.

We have used the millis() function of Arduino and previoustime (variable) in correspondence to setup the timer.

The RPM thus is finally calculated using the mathematical relation-

RPM= Counts/Time taken Converting the milliseconds to minutes and rearrangement we gets to the formula= 60*1000/(millis() – previoustime)*counts.

The delay(1000) determines the time interval after which the value of RPM will be updated on the screen, you can adjust this delay according to your needs.

This value of RPM obtained can be further used to calculate the tangential velocity of the rotating object using the relation- v= (3.14*D*N)/60 m/s.

The value of RPM can also be used to calculate the distance travelled by a rotating wheel or disc.

Instead of printing values to Serial monitor this device can be made more useful by connecting a LCD display (16*2) and battery for better usage.