When I try to control the servo through Blynk, it doesn’t do anything. tested with different servos and pins with no success. when I tried with the sweep example it worked just fine. so it’s not something to do with hardware.
Appreciate any help.
#define BLYNK_DEVICE_NAME "--------"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#define USE_ESP32S2_DEV_KIT
#include "BlynkEdgent.h"
#include <ESP32Servo.h>
#include <analogWrite.h>
bool forward = false;
bool backward = false;
bool left = false;
bool right = false;
#define MR1 26 //digital pin for controlling polarity motor1
#define MR2 27 //digital pin for controlling polarity motor1
#define ML1 14 //digital pin for controlling polarity motor2
#define ML2 32 //digital pin for controlling polarity motor2
const int ENA = 33; //PWM pin to control the speed of motor1
const int ENB = 25; //PWM pin to control the speed of motor2
const int servoPinY = 4;
const int servoPinX = 19;
Servo servoY;
Servo servoX;
void setup()
{
Serial.begin(115200);
pinMode(MR1, OUTPUT);
pinMode(MR2, OUTPUT);
pinMode(ML1, OUTPUT);
pinMode(ML2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
servoY.attach(servoPinY);
servoX.attach(servoPinX);
BlynkEdgent.begin();
}
void loop(){
BlynkEdgent.run();
if (forward == true){// forward
digitalWrite(MR1,LOW);// clockwise rotation of motor1
digitalWrite(MR2,HIGH);
digitalWrite(ML1,HIGH);// clockwise rotation of motor2
digitalWrite(ML2,LOW);
analogWrite(ENA, 255);// speed of motor1
analogWrite(ENB, 255);// speed of motor2
} else if (backward == true){ //backward
digitalWrite(MR1,HIGH);
digitalWrite(MR2,LOW);// anticlockwise rotation of motor1
digitalWrite(ML1,LOW);
digitalWrite(ML2,HIGH);// anticlockwise rotation of motor2
analogWrite(ENA, 255);// speed of motor1
analogWrite(ENB, 255);// speed of motor2
} else if (left == true){ //left
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);
digitalWrite(ML1,LOW);
digitalWrite(ML2,HIGH);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
} else if (right == true){ // right
digitalWrite(MR1,HIGH);
digitalWrite(MR2,LOW);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
analogWrite(ENA, 255);
analogWrite(ENB, 255);
} else { // stop
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
}
BLYNK_WRITE(V0){
if(param.asInt()==1){
forward = true;
}
else{
forward = false;
}
}
BLYNK_WRITE(V1){
if(param.asInt()==1){
backward = true;
}
else{
backward = false;
}
}
BLYNK_WRITE(V2){
if(param.asInt()==1){
left = true;
}
else{
left = false;
}
}
BLYNK_WRITE(V3){
if(param.asInt()==1){
right = true;
}
else{
right = false;
}
}
BLYNK_WRITE(V4){
servoY.write(param.asInt());
}
BLYNK_WRITE(V5){
servoX.write(param.asInt());
}```
@Adi_s Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
It would probably be helpful if you posted the EXACT code that you used with this example, and if you didn’t use the same pins as in your Blynk sketch above then change your sweep example to use these pins and re-test if first.
Details of the type of servo controller and types of servo that you are using, along with details of how they are wired would probably also be useful.
Have you changed the Settings.h tab at all? If not then you have a conflict with pin 19…
yes, this is the exact code. I changed the settings led pin to pin 18 to avoid conflict. Servo positive is connected to 5v and the signal is connected directly to esp32 pin 19. servo is Tower Pro SG90.
It’s difficult to comment on your sketch until we see a version where you’ve cleaned-up the void loop.
What sort of widget do you have attached to virtual pins V4 and V5, and what are the Min/Max values for the V4 and V5 datastreams (defined in the web console > template > datastreams screen)?
The sketch has the USE_ESP32S2_DEV_KIT defined so I’m confused as to why u mentioned the conflict. and yes I checked the pin 4 and 19 on sweep and they worked just fine. but both pins doesn’t seem to work when used in the blynk sketch and nor does any other pins.
that’s okay. I suspect this issue has something to do with either the blynk side of things or some type of incompatibility with the servo library and blynk. I think…