0
\$\begingroup\$

buck boost converter

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)
\$\endgroup\$
8
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – user253751
    Jul 23, 2020 at 18:17
  • \$\begingroup\$ @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 \$\endgroup\$
    – hos
    Jul 23, 2020 at 18:40
  • \$\begingroup\$ @user253751 in the schematic that you posted the output is the input of the mcp3008? \$\endgroup\$
    – hos
    Jul 24, 2020 at 17:07
  • \$\begingroup\$ yes............ \$\endgroup\$
    – user253751
    Jul 24, 2020 at 17:39
  • \$\begingroup\$ @user253751 i connect like you said , but i get a zeros in the reading \$\endgroup\$
    – hos
    Jul 24, 2020 at 18:10

1 Answer 1

1
\$\begingroup\$

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):

schematic

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 23, 2020 at 22:12
  • \$\begingroup\$ .... thus cutting your sensitivity by a factor of almost 6 \$\endgroup\$ Sep 8, 2021 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.