e_shop

Controlling Servo motor using potentiometer and display angle on LCD

Controlling Servo motor using potentiometer and display angle on LCD


Components:

1.potentiometer

2.Servo Motor

3. I2C Module

4.16x2 LCD

5.Arduinos UNO

6. Jumper Wires

Circuit Diagram:

Proteus simulation:
Hardware;





Before Going Toward Code Must Install Library LiquidCrystal_I2C
So click on 

Ctrl+Shift+I

__________________________________________________________________________________

Arduino UNO Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
//LCD settings
const uint8_t I2C_ADDRESS =0x27 ;//watch video. use I2C Scanenr to get the address
const uint8_t LCD_CHAR= 16;
const uint8_t LCD_LINE = 2;
char *TITLE_ANGLE1 ="Angle: ";
LiquidCrystal_I2C lcd(I2C_ADDRESS, LCD_CHAR, LCD_LINE);
unsigned int LCD_VCC=13;//5V pin for LCD
#include <Servo.h>
// start of servo1 settings *******
Servo myservo1;  // create servo 1 object to control a servo
int pot1pin = A0;  // analog pin used to connect the potentiometer
int pot1VccPin = 12;//pin 12 used as 5V for potentiometer
unsigned int servo1Pin=3;//any pin with ~ which means pin is PWM enabled
unsigned int servo1AngleMin=0;//servo1 minimum angle 
unsigned int servo1AngleMax=180;//servo1 maximum angle details 
//// do not change the lines below
unsigned int servo1Val=0;// variable to read the value from the analog pin
unsigned int angle1=0;
// END of servo1 settings *******
void setup()
{
  Serial.begin(9600);
  Serial.println("eshop");
  Serial.println("Angle:");
  myservo1.attach(servo1Pin);  // attaches the servo 1  to the servo object
  pinMode(pot1VccPin, OUTPUT);
  digitalWrite(pot1VccPin,HIGH);//5V for Potentiometer (variable resistor)
  // initialize the LCD
  pinMode(LCD_VCC, OUTPUT);
  digitalWrite(LCD_VCC,HIGH);//5V for LCD  
  //lcd.begin(); 
  lcd.init(); 
   lcd.init();   
  lcd.backlight();
  lcd.print("eshop");
  lcd.setCursor(0,1);
  lcd.print("Angle: "); 
  delay(2000); 
}
void loop()
{
 servo1Val = analogRead(pot1pin);    // reads the value of the potentiometer (value between 0 and 1023)
 sendServo(servo1Val);//send sevo zero
delay(30);
}// loop end
void sendServo(int value)
{
  unsigned int newAngle1;
  newAngle1 = map(value, 0, 1023, servo1AngleMin, servo1AngleMax);  // scale it to use it with the servo (value between servoAngleMin and servoAngleMax)
  if(angle1 !=newAngle1)
  {
  myservo1.write(newAngle1);  
  lcdDisplay(newAngle1);
  angle1 =newAngle1;
  delay(200);  
  } 
}//sendServo()
void lcdDisplay(int angle)
{
   clearCharacters();
      Serial.print("angle");
      Serial.println(angle);  
   lcd.setCursor((unsigned)strlen(TITLE_ANGLE1), 1);
   lcd.print(angle);//print value of angle
   lcd.print((char)223);
} //   clearCharacters()
void clearCharacters()
{
    for (int i=(unsigned)strlen(TITLE_ANGLE1)-1; i<=LCD_CHAR-1; i++)
    {
    lcd.setCursor (i,1); //  
    lcd.write(254);
    } 
}//clearCharacters

__________________________________________________________________________________

what is the meaning of  lcd.setCursor(0,1);
Answer:

Its mean second Row and First column of LCD .


What is the neam of  lcd.print((char)223);

Answer:
This is the degree symbol to display it on LCD.
__________________________________________________________________________________

Video:









Post a Comment

0 Comments

a