To print a string and an integer on Arduino, call Serial.print() for each value, or use Serial.println() for the final value to end the line. For example, Serial.print("Count: "); Serial.println(count); writes the label and integer to the Serial Monitor without needing to convert the integer yourself.
The Basic Printing Pattern
The Arduino Serial object provides text output over a board’s serial connection. Its print() and println() functions accept strings and numeric values, including integers. A string is text in quotation marks, such as "Temperature: "; an integer is a whole-number variable such as int count = 7;.
Use print() when the next output should continue on the same line. Use println() when the output should be followed by a line break. For instance, the following calls produce one readable line:
Serial.print("Count: ");
Serial.println(count);
If count is 7, the Serial Monitor displays Count: 7, then starts a new line for the next output. Calling Serial.print(count) by itself displays just the number. The quoted label and the variable are separate arguments in separate calls; the label is not treated as part of the integer.
This pattern is useful for checking sensor readings, tracking program state, and showing messages while a sketch runs. The same basic idea applies to other values: print a label, print the value, then choose whether the last call should end the line.
A Complete Serial Example
This short sketch sets up serial communication, declares a string and an integer, and prints both. Upload it to a compatible Arduino board, then open the Serial Monitor in the Arduino IDE.
const int count = 7;
String message = "Items counted: ";
void setup() {
Serial.begin(9600);
Serial.print(message);
Serial.println(count);
}
void loop() {
}
Serial.begin(9600) starts serial communication at 9600 baud. The Serial Monitor must use the same baud rate to display the output correctly. The sketch prints once in setup(), which runs when the board starts. The empty loop() then repeats without producing more text.
For a changing value, put the print calls in loop() or in the part of the sketch where the value changes. If you print on every pass through loop() with no delay or condition, output can arrive rapidly and become difficult to read. A timed or event-based print is usually more useful when debugging.
How Serial.print() Handles Types
Serial.print() is part of Arduino’s print interface, which has overloads for common data types. That is why a string and an integer can be passed directly to it. A String object is printed as its text, while an integer is converted to its ordinary decimal representation by default. You generally do not need to turn an integer into a string first.
Arduino code distinguishes between String, with a capital S, and the lowercase C-style string. A String is an Arduino class object that can hold text. A C-style string is a character array ending in a null character, often declared as char label[] = "Count: ";. Both can be printed, but they are stored and managed differently. For a basic print statement, either form can be passed to Serial.print().
An int stores a whole number, not text. Its available range depends on the board architecture: for example, an Uno based on an AVR ATmega328P uses a 16-bit int, while many 32-bit Arduino-compatible boards use a 32-bit int. If the number could exceed the range supported by the target board, choose an appropriate wider type such as long or an unsigned type and check that type’s range for the board. Printing does not prevent overflow that already happened during calculation.
Printing Numbers in Other Bases
By default, printing an integer shows it in base 10, the familiar decimal system. The optional second argument can request another base. This is helpful when inspecting bit masks, addresses, or values represented in hexadecimal. For example:
int value = 26;
Serial.println(value); // Decimal: 26
Serial.println(value, HEX); // Hexadecimal: 1A
Serial.println(value, BIN); // Binary: 11010
The base argument changes how the number is represented; it does not change the value stored in value. The output also does not automatically include a prefix such as 0x before hexadecimal digits. Add a separate string if you want a label or prefix, such as Serial.print("0x"); Serial.println(value, HEX);.
Be aware that the base option is for integer formatting. When printing a floating-point value, the second argument is used to specify the number of digits after the decimal point, not a numeric base. That distinction matters when adapting an example that prints an int to one that prints a measurement declared as float.
Putting Text and Numbers Together
The clearest and often most efficient way to combine text and a number for serial output is to make multiple calls. For example, use Serial.print("Analog reading: "); followed by Serial.println(sensorValue);. The first call leaves the cursor on the same line, and the second displays the value and ends the line.
You can add more fields in sequence. This example prints a label, an integer, a separator, and another integer:
Serial.print("Sensor ");
Serial.print(sensorNumber);
Serial.print(" = ");
Serial.println(sensorValue);
Separate calls avoid constructing a temporary combined string and make it obvious which parts are text and which are numbers. This can be especially useful on small boards with limited memory. If you do use the Arduino String class to assemble a message, make sure the operands are converted or concatenated in a way the compiler accepts, and avoid repeatedly creating many temporary strings in a memory-constrained sketch.
A common misconception is that Serial.print("Value: ", number); prints the label and then the number. It does not: the second argument to that call is interpreted as a formatting option for the first argument, not as a second value to append. Use two calls, as above, or deliberately build a String before printing it.
Serial Monitor Setup and Line Endings
In the Arduino IDE, start the Serial Monitor after uploading the sketch and select the baud rate used in Serial.begin(). In Arduino IDE 2, the Serial Monitor is available from the toolbar or the Tools menu. The exact layout can vary with the IDE version, but the required setting is the monitor’s baud-rate selector. A mismatch commonly produces unreadable characters or makes output appear unreliable.
println() adds a line ending after the printed value. The monitor may offer choices for how text entered from the monitor is sent to the board, such as a newline or carriage return; that input setting is distinct from the line ending produced by the sketch. If a receiving device expects a particular line-ending convention, check that device’s requirements instead of assuming every receiver treats line endings identically.
On boards that use a USB-to-serial connection, opening the monitor can reset the board, so a message printed only once in setup() may appear before the monitor is ready. If the first line seems to be missing, reopen the monitor or temporarily add a short delay after Serial.begin() while diagnosing the issue. Do not add delays blindly in a timing-sensitive application.
Common Printing Problems
- No output: Confirm the sketch was uploaded to the intended board, the Serial Monitor is open, and the baud rates match. Also check that the print call is in code that actually runs.
- Garbled characters: A baud-rate mismatch is a common cause. Check both
Serial.begin(...)and the monitor selector, and verify that the board and computer are communicating through the expected port. - Numbers run together:
print()does not add a separator or line break. Add a space, comma, or other label between values, or useprintln()at the end of a record. - A value looks wrong: Check its declared type, range, and calculation. An
intcan overflow on some boards if the result exceeds its range; printing the result simply reveals the stored value. - Compilation error involving String: Check capitalization and spelling, and confirm that quotation marks are paired. If a message is being built with operators, simplify it into separate print calls to isolate the error.
Excessive serial output can also interfere with a sketch’s timing. Sending characters takes time, and a slow receiving connection can cause output to queue. For a sensor loop, print at a controlled interval or only when a value changes, rather than printing on every pass. This keeps logs readable and reduces the chance that debugging output alters the behavior you are trying to observe.
Printing to an LCD or Other Display
Serial.print() sends output through the serial interface; it does not place text on an LCD. A character display using the Arduino LiquidCrystal library commonly uses lcd.print() instead, after the display has been initialized. The same general pattern applies: print a text label, then pass the integer value to the display’s print function. Other display libraries may use different object names and setup steps, so follow the library’s documentation.
On an LCD, unlike the Serial Monitor, output may remain on the screen rather than scrolling to a new line. If a later value has fewer digits than the earlier one, leftover characters can remain visible. You may need to overwrite the old characters with spaces, position the cursor, or clear the display at an appropriate time. Clearing on every loop iteration can cause visible flicker, so update only the relevant area when possible.
Choosing a Simple, Reliable Approach
For debugging or a basic status message, print the label and number with successive calls. Choose println() for the last item when each record should occupy its own line, and choose print() when building a line in pieces. Use numeric formatting only when you have a reason to show a base other than decimal. Use a display-specific library function when the destination is an LCD rather than the Serial Monitor.
For projects that run on memory-limited AVR boards, repeated dynamic String concatenation can contribute to heap fragmentation over time, depending on how the sketch allocates and releases memory. That is not a reason to avoid every String object in every project, but it is a good reason to prefer direct print calls for simple diagnostic output and to test long-running sketches. A fixed character buffer is another option when a formatted message must be assembled with tighter control over memory.
Frequently Asked Questions
How do I print an int on Arduino?
Call Serial.print(myInt); to display it on the current line, or Serial.println(myInt); to add a line ending afterward. Start serial communication with Serial.begin() in your sketch and select the same baud rate in the Serial Monitor.
Can Serial.print() print a String and an int?
Yes. The print functions accept both text and integer values. Print them in consecutive calls, such as Serial.print("Count: "); Serial.println(count);. The integer is formatted as decimal by default.
What is the difference between print() and println()?print() writes its value without ending the line, so the next output continues immediately after it. println() writes its value and then adds a line ending, making the next output start on a new line.
Why does my Arduino Serial Monitor show strange symbols?
The most frequent cause is that the baud rate selected in the Serial Monitor does not match the value passed to Serial.begin(). Check that they match, then confirm the selected board and port if the text is still incorrect.
How do I print an integer in hexadecimal?
Pass HEX as the second argument: Serial.println(value, HEX);. This changes the displayed representation, not the integer itself, and does not add a 0x prefix automatically.
Related
- JavaScript .split(): How It Works and When to Use It
- C++ String Erase: How std::string::erase Works
- How to Split an Object in Onshape: A Step-by-Step Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Duplicate a Sheet in Excel: Keyboard Shortcut and StepsIn Excel for Microsoft 365 on Windows, the quickest way to duplicate a worksheet is to hold Ctrl,…
- How to Combine Columns in Excel Without Losing DataTo combine two columns in Excel without removing their original contents, put the result in a new, empty…
- Conditional Formatting Based on Another CellYes. In Excel for Microsoft 365, you can make a cell or range change format based on another…
- How to Write Superscript in Excel: Shortcut and StepsTo write superscript in Excel for Microsoft 365 on Windows, select the character or text you want to…
- Excel vs Google Sheets: Features and DifferencesExcel and Google Sheets both handle everyday spreadsheet work, including formulas, charts, sorting, and pivot tables. Excel generally…
- How to Insert a Page Break in Excel for Better PrintingTo insert a manual page break in Excel for Microsoft 365 on Windows, select the row below or…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.