Go Down

Topic: Problems with analogread() (Read 948 times) previous topic - next topic

y33t

Hi,

I am experimenting with Arduino Uno and trying different implementations of analogread() function.

When I upload the following code to mcu ;

Quote

int analogpin = 0;
byte val = 0;

void setup() {
 Serial.begin(9600);
}

void loop() {
    val = analogRead(analogpin);
    Serial.println(val,DEC);
    delay(100);
}



and connect from serial monitor I can see the voltage from pin 0. My concern is that it sends the read data 10 times a second(delay(100)) and when I try to upload another code Arduino interface returns error because the device is constantly busy. Any idea how to get over this ?

robtillaart


analogRead returns an int (= 2 bytes) that will not fit in one byte. This can corrupt your sketch especially the variables around val (e.g. analogpin).

Please post the error message you get when the upload fails...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

Quote
This can corrupt your sketch especially the variables around val (e.g. analogpin).

There should be no problem - the compiler will simply give you an implicit narrowing cast, and would give a warning, if they were not suppressed.

Code: [Select]
const int analogpin = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
    Serial.println(analogRead(analogpin));
    delay(100);
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
My concern is that it sends the read data 10 times a second(delay(100)) and when I try to upload another code Arduino interface returns error because the device is constantly busy.

To the Arduino, 10 times a second leaves an eternity between sends.

Of course, you could make the time between sends larger, by making the sending of data faster. 9600 baud is pretty slow.

What does your concern about sending so much data have to do with a problem with analogRead?

y33t

From serial monitor I read 8 bit values (0-255), when I connect analogpin to 3.3V on board it fluctuates around 172-174. I suppose this is because internal adc is not that good.

If analogread() returns 2bytes this corresponds to 16bit of information, it states that the mcu has max 10bit adc, how is this possible ?

AWOL

#5
Jul 20, 2011, 11:29 am Last Edit: Jul 20, 2011, 11:33 am by AWOL Reason: 1
Quote
If analogread() returns 2bytes this corresponds to 16bit of information, it states that the mcu has max 10bit adc, how is this possible ?

Because there is no ten bit native data type on the AVR microcontroller, the next largest size must be used to store the ten bit result.

Quote
when I connect analogpin to 3.3V on board it fluctuates around 172-174.

You're missing precision here - you're only taking the least significant eight bits

(3.3 / 5) * 1023 = 675 (or thereabouts), which is more than you can store in a single byte.
The 172 you see is the result of the truncation of the most significant bits.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

y33t

do I need to use other function then println() ?

AWOL

No, but you need to put what "analogRead" returns into something bigger than a byte.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

robtillaart

Quote
Any idea how to get over this ?

CLose the serial monitor when uploading sometimes helps as it unlocks the comport. What platform are you on ? MAc Windows LInux other?

Quote
do I need to use other function then println() ?

For what purpose?
The more you explain the better the answers will get ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

y33t

I used ;

word val = 0

and now 3.3v returns fluctations around 682 - 686. Is this normal ?

AWOL

Yes - see my calculation above.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up