Tutorial :Arduino's analogRead() returns junk when I print via serial



Question:

I'm making a clock with the Arduino, and I want to make a button to set the time. So, just to test, I pluged a wire in the Analog Input pin 0 and wrote two lines of code to read the pin and print it via Serial, but all I get is junk.

  valm = analogRead(0);    Serial.println(valm);  

And what I get from the serial monitor is this: ?j?d?±µ?Ê??jDd?±µ???ºjRd?±µ???ºj?d?±µ?Ê??j?d?±µ?É?ªjRd?±µ???ÊjRd?±µ???Âj?d?±µ????j?d?±µ?É??j Rd?±µ????j?d?±µ???ºj?d?±µ?É??jRd?±µ????j?d?±µ?Ê?ÊjDd?±µ???Âj?d?±µ????j?d?±µ?É??jRd?±µ????j?d ?±µ?É?ÂjRd?±µ???Êj?d?±µ???ªj?d?±µ?Ê?Êj$d?±µ???ÂjRd?±µ????j?d?±µ????jdd?±µ????jRd?±µ???Âj?d?± µ?É?ÊjRd?±µ????j?d?±µ?Ã'?Êj?d?±µ?É?ÂjRd?±µ????j?d?±µ????j?d?±µ?É??jRd?±µ???Âj?d?±µ???Êj?d?±µ? ??ªj?d?±µ???Êj?d?±µ???ÂjRd?±µ????j?d?±µ????j?d?±µ?É??jRd?±µ???Âj?d?±µ?Ã'?Êj?d?±µ?É?ªj?d?±µ?É? ?jTd?±µ???Âj?d?±µ????j?d?±µ?É??jRd?±µ????j?d?±µ?É?ÂjRd?±µ???Êj?d?±µ?É?ªjRd?±µ???Êj?d?±µ???ºj ?d?±µ?È??jTd?±µ????jRd?±µ???ªj?d?±µ?È?ÂjTd?±µ?É?ÊjRd?±µ???ªj?d?±µ???Êj?d?±µ???ºj?d?±µ?É??jRd ?±µ???Êj?d?±µ???ªj?d?±µ?É?ÂjPd?±µ???Êj?d?±µ???ªj?d?±µ?Â?Êj?d?±µ?É?ºjRd?±µ????j?d?±µ?É??jRd?± µ???ªj?d?±µ?Ê?Âj?d?±µ???ÊjRd?±µ???ªj?d?±µ?Ã'?Êj?d?±µ???ºjRd?±µ????j?d?±µ???Êj?d?±µ?Ê?ªjdd?±µ? ??ÂjRd?±µ???Âj?d?±µ?É?ªjPd?±µ???Êj?d?±µ????j?d?±µ?É??jRd?±µ???Êj?d?±µ?É?ªjdd?±µ???ÂjRd?±µ??? Âj?d?±µ?É?ªjDd?±µ???Êj?d?±µ?Ê??j?d?±µ?É??jRd?±µ???ÊjRd?±µ???ªj?d?±µ?Ê?ÂjPd?±µ???Âj?d?±µ???ªj ?d?±µ?É?Êjdd?±µ?É??jRd?±µ????j?d?±µ?É?Êj$d?±µ???ªjRd?±µ???Âj?d?±µ?Â?ÂjTd?±µ???ªj?d?±µ???Êj?d ?±µ????jRd?±µ????j?d?±µ?É?ÊjTd?±µ???ªjRd?±µ???Âj?d?±µ???Âj?d?±µ?É?ªjRd?±µ???ÊjRd?±µ?Ã'??jTd?± µ????j?d?±µ???Êj

Why?


Edit:

I had set the digital pin 0 to output, and I thought maybe that was the problem so I went to the Arduino website and found the page Analog Input Pins.

So I tried to read from pin 14 which they say is the analog input pin 0, but I got the same output.

By the way, I'm trying to read from the analog only pins, not the one wich are also digital.


Solution:1

Try running this sample code found on the Arduino website. You shouldn't need to convert the variable valm or anything.

/*    Analog input        Reads an analog input on analog in 0, and prints the value out.        Created 24 March 2006      by Tom Igoe  */    int analogValue = 0;    // Variable to hold the analog value.    void setup() {      // open the serial port at 9600 bit/s:      Serial.begin(9600);  }    void loop() {      // Read the analog input on pin 0:      analogValue = analogRead(0);        // Print it out in many formats:      Serial.println(analogValue);       // print as an ASCII-encoded decimal      Serial.println(analogValue, DEC);  // print as an ASCII-encoded decimal      Serial.println(analogValue, HEX);  // print as an ASCII-encoded hexadecimal      Serial.println(analogValue, OCT);  // print as an ASCII-encoded octal      Serial.println(analogValue, BIN);  // print as an ASCII-encoded binary      Serial.println(analogValue, BYTE); // print as a raw byte value        // Delay 10 milliseconds before the next reading:      delay(10);  }  

If your output is still gibberish, there's something wrong with your serial terminal.


Solution:2

The problem Here is analog device you might connected are not @ same ground potentials. Example: If your powering on Arduino board using PC and sensor is powered on using other source , But You trying to read value from Arduino port, for this instant it gives above error. Try this power on arduino and sensor from Same power source and try to read data using Serial Port through DOcklight you will get Problem solved.


Solution:3

You need to set the baud rate in the serial monitor window (bottom right corner) to the same value that has been set in your code (e.g. Serial.begin(9600);).


Solution:4

From what your output looks like, it seems like you need to convert valm into a string, so that you can print the value numerically.

Also, is the wire you plugged into analog in floating? Because if it is, it'll just act like an antenna and grab random noise (though, might have a strong 60Hz component).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »