LED dim

I am doing street lighting project where i am using led’s and ir sensors… When ir sensor detect any object it should bright the led else led should have dim light. I can do the led on or of using ir sensor but i am not able to dim the led. Need urgent help

Show us your urgent Sketch.

Use the </> button to post your code.

int LED=13; int isObstaclePin=7; int isObstacle=HIGH;

void setup() { pinMode(LED,OUTPUT); pinMode(isObstaclePin,INPUT); Serial.begin(9600); }

void loop() { isObstacle = digitalRead(isObstaclePin); if(isObstacle == HIGH) { Serial.println("OBSTACLE!!, OBSTACLE!!"); digitalWrite(LED, HIGH); } else { Serial.println("clear"); digitalWrite(LED, LOW); } delay(200); }

How is the LED wired?

Is this an UNO?

Yes it is.

How is the LED wired?

Do you know how analogWrite works?

Sorry i am new to this area. So i have less knowledge for this… For LED i wired it on digital i/o pin13

Which way is your LED wired, D6 or D5 ignore the pin they are connected to.

2017-08-05_20-33-24.jpg

I think it is S1

ashishpandey: I think it is S1

Sorry, I meant LED.

Is the LED wired like D5 or D6 ?

Yes D5

Have you read how analogWrite works?

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

Yes i have just checked.

Use pin NINE (9) for the LED.

int LED           = 9;
int isObstaclePin = 7;
int isObstacle    = HIGH;

void setup()
{
  pinMode(isObstaclePin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop()
{
  isObstacle = digitalRead(isObstaclePin);

  if (isObstacle == HIGH) // use LOW if the dimming is reversed
  {
    Serial.println("OBSTACLE!!, OBSTACLE!!");
    analogWrite(LED, 215);
  }
  
  else
  {
    Serial.println("clear");
    analogWrite(LED, 0);
  }

}

Oh great, Led got dim. Can you please tell how can i set dim value as for current led go too much dimmed.

"how can i set dim value as for current led go too much dimmed." Not sure what you are asking. You should reread the reference on analogWrite.

analogWrite(LED, 215);

Change to:

analogWrite(LED, 250);

Yeah i understood the dim concept. Now what i need is when my ir sensor sense any object, led should get full bright and when there is no object sensed by ir sensor led should be dimmed. Any idea.

Oh thanks you solved my problem. Thank you so much. This code worked for me:

int LED=9; int isObstaclePin=7; int isObstacle=HIGH;

void setup() { pinMode(isObstaclePin,INPUT_PULLUP); Serial.begin(9600); }

void loop() { isObstacle = digitalRead(isObstaclePin); if(isObstacle == HIGH) { Serial.println("OBSTACLE!!, OBSTACLE!!"); digitalWrite(LED, HIGH); } else { Serial.println("clear"); analogWrite(LED, 10); } }

If your IR led produces a simple HIGH or LOW to the Arduino, then it can be used in place of a simple switch.

If IR LED produces an analog output, then you will need to send that to an analog input on the Arduino (0-5 volts only).