Kolay Arduino Projesi: Bir Servoyu Butonla 180 dereceden 0'a ve geri kontrol etme
Giriş
Bu makale, bir servo motoru kontrol etmek için bir buton kullanan basit bir Arduino projesini açıklamaktadır. Bir servo motor, belirli bir konuma dönebilen hassas bir motordur ve bu da onu robotik ve otomasyon gibi uygulamalar için ideal kılar. Bu proje, bir butona basıldığında servonun 0'dan 180 dereceye hareket etmesini ve bırakıldığında 0'a dönmesini sağlayacaktır.
Proje Bileşenleri ve Bağlantı
Bir Arduino kartı, bir servo motor ve bir butona ihtiyacınız olacak.
Bağlantı Talimatları:
- Servo Motor: Sinyal kablosunu (genellikle sarı/turuncu) Arduino pin 3'e, güç kablosunu (kırmızı) 5V'a ve toprak kablosunu (siyah/kahverengi) GND'ye bağlayın.
- Buton: Bir ayağını dijital pin 2'ye ve diğer ayağını bir GND pinine bağlayın. Bu kadar! Arduino'nun dahili pull-up direncini kullanarak harici bir dirence ihtiyacınız yoktur.
Kodun Açıklaması
Program üç ana bölüme ayrılmıştır: pin tanımları, setup() fonksiyonu ve loop() fonksiyonu.
Pin Tanımları
Bu bölüm gerekli kütüphaneleri kurar ve kullandığınız pinlere anlamlı isimler atar.
#include <Servo.h>
const int servoPin = 3;
const int buttonPin = 2;
Servo myServo;
setup() Fonksiyonu
setup() fonksiyonu, Arduino kartı açıldığında veya sıfırlandığında yalnızca bir kez çalışan kodu içerir. İlk yapılandırma için kullanılır.
void setup() {
myServo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
myServo.write(0);
}
Kritik değişiklik pinMode(buttonPin, INPUT_PULLUP); satırıdır. Bu, pinin voltajını HIGH seviyesine çeken dahili bir direnci etkinleştirir. Butona bastığınızda, pini toprağa bağlar ve voltaj LOW seviyesine düşer.
loop() Fonksiyonu
loop() fonksiyonu ana program mantığını içerir ve setup() fonksiyonu bittikten sonra sürekli olarak sonsuza kadar çalışır.
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
myServo.write(180);
} else {
myServo.write(0);
}
}
Buradaki mantık artık tersine çevrilmiştir. Butona basma LOW sinyaliyle sonuçlandığından, servoyu 180 dereceye hareket ettirmek için if (buttonState == LOW) koşulu kullanılır.
#include <Servo.h>
/*
* This is sketch for controlling servo motor. when push button is pressed, the servo moves to 180 degreees and stays there. when the push button is released, the servo motor returns back to 0 degree.
*/
// Define the pin for the servo motor
const int servoPin = 3;
// Define the pin for the push button
const int buttonPin = 2;
// Create a Servo object
Servo myServo;
void setup() {
// Attach the Servo object to the servo pin
myServo.attach(servoPin);
// Set the button pin as an input
pinMode(buttonPin, INPUT);
// Start the servo at 0 degrees
myServo.write(0);
}
void loop() {
// Read the state of the push button
int buttonState = digitalRead(buttonPin);
// If the button is pressed (HIGH)
if (buttonState == HIGH) {
// Move the servo to 180 degrees
myServo.write(180);
} else {
// If the button is released (LOW), move the servo back to 0 degrees
myServo.write(0);
}
}
İhtiyacın olabileceğini düşünüyor
-
AmazonServo motor on Amazonamzn.to
-
AliExpressPurchase SG90 Servo motor 180 or 360 from AliExpresss.click.aliexpress.com
Kaynaklar ve referanslar
Henüz kaynak yok.
Dosyalar📁
Dosya mevcut değil.