Servo Motor with Potentiometer and LCD with Arduino, No Breadboard - SP-LCD-2
In this video, we learn how to control the angle of a servo motor using a potentiometer (variable resistor) and display the angle on an LCD1602 or LCD2004 with an I2C module that has four wires.
This is code SP-LCD-2, where we did not use a breadboard to distribute power from 5V to the potentiometer, servo motor, and LCD. Only the servo motor is powered directly from 5V. The LCD and potentiometer are powered via digital pins. Details have been explained in the video.
Here is the code for use without a breadboard.
352-Code 1 controlling a servo motor
语言: C++
/*
* Original code is taken from the Arduino IDE
* from File->Examples->Servo->Knob
*
* This is Arduino code to use a display to show the angle of a servo motor on an LCD1602 or LCD2004 with an I2C module. The angle is set and changed by a
* potentiometer (variable resistor)
*
* This is code 2 of this tutorial where a breadboard is not needed.
* 5V is provided from digital pins of the Arduino.
*
* See code SP-LCD-1: http://robojax.com/L/?id=289
*
* Please watch video instruction: https://youtu.be/xrIiKqvm8hQ
* Updated from original code by
* Ahmad Shamshiri on Nov 22, 2020 at 15:53 in Ajax
* Ontario, Canada
* https://www.youtube.com/robojaxTV
* www.robojax.com
* Get this code and other Arduino codes from Robojax.com.
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#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 =0x3f;//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);
#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
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("Servo with Potentiometer");
Serial.println("Robojax.com");
myservo1.attach(servo1Pin); // attaches the servo 1 to the servo object
lcd.begin();
lcd.backlight();
lcd.print("Robojax Servo");
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(20);
}// loop end
/*
* sendServo(int value)
* @brief sends the servo the "value" angle position
* @param "value" integer between 0 to 180
* @return none
* Written by Ahmad Shamshiri for robojax.com
* on Nov 22, 2020 in Ajax, Ontario, Canada
*/
void sendServo(int value)
{
// Please watch video instruction: https://youtu.be/xrIiKqvm8hQ
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(100);
}
}//sendServo()
/*
* lcdDisplay(int angle)
* @brief displays the "angle" angle position
* @param "angle" integer between 0 to 180
* @return none
* Written by Ahmad Shamshiri for robojax.com
* on Nov 22, 2020 in Ajax, Ontario, Canada
*/
void lcdDisplay(int angle)
{
//Please watch video instruction: https://youtu.be/xrIiKqvm8hQ
// Robojax.com LCD1602 for Servo motor
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()
* @brief clears a line of the display (erases all characters)
* @param none
* @return does not return anything
* Written by Ahmad Shamshiri
* www.Robojax.com code May 28, 2020 at 16:21 in Ajax, Ontario, Canada
*/
void clearCharacters()
{
for (int i=(unsigned)strlen(TITLE_ANGLE1)-1; i<=LCD_CHAR-1; i++)
{
lcd.setCursor (i,1); //
lcd.write(254);
}
}//clearCharacters
资源与参考
-
外部How to use a breadboardyoutu.be
-
外部PWM pins for Arduino boardsarduino.cc
-
内部
文件📁
没有可用的文件。