I am trying to use a NodeMCU to control the speed of a brushed DC motor through the blynk IOS app. I have no experience with DC motors, so I’m hoping someone can help me out with the coding side.
Here is what I have so far:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "1de9d226f13c487c8640447e4901265f";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "wifi";
char pass[] = "password";
int motorPin = 3;
int speeed = 0;
void setup() {
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while(! Serial);{
Serial.println("Speed 225");}
Serial.println();
Serial.println("WiFi connected");
}
void loop() {
Blynk.run();
BLYNK_WRITE(V1)
{speeed = (param.asInt());}
{
analogWrite(motorPin, speeed);
}
}
I am still unsure of how to assign the speeed int to the blynk slider. Assistance would be appreciated. Thanks in advance
Hello!
Controlling DC motors require some sort of intermediary circuit inbetween the NodeMCU and motors, such as L293D IC/Shields, L298N and other types of ICs. NodeMCU alone can’t handle the current needed for the motors
It’s easier (i think) to use Arduino IDE to program the NodeMCU, since there are a lot of libraries to use, to help ease the programming.
How much current is the motor drawing? The ESP8266 has a maximum current of 12mA per GPIO pin. That won’t drive much of a motor.
Do you only want your motor to rotate in one direction? If you do then that’s okay, simply wire the negative connection to GND and the positive connection to a GPIO pin. However, if you want to reverse the direction then the connections need to be reversed. How are you going to achieve this?
There are lots of approaches you could take, but the most sensible on in my book would be going for a NodeMCU motor shield, Wemos D1 Mini with Motor shield, or building your won H Bridge instead of using a shield.
Either way, it’s not really a Blynk related issue at this stage in the process.
Bad, bad, bad … Microcontroller GPIO are designed for DATA not power, aside from a few LEDs, etc. And even then need to have current limiting resistors to mitigate overload.
And aside from large startup current (easily 2-3 times rated current) and running current requirements, motors are inductive circuits and can cause EM feedback that can damage your MCU. Use a proper H-Bridge Motor controller.