Servo motors are flexible parts used in many different applications since they are electromechanical actuators that have the ability to rotate to a certain position.
Applications for servomotors include automated manufacturing, robotics, and CNC equipment, etc.
This post will examine how to use an Arduino microcontroller to drive a servo motor while adding push buttons, capacitors, resistors and an IC 7809 voltage regulator.
Coding with Explanation:
#include <Servo.h>
Servo myservo; // Create a servo object
int servoPin = 9; // Pin connected to the servo signal
int button1Pin = 2; // Pin connected to the first push button
int button2Pin = 3; // Pin connected to the second push button
int angle = 0; // Initial servo angle
void setup() {
myservo.attach(servoPin); // Attach the servo to the specified pin
pinMode(button1Pin, INPUT); // Set the push button pins as input
pinMode(button2Pin, INPUT);
}
void loop() {
int button1State = digitalRead(button1Pin);
int button2State = digitalRead(button2Pin);
if (button1State == HIGH) {
angle += 1; // Increment the angle
if (angle > 180) {
angle = 180; // Limit the angle to 0-180 degrees
}
} else if (button2State == HIGH) {
angle -= 1; // Decrement the angle
if (angle < 0) {
angle = 0; // Limit the angle to 0-180 degrees
}
}
myservo.write(angle); // Set the servo's position
delay(15); // Delay for smooth movement
}
- The code specifies a starting angle, push button pins, and the servo pin.
- The push button pins are set to input and the servo is connected to the designated pin in the setup() method.
- States of the push buttons are read by the loop() method.
- The angle is increased when button 1 is pressed and decreased when button 2 is pressed.
- The range of angles is 0 to 180 degrees.
- Lastly, the myservo.write() method is used to set the servos position.
Circuit Working:
Parts List:
Component | Quantity |
---|---|
Arduino Uno board | 1 |
IC 7809 | 1 |
Capacitor 100μF 25V | 1 |
Resistors 1/4 watt 100k | 2 |
Servo Motor | 1 |
Tactile Switches | 2 |
In this article when necessary, the servo motor or other components can be powered by the 9V voltage that the IC 7809 controls.
Make sure there is a common ground between the servo motor and the Arduino.
To regulate the position of the servo motor, the Arduino employs Pulse Width Modulation PWM on the control pin.
The servo angle is determined by the pulse width.
To read the push button states and adjust the servo accordingly, write a sketch or program.
For instance, pushing one button may cause the servo to move in one direction, whereas pressing another would cause it to go in a different direction.
The Arduino reads the status of each button1 and button2.
The input pin reads LOW when a button is pressed because of the connection to ground.
The pull-up resistor causes the input pin to read HIGH when it is not tapped.
Make sure the voltage regulator is operating steadily, and keep noise levels down that can impact the servo motors efficiency.
The circuits clean, steady power supply is facilitated by the resistors and capacitor.
How To Build:
To build a Simple Servo Motor Control Circuit using Arduino following are the connections steps to follow:
- Gather all the components mentioned in the above circuit diagram.
- Connect a regulated IC1 7809 to provide a regulated 9V DC to the Arduino board
- Connect the ground of the servo motor to the ground pin on the Arduino.
- Connect the 5V power supply from the Arduino to the servos power input.
- Connect the signal pin (PWM pin) of the servo to a digital output pin 9 on the Arduino.
- Connect capacitor 100μF 25V one end to 5V power supply on Arduino board and other end to ground
- Connect one leg of button1 to pin 2 on the Arduino board and other other leg to ground and connect button2 to pin 3 on the Arduino board and other other leg to ground.
- Connect one resistor 100k to pin 2 and 5V power supply on Arduino board and connect second resistor 100k to pin3 and 5V power supply on Arduino board.
Conclusion:
These instructions in the article will let you use an Arduino microcontroller to operate a servo motor.
Push buttons, resistors, capacitors and an IC 7809 are added to improve the circuits usefulness and stability, giving it a flexible foundation for a range of applications.
References:
Control a servo motor with a 2 connection toggle switch (on/off switch)
Leave a Reply