 # Attiny85 analogRead always 1.3V when measuring capacitance

Hello, I rebuilt this example https://www.arduino.cc/en/Tutoria/CapacitanceMeter and added an old display(datasheet) to show the measured values.

Strangely, the value measured at A0 is always 1.3V. Via oscilloscope i could observe the typical charging curve of the capacitor and after measuring it, it shows >200k Ohms. Since it is in row with the chargingPins 10K resistor, A0 should measure 5V

As well via measuring I could see, that the chargingPin definately has 5V

Does someone have a hint or idea about this odd behaviour?

``````/*  RCTiming_capacitance_meter
*   Paul Badger 2008
*  Demonstrates use of RC time constants to measure the value of a capacitor
*
* Theory   A capcitor will charge, through a resistor, in one time constant, defined as T seconds where
*    TC = R * C
*
*    TC = time constant period in seconds
*    R = resistance in ohms
*    C = capacitance in farads (1 microfarad (ufd) = .0000001 farad = 10^-6 farads )
*
*    The capacitor's voltage at one time constant is defined as 63.2% of the charging voltage.
*
*  Hardware setup:
*  Test Capacitor between common point and ground (positive side of an electrolytic capacitor  to common)
*  Test Resistor between chargePin and common point
*  220 ohm resistor between dischargePin and common point
*  Wire between common point and analogPin (A/D input)
*/

#define analogPin 1           // analog pin for measuring capacitor voltage
#define chargePin 3           // pin to charge the capacitor - connected to one end of the charging resistor
#define dischargePin 4        // pin to discharge the capacitor
#define resistorValue 9870.0F // change this to whatever resistor value you are using \
// F formatter tells compliler it's a floating point value

unsigned long startTime;
unsigned long elapsedTime;
float microFarads; // floating point variable to preserve precision, make calculations
float nanoFarads;

bool bitArray = {false, false, false, false};
bool active = true;
char lastMessage;

const int halfClockTime = 1;
const int dataPin = 0;
const int clockPin = 1;

void writeStringToLCD(char *number);
void pause();
void getByteForChar(char inputChar, bool *incomingByte);
void writeCharToLCD(char incomingChar);
void keepDisplayAlive();

void setup()
{
pinMode(2, INPUT);
pinMode(chargePin, OUTPUT); // set chargePin to output
pinMode(dischargePin, INPUT); // set discharge pin back to input
digitalWrite(chargePin, LOW);

delay(1000); // Wait for display
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW);
sprintf(lastMessage, "%d", 101);
writeStringToLCD(lastMessage);
}

void loop()
{
digitalWrite(chargePin, HIGH); // set chargePin HIGH and capacitor charging
startTime = millis();

//while (analogRead(analogPin) < 648)
while (analogRead(analogPin) < 648)
{ // 647 is 63.2% of 1023, which corresponds to full-scale voltage
}

elapsedTime = millis() - startTime;
// convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ),  net 10^3 (1000)
microFarads = ((float)elapsedTime / resistorValue) * 1000;

if (microFarads > 1)
{
sprintf(lastMessage, "%d", (int)microFarads);
lastMessage = 'F';
writeStringToLCD(lastMessage);
}
else
{
// if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad).
// This is  a workaround because Serial.print will not print floats

nanoFarads = microFarads * 1000.0; // multiply by 1000 to convert to nanoFarads (10^-9 Farads)
sprintf(lastMessage, "%d", (int)nanoFarads);
lastMessage = '-';
writeStringToLCD(lastMessage);
}

/* dicharge the capacitor  */
digitalWrite(chargePin, LOW);    // set charge pin to  LOW
pinMode(dischargePin, OUTPUT);   // set discharge pin to output
digitalWrite(dischargePin, LOW); // set discharge pin LOW
while (analogRead(analogPin) > 0)
{ // wait until capacitor is completely discharged
}

pinMode(dischargePin, INPUT); // set discharge pin back to input
keepDisplayAlive();
}
``````

