ESP32 showing error

Hi guys I am trying to control 2 motors using a joystick. I am using an LN298N 2A motor driver. I have done it using Arduino Uno before. Now I am doing the same in ESP32 Devkit 1 but it's showing me 'analogWrite was not declared in this scope'. Does anyone know how I can resolve it? At the moment I have changed pin no.

int joystrick1 = 10;
int joystrick2 = 11;
int val1;
int val2;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(18, OUTPUT);
pinMode(20, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
int val1 = analogRead(joystrick1);
int val2 = analogRead(joystrick2);

if(val1 > 1 && val1 < 490){
val1 = map(val1, 1, 490, 255, 0);

analogWrite(12,val1);
analogWrite(13,0);
}

if(val1 > 530 && val1 < 1023){
val1 = map(val1, 530, 1023, 0, 255);

analogWrite(13,val1);
analogWrite(12,0);
}

if(val1 > 491 && val1 < 529){
val1 = map(val1, 491, 529, 0, 0);

analogWrite(13,val1);
analogWrite(12,val1);
}

if(val2 > 1 && val2 < 490){
val2 = map(val2, 1, 490, 255, 0);

analogWrite(20,val2);
analogWrite(18,0);
}

if(val2 > 530 && val2 < 1023){
val2 = map(val2, 530, 1023, 0, 255);

analogWrite(18,val2);
analogWrite(20,0);
}

if(val2 > 491 && val2 < 529){
val2 = map(val2, 491, 529, 0, 0);

analogWrite(18,val2);
analogWrite(20,val2);
}
Serial.println(val1);
Serial.println(val2);
}

Post the entire error message. Use the "copy error message" in the IDE.

Arduino: 1.8.19 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

C:\Users\csc\AppData\Local\Temp\arduino_modified_sketch_171971\sketch_dec28e.ino: In function 'void loop()':

sketch_dec28e:27:20: error: 'analogWrite' was not declared in this scope

analogWrite(12,val1);

                ^

sketch_dec28e:34:20: error: 'analogWrite' was not declared in this scope

analogWrite(13,val1);

                ^

sketch_dec28e:41:20: error: 'analogWrite' was not declared in this scope

analogWrite(13,val1);

                ^

sketch_dec28e:48:20: error: 'analogWrite' was not declared in this scope

analogWrite(20,val2);

                ^

sketch_dec28e:55:20: error: 'analogWrite' was not declared in this scope

analogWrite(18,val2);

                ^

sketch_dec28e:62:20: error: 'analogWrite' was not declared in this scope

analogWrite(18,val2);

                ^

exit status 1

'analogWrite' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I read somewhere that esp32 doesn't support analogWrite.

It does seem that way. Maybe this page will help >> https://deepbluembedded.com/esp32-pwm-tutorial-examples-analogwrite-arduino/

Here is a library that is supposed to give you analogWrite() function for ESP32. >> GitHub - erropix/ESP32_AnalogWrite: Provides an analogWrite polyfill for ESP32 using the LEDC functions

1 Like

Thanks, I will check it.

the ESP32 has a DAC (Digital to Analog converter) - see esp32-dac-example

1 Like

Update to the latest release (2.0.6) ... or 2.0.5

1 Like

I have installed the analog library and its working , thank a lot guys.

Please mark the thread as solved so that other members that wish to help will not waste their time opening the thread only to find that you no longer need help.

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