Controlling a Servo with Potentiometer and LCD1602 using Arduino

Controlling a Servo with Potentiometer and LCD1602 using Arduino

In this tutorial, we will learn how to control a servo motor using a potentiometer and display the potentiometer value as well as the servo angle on an LCD1602 screen. This project is a great way to combine multiple components and understand how they can work together to create an interactive system. By the end of this tutorial, you will have a working setup that allows you to adjust the angle of the servo with a knob while simultaneously displaying the values on the LCD.

Before we dive into the wiring and code, it's helpful to understand the components we'll be using. The potentiometer acts as a variable resistor that can adjust the voltage output based on its position, which we will use to determine the servo's angle. The LCD1602 will display the current potentiometer value and the corresponding angle of the servo. You can refer to the video (in video at 00:00) for a visual guide of the entire process.

Hardware Explained

The main components we will use in this project are:

  • Arduino Board: This will serve as the controller for the entire system, processing inputs from the potentiometer and controlling the servo's position based on those inputs.
  • Potentiometer: A 10k ohm potentiometer will be used to provide an analog input to the Arduino. It has three terminals: one connected to 5V, one to ground, and the middle one connected to an analog pin (A0) on the Arduino.
  • Servo Motor: This servo will be controlled by sending it an angle based on the potentiometer's position. The signal wire connects to a digital pin on the Arduino (pin 9).
  • LCD1602 Display: This display will show the potentiometer value and the angle of the servo. It connects to several digital pins on the Arduino for control.

The potentiometer works by varying the resistance and thus the voltage at its middle terminal, which the Arduino reads as an analog input. The servo motor is controlled by sending it a specific pulse width modulation (PWM) signal corresponding to the desired angle. The LCD1602 uses a library that allows us to easily control its display and output information regarding the potentiometer and servo.

Datasheet Details

Manufacturervarious
Part numberLM016L
Logic/IO voltage5 V
Supply voltage4.5 – 5.5 V
Output current (per channel)up to 2 mA
Peak current (per channel)20 mA
PWM frequency guidance50 Hz
Input logic thresholds0.3 x Vcc (low), 0.7 x Vcc (high)
Voltage drop / RDS(on) / saturation0.2 V max
Thermal limits85 °C
Package16-pin DIP
Notes / variants16x2 character display

  • Connect the potentiometer's middle pin to analog pin A0.
  • Ensure the servo is powered with a 5V supply and connected to digital pin 9.
  • Use the appropriate library for the LCD and ensure it is included in your Arduino IDE.
  • Be cautious with the wiring to avoid short circuits, especially with the servo connections.
  • Check that the LCD is initialized correctly in the code to avoid display issues.
Arduino wiring for LCD1602 12wires servo and pot
Arduino wiring for LCD1602 12wires servo and pot

Wiring Instructions

To wire the components together, start by connecting the potentiometer. Connect one outer pin of the potentiometer to the 5V pin on the Arduino, and the other outer pin to the ground. The middle pin goes to analog pin A0.

Next, for the servo motor, connect the brown or black wire to ground, the red wire to the 5V pin, and the yellow or orange wire to digital pin 9 on the Arduino. For the LCD1602, connect the VDD pin to 5V, RW to ground, and V0 to the middle pin of the potentiometer for contrast adjustment. Connect RS to pin 12, EN to pin 11, D4 to pin 5, D5 to pin 4, D6 to pin 3, and D7 to pin 2.

It is also recommended to include a 220-ohm resistor in series with the LED if you are using one, connecting it to the 5V supply. Additionally, a 470 µF capacitor can be connected across the power and ground of the servo to filter any noise during operation.

Code Examples & Walkthrough

The code begins by including the necessary libraries and initializing the pins for the LCD and servo. The following excerpt shows the setup of the LCD and servo:

void setup() {
  lcd.begin(16, 2);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Robojax Servo Knob");
  delay(2000);
}

This snippet initializes the LCD for 16 columns and 2 rows, attaches the servo to pin 9, and displays a welcome message on the LCD. The lcd.clear() function ensures that any previous messages are removed.

Next, the loop continuously reads the potentiometer's value and updates the servo's position and LCD display:

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer
  angle = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo
  myservo.write(angle);                  // sets the servo position according to the scaled value
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Pot. Value:");
  lcd.setCursor(11, 0);
  lcd.print(val);
  lcd.setCursor(0, 1);
  lcd.print("Angle     :");
  lcd.setCursor(11, 1);
  lcd.print(angle);
  delay(500);
}

This loop reads the potentiometer value, maps it to a corresponding angle for the servo, and updates the LCD display with both the potentiometer value and the servo angle. The delay(500) at the end of the loop prevents the display from updating too rapidly.

Demonstration / What to Expect

When you power the system, the LCD will display "Robojax Servo Knob" for 2 seconds, followed by the potentiometer value and the servo angle. As you turn the potentiometer, the servo angle will change accordingly, and the new potentiometer value will be reflected on the LCD. If you experience any unexpected behavior, such as the servo moving in the wrong direction, check the wiring of the potentiometer and servo connections (in video at 10:00).

Images

SG90_servo_motor-1
SG90_servo_motor-1
SG90_servo_motor-0
SG90_servo_motor-0
Arduino wiring for LCD1602 12wires servo and pot
Arduino wiring for LCD1602 12wires servo and pot
97-This is the Arduino code for controlling a servo with a potentiometer and an LCD1602.
Language: C++
Copied!

Resources & references

Files📁

No files available.