Last active
February 22, 2023 22:32
-
-
Save SMMC-Digital-Technology/b82d24a1372aa5fd9b13e5e3ffb0e900 to your computer and use it in GitHub Desktop.
Arduino servo motor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This program demonstrates how the servo motor can be used in an Arduino program. | |
* The motor will rotate 180 degrees and then quickly rotate back before repeating the process. | |
* This example assumes that the signal wire for the motor is connected to PIN 10 on the Arduino. | |
* | |
* Author: Mr Offner | |
* | |
* Parts Needed: | |
* Arduino x1 | |
* USB Cable x1 | |
* Servo motor x1 | |
* Male-to-male jumper wire x3 | |
* | |
* Construction: | |
* Connect the three jumper wires to the three wires from the servo | |
* The BROWN wire from the servo goes into GND | |
* The RED wire from the servo goes into 5V | |
* The ORANGE wire from the servo goes into PIN 10 | |
* | |
*/ | |
//Include the servo library in the program | |
#include <Servo.h> | |
Servo motor; //Define a servo, called 'motor' | |
int pinServo = 10; //Define pinServo as an integer with a value of 10 | |
int angle = 0; //Define a variable called angle, with a value of 0 | |
void setup() { | |
motor.attach(pinServo); //Tell the program that the servo is connected to pinServo (10) | |
motor.write(angle); //Adjust the servo to the starting position (0) | |
} | |
void loop() { | |
//Increase the value of angle by 1 | |
angle += 1; | |
if(angle > 180) //If the servo angle is larger than 180, change it to 0. | |
{ | |
angle = 0; | |
} | |
motor.write(angle); //Turn the servo to the new angle | |
delay(50); //Wait 0.05 seconds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment