2
\$\begingroup\$

I make a simple circuit with the use of Arduino and it works well, having an IC like ATTiny is good but i have a small space on my PVC Tube for DC-DC Step Down.

My goal is to make a flashlight that have a secondary switch

  1. Switch 1 (Toggle Switch) - Turn Lights On or Off
  2. Switch 2 (Momentary Switch) - Turn Lights On even if switch 1 is Off, Turn Lights Off even the switch 1 is On.

I have this code

    void setup()
{
  Serial.begin(9600);
  pinMode(2,INPUT_PULLUP); //
  pinMode(13, OUTPUT);
 
  pinMode(3,INPUT_PULLUP);
}

void loop()
{
  
  int sensorValue = digitalRead(2);
  int sensorValue3 = digitalRead(3);
  //Serial.println(sensorValue3, DEC);
  if (sensorValue == HIGH){
    //OFF via toggle
    if (sensorValue3 == LOW){
     // ON via button if OFF via toggle
        digitalWrite(13, HIGH);
    }else{
      // OFF 
        digitalWrite(13, LOW);
    }
  }else if(sensorValue == LOW){
  //ON via toggle
    if (sensorValue3 == LOW){
      digitalWrite(13, LOW);
    }else{
      digitalWrite(13, HIGH);
    }
  }

}

Ive seen an Oscillator with a relay but cant make it work.

\$\endgroup\$
3
  • \$\begingroup\$ this code is unnecessarily complex ... if (sensorValue3 == LOW){ digitalWrite(13, LOW); }else{ digitalWrite(13, HIGH); } ........... this produces the same result digitalWrite(13, sensorValue3 ); \$\endgroup\$
    – jsotola
    Commented Jun 18, 2020 at 3:38
  • \$\begingroup\$ A relay would really not be suitable for this at all, and there's no way you're going to find one smaller than an MCU. Consider the actual range of packages in which the ATtiny is available - the SOIC-8 is one you should be able to work with by hand, or for the lowest resource parts the SOT23-6. \$\endgroup\$ Commented Jun 18, 2020 at 3:58
  • \$\begingroup\$ I'm pretty sure the code is looping so fast that you cant tell it's working. Press the momentary button too fast and it wont register (to your eyes). \$\endgroup\$
    – Passerby
    Commented Jun 18, 2020 at 4:00

2 Answers 2

2
\$\begingroup\$

You can do this with a single XOR gate.

As follows:

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
0
\$\begingroup\$

I would build such a circuit using PNP, resistor and 2 switches - momentary and latching. And no MCU. No OFF power consumption.

Why not MOSFET? I don't know at what voltage your flashlight is, if your light's voltage is high enough to make sure mosfet will open, use a MOSFET. PNP is good as long as you are at AA voltage (down to 1 volt when pretty drained).

Sorry I'm a lazy pig and sketched it in paint in 2 minutes, but here it is

enter image description here

We have low minimum voltage for the circuit to work. Of course you may want to tweak resistors depending on your PNP's gain and required load current you want to provide. And if you work with 3V+, you can use a MOSFET instead (look for logic level mosfets). This circuit has very-very-very close-to-zero power off current consumption, comparable or even less than MCU in sleep mode, again, depends on specific parts, but it's possible to get it down to neglibible nanoamps if you find a good transistor. Also, let's not forget BJT will drop some 0.2V, MOSFET with milliohm-scale Rdson will drop nothing you could ever notice.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.