analogRead problem

I'm running this 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.

What is connected to analog 3?

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.

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

Is the small voltage connected to analog pin 2 or digital pin 2...

Which core are you using?

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

What do you mean by core?

Here's the serial output.

Nice random number generators.

:slight_smile:

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

Check against your code, :slight_smile:

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

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

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

Which analog pin are you using?

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?

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. Google Code Archive - Long-term storage for Google Code Project Hosting.

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

Any ideas?

Any ideas?

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

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

How about 1 or A1?

I tried 1 and it gave me odd numbers again. A1 does the same thing.

zdillman:
I tried 1 and it gave me odd numbers again. A1 does the same thing.

Then perhaps the problem is an electrical one with the signal going to the analog input pin. What is it's output impedenace? Is there a common ground wired from the arduino to the circuit supplying the measurement signal?

One way to test your software is to remove your signal wire from the analog pin and first wire a ground to the analog input pin and see if your raw count value is a stable 0 value, then remove the ground wire and wire from the shield 5V pin to the analog input pin and see if your raw count value is a stable 1023 value.

Lefty

I'm guessing door number 2. Is it hard to test the other free pins in your setup?

-br

Edit: Nevermind. The data sheet confirms (p. 63) pin 7 / PB2 is analog input channel 1. But I would still test the other pins if it's quick… fortune favors those with a slight distrust for the documentation.