Hi guys, I am having a really hard time trying to control 2 DC motors using Blynk app, NodeMCU and 12V H Bridge.
First, here are hardware parts of my project:
- NodeMCU link
- H Bridge link
- Blynk app screenshot
And here is my code:
// Mower
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define motor1IN1 D2 //4
#define motor1IN2 D5 //14
#define motor2IN1 D6 //12
#define motor2IN2 D8 //15
#define EN1 D0 //16
#define EN2 D1 //5
char auth[] = "auth"; //Blynk Authentication Token
char ssid[] = "name"; //WIFI Name
char pass[] = "password"; //WIFI Password
char command[8];
int pinValue;
int power;
void moveControl(char command[], int value, int power=0)
{
if(value != 0) {
if(strcmp(command, "forward") == 0) {
digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
digitalWrite(motor1IN1, HIGH);
analogWrite(motor1IN2, power);
digitalWrite(motor2IN1, HIGH);
analogWrite(motor2IN2, power);
}
else if(strcmp(command, "back") == 0) {
digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
analogWrite(motor1IN1, power);
digitalWrite(motor1IN2, LOW);
analogWrite(motor2IN1, power);
digitalWrite(motor2IN2, LOW);
}
else {
digitalWrite(EN1, LOW);
digitalWrite(EN2, LOW);
digitalWrite(motor1IN1, LOW);
digitalWrite(motor1IN2, LOW);
digitalWrite(motor2IN1, LOW);
digitalWrite(motor2IN2, LOW);
}
}
else {
digitalWrite(EN1, LOW);
digitalWrite(EN2, LOW);
digitalWrite(motor1IN1, LOW);
digitalWrite(motor1IN2, LOW);
digitalWrite(motor2IN1, LOW);
digitalWrite(motor2IN2, LOW);
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
analogWriteFreq(16000);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(motor1IN1, OUTPUT);
pinMode(motor1IN2, OUTPUT);
pinMode(motor2IN1, OUTPUT);
pinMode(motor2IN2, OUTPUT);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
char command[] = "forward";
moveControl(command, pinValue, power);
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt();
char command[] = "back";
moveControl(command, pinValue, power);
}
BLYNK_WRITE(V4)
{
int pinValue = param.asInt();
char command[] = "left";
moveControl(command, pinValue, power);
}
BLYNK_WRITE(V5)
{
int pinValue = param.asInt();
char command[] = "right";
moveControl(command, pinValue, power);
}
BLYNK_WRITE(V6)
{
power = param.asInt();
// moveControl(command, pinValue, power);
}
So the problem is that H Bridge behave very strange:
By using code above, it turns DC motors CCW whether I push forward or backward.
Also, it seems that PWM signal is “upside down” for forward, because lower values giving higher speed, and for backward it is normal.
I am 100% sure that the problem is in my code, and this H Bridge that doesn’t have clear datasheet on the internet.
Any code review and advice is very appreciated!
Thanks in advance!