Control a servo with Arduino

FunduinoLast updated on

Vier Positionen anfahren – mit UNO, Wemos D1 Mini oder NodeMCU

Wie funktioniert ein Servomotor?

Ein Servomotor ist ein Elektromotor, der über einen Sensor die aktuelle Position bestimmen und zielgenau anfahren kann. Der hier verwendete Sensormotor kann in 1-Grad-Schritten alle Positionen zwischen 0 und 180 Grad ansteuern.

You need this

Funduino SG90 servo motor - 9 g, 180°, 3.5-6 V Funduino SG90 servo motor - 9 g, 180°, 3.5-6 V 2,33 € *Item no. F23107201About the product — Funduino SG90 servo motor - 9 g, 180°, 3.5-6 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 3 parts together 15,19 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

Controlling a servo motor with a microcontroller

Task: A servo motor is to be controlled by a microcontroller. In this example, the servo should move to four different positions and wait briefly between them.

SG90 servo motor connected to a Funduino UNO R3
Example: TowerPro SG90 on the Funduino UNO R3 microcontroller
Circuit diagram: Servo motor with the Arduino UNO
Arduino UNO schematic
Arduino UNO
// die Bibliothek Servo.h wird eingebunden, sie steuert den Motor
#include "Servo.h"
// der Servomotor erhält den Namen Servomotor
Servo Servomotor;
void setup()
{
  // dem Servomotor wird der Pin 8 zugeordnet
  Servomotor.attach(8);
}
void loop()
{
  // der write-Befehl steuert den Servomotor
  // Zwischen den einzelnen Positionen gibt es eine Pause
  // Position 1 ansteuern mit dem Winkel 0°
  Servomotor.write(0);  // Position 1 ansteuern mit dem Winkel 0°
  // 3 Sekunden Pause
  delay(3000);
  // Position 2 mit dem Winkel 90° ansteuern
  Servomotor.write(90);
  // 3 Sekunden Pause
  delay(3000);
  // Position 3 mit dem Winkel 180° ansteuern
  Servomotor.write(180);
  // 3 Sekunden Pause
  delay(3000);
  // Position 4 mit dem Winkel 20° ansteuern
  Servomotor.write(20);
  // 3 Sekunden Pause
  delay(3000);
}

Download: arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.