The circuit has been updated but here are the original design elements. Read the comments for more details :-)


So here is the old schematic:

Requirements

Cheap. A CD4017 or 74HC4017 costs about 1$, add some "dust" (diodes, resistors) and a small PCB.

Simple and compact: yes too :-)

Low power: at most a few mA. (fail: the DC/DC draw much more, even unloaded)

Reliable: should be immune to EMI. Cat5 cable uses twisted pairs which should protect the signals over medium/long distances. A fluorescent lamp's ballast or other mains-conducted EMP could be picked up but should not disturb the system.

Easy to interface and program, with a robust algorithm that works even when the keypad is removed.

Wire-level Interface

The constraint is to read the buttons over existing Ethernel lines in a EMI-prone environment.

Many projects use a 74HC165 (for example this one ) but this method requires at least 2 control signals: PL (load data) then CP (clock pulse to shift data).

With the 4017, only one control signal is necessary: clock. This makes the system inherently more reliable (one point of failure less). Synchronisation is achieved by using one of the output signals, which will always be "on" (think about a serial link with a start bit that is on).

The 4 twisted pairs are used, one of the wire in each pair is tied to a common 0V/GND. The others are:

  1. 5V
  2. Clock
  3. Dout
  4. 5V

The original version is not isolated since there should be no electrical contact with anything else in the remote circuit. I added it anyway...

The 4017's clock input must be well filtered because any undesired pulse on this line would break the synchronisation. A RC filter + Schmitt trigger is used so far. This is less critical on the controller's side because the data is sampled just before changing the clock level, which happens at a pretty low frequency.

5V vs 3.3V

(moot: the optoisolators now remove this concern)

The Raspberry Pi is a 3.3V system but the 4017 is powered by 5V. Here is the trick:

The pull-down resistor value is chosen low to increase noise rejection. A current of 0.6mA flows when a button is pressed AND it is selected by the 4017, so this is the worst case power consumption(all buttons pressed at the same time). When no button is pressed, the "sync" signal will draw 0.06mA in average, and 0.12mA when one button is pressed.

A 100nF capacitor in parallel with the 5K pull-down will filter out some transients. As it is in series with the 1K resistor, the inrush current that charges it is limited.

For the 4017's clock input, the 5V supply wouldn't be usually compatible with the 3.3V GPIO signal. However, the margin is valid because of the diode drop: 5V-0.7V=4.3V, the Vih is usually 70% of Vcc, 4.3×0.7=3V, which is 10% lower than 3.3V.

Timing and algorithm

The keys are scanned at low speed, giving time to settle for all the electronics.

A polling routine is called at about 100Hz. This drives the clock output signal, so half the calls are just about changing the clock signal (there are 2 edges per cycle).

The other edge runs a software finite state machine, driving the synchronous bitstream, which works in either of two modes: scan or synchronisation.

Internal counters are incremented until a button is considered pressed or until the bitstream is in sync. 3 consecutive identical valid frames are considered as givin "valid" data.


Logs:
1. Isolation
2. First signals
3. Implementation
4. Last notes