internal pullup resistor?

Hello, i would like to ask about the usage of the internal pullup resistor; i've got my project (controlling a motor driven cinema screen via a two-possition switch) running, but the switch gets triggered when something else on the network is turned on/off. I've read that i should use a pulldown resistor, or enable the internal pullup resistor so this doesn't happen, so I've used the internal one to avoid this happening. but somehow the switch is still affected by the network noise.... Please coould somebody have a look at my code and check if i've used it correctly ? thanks a lot

/*
  - 2nd Arduino Sketch
  
  - to control a tube motor operating a projector screen
  - rolls down if switch is pressed
  - rolls up if switch is unpressed
*/

#define POWER   2                       //  PIN 2 operates relay 1
#define DIRECTION  3                    //  PIN 3 operates relay 2  
#define SWITCH  4                       //  PIN 4 is the switch
#define KONTROLKA  13                   // control led 

boolean remember_last = false;
boolean SWITCHstatus = false;

void setup()
{
  pinMode(POWER,OUTPUT);                //  relay 1
  pinMode(DIRECTION,OUTPUT);            //  relay 2
  pinMode(SWITCH,INPUT);
  pinMode(KONTROLKA, OUTPUT);           //   control led
  digitalWrite(POWER, HIGH);            //  main power OFF on startup
  digitalWrite(DIRECTION, HIGH);        // initial direction up
  digitalWrite(SWITCH, HIGH);           //  USE INTERNAL PULLUP RESISTOR


}

void loop()
{
    SWITCHstatus = digitalRead(SWITCH);

    
    if (SWITCHstatus==true && remember_last==false)                  //  SWITCH POSSITION '1'             // TO BE DONE??

    {
       digitalWrite(KONTROLKA, HIGH);  //  begin
       digitalWrite(DIRECTION,LOW);    //  down
       digitalWrite(POWER,LOW);        //  turns power ON
       delay(28000);                   //  rolls down for 28s
       digitalWrite(POWER,HIGH);       //  turns power OFF    
       digitalWrite(DIRECTION,HIGH);   //  sets to up
       remember_last = !remember_last; //  inverts last value
       digitalWrite(KONTROLKA,LOW);    //  confirms with led blinking
       delay(500);                  
       digitalWrite(KONTROLKA,HIGH);    
       delay(500);
       digitalWrite(KONTROLKA,LOW);    
       delay(500);                   
       digitalWrite(KONTROLKA,HIGH);    
       delay(5000);                   
       digitalWrite(KONTROLKA,LOW);    //  ends
    }

    else if(SWITCHstatus==false && remember_last==true)             //  SWITCH POSSITION '2'
    
    {
       digitalWrite(KONTROLKA, HIGH);  // begins     
       digitalWrite(DIRECTION,HIGH);   //  up
       digitalWrite(POWER,LOW);        //  turns power ON
       delay(30000);                   //  rolls op for 30s 
       digitalWrite(POWER,HIGH);       //  turns power OFF
       remember_last = !remember_last; //  inverts last value
       digitalWrite(KONTROLKA,LOW);    //  confirms with led blinking
       delay(500);                  
       digitalWrite(KONTROLKA,HIGH);    
       delay(500);
       digitalWrite(KONTROLKA,LOW);    
       delay(500);                   
       digitalWrite(KONTROLKA,HIGH);    
       delay(5000);                   
       digitalWrite(KONTROLKA,LOW);    //  ends

}   

}

You seem to have the switch input pullup enabled properly. The internal pullup is not very strong so sometimes an external pullup will help to overcome noise on long lines or noisy environment. Try a 1K ohm pullup and see if that helps.

so I've used the internal one to avoid this happening. but somehow the switch is still affected by the network noise.

Capacitors suppress noise, not resistors.

PaulS:

so I've used the internal one to avoid this happening. but somehow the switch is still affected by the network noise.

Capacitors suppress noise, not resistors.

Thank you for the info; but now i don't understand I guess... I took this page as reference: http://arduino.cc/en/Tutorial/DigitalPins where they explain that:

This also means however, that input pins with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin.

Pullup Resistors

Often it is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value.

so what would you suggest me to add to the code/circuit please?

Thanks for your help!

Moderator edit: Changed code tags to quote tags, to make the quote more readable. (Nick Gammon)

groundfungus: You seem to have the switch input pullup enabled properly. The internal pullup is not very strong so sometimes an external pullup will help to overcome noise on long lines or noisy environment. Try a 1K ohm pullup and see if that helps.

Thanks. so how would you measure 'strong' please? you say 1k, i've read that the arduino as a 20k internal pullup resistor - so actually the lower the number, the stronger the resistor ?? sorry I'm not very familiar with electronics...

it relates to 'impedance', and is not a simple subject. Its one that warrants its own study actually.

In simple terms; By using a smaller resistor, you increase the needed current to change the state of that pin, effectively making it more immune to noise which is usually lower current.

The smaller resistor is providing more current for pre-driving it (its normal state), thus also needing more 'push' than noise provides to change that pins state to 'active'. depending on if you use LOW or HIGH logic - the internal pull ups force us to use LOW logic (if grounded then its 'active') But with externals you could use HIGH active logic, and your sketch would need to be changed.

Hope that helped.

The noise source has a certain impedance, the pull up/down resistor forms a voltage divider with the noise source's impedance, the smaller the resistor the lower the noise voltage, so long as its below the noise source impedance (which it usually is for capacitively coupled noise, but usually not for inductively coupled noise)

There are other approaches to noise reduction, and it depends on the nature of the noise which method(s) work best in a given situation.

PaulS:

so I've used the internal one to avoid this happening. but somehow the switch is still affected by the network noise.

Capacitors suppress noise, not resistors.

Yes and no. In the case of a pullup, choosing the right resistor value can make all the difference in the world for capacitively coupled noise. For an illustration of this, wire a resistor beween Vcc and a digital input pin, then a switch betwee that same pin and ground. First try a 5K resitor, then try a 1 megohm resistor. Touch the pin for each test.

karatedebili: i've read that the arduino as a 20k internal pullup resistor - so actually the lower the number, the stronger the resistor ??

The specs say 20 k? to 50 k?. In other words, not a very large current would pass through it. I wouldn't use the word "stronger". A smaller resistor might possibly help in this case. A 1 k? resistor would give you 5 mA of current at 5V (ie. 5/1000).

Thank you all, very helpfull information for me. I'll buy a few resistors and experiment with the circuit to see what works.