ATTiny816 ADC not reading 0 or VDD correctly

Thread Starter

robotDR

Joined Mar 17, 2020
90
I am using pin 3 from the data sheet. 47ohm resistors. VDD=3.3v

If I pull pin 3 down to gnd with 47ohm resistor, I get ~320.
If I pull pin 3 up to VDD with 47ohm, I get ~810
If I put a divider from VDD to gnd, pin 3 is output of divider (47ohm high and low dividers) I get 512 which I expect.

But why wont pulling down to gnd read 0, or up to VDD read 1023?

Thank you for reading.

Code:
Code:
#include <Wire.h>


#define pot  2     // LED1 for Configure command communication indication
#define batt_fb  3     // LED2 for READ command communication indication

#define MySerial Serial

// Constants
byte bright1 = 0x3C;
byte bright2 = 0x78;
byte bright3 = 0xFF;


// Variables
int battV = 0;
int battV1 = 0;
int battV2 = 0;
int battV3 = 0;
int battVavg = 0;


void setup() {

  Wire.begin();
  MySerial.begin(9600);
  MySerial.println("IS323FL3238 Driver");
  MySerial.println(bright1);


analogReference(VDD);  //Sets up ADC reference to VDD

pinMode(batt_fb,INPUT);

// ***Initialize I2C communications:
    byte error, address;                      // variable for scanning all addresses
  int nDevices;                             // number of devices on circuit

  MySerial.println("IS323FL3238 Control");
  MySerial.println("Scanning for device I2C address...");

  nDevices = 0;
  for (address = 1; address < 127; address++ )  // I2C Device Address Scanning
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      MySerial.print("I2C device found at address 0x");
      if (address < 16)
        MySerial.print("0");
      MySerial.print(address, HEX);
      MySerial.println("  !");

      nDevices++;
    }
    else if (error == 4)
    {
      MySerial.print("Unknown error at address 0x");
      if (address < 16)
        MySerial.print("0");
      MySerial.println(address, HEX);
    }
  }
  if (nDevices == 0)
    MySerial.println("No I2C devices found\n");
  else
    MySerial.println("done\n");

  delay(500);
 

}


void loop() {
 
    battV1 = analogRead(batt_fb);  //read batt_fb and store in battV
    delay(10);
    battV2 = analogRead(batt_fb);
    delay(10);
    battV3 = analogRead(batt_fb);
    delay(10);
    battVavg = battV1 + battV2 + battV3;
    delay(10);
    battV = battVavg/3;
    MySerial.println("battery voltage (V):");
    MySerial.println(battV);
 

Thread Starter

robotDR

Joined Mar 17, 2020
90
What voltage is on the pin?
I am sorry. I checked the pinout of the megatinycore from spencer and he changed the pins on me!
The voltage was what i expected. 3.3 high, 0 low, and 1.65 with equal divider.

weird thing was that i should have been using pin 4 from the data sheet for pin 2 on the core. however I was using pin 2 from the datasheet and calling pin 2 on the IDE. weird thing is that since my input was floating (calling 2 on IDE corrosponds to pin 4 from datasheet) my values were changing when i went high or low. Just not correct. What is up with that? Shouldn't it had just remained floating while I manipulated another pin?
 

WBahn

Joined Mar 31, 2012
29,976
I am sorry. I checked the pinout of the megatinycore from spencer and he changed the pins on me!
The voltage was what i expected. 3.3 high, 0 low, and 1.65 with equal divider.

weird thing was that i should have been using pin 4 from the data sheet for pin 2 on the core. however I was using pin 2 from the datasheet and calling pin 2 on the IDE. weird thing is that since my input was floating (calling 2 on IDE corrosponds to pin 4 from datasheet) my values were changing when i went high or low. Just not correct. What is up with that? Shouldn't it had just remained floating while I manipulated another pin?
Not following. Are you saying that the input pin was floating when you were trying to take the measurements? If so, pretty much all bets are off as far as what you will get. Since the results are going to be largely determined by the leakage currents on the pad, and since leakage currents tend to be sensitive to lots of things, including the state of other pads, it's not surprising that your values would change. What is perplexing is why you got a solid result when you put the voltage divider output on the other pin.
 

Thread Starter

robotDR

Joined Mar 17, 2020
90
Yeah, I was using pin 2 on the datasheet, and calling pin 2 on the IDE. However the core maps 'pin 2' to pin 4 on the chip package.

So it was floating while I was using pin 2. however I got solid 512 readings when I had an equal divider on pin 2. I got solid ~800 when I pulled pin 2 high, and solid ~300 when pin2 pulled to ground. all the while, pin 4 is floating and that is what the IDE core calls 'pin 2'. now that I am actually putting the signals (divider, vdd, gnd, etc) to pin 4 on the package, i am getting perfect results that match my dmm. I change the divider and I get perfect results.

Weird that the floating pin is locked down when other pins are used.
 

Papabravo

Joined Feb 24, 2006
21,158
The other thing to remember is that an ADC has a defined input impedance. When you use a resistor to pull that input up or down or somewhere in the middle, those resistors are acting in combination with the impedance of the ADC input and may not produce the results you expect. You would be better off using a unity gain buffer on the on the ADC inputs with resistor networks on the input side of the unity gain buffer.
 
Top