Problems with analogread()

Hi,

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

When I upload the following code to mcu ;

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 ?

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...

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.

const int analogpin = 0;

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

void loop() 
{
    Serial.println(analogRead(analogpin));
    delay(100);
}

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?

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 ?

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.

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.

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

No, but you need to put what "analogRead" returns into something bigger than a byte.

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?

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

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

I used ;

word val = 0

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

Yes - see my calculation above.