Problem With Arduino IDE Serial Monitor

Hello all,

I've been having issues with the serial monitor while trying to display values of analog and digital pins. It's not a specific code that is giving me an issue, but anytime I run a digital or analog read command and use Serial.print(variable) at any baud rate, the serial monitor just displays "15" or "16". It does not display the actual value or state of the pin that I am trying to read.

Hardware: generic Uno R3 board, usb connection to computer
Software: Arduino IDE 1.8.13

Anybody know of a fix for this problem?

Post your code, let’s see what you have.
Use the </> code tag button on the menu.

This is a test code that I used to confirm that the problem was not related to anything specific on other projects:

int temperature = (analogRead, A2);

void setup() {
  // put your setup code here, to run once:

  


  pinMode(A2, INPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println(temperature);

}

Thanks for the reply

kyleh11030:
int temperature = (analogRead, A2);

This code is the equivalent of:

int temperature = A2;

A2 is a variable set to 16 when compiling for the Uno

This is not the correct way to use analogRead(). Please study the reference page for this function to learn how to use it:
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

After that, if you still have questions, we can definitely help you out.

All you need for that simple example is
Serial.print (analogRead (A2));

or
temperature = analogRead(A2);
Serial.println (temperature); // ln for line feed

It does not display the actual value or state of the pin that I am trying to read.

You are reading the analogue value only once then printing the same value repeatedly. Read the value immediately before printing it

Thank you all for the help

So here's what I've got now:

The value of the pin is now being read constantly as 0 when I have the variable "temperature" declared before loop and setup. Is this because the program is only reading the value of the A1 pin a single time and reprinting it? If so, what can I do to have the read value of the pin constantly updated and displayed on the serial monitor accordingly?

int temperature = analogRead(A1);

void setup() {
  // put your setup code here, to run once:

  


  pinMode(A1, INPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println(temperature);

}

I'm not convinced the pin is ever getting read in that configuration, honestly....

Okay, no I was too curious, I went ahead and compiled that and checked the assembly listing... the analogRead() is not happening at all. That line might as well be int temperature = 0.

I am, frankly, stunned that that doesn't even generate a compiler warning... (but it doesn't, even with all warnings enabled!)... you can't call a function like that in the declaration of a global unless it's the constructor for a class. analogRead() isn't. You need to call analogRead() inside setup() or loop() or a function called by those (though, we know it's not being called before main() (which, behind the scenes, calls init() to initialize the on-chip peripherals, and then calls setup() followed by loop() repeatedly, which is what some people above seem to imagine it is doing; if you called analogRead() before init(), it would sit there spinning forever, waiting for the result ready bit to be set in the ADC status register.... whilest the ADC has not yet been turned on)...

But yeah, move the temperature=analogRead(A1) into loop; you can still declare int temperature globally, though depending on where you want to go from here you may not need to.

Thank you all for your input! (no pun intended)

Moving the declaration and the function into loop did the trick.

Thanks everyone!