Electrical Engineering Asked on December 15, 2021
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()
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)
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.
Answered by user253751 on December 15, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP