I have a circuit with a blue led, 470ohm resistor and one IR led. I want the led to be switched off when the IR led has a digitalRead value of LOW(i move my hand over it). So I wrote the code:-

void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT); // this is the blue led
pinMode(10, INPUT);  // this is the IR sensor's O/P 


// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // first I give HIGH voltage to the blue LED

if(digitalRead(10) == LOW){  // when the voltage of the IR sensor decreases
digitalWrite(13, LOW); // the blue LED should switch off or have 0 Voltage


But the blue LED just dims and doesn't switch off. Thanks in advance

  • What is the ref of the IR LED you are using? – jfpoilpret Nov 23 '14 at 13:30

Every time through your loop, the LED is being set to HIGH voltage. This may or may not be followed by setting it to LOW voltage, but since the loop runs again immediately, it's immediately set back to high voltage. This happens so fast that the LED essentially "feels" half voltage and it will dim. Your code needs to be more like this...

void loop() {
  if(digitalRead(10) == LOW) {  
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);

OR... even like this...

void loop() {
  digitalWrite(13, digitalRead(10));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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