I want to use the hardware serial port (pin 0 RX and 1 TX) to connect a MAX485 IC. Internally these pins are also connected to the onboard USB to serial converter microcontroller which is used to flash the ATMega328 using the bootstrap code.
I know I can't have two different serial devices (USB and MAX485 in that case) at the same time without interfering each other. Given that I want to use the HardwareSerial and not a solution based on SoftwareSerial I have added two jumpers on my design that let me connect and disconnect the MAX485 to the RX pin 0 and TX pin 1 if I want to upload a new sketch.
I have two questions:
- Is there an elegant way to reduce the two jumpers TX and RX to just one that when shorted connects or desconects both pins at the same time (max RX and TX to pin 0 and 1 on the Arduino)
- Is it possible to use a transistor software controlled by an arduino pin to have the same efect and let me choose when I want the max485 connected and when I want to fallback to the arduino USB to ttl?
The idea is that if the device is reseted by the computer to upload a new sketch the USB device will be active, once the device has booted as part of the boot process will enable the MAX device and the built in USB will be disabled as the MAX IC will have preference.
Best Answer
There is no need to add extra active circuitry for this.
The MAX485 has a Receive Enable (
/RE
) pin. You can connect this to a GPIO with a pull-up resistor, so that it will be disabled when that pin is unconfigured during bootloader operation. Once your application program starts up, it can configure the enable pin as an output and drive it low to enable the receive side of the MAX485, which will overpower the series resistor in the ATmega16u2 path.Since an RS845 bus normally needs explicit transmit enable, you presumably already have the MAX485 transmit enable (DE) wired to a GPIO. Just keep in mind that unlike
/RE
,DE
is active high, so to make it disabled by default, you'd need a pull-down resistor.