Web server controlled chicken coop door - NodeMCU, L298N, DC motor

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);
}

Well, how did you get the idea to add the lines

ledcSetup(pwmChannel, freq, resolution);
ledcAttachPin(ENA, pwmChannel);

to your sketch?

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 :confused: 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);
}

I was trying to set the speed of the motor with the ENA pin by using PWM... Since I did it wrong could you post code with right way to control speed?

So you just made up some function names off the top of your head, and coded them? Probably not. Where did you find them?

here...

I tried to edit his code to make it work for me but i guess that's where my mistake was tho...

@Fission_Chips

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

oh, ok sorry for doing two posts....

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.

Fission_Chips:
here...
ESP32 with DC Motor - Control Speed and Direction | Random Nerd Tutorials
I tried to edit his code to make it work for me but i guess that's where my mistake was tho...

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:

Ok, thanks. thats what I needed, I was getting messed up because I though the code for the Esp8266 was the same as Esp32,
Thanks,
Matthias!

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per