#define POT_PIN A0
#define LED_PIN D6
int potValue = 0;
int brightness = 0;
void setup()
{
pinMode(POT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
analogReadResolution(12);
analogWriteResolution(8);
Serial.begin(115200);
}
void loop()
{
// Read potentiometer value
potValue = analogRead(POT_PIN);
// Convert ADC value into PWM brightness
brightness = map(potValue, 0, 4095, 0, 255);
// Control LED brightness
analogWrite(LED_PIN, brightness);
// Display values in Serial Monitor
Serial.print("Potentiometer: ");
Serial.print(potValue);
Serial.print(" Brightness: ");
Serial.println(brightness);
delay(100);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6