What exactly does print() do?

I was messing around with Serial.print and Serial.write, and I was curious about something. What exactly is going on across the TX pin when you use these commands?

I'm using an arduino Uno, so the serial commands should drive pins 0(RX) and 1(TX).

I was using this code to try and just send bits slowly over serial. I have an LED connected to it. It constantly stays lit, at a constant brightness. Even when TX should be outputting a 0. The same thing happens if I were to use Serial.write.

A multimeter reads a constant voltage of 5v from both pin 0 and pin 1. Shouldn't the LED flip between dim and bright during this sequence? If not, what exactly does this command do?

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  Serial.print(1,BIN);
  delay(2000);
  Serial.print(0,BIN);
  delay(2000);

  
}

The data is going out over that line WAY too fast for you to see it with an LED. Try an oscilloscope.

Delta_G:
The data is going out over that line WAY too fast for you to see it with an LED. Try an oscilloscope.

Are you saying the pins default to high when not transmitting?

I think that's right.

The Serial pins are RS-232. Google that, and you can learn more than you'll ever need to know.

Regards,
Ray L.

RS232 for me refers more to levels and control signals. Possibly better to refer to something about UARTs; e.g. a wiki page about Universal asynchronous receiver-transmitter.

Are you saying the pins default to high when not transmitting?

Yes, they do.

I was using this code to try and just send bits slowly over serial. I have an LED connected to it. It constantly stays lit, at a constant brightness. Even when TX should be outputting a 0.

It sounds like you are expecting the Tx pin to be LOW when printing a 0 and HIGH when printing a 1.

If that is the case what would you expect it to be when printing say 6, or any other digit ?

I actually wrote an instructable/demo once where I used specific character transmissions sent (continuously) on a PC serial port to PWM a motor... https://www.instructables.com/id/Serial-Controlled-Variable-Speed-Motor/
You could get a few brightness levels out of an LED that way too. But you DO have to send the output continuously. Otherwise (at 9600bps) you get a 1ms blip every 2 seconds...

Hi,
If you are using an Arduino controller with LEDs on it marked Rx and Tx, they are connected to the Rx and Tx pins.

If you watch then, you will see them blink when you upload a sketch.

As you are trying to send a 1 then a 0, the protocol wraps the bit, either 1 or 0, in a packet of bits.
The packet is defined by the baud rate, stop bit and parity.

When you send a single 1, your are sending 8 bits.
When you send a single 0, your are sending 8 bits,

Tom... :slight_smile:

TomGeorge:
Hi,
If you are using an Arduino controller with LEDs on it marked Rx and Tx, they are connected to the Rx and Tx pins.

You probably want to take another look at the schematics :wink:

Because 6 has the bit pattern of 0110, the TXD line will be LOW for 1-bit period, then HIGH for 1-bit period, then HIGH for 1-bit period, and then again LOW for 1-bit period. Does it make sense? Everything makes sense when we say that this is our protocol.

In accordance with the protocol of asynchronous serial communication, the digit 6 will be transmitted over the TXD line as:

a. Idle state (no transmission condition) of TXD line: HIGH

b. START Bit : LOW state of TXD line for 1-bit period. 1-bit period is about 1/9600 sec considering Bd = 9600.

c. Data Bits: 0011 0110 (0x36) ; It is an 8-bit ASCII code for the digit 6. The TXD line will be modulated by the bit pattern of the data item. Lowest significant bit (rightmost bit) will be transmitted first for the duration of 1-bit period, and then the remaining bits. The data bits will occupy 8-bit period (8/9600 sec).

d. One Parity Bit (optional): assume NIL

e. STOP Bit : 1 or 1 and 1/2 or 2; let us take 1. HIGH state of TXD line for 1-bit period.

** Frame Length : Total Bits = START + Data + STOP = 10-bit

Figure-1: Asynchronous wave form for the data frame of character A (TTL Logic)

@GolamMostafa: What is the point in starting off an example using the character six, and then illustrating it with a diagram of the character 'A' ?

If you like, I can modify the diagram; however, the variety has brought you in the scene!

AWOL:
You probably want to take another look at the schematics :wink:

Sorry you are correct, separate pins to drive LEDs.

Been long day, no coffee.

Tom.... :frowning:

The exact positions of TX and RX LEDs are depicted in the following diagram (extracted and compiled form the official schematics of UNO). They are, in fact, being driven by ASTTL < ----> ASUSB converter chip.

Because 6 has the bit pattern of 0110, the TXD line will be LOW for 1-bit period, then HIGH for 1-bit period, then HIGH for 1-bit period, and then again LOW for 1-bit period. Does it make sense?

This is incorrect. All bytes going out over the Serial line are 8 bits, not 4. So you skipped 4 0's there.

So, I think everyone here agrees that Serial.print sends out bytes of data, not just single bits. What then is the difference between serial.print and serial.write? Both say they send binary to the serial ports. Does write() output differently than print()?

Documentation says that serial.write() sends binary to the serial ports. How does that differ from using BIN as the format for serial.print()?
What would the difference be between these two?:
serial.write(1);
serial.print(1,BIN);

What would the difference be between these two?:
serial.write(1);
serial.print(1,BIN);

Serial write sends a single byte with the value 1.
Serial print will send a single byte with the value '1'.

Write send out the raw byte you put in. Print converts to ascii first.

So write 1 sends one byte with a value of 1. Print 1, BIN sends that out as ascii, so it sends out a byte with a value of 48 (ascii for 0) seven times and then a byte with the value 49 (ascii for 1).