Seeking LED control sample code

I’ve seen projects online that use code for breathing lights, and I want to make the LEDs brighter and dimmer depending on the number increasing and decreasing.
I use Arduino Uno and F5LED, and a touch display that can adjust the size of the numbers.
I see many examples that require the use of analogwrite( ), and PWM, does anyone have a similar simple example?

Here is a simple example to use PWM in Arduino.

int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 3;   // potentiometer connected to analog pin 3
int value = 0;         // variable to store the read value

void setup() {
  pinMode(ledPin, OUTPUT);  // sets the pin as output
}

void loop() {
  value = analogRead(analogPin);  // read the input pin
  analogWrite(ledPin, value / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}

Also, read the link below.

https://www.arduino.cc/en/Tutorial/Foundations/PWM
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

3 Likes

U sure it will work ?. Coz potentiometer is an analog input device and I got it working when I connected it to one of A0-A5 pins.

2 Likes

Yes, it will work. You can use analogWrite() to implement PWM. In the above code I have used analogRead() and analogWrite(). Both functions are different.

1 Like

Yeah that is fine, I was asking of connecting potentiometer to a pen pin got it working? If it works thn great

You’ve really helped me, I was writing the code here before using analogwrite() directly to read the values, completely forgetting that it can only read values from 0 to 255, while the values I need it to read are from 0 to 1024.

1 Like

You can read values from 0 to 1023 and write values from 0 to 255. The analogRead() function is configured internally to have a 10-bit resolution whereas the analogWrite() has an 8-bit resolution. If you are implementing a PWM use analogWrite() else if you are implementing ADC use analogRead().