ESP32: Servo Motor + DC Motor viaL298N

Hello World
I am fail to use ESP32 to drive Servo Motor and DC Motor via L298N at the same time. Especially after updating the ESP32 Board.

I try to assign different channel to Servo and L298N but fail
any sample code would provide or any advise on that??

Many Thanks!!!

Hi, @alvinchanccy
Welcome to the forum.

Can you please post your code in code tags.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What is your power supply?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Thanks a lot
I try these 2 set of codes, but both codes don't work after updating the ESP32 Board to 3.0.5

Code 1
Error shown: Documents/Arduino/libraries/ESP32_Servo/src/ESP32_Servo.cpp:144:9: error: 'ledcSetup' was not declared in this scope
and more.....

#include <ESP32_Servo.h>
int servoPin = 18;
Servo myservo; 
const int freq = 50;
const int ledChannel = 0;
const int resolution = 8;
const int dutyCycle = 26;
///////////////////////////
const int motor1Pin1 = 12; 
const int motor1Pin2 = 13; 
const int enable1Pin = 14; 
const int freq2 = 30000;
const int ledChannel2 = 1;
const int resolution2 = 8;
const int dutyCycle2 = 200;
//////////()///////////////////

void setup(){
  // ledcSetup(ledChannel,freq,resolution);
  // ledcAttachPin(servoPin, ledChannel);
  ledcAttachChannel(servoPin, freq, resolution, ledChannel);

  myservo.attach(servoPin, 700, 7000);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);  
  // ledcSetup(ledChannel2,freq2,resolution2);
  //ledcAttachPin(enable1Pin,ledChannel2);
  ledcAttachChannel(enable1Pin, freq2, resolution2, ledChannel2);
}
void loop(){
  ledcWrite(servoPin,dutyCycle);
  ledcWrite(enable1Pin,dutyCycle2);
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 

  myservo.write(0);
  delay(300);
  myservo.write(80); 
  delay(300);
}

Code 2:
L298N DC Motor work but Servo has no response.

// Values for TowerPro SG90 small servos; adjust if needed
//Servo 
#define COUNT_LOW 1638
#define COUNT_HIGH 7864
#define TIMER_WIDTH 16
#include "esp32-hal-ledc.h"

int servoPin = 18;
int servoFreq = 50;
int servoResolution = 8;
int servoChannel = 3;
int servoDutyCycle = 26;

// L298N
int motor1Pin1 = 12; 
int motor1Pin2 = 13; 
int enable1Pin = 14; 
int dcFreq = 30000;
int dcResolution = 8;
int dcChannel = 0;
int dcDutyCycle = 200;

void setup() {
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);    
  Serial.begin(9600);
  ledcAttachChannel(servoPin, servoFreq, servoResolution, servoChannel); // pin, freq, resulution, channel
  ledcAttachChannel(enable1Pin, dcFreq, dcResolution, dcChannel); // pin, freq, resulution, channel
}

void loop() {
  Serial.println("Servo Turn");
  //ledcWrite(servoChannel, servoDutyCycle);
   for(int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100){
      //ledcWrite(servoChannel, i);       // sweep servo 1
      ledcWrite(servoPin, i);       // sweep servo 1
      delay(30);
   }
   for (int i= COUNT_HIGH; i > COUNT_LOW ; i=i-100){
      //ledcWrite(servoChannel, i);       // sweep servo 1
      ledcWrite(servoPin, i);       // sweep servo 1
      delay(30);
   }
   /////////////Control L298N, Different Direction, Different Speed
  Serial.println("DC Motor Turn");
  ledcWrite(enable1Pin, dcDutyCycle);
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW); 
  delay(3000);

  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW); 
  delay(3000);
}

Many Thanks Everyone again and hopefully the problem can be solved
Thanks


Hardware connection

I think the library you are using is not updated for 3.0
Try this:

1 Like

PP3 NO2

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.