Controlling a Servo using a 10k pot and Arduino

Introduction

Here we will control a servo by rotating a 10k potentiometer, with Arduino Uno as controller. But before going through this tutorial, i suggest you go through this basic introductory article on Servos.

The 10k pot is connected to one of the analog pins and the servo is connected to one of the pwm pins of the arduino board.

A servo can be made to turn to any angle between 0 to 180 degrees by using Arduino’s inbuilt Servo library.

Here we map the analog reading between 0-1023 obtained from the 10k pot to an angle between 0 – 90 degrees, by using arduino’s inbuilt map() function and use the mapped value to run the servo. This makes the servo rotate  in accordance with the rotation of the 10k pot.

Breadboard Setup

Arduino-servo-pot-interfacing_bb.png
Breadboard setup

 

Note: A servo has three wires +VCC, GND and SIGNAL . Different servos use different color codes to indicate +VCC, GND and SIGNAL .

a4228312-146-user38_pic55606_1294883044
Servo Color codes

Schematic Diagram

Arduino-servo-pot-interfacing_schem
Schematic diagram

Code

#include<Servo.h>

Servo myServo; //Declaring a Servo object
int servoPin = 10; //declaring a variable to store the pin number to which the servo is connected
int potValue = 0; //declaring a variable to store potentiometer analog value
int potPin = 0; //10k pot connected to pin 0 of analog port
int servoPosition = 0; //declaring a variable to store the angle for servo rotation
void setup() {
  // put your setup code here, to run once:
  myServo.attach(servoPin); //attaching the servo object to the servo pin
}

void loop() {
  // put your main code here, to run repeatedly:
 potValue = analogRead(potPin); //get the analog value of 10k pot
 servoPosition = map(potValue,0,1023,0,180); //map the analog value of the 10k pot from a value between 0 - 1023 to a value between 0 - 180 and store it in a variable
 myServo.write(servoPosition); //turn the servo by the angle value stored in servoPosition variable
 delay(100);

}

 

 

 

Advertisements

Achievements

The club and its members have excelled in various fields such as autonomous robotics, manual robotics, circuit designs, complex programming using modern microcontrollers, sensor circuitries etc.

Members have also won various technical competitions conducted at various universities and engineering colleges across India. Few of them are IIT Bombay, IIT Guwhati, Tezpur University, Nagaland University, Dibrugarh University and many more.

The EHC in its capacity has also organised many technical events and seminars at the institute. The Club has also coordinated many professional workshops conducted by reputed organisations.

Using an NPN Transistor as a switch

Lets, say we want to drive a small 9V 100rpm DC geared motor, which has stall current of around 722mA. Since the Arduino pins can only provide maximum of 20mA current, we cannot directly interface the motor to the Arduino.

Here we will use an NPN transistor in common-emitter  configuration as shown in the circuit below to drive the motor from arduino pin.

npn_transistor_as_switch_schem.jpg
Driving a DC Motor using transistor as switch

VCC = 9V,  Vin = Arduino digital pin (HIGH = 5V, LOW = 0V)

Choosing a transistor and proper resistance values

  1. Our maximum load current here is 722mA, which is also the collector current of the transistor. For practical purposes it is advisable to consider 30% more than the maximum load current to ensure that the transistor always gets saturated.Therefore, IC = 722mA + 30% of 722mA = 938.6mA ≅ 940mA
  2. Now, we have to choose a transistor with maximum collector current rating greater than 940 mA.
  3. The transistor BD139 has ICmax rating of 1.5A which is well above 940mA.
  4. We know, IC = hfe x I (hfe is used to represent the dc current gain in datasheets of transistors, more commonly it is known as beta, β = IC / IB )
  5. For BD139, hfe(minimum) = 63 (we always consider minimum current gain, to ensure saturation)
  6. Therefore, the base current is given as IB = 940mA  / 63 = 14.98 mA ≅ 15mA (Arduino’s digital pins can easily provide that amount of current)
  7. The required base resistor value to supply 15mA to the transistor’s base is given as
    RB = (Vin – VBE) / I= (5V – 0.7V) / 15mA = 286.67 ohms ≅ 300 ohms (since, resistances are available in limited standard values)
  8. A general purpose diode is connected in parallel to the dc motor as a flyback diode to prevent high voltage spikes from damaging the transistor.

References

  • Using Bipolar Transistors As Switches By Mike Martell, link
  • Flyback diode, link
  • Arduino reference, link
  • 100 rpm dc motor specifications, link
  • BD139 datasheet, link
  • General purpose diode (1N4001-1N4007) datasheet,link