Libraries are one of the things that give Arduino their superpowers.
A servo motor is a specialized motor whose direction can be precisely set
This code uses the servo library. Libraries are code that adds additional functions to Arduino.
For example, to use a servo motor, we need to:
(1) #include <Servo.h>. //include the servo library
(2) Servo myservo; // create servo object to control a servo
(3) myservo.attach(9); // attaches the servo on pin 9 to the servo object
(4) myservo.write(pos); write the servo position( 0 to 180)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
Below: Servomotor with Knob and I2C OLED Display. Not shown: breadboard power supply
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define OLED screen dimensions
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // Typical I2C address for 128x64 and 128x32 OLEDs
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// Initialize the OLED display
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
// If the display fails to initialize, the code will halt here
for(;;);
}
// Clear the display buffer and set initial text properties
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.display();
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (0 to 1023)
val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (0 to 180)
myservo.write(val); // sets the servo position
// --- OLED Update Section ---
display.clearDisplay(); // Clear the previous frame
// Print "Angle:" label
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(0, 0); // Start at top-left corner
display.print("Servo Angle:");
// Print the actual angle value
display.setTextSize(3); // Scale up the text for the number
display.setCursor(0, 20); // Move the cursor down
display.print(val);
// Print a degree symbol (ASCII character 247 in standard Arduino fonts)
display.print((char)247);
display.display(); // Push the drawn buffer to the screen
// ---------------------------
delay(15); // waits for the servo to get there
}