Error : LEDC is not initialized Nodemcu ESP32 in wokwi

i am using wokwi for ESP32 simulation and i get error LEDC is not initialized, i am confused which part of my code there is error like that. I attached my code:

#define Balarm  13  
#define PIRsensor 2 
#define LedPin 14
#define pitch  262

void setup() {
  Serial.begin(9600);
  pinMode(PIRsensor, INPUT); // PIR sensor as input  
  pinMode(Balarm, OUTPUT);   // Buzzer alaram as output
  pinMode(LedPin, OUTPUT);
  digitalWrite (Balarm, LOW);// Initially buzzer off
}

void loop(){
  int state = digitalRead(PIRsensor); 
  delay(500);                         
    if(state == HIGH){                
      tone(Balarm, pitch);   
      digitalWrite (LedPin , HIGH); 
      delay(1000);  
    }
    else {
      noTone(Balarm);    //No intrusion Buzzer off
      digitalWrite (LedPin , LOW);
    }
}

I also want to ask why the buzzer sound can't be heard, how to make it sound, shouldn't it just use toner(). I also added an image, and I also want to ask if the flow of my image is suitable for making anti-theft alarms,if the sensor is on then the buzzer and led are also lit.I ask for his help to correct the location of the error I made:

here the link projects : sketch.ino - Wokwi Arduino and ESP32 Simulator

If you give a link to your Wokwi project, then we can try it.

As far as I know, it should be fixed since a few months. Before that, libraries were used for 'tone()' and 'noTone()'.
Try adding: ledcAttachPin(Balarm, channel);
I don't know what 'channel' should be, try 0, 1, and so on.
You might also need the function ledcSetup().

here the link : sketch.ino - Wokwi Arduino and ESP32 Simulator

how to use?

It is something, although it might not be want you want:

int Balarm = 13;
int PIRsensor = 2; 
int LedPin = 14;
//#define pitch  262
//const int channel = 262;


void setup() {
  //Serial.begin(9600);
  pinMode(PIRsensor, INPUT); // PIR sensor as input  
  pinMode(Balarm, OUTPUT);   // Buzzer alaram as output
  pinMode(LedPin, OUTPUT);
  //digitalWrite (Balarm, LOW);// Initially buzzer off
}

void loop(){
  int state = digitalRead(PIRsensor); 
  delay(500);                         
    if(state == HIGH){                
      //digitalWrite (Balarm , HIGH);  
      myTone(Balarm);
      digitalWrite (LedPin , HIGH); 
      delay(1000);  
      //digitalWrite (Balarm , LOW);   
      //digitalWrite (LedPin , LOW);
    }
    else {
      //digitalWrite (Balarm , LOW);   
      myNoTone(Balarm);
      digitalWrite (LedPin , LOW);
    }
}

void myTone( int pin)
{
  ledcAttachPin(pin, 0);             // pin, channel
  ledcWriteNote(0, NOTE_F, 4);    // channel, frequency, octave
}

void myNoTone( int pin)
{
  ledcDetachPin(pin);
}

This is not how it should be done, because the tone() and noTone() should have been added to the ESP32 board. I can not find the right explanation though.

thanks a lot, it's worked. I want to ask again how to connect wokwi with a bylink so that it can send a "danger" message when the alarm goes off

Start a new topic for that.
I learned a lot at https://randomnerdtutorials.com/projects-esp32/

I already made it:

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