Is it possible to share the IO for I2C protocol with Serial protocol [ATTiny85]

Hi everyone!
I know nothing is impossible but sometimes it is not worth it to do it.
So what I'm trying to accomplish is: I have to connect and control 3 I2C devices[AM2320, DS3231] plus SSD1306 display and 1[MH-Z19B] with serial interface with ATTiny85. It is easy to use 2 lines for I2C and 2 lines for Serial, but i will lose 4 IO, and i have only 2 left for everything else.....
So far i succeed to read the CO2(MH-Z19B) sensor, the temperature/humidity sensor(AM2320), the RealTimeClock(DS3231) and displaying everything on the LCD(SSD1306) using ONLY 3 I/O from ATTiny85[1 for SDA 1 for SCL and 1 for Tx/Rx for the CO2 sensor], but a want to squeeze the ATTiny on the maximum. I'm trying to use the SDA-line as Tx/Rx on the same time....Does someone did something similar?

Thank You!

KrasiSoft:
Hi everyone!
I know nothing is impossible but sometimes it is not worth it to do it.
So what I’m trying to accomplish is: I have to connect and control 3 I2C devices[AM2320, DS3231] plus SSD1306 display and 1[MH-Z19B] with serial interface with ATTiny85. It is easy to use 2 lines for I2C and 2 lines for Serial, but i will lose 4 IO, and i have only 2 left for everything else…
So far i succeed to read the CO2(MH-Z19B) sensor, the temperature/humidity sensor(AM2320), the RealTimeClock(DS3231) and displaying everything on the LCD(SSD1306) using ONLY 3 I/O from ATTiny85[1 for SDA 1 for SCL and 1 for Tx/Rx for the CO2 sensor], but a want to squeeze the ATTiny on the maximum. I’m trying to use the SDA-line as Tx/Rx on the same time…Does someone did something similar?

Thank You!

the MH-Z19B baud rate is 9600 according to its datasheet.

so you could use software serial to communicate with it.

You could poll the sensors before displaying all their values to the LCD display.

That way so can use ss.begin(), ss.end to start/end software serial comms and do the same for you I2C comms.

that way, in theory, you should be able to use the same pins for I2C and Serial communication IMHO.

hope that help…

Page for the MH-Z19B CO2 sensor: MH-Z19B ndir CO2 sensor for indoor air quality monitoring--Winsen.

A falling edge of SDA is a START on the I2C bus, so you should keep the SDA high.
The SCL on the other hand can be used for other things, as long as the SDA is high. No sensor will react when there is a signal on the SCL as long as SDA is high.

When you use the I2C bus for I2C communication, then a serial device (which is using SCL) could see the SCL as data and it could reply, or the sensor could be set in a wrong mode. The CO2 sensor uses a checksum of one byte. It will be a big gamble to rely on the checksum. I do not recommend it, in these situations you have to stay out of trouble and not embrasing it :wink:

The only option I see is a seperate SDA and TX and the SCL and RX combined (a RX for the ATtiny, that is the TX of the sensor).
Then you use three pins, which you already have by combining RX and TX :confused:

Koepel:
Page for the MH-Z19B CO2 sensor: MH-Z19B ndir CO2 sensor for indoor air quality monitoring--Winsen.

A falling edge of SDA is a START on the I2C bus, so you should keep the SDA high.
The SCL on the other hand can be used for other things, as long as the SDA is high. No sensor will react when there is a signal on the SCL as long as SDA is high.

When you use the I2C bus for I2C communication, then a serial device (which is using SCL) could see the SCL as data and it could reply, or the sensor could be set in a wrong mode. The CO2 sensor uses a checksum of one byte. It will be a big gamble to rely on the checksum. I do not recommend it, in these situations you have to stay out of trouble and not embrasing it :wink:

The only option I see is a seperate SDA and TX and the SCL and RX combined (a RX for the ATtiny, that is the TX of the sensor).
Then you use three pins, which you already have by combining RX and TX :confused:

Thank You for the replay!
You are absolutely right. The main problem is: I'm trying to connect multiply devices on a serial bus[this is what happens actually when I use SCL as a part of the serial protocol]. And this would't be a problem if the sensor did not have this very, very weak 1-byte CRC checksum. The random bits on the SCL (when I2C is used) could reprogram the sensor or even destroy it....I think this can not be accomplish without of some sort of electronics to disconnect the CO2 sensor from the SCL while the I2C is being used. I will try few more tricks but i can not risk the sensor to be reprogrammed accidentally. If i can not find reliable solution - i will stick with the 3-wire interface and that's it.