addms
Posts: 3
Joined: Wed Feb 26, 2020 8:30 pm
Location: Michigan

MCP3008 voltage values are 2x larger than expected

Wed Feb 26, 2020 9:02 pm

I'm currently using a MCP3008 and my analog input is coming from an arbitrary waveform generator. The generator is producing a sine wave with a peak to peak voltage of 0.5 and an offset of 0.5 volts. I would expect the MCP3008 to get values between 0.25 and 0.75, but instead I'm getting between 0.5 and 1.5.
Here is the code I'm using:

Code: Select all

import os
import time
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn


# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the chip select (cs)
cs = digitalio.DigitalInOut(board.CE0)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# analog input channel on pin 0
chan0 = AnalogIn(mcp, MCP.P0)

while True:
	print("Voltage: ", chan0.voltage)
	time.sleep(0.1)
Pictures of the setup are here: https://imgur.com/a/GtUIoQ7, sorry if they're not clear, I'll take more if necessary.

Thanks in advance!

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: MCP3008 voltage values are 2x larger than expected

Thu Feb 27, 2020 8:51 am

Welcome to the Raspberry Pi forums.
addms wrote:
Wed Feb 26, 2020 9:02 pm
I'm currently using a MCP3008 and my analog input is coming from an arbitrary waveform generator. The generator is producing a sine wave with a peak to peak voltage of 0.5 and an offset of 0.5 volts. I would expect the MCP3008 to get values between 0.25 and 0.75, but instead I'm getting between 0.5 and 1.5.
Here is the code I'm using:

Code: Select all

import os
import time
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn


# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the chip select (cs)
cs = digitalio.DigitalInOut(board.CE0)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# analog input channel on pin 0
chan0 = AnalogIn(mcp, MCP.P0)

while True:
	print("Voltage: ", chan0.voltage)
	time.sleep(0.1)
Pictures of the setup are here: https://imgur.com/a/GtUIoQ7, sorry if they're not clear, I'll take more if necessary.

Thanks in advance!



As a comparison for troubleshooting purposes, the gpiozero Python library has built-in support for the mcp3008 family of ADC chips.

Perhaps a quick test would help discriminate between software and hardware/wiring issues.

https://gpiozero.readthedocs.io/en/stab ... entiometer

addms
Posts: 3
Joined: Wed Feb 26, 2020 8:30 pm
Location: Michigan

Re: MCP3008 voltage values are 2x larger than expected

Thu Feb 27, 2020 9:15 pm

Hi thanks. I did use the following code using the gpiozero library and got the same numbers:

Code: Select all

import gpiozero
import time

m = gpiozero.MCP3008(channel=0)

while True:
	print(m.voltage)
	time.sleep(0.1)

User avatar
PeterO
Posts: 5966
Joined: Sun Jul 22, 2012 4:14 pm

Re: MCP3008 voltage values are 2x larger than expected

Thu Feb 27, 2020 9:26 pm

What do you have connected to Vref ?

IF your signal source was 0.5V peak (not peak to peak) and you had 3.3V (instead of 5V) conected to Vref , then I think you would see 0V to 1.5V readings.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: MCP3008 voltage values are 2x larger than expected

Thu Feb 27, 2020 9:55 pm

addms wrote:
Thu Feb 27, 2020 9:15 pm
Hi thanks. I did use the following code using the gpiozero library and got the same numbers:

Code: Select all

import gpiozero
import time

m = gpiozero.MCP3008(channel=0)

while True:
	print(m.voltage)
	time.sleep(0.1)

Also, wondering if you should be using the value attribute (scaled between 0 and 1) rather than voltage (scaled between 0 and max_voltage)?

addms
Posts: 3
Joined: Wed Feb 26, 2020 8:30 pm
Location: Michigan

Re: MCP3008 voltage values are 2x larger than expected

Fri Feb 28, 2020 2:53 pm

So I tried using the value attribute, but the values are still 2x larger, as anything above 1.65V (3.3/2) returns a value of 1.

I have 3.3V connected to Vref as that's what every tutorial did. I think my signal source is in fact 0.5V peak to peak, here are some photos of the waveform generator: https://imgur.com/a/GQKdR3x

JohnsUPS
Posts: 188
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: MCP3008 voltage values are 2x larger than expected

Tue Mar 03, 2020 12:25 pm

Have you tried applying a constant DC voltage to validate the ADC and your code? If that comes back as accurate, then your readings are being based on how the ADC is seeing the sine wave. Maybe you need to look into synchronizing the ADC reads with a zero crossing detector (with delay to catch the peaks, not the crossings ;)

Return to “Interfacing (DSI, CSI, I2C, etc.)”