12 ms·
How many Devices can you Connect to the I2C Bus?
- bambax 5y agoWow! Thank you HN! I have been trying to work with more than one time of flight sensor and an Arduino recently, and I fried them all (the sensors, not the Arduino). Will study this in detail now.
- analog31 5y agoThat's too bad, the Arduino was probably the cheaper part. I have a technique which I use when bringing up new circuits and breakout boards for the first time, which is to wire resistors in series with all of the i/o pins. This will usually be good enough to limit current flowing through output transistors and protection diodes, so each device can tolerate minor mistakes in wiring or coding, 5 versus 3.3 V disagreements, or an errant scope probe touching the wrong thing. You can use rules of thumb for RC time constants to work out resistor values, but typically 1k for output lines and 10k for input lines have served me well as starting points.
- magicalhippo 5y agoIf you actually managed to fry the components, then it's most likely either 5V vs 3.3V (or lower) mismatch or static electricity. Not all modules sold have voltage translators built-in. These two for example seem to, but be aware of it. https://www.aliexpress.com/item/4000163323280.html https://www.aliexpress.com/item/4000163323280.html https://www.aliexpress.com/item/32966164006.html https://www.aliexpress.com/item/32966164006.html
- pantalaimon 5y agoThose 8-bit 5V AVR controllers are true relics these days. Get yourself a modern 32 bit ARM Cortex-M chip, it will be cheaper and much more capable than those Atmel dinosaurs.
- hugs 5y agoGatekeeping comments like that are not helpful. There are many beginner-friendly tutorials for 8-bit AVR chips, and many projects don't need the extra features of a 32-bit ARM chip.
- pantalaimon 5y agoAre those really tutorials for AVR chips or tutorials for the Arduino ecosystem? Arduino is not limited to AVR, there is no need to put up with the limitations of those aging chips, unless you are accessing hardware registers directly you can probably run the same software on a STM32 or SAM D board.
- hugs 5y agoI was specifically thinking of the ATtiny85 when I made my comment. A beginner's first electronics project is often blinking an LED on and off. When I'm teaching a room full of kids electronics for the first time, the simplicity and low-cost nature of the ATtiny chips are hard to beat. I get it, ARM chips are great for real projects, but there is still a place for 8-bit AVR.
- regularfry 5y agoI've literally just received a delivery of through-hole ATTiny85s and a programmer, so I can dead-bug an I2C interface onto an analogue sensor. I have no idea where I'd start looking for an ARM in anything like the same form factor.
- nereye 5y agoThere are through-hole ARM ICs, e.g. see https://hackaday.com/2013/05/29/programming-a-through-hole-arm-microcontroller/ https://hackaday.com/2013/05/29/programming-a-through-hole-a.... For example: https://www.digikey.com/en/products/detail/nxp-usa-inc/LPC810M021FN8129/12102361 https://www.digikey.com/en/products/detail/nxp-usa-inc/LPC81....
- barbegal 5y agoIt's very easy to remove the pull up resistors from most break out boards. The most likely issue you will encounter with trying to connect many I2C devices is address collisions where devices have fixed addresses or at least some fixed bits in the address. This will probably limit you to about twenty or thirty useful devices on a bus.
- ashtonkem 5y agoYou can get around this with an I2C multiplexer[0]. It allows you to connect 8 devices to the multiplexer and then occupy one address on the main bud. 0 - https://www.adafruit.com/product/2717 https://www.adafruit.com/product/2717
- fh973 5y agoHow is the allocation of addresses done typically? Are they always hardcoded? Is the same chip then available with different addresses (different SKU)?
- aulin 5y agomost common practice I've seen at least with typical arduino/rpi sensors is fixed address for the same chip, sometimes you can change one or two bits so you can have two/four of the same kind in the same bus, sometimes you have address conflicts with similar devices from the same manufacturer (e.g. bosch bme680, bme280 and bmp180).
- pantalaimon 5y agoTypically addresses are only 7 bit (10 bit are possible but not all devices support that). So you either have way to set the address using resistors / address lines or you have some config more where you set the configuration for the sensors one by one (Cypress touch sensors had this)
- magicalhippo 5y ago> Is the same chip then available with different addresses This is sometimes done, though more commonly in my experience is there are some input pins which the chip uses to determine the actual address. This might be as simple as one to three inputs which should be connected to ground or supply voltage to encode the last bits of the address. Other chips are a bit more creative, and allows you to, in addition to ground and supply, tie the address pins to the I2C clock and data pins. This allows it to generate four combinations from per address pin.
- MrBuddyCasino 5y agoThis seems to only get into the electrical issues associated with connecting lots of devices to an I2C bus. Due to deficiencies in the protocol specifications and real-world implementation issues, there are logical problems as well. As a consequence, it is near impossible to make it 100% reliable. If you're interested in the unpleasant details, whitequark has posted about this at length on Twitter.
- rcxdude 5y agoeeyup, I2C is probably my least favorite protocol at this level. It's an absolute minefield of problems (often subtle and hard to reproduce), and I go out of my way to avoid it if at all possible.
- GeorgeTirebiter 5y agoHard to find any supporters of i2c these days. To be 100% compatible, sda and scl must be bi-directional. Complicates bus buffers; also complicates a bit-banging implementation. In the olden days, this was fairly easy by having an input on each pin, with the pull-down transistor per pin as shown in the article. The main drag about SPI is this Phase and Polarity. Being opinionated (!) I wish the SPI were were all CPHA=0 and CPOL=0 (bits clock in on the positive-going edge).
- SonOfKyuss 5y agoGood article and explanation on the real-world issues that can be encountered with I2C devices. I wish the author had gone into more detail on how to solve those issues by using discrete sensors instead of breakout boards.
- bambax 5y agoSensors not on breakout boards are very difficult to find (in small quantities) and even more difficult to use.
- pantalaimon 5y agoThose breakout boards are just for experimentation / prototyping. You typically design a proper PCB for the final project and of course you can also buy individual sensors.
- foldr 5y agoYou can easily order them from Digikey in unit quantities. E.g. here is an accelerometer that I've used before: https://www.digikey.co.uk/products/en?keywords=MMA8653FCR1 https://www.digikey.co.uk/products/en?keywords=MMA8653FCR1 Most I2C interface sensors require minimal external components (often just a bypass cap), so they're are quite easy to use without a breakout board. The one non-trivial thing that a breakout board might do for you is level shifting, if you're using a 5V microcontroller.
- StavrosK 5y agoHas anyone used both I2C and other protocols like PJON or CAN and can compare?
- pantalaimon 5y agoCAN is used for more robust communication at a longer distance, whereas I2C is typically used for inter-chip communication on the same PCB. Never heard of PJON.
- SAI_Peregrinus 5y agoI've used I2C and CAN. I2C is electrically far more fragile than CAN. CAN is robust against all sorts of electrical faults. While you might not be able to transmit data, you won't fry the controller, and once the fault is removed it comes back. I2C is often more tolerant of unexpected traffic on the bus. There are far too many ECUs that will put a vehicle into limp mode (limit speed drastically) if they detect unexpected CAN traffic, even traffic not directed to them. Protocol-wise I2C is significantly simpler to get right.
- StavrosK 5y agoThat's a very useful analysis, thanks!
- DoingIsLearning 5y agoTo add to this, I2C is more common between a controller and 'slaves' which are architecturally simpler, for instance single output sensors often have I2C as their comms interface. CAN is really meant for distributed embedded systems where you are having to implement full blown communication between equally complex nodes. CAN is indeed much more robust both at a physical layer with a differential line and at a transfer layer with all sorts of error detection and mitigation mechanisms.
- StavrosK 5y agoI see, thanks. I'm asking mostly from a model airplane perspective, where you have a receiver, GPS, mag, transmitter, etc. Right now they use UART, with only the mag using I2C, but CAN sounds like a better solution, especially given the amount of electrical noise you get from the motors/servos.
- aulin 5y agoAlways hated breakout board producers for this pull-up resistor thing, especially since they are often targeted at the educational market. Why would you solder fixed pull-ups guaranteed to work only when it's just you in the bus (and not even that as it's pretty common for uCs and SoCs to include their own pull ups)? Teach your users the simple calculation needed to add pull-ups themselves!
- sokoloff 5y agoIt's way easier to remove a pull-up resistor that's present and undesired than to add one that you don't have in your parts stash. And there's way more beginners/hobbyists who won't have one and won't understand why their board doesn't work (and think your board sucks) than people who will have their circuits not work for unknown reasons when they stick multiples together. By the time you're doing that, you're probably reading someone's tutorial on how they did it and monkey-see, monkey-do, or you're far enough along in your learning journey to know to desolder some of the pull-ups. Arduino "won" by making things dead-easy to get working for people who frankly didn't have a clue what they were doing. This is a good thing, IMO. If it works straight away, you learn something and are inclined to take the second step. If the first attempt starts with a theoretically better lesson on how to calculate, select, order, and solder pull up resistors, you're failing at user engagement and unboxing experience by more than than you're gaining in theory-learning.
- aulin 5y agoPartly agree, but it's not like you don't have to use resistors anywhere else with Arduino and the likes. Just to light up a LED you will need some and usually there are plenty included in starter kits. At least where I live there still are a couple of electronics retail shops where you can buy spare components. Also arguing that desoldering smd resistors is easier than finding a through hole one and plugging it into a breadboard or soldering into a prototype board is a bit of a stretch.
- scoutt 5y agoI've seen some other (often more serious) breakout producers to put solder jumpers in their boards to enable I2C pull-ups (disabled by default). That's pretty nice since you can enable the I2C pull-ups just when needed.
- syntaxing 5y agoInteresting, is the pullup resistor common for other protocols like SPI?
- jeff_ciesielski 5y agoI hate to give an 'it depends', but it depends. Typically, no. In SPI all lines are actively driven high or low rather than an open drain configuration. The bus master drives SCK (SPI clock), MOSI (master out, slave in) and CS (chip select), and the slave device(s) drive the MISO (master in, slave out) line(s). From an operating perspective, pullup/pulldown resistors are not required. Now having said that, in some cases it's considered appropriate to add weak pullup/pulldown resistors to the data lines to ensure that they're in the expected state on power up and to prevent glitches from putting slave devices into weird states.
- monocasa 5y agoThree wire SPI is pretty common where everyone's MOSI and MISO are connected together open drain style, and the master clocks out 1s to let the slave pull the data line down for read operations. You have clock out something from the master during 4 wire SPI read anyway so it doesn't change a lot most of the time.
- magicsmoke 5y agoThe pull-up resistor allows I2C to have multiple masters on the same bus. A master take control of the bus by pulling either sck or sda low (can't remember which) and if other masters detect a low voltage when it expects a high it knows the bus is currently taken. SPI only has one active master on the bus at a time, so there's no need for pullup resistors to implement this arbitration mechanism. SPI masters just drive their bus lines using push-pull outputs.
- el_isma 5y agoIt's even cooler than that: it has collision avoidance. To start a transfer you drive the clock low. If both masters drive the clock low at the same time. Then they start driving SDA with the address. If at any point in time, the address you are trying to put in the bus is not there, you stop. This could happen if you try to drive a 1 and the other master does a 0 (since it's open collector, the 0 will win), and you know you couldn't put your '1' in the bus, so you shut up and let the other master carry on.
- squarefoot 5y ago(about the number of slaves) > " ...and I'm sorry to say, the answer is way lower than 127" 127 is a really high number that nobody should encounter in real design situations. I2c was conceived for communications between chips on the same board; it is extremely unlikely that one could fit that many chips on a board, all talking i2c with each other on the same bus. I2c switch chips also can be used to split the bus in multiple parts so that only a few chips at a time are physically connected to the bus. Example: the PCA9548 from TI, NXP and possibly others. https://www.nxp.com/products/interfaces/ic-spi-serial-interface-devices/ic-multiplexers-switches/8-channel-ic-bus-switch-with-reset:PCA9548A https://www.nxp.com/products/interfaces/ic-spi-serial-interf...
- bagels 5y agoOne person's unlikely is another person's design requirement.
- airhead969 5y agoExactly. "Edge" cases. The arrogance of assumptions about unknown requirements and deployments without asking enough users/customers. Sometimes, there are obscure features and software but damn important (i.e., safety, banking, finance, government, military, healthcare, etc.), which is why apparent popularity is a nearly meaningless when deployment could be unknowingly ubiquitous. --- "No one will ever need that code, so let's remove it." "Aaaah, what did you do!? My SCADA products for a nuclear power plant no longer work!"
- baybal2 5y agoNot so uncommon use case is many identical IC devices, like sensors, or GPIO expanders on the same board.
- willis936 5y agoEven when cookies are generously handled they’re still a nuisance. At what point are they just banned outright?
- ramary 5y agoOof, yeah. Have spent so much time trying to get multiple devices to work reliably on the same bus. As more devices are added rise and fall time of the data line also becomes a thing. It can be really hard to get things working reliably and handle edge cases. Have also found different host MCUs are able to handle signal errors varyingly well. On some chips I’ve worked with the I2C peripheral won’t recover from an error on the line until the MCU is completely powered down and back up. This poses its challenges for overall system reliability. All this is to say I’m always impressed that SparkFun has managed to build a product line around plug and play I2C with their QWIIC stuff. I’ve never used it though, so not sure how well it holds up with multiple devices.
- pugworthy 5y agoQWIIC is pretty nice to use from the wiring perspective. Not just for the plug and play nature but also the fine gauge of the wire in the cables. It makes it a lot easier to create little one off devices without going the printed circuit board route. And with Adafruit also adopting the standard there are more options for devices.
- regularfry 5y agoI just wish they'd gone for something that was easier to crimp.
- mcshicks 5y agoI think the thing I've seen that's the hardest to detect is reflections on the falling edge of scl double clocking. It's been a long time since I've seen it maybe more modern controllers slew rate limit the falling edge. But definitely the i2c device at the end of a long cable is not a great idea.
- xondono 5y agoIf you are getting reflections at 400Kbps, you either have another problem or are way out of the standard. There’s a reason for things like RS422/485. The hardest bug I’ve seen on an I2C bus was an implementation that every now and then dropped the lines to GND for a small period (~50ms). All I2C devices were working correctly, but because of supply chain reasons we had to introduce a PN change that was on paper 100% compatible. The new PN was a SMBus device, and guess how SMBus signals a reset...
- mcshicks 5y agoReflections are a function of the edge rate, i.e. driver strength, not the clock rate. https://en.wikipedia.org/wiki/Signal_reflection https://en.wikipedia.org/wiki/Signal_reflection
- xondono 5y agoI know, but clock rate tends to be a good proxy, especially im cases like this. For a bus like I2C there’s basically two possibilities: 1. You are using a part with a clock rate that extends clock rate over the standard, which means you have very small pull ups on the lines because that is what the datasheet recommends for that speed (thus your rise time gets too short). 2. (And this one is way more likely) you are using I2C to communicate over a long cable.
- jeff_ciesielski 5y agoThis is a nice breakdown, however it is leaving out one interesting piece: Active slew rate controllers. In some cases where you need to exceed the typically allowed bus capacitance either due to a high number of attached devices, or over a long cable run (it happens...it sucks, but it happens), you can use a part like the LTC4311 which, rather than using resistors to passively pull the bus lines to a resting high state, detects the direction changes and actively assists in pulling the lines to their intended states. https://www.analog.com/media/en/technical-documentation/data-sheets/4311fa.pdf https://www.analog.com/media/en/technical-documentation/data...
- vipa123 5y agoExcellent recommendation, I'm going to test this out!
- axylone 5y agoI wish software packages had data sheets! It would be great to have a concise/standardized format with bullet points and a "typical applications" section on github landing pages.
- pbhjpbhj 5y agoBe the change [pull request] you want to see in the World! (I hate it when people say that!!)
- airhead969 5y agoWell, someone has to do the work. Asking someone else to do it seems kinda crappy if it's you who wants something for nothing.
- OJFord 5y agoI think GP means they hate the phrase 'be the change you want to see in the world' (as do I) but was begrudgingly applying it to pull requests.
- kirbypineapple 5y agoI took my first stab at designing and having my own PCB manufactured for a mechanical clock I'm trying to build. At the moment I'm currently stuck trying to debug the 7 x MCP23017 devices on my I2C bus. I picked up an oscilliscope and added a level converted between the RPi and my PCB, but I feel pretty lost as I'm a complete novice and have no formal background in electronics. This article provides a lot of useful information for someone like me! Online all you usually get are single MCP23017 implementations, nothing too complex.
- xondono 5y agoThe MCP23017 is a fun device. I remember designing a board with too much of them (>20 IIRC).
- jpm_sd 5y agoI2C is not a wired network standard! It's designed to be used between chips on the same PCB, typically with the same power supply rail (although level shifters are available) and the same ground reference plane. Yes, ground planes matter for best performance. Breakout boards are meant for a quick test. Then you're supposed to design a PCB with the devices you need all in one place. SparkFun spaghetti will only end in tears. The other thing that sucks about I2C is that you never know what hard-coded addresses you will be limited to, until you've selected all your parts, dug into the datasheets and then discovered the inevitable collisions. In 20 years of PCB design I think the largest number of I2C devices I've had on one bus is maybe 30? And that was pushing it.
- dheera 5y agoIf you use multiplexers you can connect a lot more devices (upto 64*254 = 16256 I think) on a single bus. https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-mult... You could of course design your own multiplexer with more bytes of multiplexer address space and extend the concept indefinitely. I wouldn't recommend it, but it's possible.
- simcop2387 5y ago64*127 = 8128, normal i2c is limited to 7 bit addresses. There are extended i2c addressing setups but I believe that requires every chip involved to support them.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- toun 5y agoWhat sucks the most about I2C for me is the fact that the bus can get stuck if the master device resets during a transmission. In which case the slave device will keep the SDA line busy while waiting indefinitely for clock pulses. Yes, you can probably send dummy clock pulses to finish the frame and get SDA released, but that's just a pain to code properly. SPI is so much more reliable with just two extra pins, I usually don't even consider I2C anymore.
- airhead969 5y agoI have an "I2C" (no clock wire necessary, but it's present) bus with 512 WS2811 devices split evenly across a 1:4 fan-out power and signal integrity multiplexer. This is because I couldn't get 384 devices to maintain signal integrity (only about 340), likely due to too much load on the bus or reflections. 256 devices worked fine. Clocked buses, especially large parallel clocked buses, are generally a bad idea due to clock skew (clock and data signals getting farther out-of-sync with different wire length and loads.)
- Taniwha 5y agoWS2811 chips are not "I2C" - they're daisy-chained, active driven (no pullups) and regenerate the signal (and re-edit it) as they go My experience is that chip to chip signal integrity for them is not an issue - however power/ground noise is, especially if you try and pass the power over the string to the far end
- deleted 5y ago[deleted]
- m463 5y agoI think dallas 1-wire is a pretty interesting bus too. you can have many devices on it. I have lots of ds18b20 temperature sensors using pi + 1-wire. Most people splurge and use three wires though...
- xyzzy21 5y agoThis is similar to the very similar HPIB/GPIB/IEEE488. In theory you can have 32 addresses but in reality because of electrical limitations (even defined in the IEEE488 spec) you can only have about 24 devices.