0
\$\begingroup\$

I'm trying to redesign this flip-flop circuit on the picture so I can use it to store one bit of memory. This circuit on the picture is operated with pushbuttons, but I want to operate it via a ESP8266 digital pis.

I will use the circuit to save the state of a PIR sensor which I will read later with an ESP8266 (ESP is in deepsleep to conserve energy).

Circuit initially has a low state (0) and should change to a high state if it recives a 3.3 V signal from the PIR.

There should also be an option to set it to a low state (erase data) via a signal from an ESP8266 pin.

I know this can be done with a 555 timer, but I'm looking for a NPN transistor version since I have a supply of BC547 transistors.

The circuit will allways be powered with 5 V or 3.3 V so memory loss is not a problem.

enter image description here

Updates from comments:

  • ESP is in deep-sleep and turns on every hour just to check if PIR was activated.

  • ESP deep sleep power consumption is acceptable for me.

  • Waking up ESP on hardware interrupt will happen too often since it will turn on every time PIR is triggered.

  • I'm using HC SR501 PIR sensor.

\$\endgroup\$
14
  • 4
    \$\begingroup\$ You must ask a question. Please edit it in so it can be answered. \$\endgroup\$
    – Justme
    Commented Jul 23, 2022 at 8:52
  • 4
    \$\begingroup\$ You have over four million bits of memory on the MCU, what use is one external bit? Just run the PIR to a GPIO? \$\endgroup\$ Commented Jul 23, 2022 at 8:53
  • 3
    \$\begingroup\$ Isn't there about 25 mA going thru the one of the 420 resistors? Lots of energy wasted there \$\endgroup\$
    – D Duck
    Commented Jul 23, 2022 at 11:55
  • 4
    \$\begingroup\$ Standby current should be as low as possible means that you should use CMOS circuits which have nanoampere order static consumption. CMOS RS-trigger like CD4043 is what you need. Another solution is to use pin-change interrupt of MCU to wake it and process signal from sensor. \$\endgroup\$
    – Vladimir
    Commented Jul 23, 2022 at 13:10
  • 2
    \$\begingroup\$ Can’t you just put your ESP in deep sleep, wake up on hardware interrupt from the PIR sensor, write that to memory and go back to deep sleep? \$\endgroup\$
    – winny
    Commented Jul 23, 2022 at 20:42

2 Answers 2

3
\$\begingroup\$

Have a go with this:

schematic

simulate this circuit – Schematic created using CircuitLab

The "data" output is Q. Its state will be indeterminate upon power on, due to the circuit's symmetry. Use the microcontroller to explicitly clear Q upon power-on as I will explain below.

S and R are "set" and "reset" inputs. A short positive pulse on S will cause Q to go high and stay there, even after the 'S' signal has returned low. Similarly, a short positive pulse on R will cause Q to go low and stay there.

Here's a plot of this happening:

enter image description here

I have replaced all resistors and LEDs with values which will be much less power-hungry, and still work. The only other thing I've done is replace the two switches with transistors that perform the exact same function in response to digital signals at their bases.

If I have understood you correctly, you should connect output Q to a microcontroller input, and input R to a microcontroller output. Then you may program the microcontroller to provide a momentary pulse on R in order to bring Q low.

Connect S to whatever source you like; a short pulse there will bring Q high, where it will stay until R input receives a reset pulse from the microcontroller.

The whole circuit draws about 500μA (0.5mA) from the 5V supply.

Alternative with fewer parts

It's safe to short circuit Q to ground (or NQ), since there's nothing but a weak pull-up resistor holding it high. That means this circuit lends itself well to a sort of diode-AND arrangement:

schematic

simulate this circuit

enter image description here

Note: I've offset two of the plots by 6V and 12V, so they all appear stacked one above the other. In reality all signals are digital 0V or 5V

