# read negative voltage with mcp3008

how can i read a negative voltage by the mcp3008 ? , i tried to do that with this code but i I could not read it , i see that the most readings are 0.00 , this is my code :

import RPi.GPIO as GPIO
import pigpio
import time
import spidev

GPIO.setwarnings(False)

spi = spidev.SpiDev()
spi.open(0,0)
pi=pigpio.pi()
if not pi.connected:
exit()

spi.max_speed_hz =  1442      #f_cpu/frequency 1442
return data

def Volts(data):
volts=(data*3.3)/float(1023)      #1024
volts=round(volts,2)
return volts

dc=718000
pi.hardware_PWM(18, 70000, dc)
while True:
out1=Volts(out)
print(out1)
time.sleep(3)

• It cannot. BUT! Maybe you can make a circuit which converts it to a positive voltage instead! Where is the negative voltage coming from? Add a schematic if necessary. – user253751 Jul 23 at 18:17
• @user253751 this is my buck boost converter , i get in the output -12V to -9V, i want to read this voltages and do with it something – hos Jul 23 at 18:40
• @user253751 in the schematic that you posted the output is the input of the mcp3008? – hos Jul 24 at 17:07
• yes............ – user253751 Jul 24 at 17:39
• @user253751 i connect like you said , but i get a zeros in the reading – hos Jul 24 at 18:10

It cannot.

However, you can make a voltage divider that will make it into a positive voltage, by using a positive voltage as one side of your divider.

You say the voltage you want to measure is -12V to -9V. If you have +3.3V available, you could use a voltage divider with about a 4:1 ratio, like so (4.7:1 in this case):

simulate this circuit – Schematic created using CircuitLab

Your output voltage will be $$V_{out} = 3.3 + \frac{10}{57}V_{in}$$

So when the input is -12V, the output will be 1.195V. When the input is -9V, the output will be 1.721V.

If you have a different voltage than +3.3V or your ADC can only measure up to 1V or something, you'll need different resistor values, but this is the general idea.

• Your schematic is large because it is narrow (compared to its height). The size it uses seems to be set by the width. Try dragging your output further out and rotating the labels on the left so they stick out that way...the whole thing will shrink. – evildemonic Jul 23 at 22:12