ATTiny85 only reading 0-255 on analogue in

Well... as the title suggests.

Running an ATtiny85 on 3.3v. Reading analogue pin A2 and it's reading 255 at 3.3v. I expected it to be 1023 or at least a lot nearer. Yes, the variable it reads to is an int (not a byte).

Seems a bit of a coincidence that its a lovely neat 255.

Any ideas?

Try posting the code and schematics.

1 Like

What core are you using?

Post your code and a schematic.

A2 is physical pin 3 using ATTinyCore.

ATtiny Microcontrollers board. ATtiny selected. It's the only one in my boards manager that has ATtiny85 listed.

It works as an analogue pin. It didn't for a while, as I had set pinMode to input and that seemed to stop it working as an analogue in.

Just downloaded the ATtinycore manager. Maybe that will fix it. Odd however

I recommend you try Attinycore. It will give you 10-bits ADC resolution for sure.

ATtinycore manager works the same....

Its scruffly test code!! ....

#include <TinyWireS.h>

#define dip1 4
#define dip2 1
int serialSW = A3;

int I2C_SLAVE_ADDRESS = 0x1C;                                                   // Default address
int serialswvalue;
byte serialdata;

void requestEvent()                                                            // We have been asked for information from the CPU
{
  if ((digitalRead(dip1) == false) and (digitalRead(dip2) == false)) {
    I2C_SLAVE_ADDRESS = 0x1C;
  }
  else if ((digitalRead(dip1) == true) and (digitalRead(dip2) == false)) {
    I2C_SLAVE_ADDRESS = 0x1D;
  }
  else if ((digitalRead(dip1) == false) and (digitalRead(dip2) == true)) {
    I2C_SLAVE_ADDRESS = 0x1E;
  }
  else if ((digitalRead(dip1) == true) and (digitalRead(dip2) == true)) {
    I2C_SLAVE_ADDRESS = 0x1F;
  }
  else {
    I2C_SLAVE_ADDRESS = 0x1C;
  }

  serialswvalue = analogRead(serialSW);                                      // 255 = +3.3v

  if (serialswvalue <= 30) {                                                 // Serial 3
    serialdata = 3;
  }
  if ((serialswvalue > 30) and (serialswvalue < 200)) {                      // Serial 6
    serialdata = 6;                                                          
  }
  if (serialswvalue >= 200) {                                                // Serial 7
    serialdata = 7;
  }

  TinyWireS.send(I2C_SLAVE_ADDRESS);                                         // Send the address we are using
  TinyWireS.send(serialdata);
  //TinyWireS.send(serialswvalue);
}


//---------------------------------------
void setup() {

  pinMode(dip1, INPUT);
  pinMode(dip2, INPUT);

  delay(1000);

  TinyWireS.begin(I2C_SLAVE_ADDRESS);                                        // Join the I2C network
  TinyWireS.onRequest(requestEvent);
}

//=======================================
void loop() {
  TinyWireS_stop_check();
  delay(1);
}
//=======================================

ATtinycore gives the same resolution. 0-255?

How can you tell with that code? It reports the same "7" for anything over 200.

1 Like

Like I said... demo code.

I you uncomment out the

TinyWireS.send(serialswvalue);

It sends the ADC value instead.

Just tested on an Attiny85 with Attinycore. When I do this:

void setup() {
  Serial.begin(19200);// prints on PB0 as TX
  pinMode(A2, INPUT_PULLUP);
}

void loop() {
  Serial.println(analogRead(A2));
  delay(500);
}

This is what I get in the serial monitor after connecting the RX line of a serial adapter to PB0 of the Attiny85

1023
1023
1023
1023
1023
1023
1023
1023
1023
1023
1023

Well I don't know then!

I think that your pinMode enter in conflict with the ADC reading. As for me, INPUT means I/O not Analog. If you want to do analog reading, just delete this line.

Yes, like I said... if I put a pinMode ( serialSW, INPUT) it doesn't work and only works as a digital switch and not analogue

On the same project....

When I request this information from the ATtiny85, I send:

Wire1.requestFrom(0x1C,2);

That asks for 2 bytes from the address 0xC1. All fine and works.

But, my I2C addresses are stored as a variable 0-127. These are stored in an int called address.

Fine if I am doing a comparison e.g: if ((address1 >= 0x1C) and (address1 <= 0x1F)) { blah }

But, I want to use that address in a later I2C command...

Wire1.requestFrom(address1,2);

This throws up an error:

Warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
Wire1.requestFrom(address1,2);

How do I write that correctly then?

OK. Fixed that error.

byte bytes_req = 2;

Wire1.requestFrom(address , bytes_req);

1.

Please, post the picture of your Attiny85 MCU Board. I use the following kinds of boards under ATtinyCore.
DigisparkAttiny85Board
Attiny85DevBoard
Figure-1:

2.

In post# 1, you are talking about ADC Channel-A2.

3.

In post #6, you are talking about ADC Channel-A3.

4.
Check carefully that the input signal is connected at Ch-A3 (Physical Pin 2 of Attiny85). Note that this pin (if using one of the Dev Board of Fig-1) is also connected at D- pin of the USB Connector.

Its not a board, its an 8 pin DIP. Bit messy I know, but it all works...

I program it with a USBASP on the 6 pin header below it

OK

Been reading this....

https://www.marcelpost.com/wiki/index.php/ATtiny85_ADC

Plus, I am running at 3.3v not 5v.

The analogue works, I just wondered why it was only 8 bit.

My my purposes its fine. I only has 3 inputs, 0v, +3.3v or in the middle (it's a 3 position switch)

I like your setup and working attitude as it resembles by profile. However, I would also expect that you own the following resources to get aid in troubleshooting of the hardware.
1. AVR programmer.
2. Universal ROM Programmer.
3. Digispark ATtiny85 Dev Board
4. Arduino UNO Board
5. Spare ATtiny85 Chips.

Is this sends more than a single byte?