Will it work with every Lego/HiTechnic/Whatever sensor out there?
Sensors that will NOT work (without modifications):
Note 04/2010: Just another quickie...
HardwareAs a deterrent, we start off with the full-featured nerd version. Most will leave here. This safes a lot of traffic and keep the pages up ;)
The 74HC4052, if you do not know it already, is an analogue multiplexer. It connects pin X (Y) to any of the pins X0, X1, X2 or X3 (Y0,Y1,Y2,Y3), which can be selected by the control pins A and B:
The 74HC4052 (as well as its pure CMOS friend CD4052) really acts like a (poor) bidirectional 1-to-4 switch or relay. The current may take any direction. "Poor" means that the internal resistance can not compete against the mOhm values of a switch or a relay. A 74HC4052 has a typical, internal "ON" resistance of ~100 Ohms. (Though one could write much, much more on this topic, but I'll stop here. The DC behaviour is is all you need to know, so far ;-).
For any equipment attached to one of the ports A, B, C or D, the 74HC4052 is transparent. If the port is not selected, Xn and Yn will have a high impedance state and SDA and SCL are pulled up on the bus side (single Lego-sensor with internal termination or real I2C bus). If selected, any clock (SCL) or data-signal (SDA) will simply be fed through X<->Xn and Y<->Yn.
The port control is operated by a tiny and cheap PCA9536, a 4 bit I/O port device, attached to the NXT side of the I2C bus.
IO0 and IO1 are connected to the 74HC4052's A and B pins, controlling it directly.
A note on I2C pull-up resistors:
An I2C bus needs pull-up resistors on both lines. These will create the "high" signal state. Any device, including the master, can not
source (put an active high state on any of the lines) but only sink current (open-collector or open-drain configuration), by pulling
the line down (to ground).
Now imagine, the NXT's processor pulls DIGIB0 to ground. No matter if the sensor has the pull-up against 5V or 3.3V, the two resistors form a simple resistor divider. The voltage, that is seen by the sensor is:
Us = U * 4k7 / (4k7 + Rpull)
"Usually" (...), all the I2C equipment operates with CMOS logic levels (NOTE: The English Wikipedia entry contains some severe errors. Because of this, the link points to the German article instead.) and even if your equipment uses TTL or whatever, the principle is the same:
The voltage needs to trip below a certain level, to generate a logical low state (the same, of course, applies to the high state which needs a voltage above a certain level).
For CMOS logic, for example, a low state is generated below ~1/3 of the supply voltage, whereas a high level will be valid for levels above ~2/3. The area beween those two is the "forbidden-zone". It might create a low OR a high state and can create excessive currents in the input stage (the only component capable of handling such levels is one that includes a hysteresis. E.g.: A Schmitt trigger).
For 5V supply, the boundary(!) value for a low state is ~1.5V. Usually, one would at least(!) need to half this value to be on the safe side under all circumstances. If we assume ~750mV, which still is a too high value (if you consider that we are talking about a dynamic behaviour, switching up and down and not a static DC signal!), we can calculate the minimal value of the external pull-up resistor, that will allow the sensor to detect a low state (in static DC applications!):
Rmin = ( 5V - 0.75V ) * 4k7 / 0.75V => ~26kOhm
For any resistance below this value, a logic low level would never be reached (And if you really understand the above, you will notice that the voltage supply term cancels out of the formula. You will get the same result with a 3.3V supply and an adapted low logic-level ;-).
Usually, one would like to have the lowest possible value...
But wait, what is this:
Yes, but only statically. While the signal lines can be brought to ground very quickly (the transistors simply short the line to ground), the rising edge will suffer from the sum of all parasitic capacitances (cable, sensor, NXT).
The pull-up resistor needs to charge them all. Charging the line up to ~63%, the time-constant, calculates to:
t = R * C
Thus, increasing the resistance will slow down the rising-edge. Your one nice rectangular waveforms become triangles and the high-logic level might be reached too late. Thus, the processor recognizes a low-level instead and your communication will fail or bring up strange results...
Why is this all important?
To overcome this issue, you have two possibilities:
These resistors will show up in parallel to the external ones, if a port with attached equipment is selected. The total resistance will be:
1/Rt = 1/Ri + 1/Re
"total" = "internal" .. "external"
For the proposed 220k and externally attached 82k (Lego), the resulting resistance calculates to:
Rg = 1 / ( (1/220k) + (1/82k) ) = ~60k
For whatever modification you make, whatever equipment you attach, make sure that the overall resistance does not fall below the critical level.
Back to the circuit:
This minimal version will operate with any I2C devices or busses except for the following:
Because the PCA9536 listens to all I2C traffic, a control byte with a value of 0x82 (or 0x83 for a read operation) will always activate it. No other device may share this address!
Although the 9V supply could be taken from a battery, we can do better:
The Xtended version introduces two, current limited, power supplies. One, simultaneously powers port A+B, the other one C+D. They can be turned on or off via PCA9536 IO2 and IO3.
If one of the IO pins is high, the attached transistor Q7 or Q8 is activated and pulls R30 or R31 to ground. This turns on the transistors Q1, Q3 or Q2, Q4, which are configured as current sources, in conjunction with the red LEDs. The LED and the emitter resistors determine the current limitation, which operates independently on any of the four ports:
Il = (Uled - Ube) / 39E = (1.7V - 0.65V) / 39E = ~27mA
considering the additional voltage drop across Rb: ~20mA
Although the circuit could be dimensioned for lower currents, by increasing R30 and R31 (~730uA@10k), choosing a proper LED and high B transistors (though it will be hard to find anything much better than a BC807-40), the proposed dimensioning will work fine with a broad range of LEDs and transistors. Lower currents through R30 or R31, the "driving currents" for the upper transistors, additionally would require lower base resistor value, which reduces their decoupling effect on the adjacent current source and make the current limiting curve less sharp...
The 9V supply is created by a LT1111, a not-that-cheap boost regulator, which is always turned on. In quiescent state (no load),
it will consume a little less than 300uA.
I guess that's fair (at least for Lego equipment ;-).
The slow rising and current limited power output of the NXT is not sufficient to power up the LT1111. At least, if you have NerdpleXT already connected and turn on your NXT afterwards!
As a workaround, do one of the following:
- first turn on your NXT, and then plug in NerdpleXT
- use a battery pack (always recommended)
- try your luck with a LT1110
If none of the "port LEDs" (LED4-7) is on, the LT1111 hangs.
This power-on lock will be solved in V2.1 or V3.0...
Note: Excessive loads on the ports' 9V and 5V supply require a battery pack! The current output of the NXT's 5V supply (pin 4) is limited to a little more that ~220mA FOR ALL 4 SENSOR PORTS AND THE MOTOR ENCODERS TOGETHER (see here)!
Instead of mounting the LT1111 and peripherals, one could of course stick to a simple 9V battery...
Note: The 9V supply should be attached all the time! Otherwise, strange things may happen!
Because of the chosen design, the 9V supply is not transparent. Turning it on in the NXT (e.g. by activating the US or light sensor),
will not power the attached equipment. You have to do it by your own. Sensors, which need 9V supply, should not be mixed with ones
that do not need it, use the A/D pin (e.g. Lego switch) or may take damage...
Because of D8, the internal "5V" supply, as well as the external A-D supply, will be ~4.4 - 4.0V, depending on load
(this circuit was developed for external 3.3V equipment, only!).
Four 10k pull-up resistors (R12, R17, R21 and R25) simulate the NXT's A/D port (pin 1). A 100k and 5V6 Zener diode combination protect the following components against >5V voltages (e.g. if the 9V supply is turned on).
A second 74HC4052 (IC2), which uses the same channel selection like the one that sits on the I2C bus, feeds one of the four port's simulated A/D-pin signal into the MCP6231 (IC1), a low power OP, configured in non-inverted mode. The OP's output is boosted by a PNP transistor Q6 (BC807-40). R16 closes the feedback loop and C10 limits the bandwidth and prevents oscillations (Although the MCP6231 already has a GBP of 300kHz ;-).
Note: This configuration will even sink an enabled 9V supply down to ground (NXT pin 1). So, right now, it indeed is possible to have 9V activated AND read values from the A/D converter. Though this does not make that much sense: Pin 1, with 9V activated, has a current limitation of about 18mA. And this current will be flowing all the time! You are advised to keep the 9V turned off. But an accidental activation of the 9V supply won't do any harm to the circuit.
Normally, the PNP transistor would not be able to sink pin 1 down to ground. Because of its Ube, it would stop at ~650mV. To get past that limitation, the MCP6231, the attached transistor and the preceeding 4052 are operated by a negative voltage (appr. -1V), which is created by IC5, a 74HC14 and surrounding parts.
IC5, the 74HC14, operates as a free running oscillator in an "improved mode". The giant HC14 is in there, with four gates unused,
because two single gates cost the same but can only delive half the current.
The negative voltage needed a limitation because the MCP6231, like most other rail-to-rail OPs, can only handle ~6V, with 7V as the
the absolute maximum specification...
At the very end, the remaining part of IC2 (74HC4052) is used to light up 1 of 4 LEDs (yes, red ones ;-) to indicate the activated port.
R10 = (Ub - Uled) / Iled = ( 5V - 1.7V ) / 2mA = ~1.8k (next from E12)
SoftwareA mini example of controlling NerdpleXT is available in the download section.