attiny85 analog read problem

Hello all,
I am having some troubles about reading the correct value in analog 1 or the P2 of the tiny.
Do i have to performe some program of fuses in order to work?
I am using also the SoftwareSerial in order to debug the sistem and to work i have burn the fuses to be 8Mhz internal clock.
Here is the code:

Code: Select all | TOGGLE FULL SIZE
#include <SoftwareSerial.h>

SoftwareSerial TinySerial(3, 4); // RX, TX

void setup() {
pinMode(A1,INPUT); //analog in batt
pinMode(0,OUTPUT); //PWM1
pinMode(1,OUTPUT); //PWM2
pinMode(3,INPUT); //RX
pinMode(4,OUTPUT); //TX
TinySerial.println(“Serial Connected”);

void loop() {

int Vbatt = 0;
Vbatt = analogRead(A1);

Any help is very much apreciate.

A couple quick questions if you please.

  1. Are you uploading through IDE?

  2. Did you get AtTiny core for IDE if using IDE so something like "AtTiny85 @ 8Mhz (Internal Oscillator, BOD Disabled)" appears under the board menu?

  3. Did you do "Burn Bootloader" after selecting the AtTiny85 from board menu (this will set the fuses)?

  4. If the answer to all of the above is "yes", what results are you getting, nothing at all, incorrect values, etc?

Also maybe try commenting out the following two lines, they are not required as they are being defined by SoftwareSerial TinySerial(3, 4); , perhaps they are interfering with the serial communication.

pinMode(3,INPUT); //RX
pinMode(4,OUTPUT); //TX

void setup()

  pinMode(A1,INPUT); //analog in batt

void loop()
int vbat=analogRead(A1);
Serial.print("vbat value:");Serial.println(vbat);


just upload your code & test it.

Hello BH72,

I have all yes ;D . and get incorrect values most of the times looks like erratic numbers.
But most of the time it as 2 or 3 and when conected to 3,3v the same value but if i connect it to the 5V rail it presents 1023.
Tried to comment as you sugest but did not work to.
Another important clue i am connecting the tiny to 3,3V...


hello AMPS-N,

have tried the code you sugested but did not work have an error: Serial not declare in this scope.
It as to be used the serial software.
Also change the delay in my code but did not perform any good results...


must be Serial.begin(9600);

Hello Gonnie08

The attiny does not have communication pins so we have to use the serial software and chose the pins 3 and 4 for communications.
the serial as the arduino uno use is not valide here.

"But most of the time it as 2 or 3 and when conected to 3,3v the same value but if i connect it to the 5V rail it presents 1023."

What is the voltage of the battery you are trying to measure? Or more specifically what is the voltage present at A1 (for example are you using a voltage divider, etc). If the voltage presented at A1 is greater than the reference voltage (in this case Vcc = 3.3) then it will exceed the measurement range.

Also it is important that whatever you are trying to measure be connected to the same common ground as the tiny, so for example from what you have said I can assume you have a 3.3v rail and a 5v rail if both are being used in the circuit their grounds should be connected, additionally if you are trying to measure an outside source (in your example it appears that you are trying to monitor the voltage on a battery) it as well will need to be connected to the same ground.

Things get a little more complicated if you are trying to measure the voltage of the battery that is powering the circuit because at all times battery voltage will equal reference voltage and in turn produce a measurement of 1023. That is of course unless the circuit is being powered by a higher voltage battery and you are using voltage regulators, and a voltage divider to present voltage at A1.

A couple other things I will mention just because I am unaware of your setup. I have attempted to use generic usb to serial adapters to read serial from an arduino with no success, the values were always incorrect regardless of baud rate. The only adapter that functions correctly that I have is the FTDI adapter that I also use to upload to the arduino. Not sure what you are using so that is why I mention this.

And finally, and this is just speculation on my part. Serial communication requires fairly accurate timing to function properly, the specifications for the internal oscillator calls for a minimum accuracy of not more than 10% error, it could be that the internal oscillator has enough error to cause garbage in serial communication, if you have an crystal or resonator to test with it may be worth trying it out to see if anything changes.

You can't measure 5v when the Arduino is powered from 3.3v. In fact this could fry the chip - the datasheet specifically says that inputs can't be higher than the supply voltage.

If you have a >3.3v battery and the Arduino is powered from a 3.3v regulator, you need two resistors to make a voltage divider and bring the voltage down to a level it can measure.


I am trying to supply the chip using a li-ion battery directly connected to the chip and it works.
It just control the pwm level of 2 outputs.
The ideia is to implement a voltage meter for the battery in case it is lower than 3V it will go to a diferent mode of a PWM output.
Because i could not implement that i have decided to use the serial software as a serial debuger.
But this came to be more dificult than i was expecting, even that i have passed and now the serial works fine.
The problem is just that voltage measure that does not work.
To implement this i use the arduino duemilanove to load the code to tiny and supply it. to debug the serial i use an ftdi 3,3V usb serial interface this:
and it also works.
The main problem now is a way to measure the voltage in pin A1. all grounds are common as i am using the arduino as voltage supplier.

hello all,

I decided to try the internal vcc read and it work pretty well i do not need a very accurate measure so this is ok.
Using the subroutine that i found on this link:

long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(AVR_ATmega32U4) || defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (AVR_ATtiny24) || defined(AVR_ATtiny44) || defined(AVR_ATtiny84)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (AVR_ATtiny25) || defined(AVR_ATtiny45) || defined(AVR_ATtiny85)
ADMUX = _BV(MUX3) | _BV(MUX2);
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);

delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring

uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both

long result = (high<<8) | low;

result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.110231000
return result; // Vcc in millivolts

Thanks all for your help