Multiplexing Serial Port for Arduino and Microcontroller Project
Background Story :
A few years back I have a project related to the IoT data logging system. In that project, I used SIM800L for internet connectivity and RS480 bus used for communicating with the sensor nodes. I used an atmega328 microcontroller to develop the product. I faced one problem: I needed 3 serial ports for the whole project. The hardware serial port for debugging, one software serial port for sim800l communication, and one serial port for RS485 communication. In Arduino One Serial port consumes an average of 256 bytes of RAM lonely for buffer. So in total of 768 bytes of RAM are needed for all the 3 serial ports which are around 36% ram of atmega328. So I had to struggle a lot optimizing RAM for the rest of the modules like SD Card and software modules etc.
In my project when SIM800l communicates RS485 communication was stopped and vice-versa. We already know that we cannot connect more than one device to a particular serial port. The TX pin will work but not RX. So if I could multiplex one serial communication for SIM800L and RS485 communication I could easily save 256 bytes of RAM.
Here I will discuss how we can multiplex one Serial communication for controlling many serial devices using a simple digital IC. The IC model is 4053(CD4053, 74HC4053). It is a triple 2 channel analog multiplexer/demultiplexer IC. The advantage of analog multiplexer ic is it works exactly like an analog switch. So it can perform bidirectional communication.
Few common features of the IC are listed below
- Wide digital input voltage range. Digital 3-15V and analog 12Vp-p
- Low on-resistance 80Ohm typically and high off resistance
- Analog Multiplexer so behaves like an analog switch. Data communication can be performed bidirectionally
So the simplified schematic we can observe that the microcontroller Serial port is served between two serial devices such as SIM800L and RS485 device. For communicating with the SIM 800l module we need 3 pins. RX, TX, and another one for Reser pin. Similarly, for communication with the RS485 ic, we need 3 pins. RX, TX, and another pin for controlling whether the IC will work as TX mode or RX mode.
So in triple 2 channel analog multiplexer IC is fully utilized here. Selecting a channel is very easy.
- When A, B, and C pins are 0, the INH bit is low. The X, Y, Z pins will be connected with X0, Y0, and Z0
- When A, B, and C pins are 1 with INH bit is low, The X, Y, Z pins will be connected with X1, Y1, and Z1
So easy to select the channel. Right?