Pages: [1]   Go Down
Author Topic: Problems with analogread()  (Read 822 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25859
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
const int analogpin = 0;

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

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25859
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 20, 2011, 04:33:29 am by AWOL » Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25859
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used ;

word val = 0

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25859
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes - see my calculation above.
Logged

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

Pages: [1]   Go Up
Jump to: