Servo ansteuern (Arduino, ESP8266, ESP32)

Servo ansteuern (Arduino, ESP8266, ESP32)

Was sind Servomotoren?

0 Grad, Arduino 90 Grad, Arduino 180 Grad, Arduino
ESP32 Mit der neuen Funktion (ledcWrite) kann die Frequenz selber bestimmt werden, mehr dazu weiter unten im Beitrag.
esp32-0 esp32-90 esp32-180

Servo mit Arduino Uno ansteuern.

Servo Arduino
Funktion
Programmcode
				
					#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

#include <Servo.h>
Servo servoblau;

void setup() {
  servoblau.attach(8);
  u8g2.begin();
  u8g2.enableUTF8Print();
}

void loop() {
  u8g2.setFont(u8g2_font_courB18_tf);
  u8g2.setFontDirection(0);

  servoblau.write(0);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "0 Grad");
  } while ( u8g2.nextPage() );
  delay(3000);
  servoblau.write(90);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "90 Grad");
  } while ( u8g2.nextPage() );
  delay(3000);
  servoblau.write(180);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "180 Grad");
  } while ( u8g2.nextPage() );
  delay(3000);
  servoblau.write(120);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "120 Grad");
  } while ( u8g2.nextPage() );
  delay(3000);
  servoblau.write(45);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "45 Grad");
  } while ( u8g2.nextPage() );
  delay(3000);
}
				
			

Servo ansteuern mit ESP8266 als Web Server (Wemos D1 mini Lite)

Servo ESP8266

Funktion

Programmcode
				
					#include <Servo.h>


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

const char* ssid     = "WLAN NAME";
const char* password = "PASSWORT";
Servo servoblau;
ESP8266WebServer server ( 80 );

void setup()
{
  Serial.begin(9600);
  servoblau.attach(12);
  u8g2.begin();
  u8g2.enableUTF8Print();
  connectToWifi();
  beginServer();
  servoblau.write(0);
  u8g2.setFont(u8g2_font_courB18_tf);
  u8g2.setFontDirection(0);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "0 Grad");
  } while ( u8g2.nextPage() );
}

void loop() {

  server.handleClient();

  delay(1000);

}

void connectToWifi()
{
  WiFi.enableSTA(true);

  delay(2000);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void beginServer()
{
  server.on ( "/", handleRoot );
  server.begin();
  Serial.println ( "HTTP server started" );
}

void handleRoot() {
  if ( server.hasArg("SERVO") ) {
    handleSubmit();
  } else {
    server.send ( 200, "text/html", getPage() );
  }
}

void handleSubmit() {

  String SERVOValue;
  SERVOValue = server.arg("SERVO");
  Serial.println("Set GPIO ");
  Serial.print(SERVOValue);

  if ( SERVOValue == "0" ) {
    servoblau.write(0);
    server.send ( 200, "text/html", getPage() );
    u8g2.setFont(u8g2_font_courB18_tf);
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.drawStr(0, 20, "0 Grad");
    } while ( u8g2.nextPage() );
  }
  else if ( SERVOValue == "90" )
  {
    servoblau.write(90);
    server.send ( 200, "text/html", getPage() );
    u8g2.setFont(u8g2_font_courB18_tf);
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.drawStr(0, 20, "90 Grad");
    } while ( u8g2.nextPage() );
  }
  else if ( SERVOValue == "180" )
  {
    servoblau.write(180);
    server.send ( 200, "text/html", getPage() );
    u8g2.setFont(u8g2_font_courB18_tf);
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.drawStr(0, 20, "180 Grad");
    } while ( u8g2.nextPage() );
  } else
  {
    Serial.println("Error Servo Value");
  }
}

