I have two identical I2C devices that I need to use with a single MCU (arduino nano). I have read here on EE that an I2C multiplexer can be used to switch between multiple I2C buses. But I find them too expensive. A single pca9540 multiplexer costs at least $5, which is too much. (A complete 40pin pic MCU would costs less...) My idea is to use a digital output pin of the mcu to switch between two different scl lines. Would that work? Is it possible to do it with a few transistors? (2n2222 or BC547 etc.) That would keep costs down.
Here is the transistor solution. You need to make sure SEL_1 and SEL_2 are not high at the same time. You need to make sure that one of them is high all the time (because the pullups are on the other side of the FET). If you are not confident of managing that in software, maybe use an inverter. The input to the inverter will be a GPIO, and will also be SEL_1. The output of the inverter will be SEL_2.
I2C_DA is the I2C data line from the processor. SEL_1 is a GPIO. When high, it selects slave 1. This means that slave 1 will be able to see data sent by the bus master. When SEL_1 is low, slave 1 will only see the data line being high, so it will never respond.
SEL2 is a different GPIO. When SEL_2 is high, it selects slave 2. You should avoid having them both be high. It will not create contention (open collector data line) but if you send out a read on the slave address, and both selects are high, you will get the type of bus collision you are trying to avoid. And you might not realize it right away. The data would not appear obviously corrupted.
Assuming you're in a single-master situation, using an IC like the TS12A12511, a single SPDT switch would let you do this with one extra I/O pin:
MCU_GPIO controls which one you talk too -- I assume you don't need a 'float / no-connect mode'. The on-state resistance of this switch is ~5 ohms, which is virtually nothing for I2C. I assume that your I2C bus runs at the MCU voltage, so you should tie that to the V+ pin on the switch. The part can slew rail-to-rail, so there should be no problems there.
I would then also place my I2C pull-up resistors on both SLAVE_0_SCL and SLAVE_1_SCL -- this prevents floating input pins at the slaves which can cause undesired behavior, which would be the case if you left the SCL pull-up on the 'MCU_SCL' side of the switch.
I don't know what your quantities are, but this drops to about $1 at 100 pieces.