user67552: 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
  Serial.begin(9600);
  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;
    delay(100);
  }    

  lightON = val;

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

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

  delay(100);

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....
cheers:

user38721: 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.

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

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
         delay(5);
 }`


Hardware. Use following schematic




67552: <p>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)</p>
<p>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 ;-)</p>
<p>Anyway... the code i'm using is this:</p>
<pre><code>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
  Serial.begin(9600);
  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 &amp;&amp; lightON == LOW){

    pushed = 1-pushed;
    delay(100);
  }    

  lightON = val;

      if(pushed == HIGH){
        Serial.println(&quot;Light ON&quot;);
        digitalWrite(relayPin, LOW); 
       
      }else{
        Serial.println(&quot;Light OFF&quot;);
        digitalWrite(relayPin, HIGH);
   
      }     

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

  delay(100);
</code></pre>
<p>The above code is straight up cut and paste... i'm clueless</p>
<p>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....</p>
<p>cheers:</p>
 38721: <p>If you want to decrease the brightness in general, the easiest solution would in deed be to use a high value resistor.</p>
<p>If you want to use PWM - for example to change brightness programmatically - you just need to replace the calls to <code>digitalWrite(HIGH|LOW)</code> with calls to <code>analogWrite(x)</code> (<a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/" rel="nofollow noreferrer">ref</a>), where <code>x</code> represents the duty cycle/brightness. The range of <code>x</code> depends on your microcontroller.</p>
<p>Note, that PWM (which is used by <code>analogWrite()</code>) only works on dedicated pins.
On the other side, there are microcontrollers wich have analog pin with a &quot;true&quot; analog output. On such an analog pin - though it is possible to use <code>analogWrite()</code> - the brightness will not be linear and have an offset, which makes it pretty much useless for dimming an LED.</p>
 20579: <p>There are two methods of dimming a LED : by PWM or by a limiting current resistor.</p>
<ol>
<li><p>PWM code</p>
<pre><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
         delay(5);
 }`
</code></pre>
</li>
<li><p>Hardware. Use following schematic
<a href="https://i.stack.imgur.com/atpIK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/atpIK.png" alt="enter image description here" /></a></p>
</li>
</ol>
