I want to use a ATtiny85 to check the voltage of two 12 V batteries (later to control a relay). Therefore I'm using the analogRead() function. For programming I'm using an Arduino Mega 2560 board.

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

For A1 this works perfectly fine. But for A2 or A3 I'm getting always a value of 15 V (thus the analog input gives 1023 respectively it thinks there is 5 V on the pin, which is not the case). I tried A0 as well, but it's the same.

For seeing the current value I put a serial - USB converter on RX, TX on PB3, PB4. So I guess they cannot work properly as analog inputs then. But still A0 does not work and without serial my LED is always on without having a voltage over 12 V.

At the moment I don't know what to do about it. No matter which of the analog pins I choose, I'm getting always a wrong value (except A1).

I'm not a hundred percent sure about A0. On some diagrams there is an A0 pin, on some not. But I tried to remove all the serial communication code and still having the LED 0 on high.

Thanks in advance!

void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  digitalWrite(0, LOW); // led for displaying the voltage
  digitalWrite(1, LOW); // blinking status led
  int sensorValue1 = 0;
  int sensorValue2 = 0;

void loop() {
  int sensorValue1 = analogRead(A1);
  int sensorValue2 = analogRead(A0);
  // <- the problem, A2, A3 do not work as well
  float voltage1 = sensorValue1 * (5 / 1023.00) * 3;
  float voltage2 = sensorValue2 * (5 / 1023.00) * 3;
  if ((voltage1 > 12) || (voltage2 > 12)) {
    digitalWrite(0, HIGH);
  } else {
    digitalWrite(0, LOW);
  digitalWrite(1, HIGH);
  digitalWrite(1, LOW);

SOLUTION: As suggested by Andrei Dragan the problem was the serial communication, which uses just the same pins as the analog inputs A2 and A3. When turned off, the problem disappeared.

As the serial communication is quite useful for me, right now I'm still using the serial communication but only with the pin for sending data. The pin for receiving data is turned off. It looks like this:

#include <SoftwareSerial.h>

#define RX    -1   // *** turned off
#define TX    4   // *** D4, Pin 3

SoftwareSerial Serial1  (RX, TX);

(I don't know if this is the best way, but it works for me.)

Once again, thanks for the help!

  • 1
    A0 is the reset pin. Unless you change the fuses (which you shouldn't) you can't use that pin.
    – Gerben
    Jan 28, 2017 at 17:18
  • Thanks for your advice! So then I have to use A2 or A3 but both are not working, do you have any idea? Jan 28, 2017 at 17:21
  • > So then I have to use A2 or A3 but both are not working: that contains very little information to help others help you. It is likely working but you just don't know yet.
    – dannyf
    Jan 28, 2017 at 18:17

1 Answer 1


Any type of communication protocol uses pull-up resistors (not 100% sure that all of them do but still, it's a rule of thumb) so using the serial trick on the attiny enables the internal pull-up resistors on PB3 and PB4. I can bet the ADC value is near 1023 isn't it? Disabling the serial communication should in theory fix your problem.

  • 1
    I though I already tested it without. But it seems as I made some mistake. After disabling the serial communication it seems to work. Is there a way to change the serial pins to get the exact voltage via serial com? Thank you! :) Jan 28, 2017 at 18:15
  • You cannot get the ADC to work properly on the pins that the serial communication is using (that lines are going to be pulled up either by the attiny or the other thing you're trying to communicate with, in your case the usb-serial chip). In your current configuration you can't do much about it without losing some of the data you want: you can either measure both of the voltages at once with the help of 2 diodes or you can get rid of the serial communication. As an alternative you can use another chip (attiny84, which has 11 pins, 8 of which are ADCs) Jan 28, 2017 at 18:33
  • At the moment I'm not sure what the final setup will look like. Originally I have not planned to use serial communication, this was more for testing purposes just as the two LED's, but I will need at least one output for controlling the relay. But I think having the exact voltage can be quite useful. For now I have changed the serial configuration in that way so that I'm only using the TX pin (PB4) for sending the data. So I can read the exact results and using PB3 as A3 as sensor input. Everything is working now and I'm getting correct voltage values. Thank you again! :) Jan 28, 2017 at 19:26
  • 1
    Yo wrote “Any type of communication protocol uses pull-up resistors”. No. I2C does use pullups, but neither SPI nor asynchronous serial does. The OP's problem has nothing to do with pullups: it's the serial ports (of the ATtiny and the one on the other end) actively driving the line to Vcc. Jan 28, 2017 at 21:32
  • Take an SD card application, it's good practice to use pull-ups on the SPI line in this case because a floating line (in the order of milliseconds, until the SPI communication is initialized) can cause problems on the SD card, possibly injecting unwanted signals onto it. It's unlikely but at least one time in our life we've seen the "This media is corrupted, reformat it bla bla" message, that's where the pull-ups on the data lines come in handy, to help you avoid that. Jan 29, 2017 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.