My USB watchdog (and relay output) box

New 5May2019, updated 8Sep2019

This note is in group Technology (plus My XMOS pages and Aquarium pages) describes my only real invention this year: a USB 5V in and USB 5V out, where the 5V out will only be kept as long as a built-in watchdog timer is fed with an edge of a signal from an output of an internal I2C chip. Since Google could not find any such for me (or I didn’t feed it correctly. Update: [1]) then I had to invent one myself (Update: I still would have to invent this one). The unit also has two 230V AC relay outputs and two open drain MOSFET outputs. Standard disclaimer for everything here and in all my blog notes.

For the TIOBE index 016:[10]: «xC programming».

Fig.1 – USB watchdog and relay output box (left). Needs pulses via I2C bus. Open drain MOSFET output connector and Loft board not shown. Øyvind Teig May2019 (press for more pixels)

Two of my needs solved

I use an XMOS startKIT as my aquarium controller, see My aquarium notes (Controller).

  1. The startKIT has no internal watchdog, and it’s difficult to make one in HW on the board or in the microprocessor’s SW. See the Watchdog on startKIT processor? and Restarting a startKIT from SW? threads on xCore Exchange. So I decided that I wanted to make one by controlling the 5V USB power supply cable instead. Such a unit would probably be a new design that even someone out there may be in need of. I already had an I2C line with SDA, SCL, 3.3V and GND that I use to talk with three temperature chips MCP9808. This new unit just intercepts that pluggable line. (Aside: There are other XMOS-related blog notes listed here)
  2. I have also seen that it may be nice to see how the aquarium migh benefit from having a permanently installed water pump and filter, that I may run at times. (Only mechanical filtering, not for a biological filter, which would require constantly running water through it). Like the EHEIM surface skimmer Skim350 that runs on 230V and draws 5W. So I needed a small power 230V AC relay output. Or two if more comes up
  3. I also added hardware to enable me to make some kind of automatic feeding

Block diagram

187_fig5_circuit_diagram_usb_5V_watchdog_and_relay_box_plus_relay_output_i2c_oyvind_teig Fig.5 – Block diagram of watchdog part only (alternative view or download is here)

The functionality of the watchdog part of this unit may just be something you have been waiting for a long time. You could build one (diagram included) – or wait for somebody to make a little breakout board of it.

The box shown in the block diagram above works like this: when 5V_IN is powered it takes about 7 seconds before 5V_OUT is applied. Then the Supervised MicroProcessor Unit (=SMPU) has about 5 seconds to write to a port on an I2C line to tell it is alive. As long as this is forever done within 5 seconds, then 5V_OUT is stable. If not, then 5V_OUT will be removed for about 7 seconds for a new trial.

Not on (=/on) means that the line needs to be pulled down for the relay to switch from off (=not activated or /activated ) to on (=activated).

A smart thing would be if the SMPU is able to read, increment and write the number of restarts every time, and upon some request this value is shown to a user of the SMPU – for diagnostic purposes. I have a FRAM (from Adafruit: here) to do this as simple as possible (even if there are libraries to modify the FLASH in the XMOS world).

Circuit diagram

Design and construction by me. However, you may copy as much as you like. Or use it as a start for your own design.

The circuit diagram is drawn in iCircuit (1.9.1). The .icircuit file is available here. Some symbols are not correct, like the N-MOSFET HEXFET TR1-TR6 and the varistors V1-V2. (I have not used the simulator part much in this diagram. But I have taken out excerpts and made smaller diagrams for simulation.)  (As I may update the diagram this unreadable front-end picture may not be updated. But the PDF is).

187_fig2_circuit_diagram_usb_5V_watchdog_and_relay_box_plus_relay_output_i2c_oyvind_teig Fig.2 – Diagram of USB watchdog and 230V AC relay output (alternative view or download is here)

Tip: To print the PDF on two pages, download Adobe Acrobat Reader DC and print as poster 30% and portrait mode. Then cut the the right side’s left margin at the index in my PDF, and glue them together.

Observe that the diagram contains 230V AC circuitry for the relay outputs. Disclaimer: even if I may have some advice here, you are responsible that your local rules and regulatory claims are adherred to.

Watchdog working

Fig.3 – Scope curves. CH1=C5 and D2.2, CH2=C2, CH3=C6 and D2.14, CH4=5V_OUT