String getPage() {
  String page = "<html lang=en-EN><head><meta http-equiv='refresh' content='60'/>";
  page += "<title>arduino-projekte.info</title>";
  page += "<style></style>";
  page += "</head><body><h1>ESP8266 WebServer</h1>";
  page += "<h3>Servo Test</h3>";
  page += "<form action='/' method='POST'>";
  //page += "<INPUT type='radio' name='SERVO' value='0'>0 Grad<br>";
 // page += "<INPUT type='radio' name='SERVO' value='90'>90 Grad<br>";
 // page += "<INPUT type='radio' name='SERVO' value='180'>180 Grad<br>";
  page += "<INPUT class='button' type='submit' name='SERVO' value='0'>&nbsp;&nbsp;&nbsp;&nbsp;";
  page += "<INPUT class='button' type='submit' name='SERVO' value='90'>&nbsp;&nbsp;&nbsp;&nbsp;";
  page += "<INPUT class='button' type='submit' name='SERVO' value='180'>";

  page += "</body></html>";
  return page;
}
				
			

Servo ansteuern mit einem ESP32 als Web Server (Wemos Lolin32)

servo wemos lolin32

Funktion

Programmcode
				
					
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

WebServer server ( 80 );

const char* ssid     = "WLANNAME";
const char* password = "password";

void setup()
{
  ledcSetup(1, 166, 8);
  ledcAttachPin(13, 1);   // GPIO 22 assigned to channel 1
  Serial.begin(9600);
  u8g2.begin();
  u8g2.enableUTF8Print();
  connectToWifi();
  beginServer();
  ledcWrite(1, 2);
  u8g2.setFont(u8g2_font_courB18_tf);
  u8g2.setFontDirection(0);
  u8g2.firstPage();
  do {
    u8g2.drawStr(0, 20, "0 Grad");
  } while ( u8g2.nextPage() );
}

void loop() {

  server.handleClient();

  delay(1000);

}

void connectToWifi()
{
  WiFi.enableSTA(true);

  delay(2000);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void beginServer()
{
  server.on ( "/", handleRoot );
  server.begin();
  Serial.println ( "HTTP server started" );
}

void handleRoot() {
  if ( server.hasArg("SERVO") ) {
    handleSubmit();
  } else {
    server.send ( 200, "text/html", getPage() );
  }
}

void handleSubmit() {

  String SERVOValue;
  SERVOValue = server.arg("SERVO");
  Serial.println("Set GPIO ");
  Serial.print(SERVOValue);

  if ( SERVOValue == "0" ) {
    ledcWrite(1, 2);
    server.send ( 200, "text/html", getPage() );
    u8g2.setFont(u8g2_font_courB18_tf);
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.drawStr(0, 20, "0 Grad");
    } while ( u8g2.nextPage() );
  }
  else if ( SERVOValue == "90" )
  {
    ledcWrite(1, 60);
    server.send ( 200, "text/html", getPage() );
    u8g2.setFont(u8g2_font_courB18_tf);
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.drawStr(0, 20, "90 Grad");
    } while ( u8g2.nextPage() );
  }
  else if ( SERVOValue == "180" )
  {
    ledcWrite(1, 125);
    server.send ( 200, "text/html", getPage() );
    u8g2.setFont(u8g2_font_courB18_tf);
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do {
      u8g2.drawStr(0, 20, "180 Grad");
    } while ( u8g2.nextPage() );
  } else
  {
    Serial.println("Error Servo Value");
  }
}

String getPage() {
  String page = "<html lang=en-EN><head><meta http-equiv='refresh' content='60'/>";
  page += "<title>arduino-projekte.info</title>";
  page += "<style></style>";
  page += "</head><body><h1>ESP32 WebServer</h1>";
  page += "<h3>Servo Test</h3>";
  page += "<form action='/' method='POST'>";
  //page += "<INPUT type='radio' name='SERVO' value='0'>0 Grad<br>";
  // page += "<INPUT type='radio' name='SERVO' value='90'>90 Grad<br>";
  // page += "<INPUT type='radio' name='SERVO' value='180'>180 Grad<br>";
  page += "<INPUT class='button' type='submit' name='SERVO' value='0'>&nbsp;&nbsp;&nbsp;&nbsp;";
  page += "<INPUT class='button' type='submit' name='SERVO' value='90'>&nbsp;&nbsp;&nbsp;&nbsp;";
  page += "<INPUT class='button' type='submit' name='SERVO' value='180'>";

  page += "</body></html>";
  return page;
}