The active level of the set and reset inputs is now low, and they are called NS and NR ("N" for "not"). When either NS (or NR) is brought momentarily low, its respective diode becomes forward biased, dragging Q (or NQ) with it, with sheer brute force. This causes the flip-flop to flip, or flop, if necessary, to adopt the new state. When the NS (or NR) input is high, its diode is reverse biased, isolating that high potential from Q (or NQ).

\$\endgroup\$
7
  • \$\begingroup\$ Thank you Simon, this is exactly what I need in funcionality terms. Power cosumption is also OK. However, is it possible to reduce the number of transistors due to PCB space ? If there is no solution with only BC547, maybe use PIR high signal to BC547 base to latch additional PNP transistor ? \$\endgroup\$
    – Marc
    Commented Jul 23, 2022 at 23:13
  • \$\begingroup\$ @Marc OK, I have a couple of ideas. If they work out, I'll append them. I was under the impression you wanted to get rid of some transistors. \$\endgroup\$ Commented Jul 24, 2022 at 7:47
  • \$\begingroup\$ Great Simon, that would be interesting for me to learn. \$\endgroup\$
    – Marc
    Commented Jul 25, 2022 at 13:35
  • \$\begingroup\$ Thank you @Simon Fitch for this alternative version. I cannot figure which input should I use on this version in order to set it HIGH by a short 3.3v PIR pulse ? \$\endgroup\$
    – Marc
    Commented Jul 28, 2022 at 17:19
  • \$\begingroup\$ @Marc See the orange trace in the last graph. A short low pulse on NS will set Q high. \$\endgroup\$ Commented Jul 28, 2022 at 17:58
0
\$\begingroup\$

Since your main issue is to conserve energy, and you only want to see if PIR was going high during your sleep, you can use a edge detection coupled with sample and hold.

enter image description here

You can see that the capacitor goes high when the PIR is going high. Then as the GPIO is in INPUT mode, you can read the value.

Then to reset the edge detector, you put briefly put the GPIO into OUTPUT mode and pull LOW.

If you revert back to INPUT mode, and go to sleep again, you're ready to wait for another detection.

The whole setup doesn't use much current. Only to charge/discharge the condensator and then it takes a couple of nA of leak.

The 3.6V Zener is used to clamp the voltage to safe levels for the MCU, adjust to your needs. You can put it eventually after the 1k resistor if the input is too high.

Additional info not directly relevant to your question:

You can even trigger an interrupt on that pin to wake the MCU up upon PIR going high to be able not to miss edges. If you want to know "how much time", you can use a discrete edge counter with several CMOS IC to be able to avoid waking up to MCU.

\$\endgroup\$
10
  • \$\begingroup\$ Thank you Steve, very interesting approach. Is the 3.6V protection zener necessary ? I've converted the PIR sensor so it also works on 3.3V logic, same as ESP. \$\endgroup\$
    – Marc
    Commented Jul 24, 2022 at 8:27
  • \$\begingroup\$ if everything is 3.3, it shouldn't. but my simulator had a spike to 5.6V without. You can also use 2 LED, as their forward voltage is around 3V \$\endgroup\$ Commented Jul 24, 2022 at 8:35
  • \$\begingroup\$ I've tested the circuit, but it doesn't hold the charge from PIR as expected, seems that the capacitor is too low value. I've replaced it with 10 uF electrolotic capacitor which holds the charge fine. Do you think that this circuit will be reliable with electrolitic instead of ceramic capacitor ? \$\endgroup\$
    – Marc
    Commented Jul 24, 2022 at 12:07
  • \$\begingroup\$ It depends on your capacitor. Testing it will tell you. Measure the voltage after an hour. If it is too little either increase the capacity or wake up earlier. As it consumes much less current than an active bistable, you might even be ok with waking up up to every 15 min. \$\endgroup\$ Commented Jul 24, 2022 at 14:47
  • 1
    \$\begingroup\$ Great, I don't think it can happen since I'm reading the capacitor state from ESP8266 analog pin which is input only. \$\endgroup\$
    – Marc
    Commented Jul 24, 2022 at 21:36

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.