Switch n-channel mosfet on/off with latching switch?

I have an 12v fan that i controll with Arduino Nano. It works, and when i turn it on/of in loop it turns on/off.

The transistor i have is an n channel logic level (FQP30N06L) resistor.

I want to turn it on/off with a pushbutton and program it to stay on/off after one push. I can,t understand how to do this. Is it possible?

Can someone show me an simple example how to so this please?

Connect the button to a pin, and Gnd.

byte buttonPin = x; // 2, for example
byte fanPin = y; // 3, for example
byte fanState = 0;  // status of fan

void setup(){
pinMode (buttonPin, INPUT_PULLUP);
pinMode (fanPin, OUTPUT);
digitalWrite (fanPin, LOW); // turn it off
}
void loop(){
while (digitalRead (buttonPin == HIGH)){ // wait here until button is pressed
  // button is pressed, continue
   if ( fanState == 0){ // fan is off, button is pressed
   digitalWrite (fanPan, HIGH); // turn MOSFET on
   fanState = 1;
   }
   if (fanState == 1){ // fan is off, button is pressed
   digitalWrite (fanPan, HIGH); // turn MOSFET on
   fanState = 0;
   }
   delay (100);  // give the button a chance to settle
}

Didn't try compiling this

Thanks, i'm going to try this code. :slight_smile:

I have tried a wery similiar code and that turn the fan on the first time, but i could not switch it off when i pushed button second time.

Can you give me an wiring chema example please?

Connect the button to a pin, and Gnd.

Switch S3 in the image.

CrossRoads:
Connect the button to a pin, and Gnd.

byte buttonPin = x; // 2, for example

byte fanPin = y; // 3, for example
byte fanState = 0;  // status of fan

void setup(){
pinMode (buttonPin, INPUT_PULLUP);
pinMode (fanPin, OUTPUT);
digitalWrite (fanPin, LOW); // turn it off
}
void loop(){
while (digitalRead (buttonPin == HIGH)){ // wait here until button is pressed
  // button is pressed, continue
  if ( fanState == 0){ // fan is off, button is pressed
  digitalWrite (fanPan, HIGH); // turn MOSFET on
  fanState = 1;
  }
  if (fanState == 1){ // fan is off, button is pressed
  digitalWrite (fanPan, HIGH); // turn MOSFET on
  fanState = 0;
  }
  delay (100);  // give the button a chance to settle
}



Didn't try compiling this

I tried this code (changes some typos ("fanPan") and added a last bracket) and the fan spinning but the button does nothing.

Here is my code:

byte buttonPin = 5; // 2, for example
byte fanPin = 4; // 3, for example
byte fanState = 0;  // status of fan

void setup(){
pinMode (buttonPin, INPUT_PULLUP);
pinMode (fanPin, OUTPUT);
digitalWrite (fanPin, LOW); // turn it off
}
void loop(){
while (digitalRead (buttonPin == HIGH)){ // wait here until button is pressed
  // button is pressed, continue
   if ( fanState == 0){ // fan is off, button is pressed
   digitalWrite (fanPin, HIGH); // turn MOSFET on
   fanState = 1;
   }
   if (fanState == 1){ // fan is off, button is pressed
   digitalWrite (fanPin, HIGH); // turn MOSFET on
   fanState = 0;
   }
   delay (100);  // give the button a chance to settle
}
}

I'd have gone for state change detection, with the logic reversed for the active low button, and have every press toggle fanState.

An little update here.

I finally get my fan switch on/off with push button. I made a little mistake when wiring. All works just fine now :slight_smile:

Thanks for all help :slight_smile:

To help the next person, maybe you should post your final code and a schematic.

Fwiw, here's how I coded it:

// https://forum.arduino.cc/index.php?topic=677648

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
               (button wired from pin to ground)
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  button = 4;

// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
bool fanState = false;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("https://forum.arduino.cc/index.php?topic=677648");
  pinMode(button, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(button);
  lastButtonState = buttonState;

  //turn builtin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.print("fanState is starting as "); Serial.println(fanState);
  Serial.println("setup() done... press the button");
  Serial.println(" ");
}//setup

void loop()
{
  // read the button:
  buttonState = digitalRead(button);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (buttonState == LOW) //... and if it's now low, that's a press
    {
      fanState = !fanState;
      Serial.print("New press, fanState is now "); Serial.println(fanState);
    }// change to low

    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  //proof:
  digitalWrite(LED_BUILTIN, fanState);

} //loop

Gives this output:

fanState is starting as 0
setup() done... press the button
 
New press, fanState is now 1
New press, fanState is now 0
New press, fanState is now 1
New press, fanState is now 0

Of course. i can post my code later today. But i does not have any schematic. Is there any good online tools for fast creating of schematic for beginners?

Bjerknez:
Is there any good online tools for fast creating of schematic for beginners?

Soft pencil or black Biro on paper and take pic with phone is as good a way as any....