Serial only works after upload

For some reason, when I upload this sketch:

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Hello, world!");
  delay(1000);
}

to my Uno, the serial out (pin 1) only works after I upload it. If I hit reset or remove the power and repower it, it doesn't work. It also works if I open the serial monitor in the IDE and close it, but that's not very convenient.

Since it starts after I open the serial monitor, I'd guess it's not starting because there's nothing capturing the output. But even when I have my iPhone connected and run the sketch, the serial output doesn't work.

But that shouldn't be the case. I have Serial.begin(9600); there, so the sketch should be sending data regardless if something's using it or not.

Does anyone know why the Arduino doesn't like working here?

There could be a difference between the sketch not running and just not getting serial output. Try flashing a led on and off every second and see if that is going even when you don't get serial output.

Which Arduino is it? How do you know the serial output isn't working?

OP said it was an Uno in his post.

The sketch will indeed run, sending on the serial lines when it is repowered or reset.

After upload, the TxData LED starts flashing, indicating that serial transmit is happening.

If you don’t open a Serial Monitor or a terminal program, or any program that takes data from the serial port, the TxD LED will eventually stop flashing. What happened? Well, nothing was taking the data over the USB port, The TxD LED is controlled by the USB<–>Serial chip, and once the serial buffer is full, the LED comes on and stays on.

If uou unplug and replug the Arduino, the TxD LED will not come on. This does not mean that the AVR on the board is not transmitting; only that the LED is not being turned on by the USB<–>serial adapter, because there is nothing on the PC connecting to the USB the Arduino is on.

Were you to connect the TxD line to the RxD line of another Arduino, you would see that it’s still transmitting.

Here’s a little experiment to help convince you that it works.

Attach a resistor (I used 100 ohms) and an LED in series between TxD (pin 1) and +5V. Plug tyou Arduino in. You’ll see that even though the board’s TxD LED does not flash, the LED you attached does flash, because it IS sending data.

Now change your sketch to send a longer string, say 50 characters or so, and make the delay() 500. Open the Serial Monitor, and observe the LEDs. They will both flash in uison, and the string will appear on the Serial Monitor. Exit the monitor and let the Arduino keep running. Soon, the Arduino’s LED will stay on, indicatind a full buffer, but the LED you attached will keep flashing. Why? Because the Serial Monitor WAS attached, and communicated with the Arduino, and when you detached Serial Monitor, the Arduino stopped having its Serial Buffer emptied.

Remember, USB is NOT the same as Serial. it requires handshaking to take data out of the buffer.

I’ve tried your experiments lar3ry, and I guess you’re right. But earlier, when I tried receiving data on my iPhone, with the same sketch as the first post, while the TX LED wasn’t blinking, I wasn’t getting any data. It’s very possible I did something wrong, though.

I’d try it again, but first I need to figure out how to get a working voltage divider between the Arduino TX and iPhone RX. Because right now the iPhone is taking in too many volts and resets whenever I unplug the homemade Arduino<>iPhone adapter, which probably doesn’t signify a good thing.

If you or anyone else happens to know anything about voltage dividers, don’t hesitate to post on another thread of mine here:

Thanks for the explanation. :slight_smile:

I'm not sure I'd want to make a simple voltage divider for that job. I'd probably go for the voltage level converter from Sparkfun, mentioned in the thread you linked to. It's only about 2 bucks, and it would be a lot safer for an electronic novice.

Well I actually bought that logic level converter, but haven't used it yet since it seems like the 12 holes for wires/pins aren't really metal... but I guess they are. Look like matte white plastic to me though. :P

Yes, they will most certainly be metal, and will take solder.

OK then. They don't look like no metal I ever saw!

I think I’ve got one of those, and I agree they don’t look metal, but you can solder to them OK.

I'm actually soldering it right this second. 3 more pins!

EDIT: Done! Will test it soon. I am still very much a newbie when it comes to soldering, so it was pretty difficult with how small it was. On two pins, somehow I got the solder to envelop itself around the pin pretty much perfectly, as if a machine did it. Most of the others look ugly, but I don't think I damaged the board at all, or connected any of the components by mistake.