Servo with arduino

In this tutorial we will be learning how to control a servo with an arduino and perform multiple tasks. We will also display the servo's position on to the I2C LCD display.


In this lesson I will cover: creating a servo object, using basic functions and displaying positional data onto the I2C LCD

Servos

A servo is a electronic component that translates digital information to the motor's positional information . Servos know their position from an onboard potentiometer, they also have gears to increase their torque. They are not meant for turning wheels or anything like that. They are more suitable for jobs requiring high precision such as controlling robotic arms, moving the control surfaces of an rc plane, moving sensors, etc. Servos come in many kinds, the most popular ones are 180 and 360 degrees servos, these numbers indicates the maximum angle the servo can rotate.

Wiring

The servo connections are on the bottom to the text, as well as the servo lead on the left.


Note: If you are going to use the 3rd example then, connect the I2C as per the table below. For more info on how to use an I2C LCD check out my I2C tutorial

Programming

The first example code below performs a sweep action with the servo when executed. For starters we need to import the Servo library , we then create an instance (servo) of the Servo class, next we define our servo pin with #define keyword, later on we tell the arduino which pin the servo is attached to by passing in the servoPin variable to the attach() function and we tell the servo to go to 0 degrees with the write() function. In the void loop section, we tell the servo to go to 0 degrees, then wait 500 milliseconds, later which we tell it to go to 180 degrees and wait 500 milliseconds again.

/*

* Arduino I2C LCD Tutorial

*

* program by Rohan Katreddy

* www.justknowhow.ca

*

*/

#include <Servo.h> // importing the servo library


Servo servo; // instantiating the servo class (new object is "servo")

#define servoPin 8 // define servoPin to servo pin number


void setup() {

servo.attach(servoPin); // giving the arduino the pin the servo is connected to

servo.write(0); // telling the servo to rotate to 0 degrees


}


void loop()

{

servo.write(0); // tell servo to go to 0 degrees

delay(500); // wait 500 milliseconds

servo.write(180); // tell servo to go to 180 degrees

delay(500); // wait 500 milliseconds

}

In the second example, I will demonstrate how to control the servo with serial communication


/*

* Arduino I2C LCD Tutorial

*

* program by Rohan Katreddy

* www.justknowhow.ca

*

*/

#include <Servo.h> // importing the servo library


Servo servo; // instantiating the servo class (new object is "servo")

#define servoPin 8 // define servoPin to servo pin number


void setup() {

Serial.begin(9600);// beginning serial communication at 9600 baud rate

servo.attach(servoPin); // giving the arduino the pin the servo is connected to

servo.write(0); // telling the servo to rotate to 0 degrees


}


void loop()

{

if (Serial.available()) {// check for incoming data at serial port

delay(100);

lcd.clear();

while (Serial.available() > 0) {//

servo.write(int(Serial.read()));// print incoming data if any

}}

In this example the servo will sweep by incrementing its position by 5 and its positional data is displayed on the I2C LCD display. For more on I2C LCD display.


/*

* Arduino I2C LCD Tutorial

*

* program by Rohan Katreddy

* www.justknowhow.ca

*

*/

#include <Wire.h> //importing the wire library

#include <LiquidCrystal_I2C.h>// importing the I2C lcd library

#include <Servo.h> // importing the servo library


Servo servo; // instantiating the servo class (new object is "servo")

LiquidCrystal_I2C lcd(0x27, 16, 2);// instantiating of the I2C lcd with the parameters(address, columns, rows)

const int servoPin = 8;


void setup() {

servo.attach(servoPin); // giving the arduino the pin the servo is connected to

servo.write(0); // telling the servo to rotate to 0 degrees

lcd.begin();// Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

lcd.clear();// clearing the lcd

lcd.backlight();// turning on the backlight

lcd.print("Rohan Katreddy");

lcd.setCursor(1,1);// sets cursor to the 3 column and 2 row

lcd.print("justknowhow.ca");

delay(4000);// wait 4000 milliseconds

lcd.clear();

}


void loop() {

for(int i=0; i < 180; i=i+5) // increment i with 5 every time this code is executed till 180

{

lcd.print("Servo"); // lcd prints "Servo"

lcd.setCursor(0,1); // set lcd cursor to 1st column and 2nd row

lcd.print("position: "); // lcd prints "Position"

servo.write(i); // tell servo to go to the incremented value(i)

lcd.print(i);

delay(100); // wait 100 milliseconds

lcd.clear(); // clear the lcd

}


for(int r=180; r > -1; r=r-5) // reduce i with 5 every time this code is executed till 0

{

lcd.print("Servo");

lcd.setCursor(0,1);

lcd.print("position: ");

servo.write(r); // tell servo to go to the reduced value(r)

lcd.print(r);

delay(100);

lcd.clear();

}


}

Troubleshooting tips:

  • My lcd backlight does not turn on?

Check your power and backlight power pins,

switch your jumper cables,

connect your VCC of the lcd to 3.3v on the arduino, if that works, then that might mean your 5v is not outputting power.


  • Lcd powers up but does not display anything?

If you are using a different arduino then your sda and scl pins might be different. Check for you pins.


  • My custom characters are not being displayed or they are not displaying it correctly?

In your code, make sure your begin() function is called before your createChar() function, this ensures your lcd is initialized before creating characters.


  • Servo does not turn, but arduino powers up?

You may have a faulty servo


  • Servo jitters?

Are you using low quality servos?

Answer should be NO

Is the controller sending a constant output?

Answer should be YES

Is your external servo power supply steady?

Answer should be YES


  • Arduino powers up but not lcd?

Try the ones above. If one of the above solutions work then you may have a faulty lcd or i2c backpack.


  • Arduino does not power up?

You may have a faulty arduino


My Other Tutorials: