Arduino Serial.print function

Hi all,

In my project I'm using a Uno to read from a navigation sensor over the I2C bus, and am printing the results in the serial monitor.

When I try the following:
Serial.print("The value of pitch is: ");

i see that every 5-6 samples the result displayed from the sensor in the serial monitor is grossly incorrect. If I remove the first line, i.e. "Serial.print(" ");", and instead only print out the reading from the sensor, it works perfectly all the time.

What is the problem with the text in quotations marks, and why this would impact the correct display of the reading from the sensor?

Your code is incomplete and therefore does not compile.

Sorry to state the obvious

Clearly what I wrote in the post is just an excerpt of the full code listing, with the code that is irrelevant to the problem left out of the post.

Or is there something wrong with the lines of code that I posted? i.e.

Serial.print("The value of pitch is: ");

Clearly, what I wrote was just an excerpt of an answer.

Equally clearly, there's nothing wrong with the print, (it wastes RAM, but, meh) per se, but in an unknown context, it may be a source of unintentional blocking.


Posting tips.

  • Always list the version of the IDE you are using and the board version if applicable.
  • How to insert an image into your post. ( Thanks Robin2 )
  • Add your sketch where applicable but please use CODE TAGS and avoid "snippets" ( </> )
  • Add a SCHEMATIC were needed even if it is hand drawn
  • Add working links to any specific hardware as needed (NOT links to similar items)
  • Guessing is not the best way to describe an issue. Actual facts are the most important.
  • Remember that the people trying to help cannot see your problem so give as much information as you can

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.


There does not appear to be anything wrong with the code you posted, implying the problem is with some other part of the code. One possibility is that you are low on dynamic memory, and the text from the print statement uses just enough additional memory to cause a variable to be overwritten. Try enclosing the quoted text in the F() macro.

Hi all, thanks for all the replies.

First of all apologies for lack of detail in the original post. I'm a newbie here so wasn't quite up to speed with the info that's required in a post. Didn't mean to sound like a smart ar5e either, just didn't think the remainder of the code was relevant so didn't want to waste anyone's time with posting it all.

Anyway I'm off to the mechanic now to find out why my tyre is causing the car to pull to the left

For what it's worth, the full code listing is provided at the bottom of this post.

When I compile it I get the following:
Sketch uses 5582 bytes (17%) of program storage space. Maximum is 32256 bytes.
Global variables use 450 bytes (21%) of dynamic memory, leaving 1598 bytes for local variables. Maximum is 2048 bytes.

It doesn't appear that lack of dynamic memory is the problem given that it only takes up 21%. However I still tried enclosing the quoted text in the F() macro as suggested by @david_2018 but alas it doesn't seem to resolve it.

The following is a small excerpt of the serial monitor when using this code:
Pitch: -1.06 Roll: -1.47 Yaw: 0.23
Pitch: -1.09 Roll: -1.40 Yaw: 0.22
Pitch: -123.51 Roll: -46.56 Yaw: -156.27
Pitch: -1.11 Roll: -1.45 Yaw: 0.17
Pitch: -0.98 Roll: -1.47 Yaw: 0.20
Pitch: -1.14 Roll: -1.46 Yaw: 0.19
Pitch: -1.14 Roll: -1.44 Yaw: 0.17
Pitch: -1.11 Roll: -1.40 Yaw: 0.18
Pitch: -123.51 Roll: -46.56 Yaw: -156.27
Pitch: -1.05 Roll: -1.40 Yaw: 0.21
Pitch: -1.06 Roll: -1.37 Yaw: 0.23

I know from a separate application that the correct readings are :
Pitch: -1.1 ish,
Roll: -1.45 ish, &
Yaw: 0.2.
You can see that occasionally a grossly incorrect reading of "Pitch: -123.51 Roll: -46.56 Yaw: -156.27" is provided.

Noting that it could also be an issue with the sensor, or an electrical problem, grounding, etc, but is there anything in the code that is a bit sus and could explain this issue?

#include <Wire.h>

int IMU_Address = 104; //decimal conversion of B1101000
int cal_int;

int8_t X0, X2, X4 = 0;
uint8_t X1, X3, X5 = 0;

byte gyro_mask = B00000000;

float gyro_pitch, gyro_roll, gyro_yaw = 0;
float gyro_pitch_cal, gyro_roll_cal, gyro_yaw_cal = 0;

#define GYRO_CONFIG 0x1B //Hex address of gyro config register
#define GYRO_XOUT_H 0x43 //Starting register of gyro readings

#define XG_OFFSET_H 0x13

void setup() {







void loop() {


gyro_pitch /= 131;
gyro_roll /= 131;
gyro_yaw /= 131;

Serial.print("Pitch: ");

Serial.print("Roll: ");

Serial.print("Yaw: ");


void read_gyro(){



Wire.requestFrom(IMU_Address, 6);

if(Wire.available() <= 6)
X0 =;
X1 =;

X2 =;
X3 =;

X4 =;
X5 =;

gyro_pitch = X0 << 8 | X1;
gyro_roll = X2 << 8 | X3;
gyro_yaw = X4 << 8 | X5;

 if(Wire.available() <= 6)
    X0 =;
    X1 =;

    X2 =;
    X3 =;

    X4 =;
    X5 =;

You are checking to see if there have been six or fewer bytes received into the Wire buffer, then reading six bytes regardless of how many bytes there actually are. Most likely your incorrect readings are coming from reading in the six bytes when no bytes have been received yet.

The only way I can think that the print statement would affect the output is when the Serial buffer is full, the code will delay at the print statement until there is sufficient space in the buffer to send the entire text before proceeding to the next line of code. At 9600 baud that can easily occur when you are printing text at rapid intervals.

Thanks so much David. I will give it a try tonight and let you know how it goes.