I'm learning to control a MCP4131 digital potentiometer with the RPi. I've successfully used the MCP4131 by sending my data to the SDI pin, and then a HIGH value to the CLK followed by sending a LOW value on the same CLK. This indicates data is ready to be read.

My code for sending a bit to the MCP4131 looks like this, where bit_value contains either a 0 or a 1:

    GPIO.output(SPI_SDI_PIN, bit_value)
    GPIO.output(SPI_CLK_PIN, True)
    GPIO.output(SPI_CLK_PIN, False)

You can notice that I'm not sleeping between setting the clock HIGH and LOW, so how can I be sure that the MCP4131 will notice that change? Maybe the RPi is too fast and the MCP4131 will not be able to see the HIGH value.

From what I've researched, the RPi SPI interface goes at 250MHz and the MCP4131 just at 10MHz, so I was expecting that without a time:sleep() in between the two clock signals this would not work.

Why does it work and what are better practices to avoid putting time.sleep() with hard-coded delays?

(My RPi is running Raspbian if that matters)



I doubt the RPi SPI runs at 250MHz, 25MHz possibly.

But you are not using SPI. The rpi.gpio tag and your code snippet suggest you are using RPi.GPIO, a Python module.

Python is an interpreted language. RPi.GPIO will take of the order of a minimum of 10 microseconds to toggle a gpio.

  • Yes, I'm using rpi.gpio, sorry for the terminology, I'm newbie at this. This means that using that module the pins are toggled at a maximum of 100KHz, right ? So that is why my chip that runs at 10MHz can see the signals. Is it so ?
    – MondKin
    Apr 27 '14 at 17:59
  • The RPi.GPIO toggle will be closer to 60Khz (just did a quick test). That will give plenty of time for your SPI devices to see the clock.
    – joan
    Apr 27 '14 at 18:07

Based on what @joan has said, consider maybe using the Python SPI library, seen here. You can also open /dev/spidev0.0 as a file and read/write bytes to it. See here for an example.

  • I'm doing exactly what slowspiwrite is doing there, not what writestrip is doing. Are those 2 different ways of doing the exact same thing but the spi one will be faster ?
    – MondKin
    Apr 27 '14 at 18:11
  • 1
    slowspiwrite is using a technique called bit-banging, which is using software to implement a function usually implemented in hardware (probably an inaccurate definition but good enough for now). Bit-banging is usually slower and less accurate than the hardware version. spidev, the Python SPI library, is the proper Python hardware method.
    – joan
    Apr 27 '14 at 18:46
  • Bit-banging is slow and inaccurate because the clock on a PC is a bit inaccurate at very high frequencies due to thread interruption. Linux switching back and forth from different tasks (say updating the screen) puts your bit-banging on hold for an uncontrolled period. This causes a lot of drift in timings, causing problems if something is expecting a specific frequency. Generally you can't get better then about 100 Hz (sleep 0.01).
    – Fred
    Apr 28 '14 at 17:54

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.