Electronics notes/IO and wired communication

From Helpful
(Redirected from SPI)
Jump to: navigation, search
This is for beginners and very much by a beginner.

It's intended to get an intuitive overview for hobbyist needs. It may get you started, but to be able to do anything remotely clever, follow a proper course.

Some basics and reference: Volts, amps, energy, power · Ground · batteries · resistors · changing voltage · transistors · fuses · diodes · varistors · capacitors · inductors · transformers · baluns · amplifier notes · frequency generation · skin effect

And some more applied stuff:

IO: Input and output pins · wired local IO wired local-ish IO · · · · Shorter-range wireless (IR, ISM RF, RFID) · bluetooth · 802.15 (including zigbee) · 802.11 (WiFi) · cell phone

Sensors: General sensor notes, voltage and current sensing · Knobs and dials · Pressure sensing · Temperature sensing · humidity sensing · Light sensing · Movement sensing · Capacitive sensing · Touch screen notes

Actuators: General actuator notes, circuit protection · Motors and servos · Solenoids

Some stuff I've messed with: Avrusb500v2 · GPS · Hilo GPRS · JY-MCU · DMX · Thermal printer

Audio notes: basic audio hacks · microphones · amps and speakers · device voltage and impedance, audio and otherwise ·

Less sorted: Common terms, useful basics, soldering · Microcontroller and computer platforms · Arduino and AVR notes · ESP series notes · Electronics notes/Phase Locked Loop notes · mounts, chip carriers, packages, connectors · signal reflection · pulse modulation · electricity and humans · Unsorted stuff

See also Category:Electronics.

Simple wire/circuit protocols

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

On voltage differences

See Electronics project notes/Unsorted#Dealing_with_voltage_differences

I2C and TWI (fairly common)

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

I2C (Inter-Integrated Circuit), often written as I2C. Two-wire a.k.a. TWI (Two Wire Interface) is almost exactly the same, but under a different name (apparently for reasons of potential legal bother?(verify))


  • clock (SCL)
  • data (SDA)
  • a shared ground.

Allows various slaves on a bus. Slaves only talk to masters, not to other slaves.

Multiple masters on a bus is possible, since bus arbitration is part of the protocol definition.


Often one of:

  • 10 kbit/s low-speed mode
  • 100 kbit/s standard mode
  • 400 kbit/s Fast mode
  • 1 Mbit/s Fast mode plus (Fm+)
  • 3.4 Mbit/s High Speed mode

100KHz is fairly standard, 400kHz (and 10kHz?) are also commonly seen.


Bus capacitance should be less than 400pF for standard mode (Fm+ devices should be able to deal with 550 pF), and you should assume it's one to few meters at best(verify), a bit more for low-speed mode.

There are basic extender ICs, and some other tricks, but if you need distance you're better off with things designed for it.

Wiring details

SDA and SCL are open-drain (open-collector in TTL terms), which means they usually ought to be pulled up to Vcc, for when all devices are tristated so no device is pulling it down. It may work without this (probably more likely to work on short, few-cm connections, at low speeds), but is more error-prone, easily leading to confused masters(verify).

Devices rarely have pull-up resistors integrated, because you ought to do this only once per bus.

The best resistance value depends on the bus capacitance, and also on the bus voltage.

  • You probably want ≥1kΩ, as less may mean current becomes a problem
  • You may well want want < 10kΩ, as more might make communication fail at higher speeds (and may not comply to rise times even when it does work). (verify)
    • it seems 400kHz probably needs <5kΩ


Device addresses may be

  • fixed for a device - meaning you can't put two of the same thing on one bus
