Projet Arduino facile : Contrôler un servo avec un bouton poussoir de 180 degrés à 0 et retour
Bokɔ na likeši
Lisolo oyo elimbisi projɛ́ ya Arduino ya pete oyo esalelaka butɔ ya kopɛndɛmɛ (pushbutton) mpo na kokamba moteur ya servo. Moteur ya servo ezali moteur ya nséngá oyo ekoki kobaluka na esika ya sikisiki, mpo na yango ezali malamu mpo na misala lokola robotiki mpe automatisation. Projɛ́ oyo ekosalisa servo ekende longwa na 0 kino na 180 degré ntango butɔ epɛndɛmɛ mpe ekozonga na 0 ntango butɔ elongoli.
Biloko ya projɛ́ mpe kokangisa yango
Okosalela carte Arduino, moteur ya servo, mpe butɔ ya kopɛndɛmɛ.
Malako ya kokangisa:
- Moteur ya servo: Kanga nzela ya signal (mbala mingi ya jaune/orange) na pin 3 ya Arduino, nzela ya courant (motane) na 5V, mpe nzela ya mabele (noir/marron) na GND.
- Butɔ ya kopɛndɛmɛ: Kanga lokolo moko na pin digitale 2 mpe lokolo mosusu na pin ya GND. Wana nde nyonso! Na kosalela résistance ya kati ya Arduino, okosalela te résistance ya libándá.
Kodɛ oyo elimbisami
Programɛ yango ekabwani na biteni misato ya minene: biloko ya pin, fonction setup(), mpe fonction loop().
Biloko ya Pin
Eteni oyo esalelaka ba bibliothèque oyo esengeli mpe epesaka bankombo ya malamu na ba pin oyo ozali kosalela.
#include <Servo.h>
const int servoPin = 3;
const int buttonPin = 2;
Servo myServo;
Fonction setup()
Fonction setup() ezali na kodɛ oyo esalaka mbala moko kaka ntango carte Arduino ezali na courant to esili kozongisama. Esalelamaka mpo na kokamba liboso.
void setup() {
myServo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
myServo.write(0);
}
Mbongwana ya ntina ezali pinMode(buttonPin, INPUT_PULLUP);. Oyo epesaka nguya na résistance ya kati oyo ememaka voltage ya pin na likoló (HIGH). Ntango opɛndɛmɛ butɔ, yango ekangisaka pin na mabele, mpe voltage ekweyaka na nsé (LOW).
Fonction loop()
Fonction loop() ezali na logique ya liboso ya programɛ mpe esalaka mbala na mbala mpo na libela nsima ya fonction setup() esili.
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
myServo.write(180);
} else {
myServo.write(0);
}
}
Logique awa ebongwani sikoyo. Lokola kopɛndɛmɛ butɔ epesaka signal ya LOW, kondisyo if (buttonState == LOW) esalelamaka mpo na kotinda servo na degré 180.
#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);
}
}
Nko o na nki.
-
AmazonServo motor on Amazonamzn.to
-
AliExpressPurchase SG90 Servo motor 180 or 360 from AliExpresss.click.aliexpress.com
Mokili na bisika
Pas encore de ressources.
Fichiers📁
Aucun fichier disponible.