const int ledPin = 2; // PWM-capable pin for LED
const int ldrPin = 35; // Analog input pin for LDR
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Read the analog value from the LDR
int ldrValue = analogRead(ldrPin);
// Map the LDR value to the range of LED brightness (0-255)
int brightness = map(ldrValue, 0, 4095, 0, 255);
// Use analogWrite to control the LED brightness
analogWrite(ledPin, brightness);
// Print LDR value and adjusted brightness to Serial Monitor
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(", Adjusted Brightness: ");
Serial.println(brightness);
delay(100); // Adjust the delay based on your requirements
}
Output:
Compilation error: 'analogWrite' was not declared in this scope
The above program that the instruction analogWrite() brings up the error
'analogWrite' was not declared in this scope
when I click on compile.
So I loaded the analoginoutserial sketch from the analog section of examples and get the same error?
Any idea what I am doing wrong?
I am using arduino IDE 2.2.1
With the ESP32 you should use ledcWrite() instead of analogWrite().
Here is a link to a tutorial
ESP32 PWM Tutorial & Examples (AnalogWrite) - Arduino
1 Like
Hello @mohit_06,
Welcome to the forum! We're glad to have you here. I noticed that you're sharing code in your post. To make it more readable for everyone, please consider using the <code/>
format when sharing code snippets. You can learn more about best practices for posting on the forum here.
Regarding your issue with the analogWrite()
function, it should work without any problems. I recommend trying the following example to test the function:
/*
Fading
This example shows how to fade an LED using the analogWrite() function.
The circuit:
- LED attached from digital pin 9 to ground through 220 ohm resistor.
created 1 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fading
*/
int ledPin = 13; // LED connected to digital pin 9
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Please try this example and let us know if you encounter any issues. Also, ensure that you're using the correct pin for the LED. In the example above, we're using the builtin LED pin (pin 13) for testing.
Best regards,
Jorge
@mohit_06 Additionally, please confirm if you are using an Arduino Nano ESP32 or another ESP32 board. If you're using an Arduino Nano ESP32, please let me know which version of the ESP32 core you have installed in your Arduino IDE?