Atmega328P - How to use PB5/pin17 to measure two voltages

Hi,
I have a project using the Atmega328p TQFP32-08 and want to use the PB5/D13/Pin17 as an analog input.

Currently the only time it is used is for uploading binary updates via ICSP. During normal running of the sketch it is not connected to anything and left open. I'd like to use is as analog input when running the sketch.

Details:

  • system running on 5v
  • Input being measure will be between 3v or 2.143v level.

I'm not sure if the two voltages fall in a way that would trigger a HI or LOW reading. The voltages are a result of a resistor-divider circuit with a physical Switch, so I can adjust those to move the voltages up and down somewhat.

Any suggestions?

Here's a diagram of the switch circuitry that I want to measure. Circuit Diagram

The switch has three positions.

  • Left R6 only
  • Center R6 & R7 (Parallel resistor circuit)
  • **Right ** R7 only

Pin 13 is not an analog input. Analog inputs are A0 to A5 or IDE pins 14 to 19.

The input to a digital pin will be seen as HIGH if the voltage is greater than 0.6 * Vcc and will be read as LOW if the voltage is less than 0.3 * Vcc. See page 313 of the data sheet.

Between 0.3vcc (1.5v) and 0.6vcc (3.0v), digital pin input is undefined (could read HIGH or LOW, potentially depending on individual chip, and the direction that the pin approached that voltage from).

Since that pin does not have an ADC channel associated with it, it cannot be used to measure an analog voltage.

No pin without an ADC can be used to determine which of three applied voltages is applied to it.

You must use a different pin. Either an analog pin, or external circuitry and two digital pins could be used. Are you using all the analog pins, even A6 and A7?

I can't use any other pins as this is with a current project. The only pin is this one.

The switch actually one has two outputs, since POS 1 & 3 will result in only one of the 270k resistors being in the circuit and causing 3v.

The steady state is with 2.143v with the switch in the middle with both resistors in the circuit.

Is there a better range of high/low voltages I should use? I can change the resistor values and raise the two voltages.

The problem remains that a digital input can only sense 2 levels, LOW and HIGH.

groundFungus:
The problem remains that a digital input can only sense 2 levels, LOW and HIGH.

That's all I want. So it seems to be in finding the right voltage. Due to the project design the only variables are the resistor values. Plus it's likely that R6 & R7 have to be the same value...So the output is the same in both position 1&3

I could make it 3.214v & 2.368v by changing R8 to 150k. Think that might be better?

I did setup a test with the 180k and two 270k and it seems to work well and consistent.