MCP3008 will module throws error when initiating multiple ch

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
LaserBread
 
Posts: 3
Joined: Sun Oct 03, 2021 8:51 pm

MCP3008 will module throws error when initiating multiple ch

Post by LaserBread »

Hi,

I'm trying to create a controller for Wiz Light Bulbs that use the sliders for different colors using a raspberry pi. My issue is that I can't get the MCP3008 module to read more than one channel without throwing an error in it's backend programs.

Here is the entire code, complete with snarky comments:

Code: Select all

#This little test allows me to change the color of a thing using the sliders.
import colour #This is the module that defines Colo(u)r objects allowing me to store them.  Here's where this gets stupid.  So, the module I want isn't a built-in, which is okay.  But python has a module built-in called "color," which does a completely different thing than "colour."  The built-in module influences colors on the terminal, while this one allows colors to be defined as objects.  I hate everything.

import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import asyncio
from pywizlight import wizlight, PilotBuilder, discovery

#I want to put this in a function.  But scope is weird.
#CHAPTER 1:  INITIALIZE THE POT
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

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

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

#Start the Channels.  This is where I encounter the problem.  
redChan = MCP.MCP3008(mcp,MCP.P0) #Pot 0 controls red
greenChan = MCP.MCP3008(mcp,MCP.P1) #Pot 1 controls green
blueChan = MCP.MCP3008(mcp,MCP.P2) #Pot 2 controls hamsters

#CHAPTER 2:  C(O/U)L(O(U/E)/(U/E))R
#Initiate the color object to hold our color.
col = Color(redChan,greenChan,blueChan) #NOW WAIT A SECOND!  I THOUGHT THE MODULE WAS CALLED COLOUR!  WHY IS THE OBJECT NAME FROM THE COLOUR PACKAGE SPELLED COLOR!?  WHAT IS THIS MADNESS!?

#CHAPTER 3:  GRAB OUR COLORS!!
def remap_range(value, left_min, left_max, right_min, right_max):
    # this remaps a value from original (left) range to new (right) range
    # Figure out how 'wide' each range is
    left_span = left_max - left_min
    right_span = right_max - right_min

    # Convert the left range into a 0-1 range (int)
    valueScaled = int(value - left_min) / int(left_span)

    # Convert the 0-1 range into a value in the right range.
    return int(right_min + (valueScaled * right_span))


def getColorPot():
    col.red = remap_range(redChan.value,0,65408,0,255)#it's not 65535 because the pot is weird.
    col.green = remap_range(redChan.value,0,65408,0,255)#it's not 65535 because the pot is weird.
    col.blue = remap_range(redChan.value,0,65408,0,255)#it's not 65535 because the pot is weird.

#CHAPTER 4: THE LIGHTS LMFAO
bulbs = []  #Make the bulbs a global variable so all coroutines can access it.
async def discover():
    global bulbs
    #Use UDP to find all the bulbs.
    bulbs = await discovery.discover_lights(broadcast_space="192.168.1.255")

    #read the bulbs
    for bulb in bulbs:
        print(bulb.__dict__)

async def set_color(r,g,b): #Subroutine for setting light colors.  Must be passed RGB values.
    print("setting lights")
    await asyncio.gather(bulbs[0].turn_on(PilotBuilder(rgb = (r,g,b))),bulbs[1].turn_on(PilotBuilder(rgb = (r,g,b))), loop = loop)

#RUNTIME PART

loop = asyncio.get_event_loop()
loop.run_until_complete(discover())


colorLoop = asyncio.get_event_loop()

while(True):
    getColorPot()
    set_color(col.red,col.green,col.blue)
Whenever this code is run, it throws this exception:

Code: Select all

File "PiLight/color.py", line 25, in <module>
    greenChan = MCP.MCP3008(mcp,MCP.P1) #Pot 1 controls green
  File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp3xxx/mcp3008.py", line 61, in __init__
    super().__init__(spi_bus, cs, ref_voltage=ref_voltage)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_mcp3xxx/mcp3xxx.py", line 57, in __init__
    self._spi_device = SPIDevice(spi_bus, cs)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/spi_device.py", line 70, in __init__
    self.chip_select.switch_to_output(value=True)
AttributeError: 'int' object has no attribute 'switch_to_output'
What am I doing wrong? Can someone please help me with this?

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: MCP3008 will module throws error when initiating multipl

Post by mikeysklar »

Skipping the light control and just reading the pot / slider is it working correctly for one channel? eg. If you just use our example code are you able to read MCP.P0 correctly?

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

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 cs (chip select)
cs = digitalio.DigitalInOut(board.D5)

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

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

print("Raw ADC Value: ", chan.value)
print("ADC Voltage: " + str(chan.voltage) + "V")
https://github.com/adafruit/Adafruit_Ci ... pletest.py

User avatar
LaserBread
 
Posts: 3
Joined: Sun Oct 03, 2021 8:51 pm

Re: MCP3008 will module throws error when initiating multipl

Post by LaserBread »

Yes, it is working properly with one channel.

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: MCP3008 will module throws error when initiating multipl

Post by mikeysklar »

When you add in a second analog channel to the same code like the following (plus the print lines). What happens?

Code: Select all

chan1 = AnalogIn(mcp, MCP.P1)

User avatar
LaserBread
 
Posts: 3
Joined: Sun Oct 03, 2021 8:51 pm

Re: MCP3008 will module throws error when initiating multipl

Post by LaserBread »

I've done that, and it works as intended.

Code: Select all

Raw ADC Value for Channel 0:  65472
ADC Voltage: for Channel 0: 3.2968276493476765V
Raw ADC Value for Channel 1:  0
ADC Voltage: for Channel 1: 0.0V

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: MCP3008 will module throws error when initiating multipl

Post by mikeysklar »

@LaserBread,

In this case the code is working as intended while reading multiple channels on the MCP3008.

Are you able to integrate your light bulb code with a working example in small steps and see if it is possible to isolate what is causing the SPI error.

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”