As an exercise to learn rather then the most efficient way to accomplish a goal.... (i'm a hands on hardware guy, but I need to get my Sh!T together and learn some coding)

I would like to make an LED less bright within a project. I have a nano set up to control a relay to control a dc load when a momentary switch is pressed (this is kinda needlessly complicated solely to allow me to use a particular momentary switch I wanted to use for a bunch of not that great reasons ;-)

Anyway... the code i'm using is this:

int pbuttonPin = 2;// connect output to push button
int relayPin = 10;// Connected to relay (LED)

int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status

void setup() {
  // Robojax.com code and video tutorial for push button ON and OFF
  pinMode(pbuttonPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
 digitalWrite(relayPin, HIGH);// keep the load OFF at the begining. If you wanted to be ON, change the HIGH to LOW

void loop() {
// Robojax.com code and video tutorial for push button ON and OFF
  val = digitalRead(pbuttonPin);// read the push button value

  if(val == HIGH && lightON == LOW){

    pushed = 1-pushed;

  lightON = val;

      if(pushed == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); 
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);

// Robojax.com code and video tutorial for push button ON and OFF


The above code is straight up cut and paste... i'm clueless

I have a LED cathode on pin 10, (so i know the relay is on more easily from a distance). however the application is on a boat, and at night it is too bright and distracting. So i want to make the LED dimmer... I know I could add a larger value resistor, but i want to expand my understanding and would like suggestions on other ways PWM maybe....


  • 1
    What's your Arduino? If you can connect the LED to the Arduino (=if the Arduino provides enough power), you can connect it to a PWM pin and use analogWrite(10, brightness); to change the brightness. Of course, you'd have to initialize it in the setup and replace brightness with a number in the range of 0-255. Feb 14, 2021 at 10:26
  • If using the relay is your only option, here is an discussion about it in the Arduino forums. Feb 14, 2021 at 10:28
  • 2
    add a resistor, don't waste resources of the MCU for this
    – Juraj
    Feb 14, 2021 at 10:55
  • 1
    Yeah, use Jurajs method if you want to change it just once to a lower brightness. Feb 14, 2021 at 11:06
  • 1
    You could add a second relay, with a resistor, like Juraj suggested. Connect that in parallel to the cathode of the led. You can then switch between the two relays for two different brightness levels. But I think it would be better, depending on the LED you used, to swap out the relay for a mosfet. Then you can change the brightness using PWM. Add a light sensor, to automatically switch between day, and night mode, to somehow justify using an Arduino in the first place ;-p
    – Gerben
    Feb 14, 2021 at 14:13

3 Answers 3


There are two methods of dimming a LED : by PWM or by a limiting current resistor.

  1. PWM code

     `//Initializing LED Pin
     int led_pin = 10;
     void setup() {
     //Declaring LED pin as output
             pinMode(led_pin, OUTPUT);
     void loop() {
             analogWrite(led_pin, 127); // half intensity
  2. Hardware. Use following schematic enter image description here

  • 2
    This soulution doesn't consider the LED needs more power than the Arduino can provide. Feb 14, 2021 at 10:55
  • not all POTs will like all LEDs. while a fire is a remote chance, losing the POT isn't.
    – dandavis
    Feb 15, 2021 at 18:08
  • Unless you have regulated current (which would be the third option) you need a current limiting resistor in any case. No matter if you're going to dim using pwm afterwards. Your answer is misleading in that point. Also, how are you dimming the led in the second example? Isn't this pwm as well?
    – Sim Son
    Feb 16, 2021 at 17:47

If you want to decrease the brightness in general, the easiest solution would in deed be to use a high value resistor.

If you want to use PWM - for example to change brightness programmatically - you just need to replace the calls to digitalWrite(HIGH|LOW) with calls to analogWrite(x) (ref), where x represents the duty cycle/brightness. The range of x depends on your microcontroller.

Note, that PWM (which is used by analogWrite()) only works on dedicated pins. On the other side, there are microcontrollers wich have analog pin with a "true" analog output. On such an analog pin - though it is possible to use analogWrite() - the brightness will not be linear and have an offset, which makes it pretty much useless for dimming an LED.

  • "...the brightness will not be linear and have an offset." I don't know what you mean when you say the brightness will have an offset. It won't. It just won't be linear is all, but all PWM values > 0 will indeed produce light. There isn't a "dead band" in PWM as there is with potentiometers and joysticks, for instance. Mar 22, 2021 at 7:56
  • That point referes to true analog outputs - I'll make this more clear. If I remember right, those analog pins are also used with analogWrite() on the respective arduinos, but show a much different behaviour than a PWM pin.
    – Sim Son
    Mar 23, 2021 at 7:18
  • Oh, I see what you're saying. PWMing an output voltage which is > Vthreshold when HIGH always produces light on an LED, but if it's a true analog output there will be a deadband, or "offset", equal to the threshold voltage before the LED starts producing light. This is one of the benefits of PWM pseudo-analog output for driving LEDs over using true analog output for driving LEDs. PWM, in this case, is probably actually better. Mar 23, 2021 at 7:22
  • It was a bit unclear what I meant, I guess.
    – Sim Son
    Mar 23, 2021 at 7:30

Follow this tutorial. It doesn't get any better than this. https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.