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:
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().
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.