How can I read a negative voltages with the MCP3008? I tried to do that with this code but I could not read them; the largest 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()
def analogInput(channel):
spi.max_speed_hz = 1442 #f_cpu/frequency 1442
adc=spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3)<<8) + adc[2]
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:
out = analogInput(0)
out1 = Volts(out)
print(out1)
time.sleep(3)