Sharing HardwareSerial with XBee in Arduino

When using ATMega328 (i.e. Arduino Uno) and XBee-Arduino library, this is design to share HardwareSerial (pins 0 and 1).
If you want to upload new firmware to arduino using USB cable you have to unplug XBee.
I'm working in a project I want to upload new firmware to Arduino without unplugging XBee.
Does anyone have done something similar??

You could use the softwareSerial-library
http://arduino.cc/hu/Reference/SoftwareSerial
With this library you could use other pins for serial communication and also connect multiple Xbees if you need.
But I suppose your "Xbee Arduino lib" mentioned above, doesn't work with this software connection.

I have used SoftwareSerial in other project, with successed, but there are two reasons I need to use hardware serial here.

1st. XBee-Arduino library is designed to use HardwareSerial. This library use API UART mode for XBee to build more sophisticated applications, that I need for this project.

2nd. HardwareSerial support asynchronous communications, needed when MPU is busy in other tasks, as 8-bit MPU are not design to run multiple threads. As I can understand, Hardware serial use hard shift-registers as a way of buffering tx and rx, while SoftwareSerial are not real asynchronous as they use RAM to buffer. A busy MPU could drive to data lose if softserial is used.

So, what I'm looking for is the way to switch Arduino Uno UART to USB when I try to upload new arduino firmware and switch Arduino Uno UART to XBee when USB is not connected.
Anyone have a clue?

Sounds like you will need some external hardware but the trouble with that is you don't really have access to the pins to insert some logic.

What about using a Mega with more hardware UARTs?

If you are using an Arduino with a DIP processor you can make a small board to switch the signals. You could also remove the 1k protection resistors and bodge up some hardware in their place.


Rob

I think the reason why you can't upload sketches along the Xbee is connected, is that the Xbee's TX line is high (in idle state). So any other data connected on this bus interferes with it and gets lost. (please tell me if I'm wrong)
I am thinking of a diode that blocks any high signal coming out of the Xbee TX-line (making an open-collector-output) and pulling it high with a pullup. So if data arrives along the other lines, it shouldn't interfere with the idle state of the Xbee (as long as the Xbee doesn't send anything during uploading).

I'm not shure if that works, feel free to correct me

I'm thinking more about a 3-state buffer to enable or disable XBee line. But I wanted to know if anyone have experience with it, if someone have faced the problem and delays add by buffers are not relevants.

A buffer won't affect the signal because of delays. You could use 2 74xx125s and two 74xx126s. Just off the top of my head maybe something like this

When the chip is reset for programming the IO pin goes tri-state and the resistor enables U1 and U2. After programming the first thing you do is set the IO pin high to enable U3 and U4 and therefore the XBEE.

Technically you don't need buffers on Tx but it's cleaner and stops the XBEE from trying to decipher all the programming traffic.

It needs a spare IO pin though and I don't know how you would insert U1 and U2 into the signal path unless you have a DIP mega328.


Rob

Great! I think this is the solution. Just notice that if you want USB port for debugging, U1 and U2 are not needed. Now I need a 74X125 to test and share results, but thanks to Rob :wink:

I'm working in a project I want to upload new firmware to Arduino without unplugging XBee.

Have you considered uploading new firmware via the ICSP interface? This approach would also free up some flash memory (bootloader area) if you need it.

Don

Sure! But this is not user friendly :wink: I'm thinking about firmware update application for end-user.

Xbee is also 3.3V interface, yes? So you need 3.3V level conversion also.
Look at TXB0104 for the interface

You can leave U1 and U2 in, just swap the IO pin level depending on whether you want to talk to the PC or the XBEE. That way you aren't spewing XBEE crap up to the PC. Anyway you really still have to isolate the FTDIs TX output.

CR is right about the level conversion, you could replace U3 and U4 with a TXB0104 (or TXB0102).


Rob

One question (Sorry if I interrupt your conversation)

Anyway you really still have to isolate the FTDIs TX output.

Why does he have to isolate it? Wouldn't it just work if he connects or disconnects only the Xbee's TX output (as he did it before by unplugging the whole Xbee)?

jcsaez:
So, what I'm looking for is the way to switch Arduino Uno UART to USB when I try to upload new arduino firmware and switch Arduino Uno UART to XBee when USB is not connected.

Well I didn't see this mentioned, but there is an interesting blog post I ran into (How to Add Multiple Serial Connections to your Custom Arduino Board or What Those 1k Resistors Do « The Mind of Bill Porter) that suggests using a pair of 1K resistors on the xmit/receive lines.

This is on my "to-do" list to experiment with but I haven't actually tried it so YMMV, but I thought it might help.

Brad (KF7FER)