Im trying to turn an LED on with an on off on switch, but the serial monitor is being weird

I followed a guide on it and just copied the code, to me I see it as fine but I'm still new ish to Arduino.

The serial monitor read:
103
867
206
771
337
643
441
533
577
406
695
297
803

It read this no matter what position the switch was in. Its a 3 prong on/off/on spdt switch.

int switchpin = A4;
int redLED = 5;
void setup() {
  
  pinMode(switchpin, INPUT);
  pinMode(redLED, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(A4);
  Serial.println(sensorValue);
  if (analogRead(A4) > 100) {
  digitalWrite(redLED, HIGH);
}
  else digitalWrite(redLED, LOW);
  delay(10);
}

I will also put a photo of the breadboard in the replies, but be wear the wiring is extremely messy because I only have long wires at this moment.

Can someone help me figure out what's wrong because i have no clue.

The schematic for what i used but i used a different switch, the switch i used was

Just noticed your code doesn't use the switch at all…

Check your wiring. This looks like random values coming from a completely unconnected analog input.

Confirm you are using all the components in the circuit you found.

Try a jumper wire where the switch contacts are supposed to be instead of the switch.

Confirm you are using the switch correctly, ohmmeter or a simple circuit.

a7

Wait im confused, wdym by its not using the switch at all? Sorry im still new ish to arduino

For a switch like the one you are using you are better to use digitalRead rather than analogRead. It will return either HIGH or LOW.

You have a name for the analog input.

But you don't use the name

  int sensorValue = analogRead(A4);

so I got confused. Usually once you have a name for a pin, it is referred to by that name, that's the hole point of naming it.

Works the same, reads funny.

a7

Yeah, the site uses analofRead() for some reason.

Tropical Engineer, maybe the heat has gotten to them.

a7

I changed the code to

int switchpin = A4;
int redLED = 5;
void setup() {
  
  pinMode(switchpin, INPUT);
  pinMode(redLED, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  int sensorValue = digitalRead(A4);
  Serial.println(sensorValue);
  if (digitalRead(A4) > 100) {
  digitalWrite(redLED, HIGH);
}
  else digitalWrite(redLED, LOW);
  delay(100);
}

All i did was change the analogReads to digitalReads and now on the serial monitor theres only 1's and 0's showing up, but they still seem random and non reactive to the switch.

I changed the code to

int switchpin = A4;
int redLED = 5;
void setup() {
  
  pinMode(switchpin, INPUT);
  pinMode(redLED, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  int sensorValue = digitalRead(A4);
  Serial.println(sensorValue);
  if (digitalRead(A4) > 100) {
  digitalWrite(redLED, HIGH);
}
  else digitalWrite(redLED, LOW);
  delay(100);
}

All i did was change the analogReads to digitalReads and now on the serial monitor theres only 1's and 0's showing up, but they still seem random and non reactive to the switch. I am very confused lmao

digitalRead(A4) will give you a HIGH (1) or LOW (0) :thinking:

This is not relevant if using digitalRead. Just use...
if (digitalRead(switchPin) == HIGH)

I agree with a777... you likely have a wiring issue if you are getting random results.

BTW... the code on that site looks pretty rubbish... I would be reluctant to use their examples.

FYI

Or

if (sensorValue == HIGH)…

since it was digitalRead() earlier.

sensorValue is a misleading name, probably leftover form when analogRead() was being used to actually read some analog sensor value...

a7

It must be a wiring issue then is my guess, but everything seems right and all my parts are new.

Wire your switches as S3 above, look for a LOW when the switch is closed.

Uhh sorry wdym by S3?

Wire switches similar to the way S3 is wired in the schematic.

i.e. one side of your switch goes to the Arduino input pin, the other side of your switch goes to Arduino GND (0 volts).

In setup( ) add a line similar to below for each hardware switch you have:


const byte mySwitch   = 3;

. . .

void setup( )
{
. . .

pinMode(mySwitch, INPUT_PULLUP);  //turn on internal pull-up resistor on this input pin

. . .

} //END of setup( )

void loop( )
{
  . . .

// is this switch pushed ?
if (digitalRead(mySwitch) == LOW)
{
   //do something
}

. . .

} //END of loop( )

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