you could power all but one down (but that's awkward)
you could use a I2C multiplexer, like the TCA9548A, a I2C device that switches the bus based on what you write to it.
  • jumpered
  • programmable and forgotten at resets
  • programmable and stored in eeprom

Typical addressing is 7 bits, in the first 8 bits of a transfer (alongside 1 bit for direction).

Using 7-bit addresses lets you use up to 112 7-bit-address devices on a bus (not 128, there are some reserved values, including a bit pattern that signals use of 10-bit addresses (which is rarely used). There are other reasons you may sometimes be able to use fewer on a bus).

...so that first bytes will be (address<<1)|1 for reading and (address<<1) for writing. It seems most libraries abstract that away and take just the address (though some datasheet give address as this already-shifted thing, which can be confusing).

Exchange - lowish level


Hanging communication

  • More likely when there are no pullups on the bus
  • Does your IC have internal pullups? (they may be too strong(verify))
  • your library may not have timeouts

See also:

SPI (fairly common)

'Serial Peripheral Interface (Bus)' [1]

Range: Short. Unsure how much exactly. Meters?

Speed: Masters often support 125kHz .. 4MHz (often clock-divided steps, factors of two). (There are masters that support speeds up to something like 10, 12MHz, even 40MHz or 50MHz.)

Simpler slaves (often those that will never need to move much data) may have a maximum speed of 500kHz or less.


  • SCLK, clock (output from the master)
  • MOSI, master out slave in (output from the master))
  • MISO, master in slave out (input from slave to master))
  • SS, slave select (output from the master)) (also known as CSB)
  • a shared ground


  • MISO is optional for devices you only command and don't read out,
(MOSI is basically only optional for snoopers and logic analysers)
  • There is also a three-wire mode, which lets you puts MOSI and MISO on a single wire.