The idea with the above scope measurement (made on my Siglent scope) is to see how no watchdog pulses will keep the lower green 5V_OUT pulsing to 5V (about 5 seconds) and back to zero (about 7 seconds) until watchdog pulses (seen as the blue right part’s sawtooth pattern) finally will keep 5V_OUT stable. Until these pulses fail.. (then repeat).

  1. CH1 (yellow) is the voltage on the C5 capacitor of the monostable multivibrator D2.2. It times out in lack of pulses at /1B
  2. CH2 (purple) is the voltage at C2. It charges as long as 5V_OUT is not present and finally triggers the schmitt trigger D3
  3. CH3 (blue) is the voltage on C6 that is charged until watchdog pulses on D2 /2B arrives
  4. CH4 (green) is the 5V_OUT line on the USB. It’s stable only after watchdog pulses are seen

Both parts of the monostable multivibrator is triggered with a high-to-low pulse on /1B and /2B inputs. This is to avoid an ill defined 5V_OUT condition when 5V_IN power is on the rise. Now it takes about 7 seconds before 5V_OUT is applied. I have no LED for 5V simply because the SMPU box is rather visible beside the aquarium. Also, such a LED would give me little. It’s rather easy to see when my aquarium box has no 5V in!

The diodes are all to discharge large capacitors during power-down. They are schottky diodes, measured to 350 mV reverse voltage.

The boards have two powers that are independent of each other:

  • (Power 1) 5V_IN from the USB Micro-B connector. It powers the NAND gate and the watchdog (74HC4538D multivibrator) and all the three relay coils. Also, passed in through the relay to 5V_OUT
  • (Power 2) 3V3 from the I2C cable. It powers the I2C chip (MCP23008) only. My I2C line is 3.3V, finito
  • GND is common for both

When 5V_OUT is at 5V it’s assumed that the powered unit comes up with a functional I2C line, including the 3.3V power.

230V AC relay outputs

Fig.4 – 230V AC outputs (press for more pixels)

See description of the relays below.

Relay EMP outputs interfering with ongoing I2C?

Fig.6 – Relays switching 5V-1k and. Bouncing when closing contacts

Above we see oscilloscope picture of the two relays alternating between one on and the other off and then changing both. 5V is switched by pulling a 1k down. The relay needs about 4.3 ms to saturate the coil and pull the switch and about 760 μs to de-energize to release the switch.

You can see the rather extraneous bouncing of the relay contacts when they close. For this test it would enter the other contact set through via the decoupling network C8/V1 and C9/V2 that I did not passivate by shorting the two 230V AC inputs 18 and 24. When I did that only some very high frequent noise passed.

The purpose of this measurement is to see if I ever had noise from the relays disturbing the I2C message. Even if the lines MCP230008 output pins change on the second to last falling edge of SCL then the relays as both being activated and released have so much delay that there will be no noise entering the I2C comms. That is, if it is fast enough. I use I2C speed of 100 kbit/s (kHz). The full I2C message is seen above. However, there should probably be no new I2C comms before well after 10 ms!

Automatic feeding: solenoid, electromagnet, motor or servo?

«Feeding» makes sense in an aquarium context. In any other context, just read «extra general pins» for some actuator type. In Fig.2 and you will find a «Loft board» (added in an update). It’s a tiny board routing the two vacant outputs of the MCP23008 via the standard MOSFET output transistors. I have added the two «open collector» (now: «open drain») outputs plus GND, three pins in all. No power, as the general advice is to avoid this, not to introduce electrical noise getting into the USB 5V as used by the processor. Besides, this could open for 5V or 12V or even 24V powering of the actuators. Remember that the MOSFETS have built in zeners to protect them. Externally, in addition to power, I would need to add (depending on actuator type) a reverse diode across the actuator to short the reverse current when the unit is unpowered. I think this is what they call a «kickback» diode.

I have not designed the feeding automation yet, but I assume it would be a mask with a small hole that would open for flow of the tiny food pellets for some time, like 100 ms. (Aside: It would have to be situated away from the damp of the aquarium to avoid the food becoming wet and clogged. But I assume I don’t need any automation to position and park the unit.) There would basically be four actuator types to choose from, but I have allowed for the first three types only with my present Loft board (as always: standard disclaimer):

  1. Solenoid. Like the Mini Push-Pull Solenoid – 5V (no data sheet provided). This draws about 1.1A @ 5V. 5.5W in a small enclosure is a lot should it become permanently powered, by some fault. The Small Push-Pull Solenoid – 12VDC (data sheet given) draws about 250mA @ 12V. 3W in a larger enclosure gives better margin. DC coil resistance is 40 Ohm. Testing it at 9V we would have P = R * I * I = 40 * (9/40) * (9/40) = 2.025W, which is even better
  2. Electromagnet. Like the 5V Electromagnet – 2.5 Kg Holding Force – P20/15. This one draws only 1W. I guess that the good thing about an electromagnet is that the movement it would endorse is less in need of precise alignment. Not like «3mm» movement, but until it reaches my stopper of some kind
  3. DC motor. Like the DC Toy / Hobby Motor – 130 Size
  4. Not directly supported with my present Loft board
    1. Stepper motor. Like theSmall Reduction Stepper Motor – 5VDC 32-Step 1/16 Gearing
    2. Servo. Like the Micro servo. I would need to make another Loft board so that I may supply GND, power and a MCP23008 I/O pin. I would want to read and set the angle. Or drop the power and take both pins through. That would make it possible to write and read to two servos
    3. Plus a lot of variants!