Scheme in attachment (german :) "LadePin" -> chargingPin, "EntladePin" -> dischargePin) Strangely, the value measured at A0 is always 1.3V.

The code never reads A0, so how do you know that?

Tell us which Arduino ATtiny core you are using. I'm aware of only A1, A2 and A3 being available on the ATtiny85, on package corresponding to digital pins 2, 3 and 4. For code clarity, use only "An" pin designations with analogRead().

You forgot to put pin numbers on your drawing, so we have no idea which pins are connected to what components.

Good points to clearify.

jremington: The code never reads A0, so how do you know that?

Tell us which Arduino ATtiny core you are using.

Well - I know that by observing that "A0 pin" (Ofcourse A1 is meant here, see clearification below) in the oscilloscope. - I guess by the core you mean the boards software I inserted to the ArduinoIDE? (this: https://github.com/damellis/attiny)

jremington: The code never reads A0, so how do you know that?

Tell us which Arduino ATtiny core you are using. I'm aware of only A1, A2 and A3 being available on the ATtiny85, on package pins 2, 3 and 4. For code clarity, use only "An" pin designations with analogRead().

• Of course you are right, its A1 I mean and which you surely already found out by taking a brief look into the code

jremington: You forgot to put pin numbers on your drawing, so we have no idea which pins are connected to what components.

I assumed one would conclude by seeing vcc/GND on the drawing that I thought of the normal pin layout which can be found everywhere, f.e. Take this as given.

I updated the drawing of the initial post.

I assumed

Not a good idea. If you want help, make sure that the information you present is clear and correct.

From experience, I assure you that it is not a good idea to assume that first time posters know what they are doing, and most of us find it a waste of time to guess what the poster "really meant".

which you surely already found out by taking a brief look into the code

Long ago, the Arduino developers made the incredibly stupid decision to allow people to use references like "1" or "A1" for the same analog pin on the Arduino Uno, even though such designations have no obvious connection to the physical pins, and the assignment is not checked by the compiler.

I do not assume that developers of new Arduino cores all make the same mistake (although it is possible), and can't be bothered to check.

Ok.

Apart from that, do you have some advice on the topic?

The ADC really does work! Start by replacing the "1" in the analogRead() call with "A1", double check the wiring and pin assignments, and see if that makes a difference.

Edit: the dmellis core does allow you to specify a channel number (1) instead of A1. I would not have gone that route.

``````static const uint8_t A0 = 6;
static const uint8_t A1 = 7;
static const uint8_t A2 = 8;
static const uint8_t A3 = 9;

...

#define analogPinToChannel(p)   ( (p) < 6 ? (p) : (p) - 6 )
``````

I don't see anything wrong. Check the wiring to the ADC pin and function by disconnecting it from the cap circuitry and applying, say, 1.5V from the pin to ground.

I doubt the coding is wrong since it does not exit the while(analogRead > 648) loop. So its waiting for the voltage to crossing the 5V*0.63 value. When applying 5V to the pin it print a value to the screen. So as well the assingnig of the pin in the code seems to be right.

I rather ask myself if there is something wrong electronically. Measuring shows me the resistance values I expect.

The 1.3V seems may be relating to the voltage divider of the screen. The measure line of the capacitor has about 50k ohms resistance to the screens lines. Seems a bit weak.

is something wrong electronically

Probably, but I don't see it.

I suggest to remove the display and test the cap circuit in isolation, using Serial.print().

jremington: Probably, but I don't see it.

Test the cap circuit alone, using Serial.print().

Ye thought about that. Until now I go along by attaching the osci, but I think Serial connection may be unavoidable.

Btw voltage divider of the display has 1.3-1.6V.

Thread moved as requested.

Just to keep you up to date: Works flawlessly without the display & its resistors attached, going to research in that direction.

With display attached and witchout, with attiny attached and without: 5k ohm between the switch on the + side of the cap and gnd, No capacitor attached. Just wondering where these come from.

Seems to be a wiring issue.