Full Control of a DC Motor with an ESP8266 NodeMCU D1 Mini over Wi-Fi
This video shows how to use an L298N module with an ESP8266 NodeMCU or D1 Mini (e.g., LOLIN) to fully control a DC motor. Please use the "Download" button to get the Arduino code or download the zip file, which contains the Arduino code and .h file. This code is to fully control a DC motor. Speed-only control code is also available.
262-Control DC motor with ESP8266 NodeMCU and D1 Mini with L298N
语言: C++
/*
* Control Speed and direction of rotation CW/CCW of DC Motor with ESP8266 NodeMCU and D1 Mini with L298N module
* Motor is controlled using Robojax_L298N_DC_motor library
*
* Watch Video instruction for this code: https://youtu.be/gOMHU0Q8upA
* Written by Ahmad Shamshiri on Nov 02, 2019
* in Ajax, Ontario, Canada. www.robojax.com
*
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) 2015, Majenko Technologies
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* * Neither the name of Majenko Technologies nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 D6
#define IN2 D7
#define ENA D8 // this pin must be PWM enabled pin
const int CCW = 2; // do not change
const int CW = 1; // do not change
int motorDirection = CW;// default direction
#define motor1 1 // do not change
// use the line below for single motor
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);
String accessName ="robojaxESP8266";
const int changeStep = 5;// 10 is 10% every time button is pushed
int outPutValue = 40;// variable holding the light output value (initial value) 40 means 40%
const int motorMinimumSpeed=20;
int motor1StopState=HIGH;//Stope state of motor HIGH=STOP
#include "robojax_speed__direction_control_page.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "Robojax"
#define STAPSK "YouTube2019_o_"
#endif
const char *ssid = STASSID;
const char *password = STAPSK;
ESP8266WebServer server(80);
void handleRoot() {
String HTML_page = speed_control_page_part1;
HTML_page.concat(outPutValue);
HTML_page.concat(speed_control_page_part2);
HTML_page.concat(outPutValue);
HTML_page.concat(speed_control_page_part3);
server.send(200, "text/html", HTML_page);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
Serial.begin(115200);
motor.begin();
//L298N DC Motor by Robojax.com
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for 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());
if (MDNS.begin(accessName)) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/speed", HTTP_GET, handleMotorSpeed);
server.on("/direction", HTTP_GET, handleMotorDirection);
server.on("/stop", HTTP_GET, handleMotorBrake);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
if(motor1StopState ==HIGH)
{
motor.brake(motor1);
}else{
motor.rotate(motor1, outPutValue, motorDirection);//run motor1 at 60% speed in CW direction
}
// delay(3000);
//motor.brake(1);
delay(100);
}
/*
* handleMotorSpeed()
* Slows down or speeds up the motor
* returns nothing
*/
void handleMotorSpeed() {
if(server.arg("do") == "slower")
{
outPutValue -=changeStep;
if(outPutValue < motorMinimumSpeed)
{
outPutValue = motorMinimumSpeed;
}
}else{
outPutValue +=changeStep;
if(outPutValue > 100)
{
outPutValue =100;
}
}
handleRoot();
}//
/*
* handleMotorDirection()
* changes the direction of the motor
* returns nothing
* Written by Ahmad Shamshiri on Nov 02, 2019
* www.Robojax.com
*/
void handleMotorDirection() {
if(server.arg("dir") == "CW")
{
motorDirection =CW;
}else{
motorDirection =CCW;
}
handleRoot();
}//
/*
* handleMotorBrake()
* applies brake to the motor
* returns nothing
* Written by Ahmad Shamshiri on Nov 02, 2019
* www.Robojax.com
*/
void handleMotorBrake() {
if(server.arg("do") == "START")
{
motor1StopState=LOW;
}else{
motor1StopState=HIGH;
}
handleRoot();
}//
|||您可能需要的东西
-
全球速卖通AliExpress.com 产品 - 1件 新款双H桥直流步进电机驱动控制器板模块 L298N 电机驱动器s.click.aliexpress.com
-
全球速卖通
-
全球速卖通阿里巴巴国际站产品 - 新无线模块 CH340 NodeMCU V3 Lua WiFi 物联网开发板 基于 ESP8266s.click.aliexpress.com
-
全球速卖通阿里巴巴网产品 - WOTT 4件套Arduino智能汽车机器人塑料轮胎轮子,配有直流3-6V齿轮电机s.click.aliexpress.com
-
BanggoodDC 3V-6V 双轴齿轮电机 TT 电机用于智能底盘车banggood.com
资源与参考
-
外部AliExpress.com 产品 - 1件 新款双H桥直流步进电机驱动控制器板模块 L298N 电机驱动器s.click.aliexpress.com
-
外部DC 3V-6V 双轴齿轮电机 TT 电机用于智能底盘车banggood.com
-
外部阿里巴巴国际站产品 - 新无线模块 CH340 NodeMCU V3 Lua WiFi 物联网开发板 基于 ESP8266s.click.aliexpress.com
-
外部阿里巴巴网产品 - WOTT 4件套Arduino智能汽车机器人塑料轮胎轮子,配有直流3-6V齿轮电机s.click.aliexpress.com
文件📁
没有可用的文件。