Hal_cat
Posts: 5
Joined: Fri Dec 15, 2017 6:39 pm

gpiozero SPI select CS1 issue

Wed Jul 17, 2019 5:47 pm

I am working with Python 3.5 with gpiozero 1.5.0. I have successfully used the following script:

Code: Select all

from gpiozero import MCP3002
from time import sleep
import spidev

spi_max_speed = 50000     # 1 MHz for SPI

spi = spidev.SpiDev()
spi.open(0, 0)              
spi.max_speed_hz = spi_max_speed

while True:
    #read channel 0 on device 0
    pot = MCP3002(chan)
    print("pot value = ", pot.value)
    print()
    sleep(1)
However, I would like to access this device using CS1.
When I tried to specify CS1 in the open statement:

Code: Select all

spi.open(0,0,1)
I get "Type error: open() takes at most 2 arguments" even though the documentation presents this as an option.

Is there another way to specify the chip select line to spi?

trejan
Posts: 926
Joined: Tue Jul 02, 2019 2:28 pm

Re: gpiozero SPI select CS1 issue

Wed Jul 17, 2019 6:03 pm

spidev.open is only 2 arguments. The device argument is the CE pin as the bus is which SPI interface.

Code: Select all

open(bus, device)

Connects to the specified SPI device, opening `/dev/spidev<bus>.<device>`
A bigger issue is that you're mixing up the gpiozero module with the spidev module. They're two separate APIs. If you want to specify which SPI interface and which chip enable line for the MCP3002 module then you give it some optional arguments.

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: gpiozero SPI select CS1 issue

Thu Jul 18, 2019 9:40 pm

You don't need to import spidev. Use the keyword arguments in gpiozero's MCP3002 class documented here: https://gpiozero.readthedocs.io/en/stab ... yword-args
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

Hal_cat
Posts: 5
Joined: Fri Dec 15, 2017 6:39 pm

Re: gpiozero SPI select CS1 issue

Mon Jul 22, 2019 5:34 pm

I get your point that mixing spidev and gpiozero could cause problems. The only reason I included spidev is that it provides a way to set the SPI clock speed.

I've retested after removing spidev and using the following:

Code: Select all

    pot = MCP3002(channel=0, device=1)
and it works perfectly.

It appears the gpiozero must be setting the clock speed to about 308KHz, although I cannot find any documentation indicating that it sets clock speed, nor any way for me to set it. At this point everything is OK, so until becomes a problem I'm good with it.

Thank you both.

Return to “Python”