Features, in comparison to others:

  • Potentially faster than I2C, SMBus (depending on master, slave capabilities)
  • only one master on a bus
  • as many slaves as you can connect, but needs an extra wire for each (though you can daisy chain, use address lines, or such)
  • don't need pre-set address (can matter for usability)
  • Full duplex communication (most others aren't, though you often do not need or use this)
  • more wires than most others
  • lower power than various others

See also:

Dual, quad
This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Dual SPI and Quad SPI are the same idea but can transfer more bits per transfer.

Dual SPI reuses one line in the other direction and becomes half duplex.
Quad SPI adds two more lines

This is e.g. seen on some SRAM ICs, for faster transfers.

SPIFI (SPI Flash Interface) seems almost but not quite the same thing (verify)

(not the same as 4-bit transfers as used in SD cards (verify))


There is also a distinct 'quad everything' setup, variably called QPI or SQI ("Serial Quad IO") (verify)



A predecessor (and subset) of SPI.

Half duplex, tend to have a slower maximum speed than SPI often does.

See also:


'System Management Bus'

A two-wire bus derived from I2C (but not necessarily compatible unless made to be(verify)).

Wire speed: 10 kHz to 100 kHz

See also:


Variant of SMBus targeted at power supplies, adding a bunch of domain-specific commands.

Wire speed: up to 400 kHz


A shared master/slave bus which can have multiple devices on it.

The one-wire refers to using one pin for communication both ways, you'll still also need a shared ground between master and slave (though it might work via touch(verify)).

Seen e.g. in various sensors, and e.g. iButtons. Sensors will often also have a power line (though some could theoretically get power from the data line).

Devices such as iButtons use a capacitor for short-term storage of power (e.g. when replying)

Each 1-wire device has a unique ID, as parts of 64-bit ROM:

  • 8-bit family code [2] [3]
  • 48-bit serial number
  • 8-bit CRC

Simple iButtons have nothing more than this (e.g. used for simple key systems). Other devices have more to say.

See also:



One wire (aside from power)



Vehicle buses

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

These are often serial interfaces, with more robustness provisions, e.g. having specs or protocol deal with things like noise, collisons, a few meters of wiring, and possibly things like priorities.

There are many of these (and occasional use of more generic things like I2C and SPI), and many are a layer of extra care on top of something relatively simple.

A few names pop up more often, say,

In boats you have RS422 as per NMEA0183 which became the thing many types of devices adhered to
In cars you now regularly see CAN[4].
...though CAN was targeted for automotive use, but it turned out to be a robust and flexible shared (multi-master) bus and found somewhat wider adoption.

See also:


Serial ports

Serial communication as the concept is much, much more widespread, while 'serial port' usually refers to a much more specific series of interfaces. The most central part of these is probably the means of signalling.

Note that the electrical levels, the method of signalling, and the plugs are separate concepts, which is relevant in the official as well as the unofficial variants.

Terminology and pins

Common variants

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Most common serial ports seem to be:

RS-232, (a.k.a. EIA RS-232, EIA 232, and TIA 232)

  • (probably the most common variant because of desktop PCs)
  • Standard (currently TIA/EIA-232-F) defines Defines voltage levels, signaling rate, timing, slew-rate, short-circuit behavior, pluggable connectors and pin identification, and some more
  • single-ended communication
  • speed and range: usually at most a few hundred kbps at short range (in theory up to a few MBps), aa few dozen kbps at a dozen meters or so(verify)
  • one-to-one (1 driver, 1 receiver)
  • full duplex (separate line for send and receive)
  • ITU-T V.24 is most related, others (like ITU-T V.28) can also be relevant.
  • http://en.wikipedia.org/wiki/RS-232


  • probably the most common variant after RS-232
  • differential communication
  • speed and range: <10MBps at a few meters, <100kb/s at ~1km
  • one pair of wires can do half duplex (seems more common), two pairs can do full duplex
  • up to 32 drivers/receivers on a single bus
    • though not arbitrarily, and with limitations on range (verify)
    • only one driver active at a time. Protocols are often written to avoid collisions, and some setups detect them by checking what's on the bus while you're sending.(verify)
  • http://en.wikipedia.org/wiki/RS-485


  • differential communication
  • speed and range: <10MBps at a few meters, <100kb/s at ~1km
  • full duplex - RX pair, TX pair. Also a CTS pair and RTS pair but these seem optional(verify)
  • multi-drop: 1 driver, up to 10 receivers
  • Compatible: ITU-T V.11 (verify)
  • http://en.wikipedia.org/wiki/RS-422
  • Yes, this is much like RS485. The largest practical difference is often the full duplex nature of RS422, and that that can simplify how the protocols work
(also lower max receiver number - due to different high-impedance specs?)

There's a bunch of other RS-othernumbers


  • The names are seen as RS-something, EIA-something, TIA-something, and sometimes ANSI-something, and some with a similar-enough (or equivalent) ITU-T standard.
    • This and related ground is covered by ITU-T V series (ports, lots of modem stuff, and such).
  • There are further related standards in the series, but most of them are not so interesting now.

On voltage levels

The RS-232 standard mentions to use +15V and -15V to represent boolean levels, to be robust to +25V..-25V, and to still work with signals weakened to +3V..-3V.

Computers regularly used +12V and -12V, because computer power supplies usually provide those and this is well within spec for most any use.

If a serial port comes from a motherboard connection, and/or has a DB-25 or DE-9 plug, you may want to suspect it may have this higher voltage until you've checked, because such voltages will eventually kill the kind mentioned next:

More recent serial ports often have different voltage levels.

Usually TTL levels: 0V and 5V.

Devices with such as microcontrollers, PCBs with serial headers, hand-held devices, phones, and such may have 0 and 5V, or 0 and 3.3V. (3.3V may (or may not!) be robust to 0..5V communication depending on design)

This is not compatible with RS-232.

For one, RS-232 considers -3V..3V as a transition range where the values is undefined. Still, I've seen 0..5V interact perfectly well with RS-232.

...but, more importantly, fry the 5V device eventually. I found out a device had a classic RS232 port by frying a cheap serial-to-USB cable after apparently working fine for a few days.

USB-to-serial adapters frequently choose to just use the 5V that USB itself provides.

A few may be designed to provide real RS-232 voltages (the DC-DC voltage conversion is about a buck worth of components, so price isn't necessarily a good indicator).

Level shifting can be done in various ways, is probably easiest via specific-purpose ICs, such as the MAX232, MAX211, and others in the MAX220..MAX249 series, MAX3232, and similar products from other companies. There are quite a few options, many of which are cheap (and a few fancier ones that are not).

MAX232 and similar
This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

A converter between oldschool RS-232 voltage levels (-15V and +15V) to recently typical unipolar levels (5V TTL), so that you can interface oldschool serial ports to microcontrollers and such.

Powered from 5V, translated IO at 5V. Has a charge pump to generate the larger voltages (only as much current as is necessary, order of magnitude 10mA(verify), so rarely useful for other purposes)

Has two lines in one direction, two lines in the other. Usually one pair used for TX/RX, the other pair often unused, or e.g. used for flow control, e.g. CTS/RTS-style.

Further components you'll need:

  • Two external capacitors to support the the voltage pump.
    • specced at at least 16V. Capacitance depends on variant, e.g. 1µF for MAX232, 0.1µF for some others
  • capacitors(verify) on VS+ (to Vcc) and VS- (to Gnd) (for stability? required or not?)
  • bypass capacitor on Vcc can't hurt (1uF?)

Pin-compatible: MAX232, ICL232, ST232, ADM232, HIN232, and probably others.

There are variants, e.g.

  • Some are 3.3V instead of 5V (e.g. MAX-3232, RS-2323)
  • some have the capacitors built in (but can be more expensive than adding separate capacitors)
  • Some have more transmit and receive ports (e.g. MAX3241, MAX3237)
  • some are power-saving variants

See also:

Some more comparison between RS-232, RS-422, RS-485

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Almost all converters between these serial variants (and also to USB and other things) exist. Conversion between the serial buses usually just deals with the electronics (like voltage differences) and is otherwise transparent, in that it adds no buffering and negligible change in timing.

Differential signaling uses two wires in a way that makes them much less susceptible to noise (noise that is consistent on both lines is electrically subtracted, not perfectly, but it works well), which means you can transmit further, with fewer errors, and/or faster (speed capabilities mostly matter on lines more than a few meters long. On short-enough wires everything can be fast).

When you interconnect more than two devices, it is often handy to do so with a single bus, regardless of whether it's a one-to-many, or an (often half-duplex) anyone-can-speak setup.

Details related to termination can vary.

When you convert, you do often inherit some conventions.

RS-232 stuff is typically a single-device query-response where parties can send at any time whatsoever. The fact that RS-485 can have many devices on the same bus means there is often some mechanism to deal cleanly, e.g. have identifiers they can be queried by, both to tell devices apart, and having only one speak via some easy convention (e.g. an initial "are you here, x?" scan of all addresses, then querying all known devices).

You may need a RTS-to-speak setup, or the converter may detect when you're talking. Depending on details, the RS232 device may effectively listen to itself, so you may need to ignore that, and/or write your RS-232 code in a half-duplex sort of way. (verify)

On Ground

Even for differential signalling you may want a ground line as well. You don't need it for the signal - that's fine because the voltage difference between the two data wires is what matters, and well controlled.

However, you also need to consider whether the voltage difference between the devices is different. The spec basically says their difference should stay within -7V and +12V.

If it's more, communication will stop working, and and it may damage the electronics. (though frankly, if it's large enough for the latter, you probably need to look at why in more detail)

Adding a ground line effectively limits the amount of (common mode) voltage that the input terminals will see.

You're fine without ground if both ends will never vary much. In some cases, you can design it that way. In some cases it's implicit, e.g. when all parts are powered from the same thing. (You may even avoid some ground loop problems if you don't connect ground.(verify))

See also:

Flow control

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

When the receiving side cannot guarantee it consumes the received data fast enough, flow control is preferable, arguably necessary.

The problem comes when the receiving end cannot guarantee (or just practically doesn't) that incoming data is moved out of the receiver's small buffer fast enough. Once that hardware buffer is full, there is no choice but to throw away data.

Strictly speaking a guarantee to do this is hard, particularly between devices of different speeds and designs, and for high speed transfers.

Still, a number of applications don't continuously stream data, so may rarely or just not see trouble.

(RS232) Hardware flow control refers to using the CTS/RTS lines to signal readiness to receive.


  • efficient mechanism
  • out of band; no implications to the data stream itself


  • You need extra wires. Only practical in short-length connections
  • impossible or just very impractical for transmission in other media (e.g. POTS modems, wireless tranmission of serial signals)

Software flow control refers to using some codes within the data stream to stop and restart flow. These are usually named XON and XOFF:

  • XOFF, 19 decimal, 0x13 hex, and also known as DC3 (also Ctrl-S in some terminals; see Shells_and_terminals#Shell_flow_control)
  • XON, 17 decimal 0x11 hex, and also known as DC1 (also Ctrl-Q in some terminals)

These ask the other end to stop sending, and start sending again.


  • You don't need extra wires


  • You can't send data that contains these vales. You need to encode/escape around that, using some extra logic on both ends, using some protocol that both ends agree on.
  • noisy lines may mean the characters are missed

See also:

Parallel ports

microcontroller programming

ISP (In-System Programming)

Refers (somewhat generally) to the ability to program a chip while it is in its circuit, (rather than having to be programmed beforehand, or taken out to program).

At a lower level, ISP usually means some serial protocol, and that the chip can itself generate the programming voltage it needs (from its typical supply voltage).

So it often means

  • can update firmware
  • you don't need an expensive hardware programmer (nice for the DIY crowd)
  • less time consuming testing/development cycle

See also JTAG, which can regularly also do firmware updates, lus a few debug-like things.


ICSP (In Circuit Serial Programming)

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Used by PIC microcontrollers, AVRs, and Parallax Propellers.

Has a line for programming voltage (voltage varies; may be between 5V and 13.5V), so not quite the same as #ISP_.28In-System_Programming.29, though similar enough in a "you connect it and it programs" way.

Typically (actively) uses five pins.

Associated with various different plugs, typically 0.1" pitch pin headers, though smaller (and larger) variants exist.

Note that you can't go by plug size alone - some have multiple possible pinouts. To avoid damage and confusion, always check the pinout.

See also:


See also:


LVP (Low Voltage Programming)

See also

Network over power (Homeplug, other names)

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

(not to be confused with Power over Ethernet (PoE))

Often marketed as a cable reducer, a way to connect rooms or floors without having to pull wires and drill holes. The systems should probably only be used when wiring is prohibitively hard, as cables are simpler, can be faster / upgraded more easily, and there are potentially fewer problems.

You could also use it to run a longish extension cord (say, to a shed) and avoid running (and protecting) a long network cord beside it.

The devices usually come in pairs.

The medium is shared (most underlying technologies do frequency multiplexing) so more devices mean less speed, and there's a maximum to the amount of distinct channels/devices.

Speed will also degrade with medium noise.

Once it works it can work well, but it may be a little unpredictable to set up, and don't count on always getting the advertised speed.


You need to be on the same local (phase of your) transformer for operation at all. Most houses are on a single transformer (in most of the world; in the US houses may be wired in two different phases in different legs of their house).

It's likely that you'll see some connectivity, but in multiple-leg buildings (including larger buildings such as apartment complexes, stores, and such) you should probably not do any larger investments without doing some tests first. In some cases you're better off spending a few more hours adding and beautifying/hiding a wired connection.

You may run into other problems, such as getting little or no communication through power conditioning devices (may be central in a large building), even a power strip that does radio-frequency or general electromagnetic interference filtering (often extra features on those fancy semi-nonsense surge protection strips), or interference from certain devices (like, apparently, some light dimmers).

On speed

Long and/or noisy connections are likely to fall back to lower speeds. Speeds vary with design; there is no single standard, although there seem to be relatively few underlying chips.

Theoretically there seems little reason you can't the quoted maximum speed, but you can get lower speeds because of on aspects of your electrical wiring that you can often neither predict or change.

Your house and the locations you plug these devices into may work out as better or worse than average, but in general you probably shouldn't expect more than 80 megabits, and in some bad cases you may get little over ~5-10mbit/s.

The devices are marketed at speeds like 14MBps, 85MBps, 200Mbps, even gigabit, but note that these are often maximum/ideal figures (as with wireless networking), not fixed figures (as with most wired networking). (Also, a few figures are marketing lies. There have apparently been lawsuits over this)

Quotes on range are a little harder to find. Quotes on what speed you get at different distances are even rarer (how does speed degrade?(verify)).

Apparently relatively ideal conditions allows for on the order of 200 meters, sometimes more, sometimes less. Note that this is meters of wire, which is more than distance between rooms. Still, this is enough to cover many houses, make floor-to-floor connections, and (and technically more than the length of a single twisted-pair (Cat5e/Cat6) wire.