Box with open drain outputs from Loft board

Fig.7 – The box with the Loft board and open drain output connector (press for more pixels)

The updated box is seen above. This is how a design looks when it has been through organic growth! The Loft board is seen (center and right, front=top of picture), plus the 3-pole female Binder connector (left, top). (Left bottom are I2C in (right cable) and I2C out (left 4-pole mail Binder connector)). Internally in the box the boards are also pluggable, both the inputs and the outputs are disconnectable. As with all cabling between boards/boards and boards/connectors.

See all boxes mounted and in use at My aquarium notes (chapter Four boxes seen). There you will also see this box in front, so that the single button is seen

Fish feeder

I did find a solution. It is described in detail in My aquarium holiday automatic fish feeder (for granules).

Since I did not want to make two of the boxes described here I made a half-way «simulator» board that I use by by desk. The real thing is working with the proper aquarium controller now. I also made a connection box that takes control from the open drain output cable shown above. One output is for a solenoid and another for a LED. Then 12V DC for the solenoid and the cable to the solenoid. It’s shown here. The circuit diagram is seen here:

187_fig8_circuit_diagram_output_and_fish_feeder_oyvind_teig Fig.8 – Circuit diagram of the half-way board, the connector box and the feeder (download PDF here)

Version 1 did not have the Z1 (1N6263 15 mA Schottky diode), C1 (10 nF to take EMC) and C2  (10 μF to take bigger discharges) but I experienced that the SW read the input but did not set the outputs. No blinking LEDs and no feeder working, but the button was taken and the SW did talk with the chip. I had not seen this over a month for the real board (Fig.2, but without the solenoid in the feeder present) so I assume that the MCP23008 I2C chip had been reset by some noise and gone into input-only configuration. I have now added the above, but has for the general case also changed the SW to set pin direction on every some writes that would have no side effect (since all output pins would be low until next write), since the watchdog functionality is so important. Remember that I2C is low OSI level protocol with no parity, checksum/CRC or retransmission. It is assumed to be inside boxes or on boards where noise is at minimum, or where just sending often enough would mask any problems. Now, was the source of the noise current in my cable, caused by me not having a Schottky in the feeder? Answer: no it was not. Have a look at the current in that wire in the link above the diagram.


  1. Detailed discussion of the design and parts list are not given. However, the circuit diagram is extensively annotated. But as this list has developed, I am not sure any more. It’s become close to a combined description and parts list:
  2. The box contains three boards, one mounted on top of the other
    1. The Loft board, already discussed. See picture (above)
    2. The Upper board has a 230V AC section with enough distance, and a part of the box for the 230V input, fuse (250mA fast) and the double output connector, plus the 300V AC varistors (230V AC plus 30%, for contact gap arch limitation) and 760V 10 nF capacitors C8 and C9 (class X1, for EMI/RFI noise reduction) – all mounted as 3-D. However, they will stay in place and not mess around should I take the box for a bicycle ride (I did the other day when I showed it in the pause of a lecture). There also is a mechanical shield with a grounded copper foil between this compartment and the USB connectors
    3. The Base board contains the logic, discussed below
  3. The D1 I2C chip is MCP23008 (from Adafruit, here). SPI version, see below
  4. The D2 double retriggerable monostable multivibrator is PDIP-16 (plastic dual in-line package) or DIL-16 (dual in line) pin version CD74HC4538E. This is hard to get by these days, so a 74HC4538D in a SOIC-16 is eprhaps easier obtainable. If you do hand soldering then an SMT Breakout PCB for SOIC-16 or TSSOP-16 from Adafruit is ok, here
  5. The D3 chip quad 2-input NAND schmitt trigger is 74HC132 DIL-16. With inverted inputs for D3.2 it functions as an OR. See Wikipedia here. The inverted inputs are the /1Q and /2Q from D2. The schmitt function (with hysteresis) gives a precise transition when C2 charges and decharges
  6. I bought the USB Micro-B input (from Adafruit, here) and the USB A output (horizontal USB 2.0 A type THT slim type from Würth Elektronik) from Elfa Distrelec (here). However, the latter is upside down if you ask me
  7. The I2C connectors are of series 719 from Binder, bought at Elfa Distrelec
  8. All three relays are of type HJR1-2C from TIANBO GANGLIAN ELECTRONICS, bought at Elfa Distrelec. The coil is 5V and the contact set’s «Max Switching Voltage» is 240V AC, 60W @ 120V AC. The  «Contact Rating» is given at 24V DC and 120V AC, not 240V AC. I switch inductive 5W, and I have protected the contact set well with the above mentioned varistor and capacitor, plus a shared 250 mA fast fuse. I probably would not have used these relays in a product, but for my hobby use it should be ok
  9. The 230V AC four pin connector for the relay outputs I have no idea where I got. I must have found it in the reuse waste container when we moved location at work. It looked like they threw everything away (I know they didn’t). From the h-type logo and number 732 063 on the unit and research on the net it’s probably a Hirschmann™ GDM or GDMF valve connector or the like. When it’s plugged in there is a screw holding it in place
  10. The box is made from an acrylic Saltsjön box bought at an Åhlens shop in Sweden (obsoleted?) and a green polyethylene chopping board from Ikea. Screws are Märklin 1.6 * 13 mm of type 74990. I have not studied the flammable properties of my boxes. For my hobby use they should be ok
  11. The two contact sets on relays REL1 and REL2, used for 230V AC are not paralleled. I don’t like to parallel contact sets since they are mechanical. A single mechanical switch is better than two if they don’t switch on the microsends simultaneously
  12. The transistors TR1-TR6 MOSFETS that I use for driving the relays have internal zeners. so that drain (D) to source (S) voltage is limited, which again protects the transistor against the coil’s spikes when it’s not fed any more. (The zener’s cathode is connected to the drain (D) of the MOSFET.) That’s why I haven’t connected a diode across the coils, since it’s not necessary. But I added the Z1 diode across 5V_IN so that I would not get any negative spikes at any time on 5V_IN


  1. If you have access to the USB A on the SMPU side, then maybe you could use the Data+ and Data- lines for SDA and SCL – or just use a line for a watchdog pulse of some sort.
    • On the startKIT one of the «slices» is equipped with a USB driver from the factory, so it could perhaps have been done. But then I had the I2C already in use, so it was natural to use it in this case. I guess this would be the most general case as well
    • Update: [1] does this!
  2. Use SPI instead of I2C? It should be possible to make a variant with the SPI version of the I2C chip. It’s called MCP23S08. It would need three lines instead of the two I2C lines: SCK, SI (connects to MOSI on master) and SO (connects to MISO on master). With a 5-wire variant of the update watchdog cable it could be possbible to make jumbers to route the chip’s pin 3 to either SO or A2 (high er low)
  3. Maybe a more general case would be to also switch or just route the USB data lines? I dropped them simply because I made a 5V watchdog, used as such. When I would use the USB line for something else (like upgrading the SW) I would need to do that, not with a watchdog box that would break the whole line
  4. The disable watchdog jumper could have been an external switch. But why disable it that easy?
  5. I have used FET transistors to take the 3.3V as far down as possible, to give the relay coils as much voltage as possible. A standard NPN transistor might also be ok
  6. The FET transistors have a R-R ladder on the gates, simply to make it possible to unplug D1 from its socket for testing, and to work with the two boards separately. Then the transistors would all be off. Also, because the small capacitor in the gate might keep the FET on for too long in an unconnected case. Besides, I don’t like floating FET gates
    • Aside: With very high speed a chip like TC4420 high-speed MOSFET driver may be a good choice. But be sure to decouple it correctly, see here («How to choose the right capacitor type for a circuit?! Film vs. Ceramic vs. Electrolytic», a very recent Adafruit blog note (today!)). This would also go for C8 and C9 I guess
  7. X6 (from the not activated output REL3.6 of relay REL3) could perhaps just have been connected to 5V_OUT and then one could have inverted the logic. C2 would have needed to be fast charged (instead of fast discharged), but from where? Is there a Catch-22 here? I did not spend any more time on this, since that relay contact REL3.6 was available


Wiki-refs: Electromagnetic pulse (EMP)

  1. How to create your own usb watchdog script by David Gouveia (2018). It pulses a «USB watchdog» that’s easy to find by searching for just that. As Gouveia, I didn’t set up a url, for me because I didn’t know the original manufacturer

Leave a Reply

Dette nettstedet bruker Akismet for å redusere spam. Lær om hvordan dine kommentar-data prosesseres.