Sometimes I need to switch digital signals between microcontrollers or devices. For example I have a serial device and I want to control to which microcontroller it is connected per digital pin. So must of the times I used a relay. But relays are too big and need high currents. Are there small ICs for digital signal switching or multiplexing. For example for Serial, I2C, SPI or USB? Or could I use a normal transistor for this job?
What you want is called a multiplexer. There are stand-alone chips that do that, and some microcontrollers have them built in. For example, that is basically what the peripheral pin select feature of many of the newer PIC micros is. It allows routing output signals from the built-in peripherals to different pins on the fly. It also includes the reverse, which is taking the input for a peripheral from a selected pin on the fly.
In the case of something like a UART output signal that is idle in the high state, you can use individual OR gates per destination. When one input of the OR is high, the output will be high regardless of the other input. You wire the UART output to one input of all the OR gates. The other inputs are the individual active-low enables for that destination.