buck boost converterhow 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


spi = spidev.SpiDev()
if not pi.connected:

def analogInput(channel):
 spi.max_speed_hz =  1442      #f_cpu/frequency 1442
 data = ((adc[1]&3)<<8) + adc[2]
 return data

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

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

| improve this answer | |
  • \$\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\$ – evildemonic Jul 23 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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