Here is the super simple program I'm trying to use:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write('A');
delay(100); // wait for a second
}
When I upload the code to my bare Arduino Mega 2560, Everything works perfectly and I can see the "TX" LED flash.
However, If I unplug and re-plug-in the cable, the "TX" LED doesn't flash and no serial data is written to TX0. Pressing the reset button doesn't help either.
Even wierder, if now I open the Arduino Serial Monitor, then the "TX" LED starts flashing and I can see the data on the serial monitor.
I Have had the same problem with 4 Arduino Mega 2560 Boards, using different cables, and on 2 computers (Windows Vista running v1.0.1 of the arduino IDE and Windows 8 running v1.0.5 of the IDE).
I didn't have this problem before and all other programs work fine (except for the serial part).
I have no idea what is going on. I hope someone can help me.
Yes, but when I press the reset button, the board is also reset but I get different results:
-The serial monitor's reset (or other actions) resets the board and makes the serial part of my program work.
-The button reset resets the board but the serial part of the program remains unchanged (If it was working it still works and if it wasn't then it still doesn't)
When I upload the program, It works perfectly: Every 1/2 second, the pin 13 LED toggles on and off and the "TX" LED flashes. Opening the serial monitor shows that the characters "A" and "B" keep getting received.
Then, After unplugging and plugging the board back in, The program works fine except for the serial communication part:
Every 1/2 second the Pin 13 LED toggles on and off but the "TX" LED always stays off.
Pressing the reset button changes nothing, same as unplugging and plugging the board back in.
However, if I turn on the Arduino IDE's serial monitor, then the program works perfectly (Every 1/2 second, the pin 13 LED toggles on and off and the "TX" LED flashes and characters "A" and "B" are received).
After closing the serial monitor and/or pressing the reset button, the program still works perfectly.
Removing power from the board seems to "mess up" the serial part of my program and the Arduino IDE's serial monitor seems to "fix" it. I have no idea whats going on. Again, I have tried this on 4 Arduino Mega 2560 boards and with 2 windows PCs and 2 versions of the Arduino IDE all with exactly the same result.
I don't understand what the problem is. What do you expect to have happen when you plug the Arduino back in? What does happen?
Plugging the Arduino in to the USB port simply provides power to the Arduino. It does not cause the serial port on the PC to be opened. The RX LED on the Arduino only blinks when the separate USB to Serial chip is able to (and requested to) send serial data. Since nothing is opening the serial port (resetting the Arduino) when you plug the Arduino in, I wouldn't expect the RX LED to blink.
Opening the Serial Monitor does open the serial port, and resets the Arduino. When the Arduino boots up again, and opens its end of the serial port, the USB to serial chip can communicate, and so it blinks the RX LED when it sends a byte.
The only problem that I see is that you have unrealistic expectations. Only you can fix that.
After I upload the code to my Arduino Mega 2560, everything works perfectly and I can see the "TX" LED flash (The Arduino board is sending serial data through the TX pin every 100ms which causes the "TX" LED on the board to flash).
Now I unplug the board and plug it back in.
Normally, the "TX" LED should start flashing again just like it did after I first uploaded the program (because the program is still in the board and after booting up, it should be executed).
However, this does not happen. Instead, the "TX" LED doesn't flash: the board isn't sending any serial data despite the program having "Serial.write('A');" in the "loop()" part of the program.
That is my problem. It means I can't use serial read/write unless the board stays plugged into the computer after program upload.
That is my problem. It means I can't use serial read/write unless the board stays plugged into the computer after program upload.
What IS the point in doing Serial.print() or Serial.write() if nothing is listening? When something IS listening, that something is supposed to reset the Arduino on opening the serial port, so that the behavior of the Arduino changes (to actually sending data to the port).
I just tried it again but this time with an XBee pro connected to my board (with a shield).
The XBee should be the thing that is "listening" to the arduino's serial port 0 right?
It gives me the same problem: The "TX" LED doesn't flash and the board doesn't output serial data (after unplugging and replugging-in the board).
The TX LED is not driven by the ATMega2560 chip. It is driven by the USB to Serial chip, which is NOT involved in the ATMega2560 chip talking to the XBee.
and the board doesn't output serial data
It IS. It is sending data to the pins 0 and 1 and to the USB to serial chip. The USB to serial chip discards the data unless there is something listening to the serial port.
You start with the mega conected to the PC and the monitor open - the lights flash and you see something in the monitor.
You unplug the mega. The PC removes the COM port as there is no connection. Then the 16U2 chip on your mega does not flash the lights when it gets data from the big chip as it can't send on the disconnected USB port. (Even though data is still being sent from the big chip to the little chip. Checking that the program is still running was why I had you flash the led as well as send data).
The monitor is not connected to the COM port as it nolonger exists.
You plug it back in but the PC does automatically connect the monitor to the com port so you see nothing in the monitor.
You close and reopen the monitor which resets the mega and we are back where we started with flashing lights and data in the monitor.
The TX LED is not driven by the ATMega2560 chip. It is driven by the USB to Serial chip, which is NOT involved in the ATMega2560 chip talking to the XBee.
After seeing that I uploaded my original programs to 2 arduino boards and they successfully communicated via XBee.
I made a mistake assuming that the "TX" and "RX" LEDs' inactivity meant that the TX and RX pins were not working.
Thank you so much for your help PaulS and holmes4!