0

I have a potentiometer that is used to adjust volume level along with MCP3008 but even if I don't touch it, its values are going and coming as following.

Press Ctrl-C to quit...
Channel 0 minus 1: 546
Channel 0 minus 1: 531
Channel 0 minus 1: 557
Channel 0 minus 1: 536
Channel 0 minus 1: 537
Channel 0 minus 1: 552
Channel 0 minus 1: 531
Channel 0 minus 1: 556
Channel 0 minus 1: 535
Channel 0 minus 1: 535
Channel 0 minus 1: 545
Channel 0 minus 1: 528
Channel 0 minus 1: 555
Channel 0 minus 1: 528
Channel 0 minus 1: 553

But I don't want it to change when I'm not adjusting it. As a solution I make a range in which if the range difference greater than 10, do alteration. But If I do that, volume adjusting is immediately going down or up not slowly(gradually). How can I solve it? I checked that it has ground connection well.

Code:

# Simple example of reading the MCP3008 analog input channels using its
# differential mode.  Will print the difference of channel 0 and 1.
# Author: Tony DiCola
# License: Public Domain
import time

# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008


# Software SPI configuration:
CLK  = 18
MISO = 23
MOSI = 24
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Hardware SPI configuration:
#SPI_PORT   = 0
#SPI_DEVICE = 0
#mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

print('Press Ctrl-C to quit...')
while True:
    # Grab the difference between channel 0 and 1 (i.e. channel 0 minus 1).
    # Note you can specify any value in 0-7 to grab other differences:
    #  - 0: Return channel 0 minus channel 1
    #  - 1: Return channel 1 minus channel 0
    #  - 2: Return channel 2 minus channel 3
    #  - 3: Return channel 3 minus channel 2
    #  - 4: Return channel 4 minus channel 5
    #  - 5: Return channel 5 minus channel 4
    #  - 6: Return channel 6 minus channel 7
    #  - 7: Return channel 7 minus channel 6
    value = mcp.read_adc_difference(0)
    print('Channel 0 minus 1: {0}'.format(value))
    time.sleep(0.5)
  • 1
    please post a schematic or a wiring diagram – jsotola Jan 16 at 7:25
1

You seem to have noise in your signal, and you should find out where that noise comes from. The first thing I would check is whether you actually need diff mode, since that picks up noise on two channels instead of one. Unless you have your reasons, switch to single-channel mode.

Another solution is to filter out the noise. The easiest option I can think of is to add a low-pass filter. Pick a capacitor in 10-100 μF range and a resistor in 10-100 kΩ range and see if that helps.

enter image description here

0

This is normal - analog devices are not precise, you can expect some variance even when still.

However you can deal with this in software. I'd recommend using the gpiozero library which has an MCP3008 class and normalises readings on a 0 to 1 scale:

from gpiozero import MCP3008

adc = MCP3008(channel=0)

for value in adc.values:
    print(value)

You can combine it with gpiozero's smoothed function which will smoothen out your values by providing the mean of every 5 readings:

from gpiozero import MCP3008
from gpiozero.tools import smoothed

adc = MCP3008(channel=0)

for value in smoothed(adc.values, 5):
    print(value)

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.