Hey there, I have been trying to make a code that alternates the direction of a dc motor at a set speed (the dutycycle) but I keep getting this error, "'ledcSetup' was not declared in this scope"... I'm 13, not very advanced in arduino ide and would love some help if you can. Cheers, Matthias
The code...
int ENA = 2;
int IN1 = 3;
int IN2 = 4;
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;
void setup() {
// put your setup code here, to run once:
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
ledcSetup(pwmChannel, freq, resolution);
ledcAttachPin(ENA, pwmChannel);
}
void loop() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(1000);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
The error happens because those functions are not implemented in your code, or in the predefined Arduino functions like pinMode() that the Arduino IDE gives you.
If you came across them in some sketch somewhere, you need to go back and install it, and also use #include in your sketch.
Hello all,
I have been trying to figure out a good way to control the direction and speed of a DC motor using an L298N, a NodMCU, and Thinger.io. I would like to have a slider that controls the speed, a switch that turns motor on and off, and another switch that changes the direction... I figured out how to make the motor turn on and off one direction with the code below which is based off of thingers NodeMCU example but I cant figure anything else out I'm sorta new to Arduino and any help with the code would be appreciated!
Thanks in advance,
Matthias.
P.S when I was uploading my code I double checked that the info in all of the fields were correct...
#include <ThingerESP8266.h>
// I put my user name in here
#define USERNAME "------------"
// I put my device id in here
#define DEVICE_ID "----------"
// I put my Device_Credential here
#define DEVICE_CREDENTIAL "-----------"
// I put my SSID in here
#define SSID "----------"
// And my SSID password in here
#define SSID_PASSWORD "----------"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
// Sets all necessary pins as output
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
// Enables/Disables Motor A (ENA pin on L298N)
thing["ENA"] << digitalPin(2);
}
void loop() {
thing.handle();
// sets the dc motor direction
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
Ok, so basically I am trying to make a web server controlled chicken coop door and to do that I need to be able to control both the speed and the direction and I have no idea how to do either... I found Thinger.io which looked like it would work but I have only figured out how to turn the motor on and off, still no idea about direction and speed tho.
Any help is still appreciated,
Matthias.
You may need to break your project into the main parts and get them working first, then work on combining them into "do it all" code. I suggest you use the forum Google search function in the upper right of this page to search for the key words of your project, like "coop door", "motor L298n", and "NodMCU web server". You will probably find many similar previous project discussions and code to get you started.
That code is written for the ESP32. The ESP32 core defines the ledcSetup() and ledcAttachPin() functions. The ESP8266 core you use for your NodeMCU does not have these function. That's why you're getting the error.
For PWM on the ESP8266, just use the normal analogWrite() function: