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