Arduino digital Read affected by house light switch

Hi everyone

I'm Having the following Problem:
I built a IR-Sender for our ceiling fan. The IR-Signal gets triggered by the push of a switch. the switch is pulled up by the Arduino (Arduino Nano).
When i turn on the lights in our bathroom (adjacent to the bedroom, where the ceiling fan is installed), the IR-Signal gets triggered as well. It does not happen all the time, but it got worse over last few weeks.

This is my code:

#include <IRremote.h>
#define irRecvPin 2
#define irLedPin 3 // PWM-Pin for IR
#define lightSwitch 5
#define fanPoti A0


byte fanPotiState = 0;
byte lastPotiState = 0;
byte lightSwitchState = 1;
byte lastLightSwitchState = 1;

void setup() {
  pinMode(irLedPin, OUTPUT);
  pinMode(lightSwitch, INPUT_PULLUP);
  pinMode(fanPoti, INPUT);
  //Serial.begin(9600);
}

void loop() {
  //Serial.println(map(analogRead(fanPoti), 0, 1023, 4, 0));
  lightSwitchState = digitalRead(lightSwitch);
  if (lightSwitchState != lastLightSwitchState) {
    if (lightSwitchState == LOW) {
      delay(50);
      sendCodeLight();
    }
   delay(200);
  }
 lastLightSwitchState = lightSwitchState;

fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
if (fanPotiState != lastPotiState) {
  if ((map(analogRead(fanPoti), 0, 1023, 4, 0) == 0)||(map(analogRead(fanPoti), 0, 1023, 4, 0) == 1)) {
    fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
    delay(50);
    sendCodeFan0();
    delay(200);
    sendCodeFan0();
  //Serial.println("senden");
  }
  else if (map(analogRead(fanPoti), 0, 1023, 4, 0) == 2) {
    fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
    delay(50);
    sendCodeFan1();
    delay(200);
    sendCodeFan1();
  //Serial.println("senden");
  }
  else if (map(analogRead(fanPoti), 0, 1023, 4, 0) == 3) {
    fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
    sendCodeFan2();
    delay(200);
    sendCodeFan2();
  //Serial.println("senden");
  }
  else if (map(analogRead(fanPoti), 0, 1023, 4, 0) == 4) {
    fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
    delay(50);
    sendCodeFan3();
    delay(200);
    sendCodeFan3();
  //Serial.println("senden");
    
  }
}
lastPotiState = fanPotiState;
}

// IR-Codes definieren
void sendCodeLight() {
  byte irCode1[] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}  ;
  byte irCode2[] = {1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}  ;
  sendCodeLight(irCode1,irCode2);
}

void sendCodeFan0() {
  byte irCode[] = {1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0};
  sendCode(irCode);
}
void sendCodeFan3() {
  byte irCode[] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  sendCode(irCode);
}
void sendCodeFan2() {
  byte irCode[] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
  sendCode(irCode);
}
void sendCodeFan1() {
  byte irCode[] = {1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1};
  sendCode(irCode);
}

// IR-Code senden
void sendCode(byte irCode[]) {
  for (byte r = 0; r < 3; r++) {
    for (byte i = 0; i < 12; i++) {
      sendCodeStep(irCode[i]);
    }
    delayMicroseconds(6620);
  }
}

void sendCodeLight(byte irCode1[], byte irCode2[]) {
    for (byte i = 0; i < 12; i++) {
      sendCodeStep(irCode1[i]);
    }
    delayMicroseconds(6620);
    
    for (byte i = 0; i < 12; i++) {
      sendCodeStep(irCode2[i]);
    }
    delayMicroseconds(6620);
    
    for (byte i = 0; i < 12; i++) {
      sendCodeStep(irCode2[i]);
    }
    delayMicroseconds(6620);
    
    for (byte i = 0; i < 12; i++) {
      sendCodeStep(irCode2[i]);
    }
    delayMicroseconds(6620);
  
}

// aktuelle Stelle vom IR-Code senden
void sendCodeStep(byte actualStep) {
  if (actualStep == 0) {
    sendPulse(360);
    delayMicroseconds(1300);
  } else {
    sendPulse(1200);
    delayMicroseconds(450);
  }
}

// Puls mit ca. 38 kHz senden
void sendPulse(int pulseLength) {
  cli();
  while (pulseLength > 0) {
    digitalWrite(irLedPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(irLedPin, LOW);
    delayMicroseconds(10);
    pulseLength -= 26;
  }
  sei();
}

Has anyone an idea why this is happening?
Why do our bathroom lights affect the Arduinos digitalRead?

Thank You for any Inputs!

Have a great day :slight_smile:

I would think your sender is picking up electrical noise from the house wiring. It would help to see photos of what you have built.

Try a 0u1 capacitor and an external 10k pull up resistor on the input that the switch is wired to.

Hi,
If you have the button wire between the Nano digital input and 5V.
Have you go a pull down resisitor, 10K, between the digital input and gnd.
You cannot leave an input open and expect it to be a LOW input.

Thanks.. Tom... :slight_smile:

How are you powering the Arduino nano? are you using a charger connected to the main board?

Thanks for your inputs.
I'll post pictures of the wiring as soon as possible (hopefully tomorrow).

@PerryBebbington: Did you mean to add an external pullup resistor and capacitor like this (Except that i would use a 0.1uF capacitor)?

@TomGeorge: I think I understand your idea. With a pulp input, 5 volts are constantly sent through the switch to the input pin. In the event of a rapid drop in power (as can happen in older houses), the Arduino may understand this rapid drop as a LOW signal. This can't happen with your suggestion because the Arduino is waiting for a HIGH signal, correct? At least this would make sense to me...

@yasinzaii: I use a 5 V, 1.5 A power supply.

Did you mean to add an external pullup resistor and capacitor like this (Except that i would use a 0.1uF capacitor)?

Yes and no. Yes I meant an external pull up resistor, but not like that because you have it as a pull down resistor. Swap the resistor and capacitor over.

Thank you for your suggestions. I only had to add an external pull up resistor as Perry mentioned. Works like a charm.
Now we can sleep again without having to fear waking up each other in the morning :slight_smile:
Thank You!

blacklotus1990:
Thank you for your suggestions. I only had to add an external pull up resistor as Perry mentioned. Works like a charm.

Could you post the circuit with the resistor and capacitor, I'd like to see how you connected that. Nothing fancy, just written out as in Pin Reistor Capacitor Ground Thank you.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.