Easy Arduino Project: Control a Servo with a Pushbutton 180 degree to 0 and back
Introduction
This article explains a simple Arduino project that uses a pushbutton to control a servo motor. A servo motor is a precise motor that can rotate to a specific position, making it ideal for applications like robotics and automation. This project will make a servo move from 0 to 180 degrees when a button is pressed and return to 0 when released.
Project Components and Wiring
You will need an Arduino board, a servo motor, and a pushbutton.
Wiring Instructions:
- Servo Motor: Connect the signal wire (usually yellow/orange) to Arduino pin 3, the power wire (red) to 5V, and the ground wire (black/brown) to GND.
- Pushbutton: Connect one leg to digital pin 2 and the other leg to a GND pin. That's it! By using the Arduino's internal pull-up resistor, you do not need an external resistor.
The Code Explained
The program is broken down into three main parts: the pin definitions, the setup() function, and the loop() function.
Pin Definitions
This section sets up the required libraries and assigns meaningful names to the pins you're using.
#include <Servo.h>
const int servoPin = 3;
const int buttonPin = 2;
Servo myServo;
The setup() Function
The setup() function contains code that runs only once when the Arduino board is powered on or reset. It's used for initial configuration.
void setup() {
myServo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
myServo.write(0);
}
The crucial change is pinMode(buttonPin, INPUT_PULLUP);. This activates an internal resistor that pulls the pin's voltage to HIGH. When you press the button, it connects the pin to ground, and the voltage drops to LOW.
The loop() Function
The loop() function contains the main program logic and runs repeatedly forever after the setup() function is finished.
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
myServo.write(180);
} else {
myServo.write(0);
}
}
The logic here is now reversed. Since the button press results in a LOW signal, the if (buttonState == LOW) condition is used to move the servo to 180 degrees.
#include <Servo.h>
/*
* Dies ist eine Skizze zur Steuerung eines Servomotors. Wenn der Druckknopf gedrückt wird, bewegt sich der Servomotor auf 180 Grad und bleibt dort. Wenn der Druckknopf losgelassen wird, kehrt der Servomotor auf 0 Grad zurück.
*/
// Definieren Sie den Pin für den Servomotor.
const int servoPin = 3;
// Definieren Sie den Pin für den Druckknopf.
const int buttonPin = 2;
// Erstellen Sie ein Servo-Objekt
Servo myServo;
void setup() {
// Befestigen Sie das Servo-Objekt am Servo-Pin
myServo.attach(servoPin);
// Setzen Sie den Taster-Pin als Eingang.
pinMode(buttonPin, INPUT);
// Starte den Servomotor bei 0 Grad
myServo.write(0);
}
void loop() {
// Lesen Sie den Zustand des Druckknopfs
int buttonState = digitalRead(buttonPin);
// Wenn der Knopf gedrückt wird (HIGH)
if (buttonState == HIGH) {
// Bewege den Servomotor auf 180 Grad.
myServo.write(180);
} else {
// Wenn der Knopf losgelassen wird (LOW), bewege den Servo zurück auf 0 Grad.
myServo.write(0);
}
}
Dinge, die Sie vielleicht brauchen
-
AmazonServomotor auf Amazonamzn.to
-
AliExpressKaufen Sie den SG90 Servomotor 180 oder 360 von AliExpresss.click.aliexpress.com
Ressourcen & Referenzen
Noch keine Ressourcen vorhanden.
Dateien📁
Keine Dateien verfügbar.