# Measuring negitive voltages as positiveand sensing that it is netitive?

#### spinnaker

Joined Oct 29, 2009
7,815
I am working on my first project in a very long time. A bench power supply with built in digital voltmeter. The voltmeter will have a PIC 18F45K20. The PIC will also operate the front panel.

After ordering everything, I quickly realized that it is probably cheaper to buy a bench supply rather than build it on my own. But I sort of already knew this, from what others have said on this forum. But it is hard to put a value on the challenge, the education and the exercise to my old brain cells.

Anyway, I knew I could not directly measure negative voltages with the PIC. So I thought what I really need to do is to measure absolute voltages and I probably would need to use an op amp to do this. So I did some searching and sure enough I found an op amp circuit to measure absolute voltages.

But what I can't figure out is how the PIC would know a negative or positive voltage was being measured. How would I do this? Maybe a voltage divider and a diode on the input to the op amp?

Or is there an easier way to measure both negative and positive voltages altogether?

Last edited:

#### rjenkins

Joined Nov 6, 2005
1,015
The simplest way is to use a resistive divider with it's 'common' end to the PIC +5V.

eg. If you want to read somewhere around -12V, calculate the divider to cover 17V, say a 3:1 ratio giving 1/4 input to the ADC, full scale 20V (= -15).

Invert the reading from that ADC channel and subtract the appropriate offset for the +5V bias. With the 3:1 example, the offset would be 1/4 of 256 or 64.

-12V would give an ADC input of 5 - (17 / 4) = 0.75V

Invert: 256 - 38 = 218
Remove offset: 218 - 64 = 154

(Allowing for the 1/4 input from the divider)
ADC steps, based on full scale of 20V: 20V / 256 = 0.078125V per step.

0.078125 * 154 = 12.03125V

#### spinnaker

Joined Oct 29, 2009
7,815
The simplest way is to use a resistive divider with it's 'common' end to the PIC +5V.

eg. If you want to read somewhere around -12V, calculate the divider to cover 17V, say a 3:1 ratio giving 1/4 input to the ADC, full scale 20V (= -15).

Invert the reading from that ADC channel and subtract the appropriate offset for the +5V bias. With the 3:1 example, the offset would be 1/4 of 256 or 64.

-12V would give an ADC input of 5 - (17 / 4) = 0.75V

Invert: 256 - 38 = 218
Remove offset: 218 - 64 = 154

(Allowing for the 1/4 input from the divider)
ADC steps, based on full scale of 20V: 20V / 256 = 0.078125V per step.

0.078125 * 154 = 12.03125V