beginners in coding

Hi,
I am new to Arduino I am trying some basic codes but they are not working.

with the code below I am trying to turn on an led with a switch. can someone tell me why it is not working?

int led = 2;
int sw = 5;

void setup (){

pinMode(led, OUTPUT);
pinMode(sw, INPUT);
}

void loop() {

if (sw == HIGH) {
digitalWrite(led , HIGH);
} else{
digitalWrite(led , LOW);
}
}

Thanks in advance

sw has the value 5.
HIGH has the value 1.

They are never going to be equal.

You forgot a digitalRead.

Also, switches are normally inputs, and LEDs are outputs.

Please remember to use code tags when posting code

thanks for your answer, this time I decided to use the pin number instead of the sw and led and then changed the input and output problem.

void setup (){

pinMode(2 , INPUT); //switch
pinMode(5, OUTPUT); //led
}

void loop() {

if (digitalRead(2 , HIGH) ){
digitalWrite(5 , HIGH);
} else{
digitalWrite(5 , LOW);
}

this is still not working.

2 has the value 2.
HIGH still has the value 1.

1 will never equal 2.

You need to use digitalRead.

This has already been mentioned.

Please remember to use code tags when posting code.
This has also already been mentioned.

Try it like this (not tested)

byte switchPin = 2;
byte ledPin = 5;

void setup (){
    pinMode(switchPin, INPUT);   //switch
    pinMode(ledPin, OUTPUT);  //led 
}

void loop() {
    byte switchState = digitalRead(switchPin);
    if (switchState == HIGH) {
        digitalWrite(ledPin, HIGH);
    } else {
        digitalWrite(ledPin, LOW);
    }
}

Giving things meaningful names makes it much easier to see what is happening

Also see the advantage of using the code button </> when posting code

...R

Edit ... changed bool to byte as mentioned in Reply #5 - sorry for any confusion

Similarly uncompiled/untested

const byte switchPin = 2;
const byte ledPin = 5;

void setup ()
{
    pinMode(switchPin, INPUT);  
    pinMode(ledPin, OUTPUT); 
}

void loop() 
{
    digitalWrite(ledPin, digitalRead(switchPin));
}

(Note: digitalRead does not return a bool.)

79461766 - why do you keep editing the code in your posts after you have been given advice on it ? Please do not do that as it makes a nonsense of the thread for anyone reading it later.

if (digitalRead(2 , HIGH) ){

this is still not working compiling.

Fixed that for you.

And yes, don't post-edit posts

Except, after reading the instructions, to go back and put your code snippets into "code" tags. :grinning: