how to connect arduino rocker switch?

Hi,
I'm trying to connect a rocker switch to my code and am having trouble. My code function is a temperature sensor that triggers a buzzer when a certain temperature is reached. I would like my rocker switch to be able to turn this function off/on.

I have tried multiple times and really don't know if i've gone wrong in my circuit or code (both attached). Would anyone please be able to tell me what the error is and how i can fix it?

Thanks, Tegan :smiley:

float temp; 
int buzzerPin = 8;
int outPin = A0;
int switchPin = 3;


int state= HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

void setup() 
{
pinMode(buzzerPin,OUTPUT);//Initialise the buzzerPin as an output 
pinMode(A0,INPUT);
pinMode (switchPin, INPUT);
Serial.begin (9600) ;  // put your setup code here, to run once:


}

void loop() 
{
 
 reading = digitalRead(buzzerPin);
  
if (reading == HIGH && previous == LOW && millis () - time > debounce) {
  if (state == HIGH)
    state = LOW;
   else
    state = HIGH;

   time= millis ();
    
}

{
 temp = analogRead (A0) ;
 //read the value from the sensor
 temp = temp * 0.048828125 ; 
 Serial.print ( "TEMPERATURE: ") ; //prints temperature in serial monitor 
 Serial.print (temp) ;
 Serial.print ("*C") ;
 Serial.println () ;
 delay (1000) ;     // put your main code here, to run repeatedly:

if ((temp >= 23.0) && (switchPin == HIGH)) { 
  digitalWrite(buzzerPin, HIGH);
  tone(buzzerPin, 1500, 1000); delay(150);
}

digitalWrite(buzzerPin, state);

previous = reading;
  
 
}

}

Components used:
Rocker Switch: https://www.jaycar.com.au/spst-mini-rocker-switch/p/SK0984
Temperature sensor: https://www.jaycar.com.au/temperature-sensor-module-arduino-compatible/p/XC4494
Buzzer: https://www.jaycar.com.au/4khz-audio-transducer/p/AB3442
Arduino board UNO
10K resistors

Hi,
Have you got your switch wired from input to gnd?
If so then you need a 10K resistor from input to 5V, to pull the input up to 5V when the switch is open.

Have you got your switch wired from input to 5V?
If so then you need a 10K resistor from input to gnd, to pull the input down to gnd when the switch is open.

Do you have a DMM to measure resistance and voltages?
Ops image;


It looks like you have the rocker switch correctly wired.

Thanks.. Tom.. :slight_smile:

Always:
Show us a good schematic of your circuit.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

Delta_G:
I'm starting off confused.

We have buzzerPin, which sounds like where you'd put the buzzer.

And it's an output:

pinMode(buzzerPin,OUTPUT);

That's good,

And then:

reading = digitalRead(buzzerPin);

What the hell, now we're reading the buzzer? That makes no sense.

if ((temp >= 23.0) && (switchPin == HIGH)) { 

digitalWrite(buzzerPin, HIGH);
  tone(buzzerPin, 1500, 1000); delay(150);
}




Now we're going to digitalWrite AND tone to that same pin, you know just in case it could be either style of buzzer. 


So what have you got there on that buzzer pin?

Hi Delta,
I see the issues here, is this affecting the switch and if so how would i go about fixing this?
Thanks Tegan :smiley:

If you set the buzzerPin to state on exit of loop(), and read it at the begin of loop() again, reading and state will always have the same value. Once state is set to LOW, it never can be set to HIGH again. That does not make sense, so please explain the intended purpose of state.

Delta_G:
Is the switch connected to buzzerPin? Or to switchPin? Which pin do you have the switch connected to? I'm just taking a guess that the buzzer is on buzzerPin and the switch is on switchPin. If I'm wrong then let me know.

So why are you reading buzzerPin to get the state of the switch? Does that sound right? To get the state of the switch check the pin the buzzer is on? I'm just taking a stab in the dark but to get the state of the switch it seems like we might want to read switchPin instead.

Hi Delta,
Thanks for the help!
This is what i believe you meant:

float temp; 
int buzzerPin = 8;
int outPin = A0;
int switchPin = 3;


int state= HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

void setup() 
{
pinMode(buzzerPin,OUTPUT);//Initialise the buzzerPin as an output 
pinMode(A0,INPUT);
pinMode (switchPin, INPUT);
Serial.begin (9600) ;  // put your setup code here, to run once:


}

void loop() 
{
 
 reading = digitalRead(switchPin);
  
if (reading == HIGH && previous == LOW && millis () - time > debounce) {
  if (state == HIGH)
    state = LOW;
   else
    state = HIGH;

   time= millis ();
    
}

{
 temp = analogRead (A0) ;
 //read the value from the sensor
 temp = temp * 0.048828125 ; 
 Serial.print ( "TEMPERATURE: ") ; //prints temperature in serial monitor 
 Serial.print (temp) ;
 Serial.print ("*C") ;
 Serial.println () ;
 delay (1000) ;     // put your main code here, to run repeatedly:

if ((temp >= 23.0) && (switchPin == HIGH)) { //DON'T FORGET TO CHANGE TO 42 FOR APRON
  digitalWrite(buzzerPin, HIGH);
  tone(buzzerPin, 1500, 1000); delay(150);
}

digitalWrite(switchPin, state);

previous = reading;
  
 
}

}

Sadly, it still did not work :frowning:
Sorry if i did/am not understanding your instruction sim very new to this and still trying to grasp the language.
Thanks Tegan

Hi,
Can you write some code, just to see if the switch is working?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We cannot see much from your picture.

Thanks.. Tom.. :slight_smile: