Pages: [1] 2   Go Down
Author Topic: analogRead problem  (Read 979 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm running this code,
Code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0,1);

int val = 0;

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

void loop() {
 
  val = analogRead(2);
 
  mySerial.println(val);
 
  delay(3000);
 
}

on an ATTiny85 and I'm measuring the voltage through one of the analog inputs. I am using a bench DC power supply and it's staying at a constant voltage, but the serial print that it does when it reads a voltage is erratic, and inconsistent. I have tried averaging, as seen in the code, but it doesn't seem to help. Please take a look!


Here's the serial output.
736
724
860
712
751
856
696
790
838
687
822
816
673
850
797
686
868
765
706
870
736
728
861
708
756
851
691
791
833
682
824
815
674
852
790
686
869
756
708
867
725
735
860
704
762

Edit: Changed code to test code. Same situation.
« Last Edit: November 25, 2012, 07:53:05 pm by zdillman » Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is connected to analog 3?
Logged

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

A small votage under 5 volts, specifically 3.9v. I just wrote this code really quickly to just test the voltage and it's still giving me erratic numbers.

Code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0,1);

int val = 0;

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

void loop() {
 
  val = analogRead(2);
 
  mySerial.println(val);
 
  delay(3000);
 
}

Here's the serial output.
483
464
451
440
438
436
438
443
457
465
464
 It should be around 700, not around 400.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13030
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Is the small voltage connected to analog pin 2 or digital pin 2...
http://arduino.cc/forum/index.php/topic,87517.msg657039.html#msg657039
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13030
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Which core are you using?
Logged

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

I'm using Pin 2, (Analog input 1).

What do you mean by core?
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here's the serial output.

Nice random number generators.

smiley
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm using Pin 2, (Analog input 1).

Check against your code, smiley
Logged

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

In the revised test code I'm using the 2. I've also used 3 and 4 with the same results.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13030
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using Pin 2, (Analog input 1).

void loop() {
  val = analogRead(  2  );


Which analog pin are you using?


Quote
What do you mean by core?

You downloaded something to add support for the ATtiny85 processor to the Arduino IDE.  Where did you get the download?
Logged

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

I am using analog pin 1, but it is pin 2 in the pinout chart. I am using a core I found that is from here. http://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0022-0008.zip&can=2&q=

This added support for the Tone function, which I need once I can get the analogread to work properly.
Logged

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

Any ideas?
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any ideas?

Reading some documents on Aruidno and its functions would be greatly helpful to you now.
Logged

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

I've found the pin diagram in the core I am using. Could someone tell me what I should put in the parenthesis for analogRead() for analog input 1?

//
//                                +-\/-+
//  Ain0       (D  5)  PB5  1|    |8   VCC
//  Ain3       (D  3)  PB3  2|    |7   PB2  (D  2)  INT0  Ain1
//  Ain2       (D  4)  PB4  3|    |6   PB1  (D  1)        pwm1
//                        GND  4|    |5   PB0  (D  0)        pwm0
//                           +----+
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about 1 or A1?
Logged

Pages: [1] 2   Go Up
Jump to: