How can I set brightness on my LED

How can I set brightness on my LED?

My led is very strong brightness, like to set the brightness in the code :slight_smile:

/*


 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 1;     // the number of the pushbutton pin
const int buttonPin2 = 2;     // the number of the pushbutton pin
const int buttonPin3 = 3;     // the number of the pushbutton pin
const int buttonPin4 = 4;     // the number of the pushbutton pin

const int ledPin1 =  5;      // the number of the LED pin  Starter når arduino starter
const int ledPin2 =  6;      // the number of the LED pin
const int ledPin3 =  7;      // the number of the LED pin
const int ledPin4 =  8;      // the number of the LED pin
const int ledPin5 =  9;      // the number of the LED pin

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)
// Variables will change:
int ledState = LOW;             // ledState used to set the LED

#define RELAY1  10 
#define RELAY2  11
#define RELAY3  12
#define RELAY4  13

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttonState3 = 0;         // variable for reading the pushbutton status
int buttonState4 = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
// initialize the relay
   pinMode(RELAY1, OUTPUT);  
   pinMode(RELAY2, OUTPUT);  
   pinMode(RELAY3, OUTPUT);  
   pinMode(RELAY4, OUTPUT);  
   
//oppstart PowerOnLed
    digitalWrite(ledPin1, HIGH);
}

void loop() 

{
  

  //blink til led
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn blink LED on:
    digitalWrite(ledPin2, HIGH);
    delay(500);
    digitalWrite(ledPin2, LOW);
    delay(500);
  } else {
    // turn blink LED off:
    digitalWrite(ledPin2, LOW);
   }
   
  //button1
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(RELAY1,0);           // Turns ON Relays 1
  } else {
    // turn LED off:
    digitalWrite(RELAY1,1);          // Turns Relay Off
  }
  
  //button2
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {
     digitalWrite(RELAY2,0);           // Turns ON Relays 1
    // turn LED on:
    digitalWrite(ledPin3, HIGH);
  } else {
     digitalWrite(RELAY2,1);          // Turns Relay Off
    // turn LED off:
    digitalWrite(ledPin3, LOW);
 } 


  //button3
  // read the state of the pushbutton value:
  buttonState3 = digitalRead(buttonPin3);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState3 == HIGH) {
     digitalWrite(RELAY3,0);           // Turns ON Relays 1
    // turn LED on:
    digitalWrite(ledPin4, HIGH);
  } else {
     digitalWrite(RELAY3,1);          // Turns Relay Off
    // turn LED off:
    digitalWrite(ledPin4, LOW);
 }
    
   
  //button4
  // read the state of the pushbutton value:
  buttonState4 = digitalRead(buttonPin4);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState4 == HIGH) {
     digitalWrite(RELAY4,0);           // Turns ON Relays 1
    // turn LED on:
    digitalWrite(ledPin5, HIGH);
  } else {
     digitalWrite(RELAY4,1);          // Turns Relay Off
    // turn LED off:
    digitalWrite(ledPin5, LOW);


  }
}

If it's simply too bright, that's a hardware problem. Simply increase the value of the current limiting resistor(s) until you're happy with the result.

You do have current limiting resistors on all of your LEDs, right?

If you want software-adjustable brightness use [u]analogWrite()[/u]. Make sure you're connected to pins that support [u]PWM[/u].

See the [u]Fade Example[/u].

Is work on analogWrite(). :slight_smile: thx. but do i need to connect resistors? and way?

Resistor needs to be in series with the LED to prevent damage to the Arduino output pins - I use 470 ohm ones since I don't need bright for test and learning - on actual projects I adjust the value to get the proper brightness or limit the current so as to not damage anything - depends on the LED -

I am assuming you are using the standard 5mm ones that come with most starter packages

I use 5 of this:

https://www.ebay.com/itm/2x3x4mm-Water-Clear-Diffused-8-Colours-Rectangle-Square-LED-Diodes-/272632454536?var=&hash=item3f7a291d88:m:mmdPtKA-dQKZjzrZg_srNrg

What type og trans do i need to put on this?

What type og trans do i need to put on this?

What is an "og trans". As you have been told you NEED a resistor in series with EVERY LED.

Here is why http://www.thebox.myzen.co.uk/Tutorial/LEDs.html

og trans >>>—-> of transistor
Sloppiness :wink:

My phone make his own text

What type of resistors do I need ? 220 ?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Like this :slight_smile:

If those LEDs do not have a series resistor ~220Ω you will blow your Arduino.

.

Hi,
Thanks for the picture, fitzy image unfortunately is not a circuit diagram.
You need 220R resistor in series with each of your LEDs to limit the maximum current through them, this will protect the LED and the controllers output as it can only supply up to 40mA.

Tom... :slight_smile:

thx, then I going to ordr it from ebay and put on :smiley: