Go Down

Topic: analogRead problem (Read 1 time) previous topic - next topic

zdillman

Nov 26, 2012, 01:26 am Last Edit: Nov 26, 2012, 01:53 am by zdillman Reason: 1
I'm running this code,
Code: [Select]
#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.

tuxduino

What is connected to analog 3?

zdillman

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: [Select]
#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.

Coding Badly


Coding Badly


Which core are you using?

zdillman

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

What do you mean by core?

dhenry

Quote
Here's the serial output.


Nice random number generators.

:)

dhenry

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


Check against your code, :)

zdillman

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

Coding Badly

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


[font=Courier New]void loop() {
 val = analogRead(  2  );
[/font]

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?

zdillman

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.

zdillman


dhenry

Quote
Any ideas?


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

zdillman

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
//                           +----+

dhenry


Go Up