ESP32 DC Motor and Servo control from WiFi

Ginz09
Posts: 2
Joined: Sat May 09, 2020 9:43 am

ESP32 DC Motor and Servo control from WiFi

Postby Ginz09 » Sat May 09, 2020 9:52 am

Hi, all
i have arduino program for controlling DC Motor and Servo.. the issue is in servo not moving.. for the dc motor it works well
is any something wrong with my program? please help.. and thanks before
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <Servo.h>

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

WebServer server(80);
String page = ""; //For the Web Server
String page2=""; //For updating Status of motor

// Motor A
int motor1Pin1 = 18;
int motor1Pin2 = 19;
int enable1Pin = 17;

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

Servo myservo;
int pos = 90;

void setup(void) {

//the HTML of the web page
page =
#include "webPage.h"
;
// sets the pins as outputs:
myservo.attach(16);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);

// configure LED PWM functionalitites
ledcSetup(pwmChannel, freq, resolution);

// attach the channel to the GPIO to be controlled
ledcAttachPin(enable1Pin, pwmChannel);
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password); //begin WiFi connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //provides IP address
server.on("/", [](){
server.send(200, "text/html", page+page2);
});
server.on("/Forward",Forward);
server.on("/Backward",Backward);
server.on("/Right",Right);
server.on("/Left",Left);
server.on("/Stop",[](){ // turns all the motor input pins low
page2="<center><p> motor 1 Status : Off</p></center>";
server.send(200,"text/html",page+page2);
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2,LOW);
delay(200);
});
server.begin();

// testing
Serial.print("Testing DC Motor and Servo...");
}

void loop(void) {
server.handleClient();
}
void Forward(){
// Move DC motor forward with increasing speed
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
while (dutyCycle <= 255){
ledcWrite(pwmChannel, dutyCycle);
Serial.print("Forward with duty cycle: ");
Serial.println(dutyCycle);
dutyCycle = dutyCycle + 5;
delay(500);
}
page2="<center><p> motor 1 Status : Forward </p></center>";
server.send(200,"text/html", page+page2);
delay(200);

}
void Backward(){
// Move DC motor Backward with increasing speed
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
while (dutyCycle <= 255){
ledcWrite(pwmChannel, dutyCycle);
Serial.print("Backward with duty cycle: ");
Serial.println(dutyCycle);
dutyCycle = dutyCycle + 5;
delay(500);
}
page2="<center><p> motor 1 Status : Backward </p></center>";
server.send(200,"text/html", page+page2);
delay(200);
}

void Right(){
for(int pos = 90; pos <= 135; pos += 3) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(pos);
delay(15);
}
}

void Left(){
for(int pos = 90; pos >= 45; pos-=3) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
}

Ginz09
Posts: 2
Joined: Sat May 09, 2020 9:43 am

Re: ESP32 DC Motor and Servo control from WiFi

Postby Ginz09 » Sat May 09, 2020 3:57 pm

Any Body?

Who is online

Users browsing this forum: No registered users and 65 guests