1

I'm trying to read light value from an LED bilking very fast. So I choose TSL12S as a light sensor, and MCP3204 as an ADC. In datasheet, MCP3204 have sampling rate up to 100 kSps when Vdd = 5 V is applied and I have done so.

But when I tested my program, it outputs every 0.04 seconds, 40 milliseconds. That is, it is only 25 outputs per second. What a such slow rate! I'm not expecting 50 or 100 kilo outputs, but just wanting to see more than 1-5 kilo outputs.

A source code is written in Python and described down below:

#!/usr/bin/env python

import time

import pigpio

if __name__ == "__main__":
    spi_ch = 0
    spi_baud = 10000000
    spi_flag = 0b00 # 0, 0: SCLK idles LOW / 1, 1: SCLK idles HIGH

    pi = pigpio.pi()
    h = pi.spi_open(spi_ch, spi_baud, spi_flag)

    mcp_ch = 0;

    byte1 = 0b110 | mcp_ch >> 2
    byte2 = (mcp_ch & 0b11) << 6
    byte3 = 0

    if h >= 0:
        start = time.time()
        time_old = start

        while (time.time() - start) < 5:
            (b, d) = pi.spi_xfer(h, [byte1, byte2, byte3])

            if b == 3:
                reading = (d[1] & 0b00001111) << 8 | d[2]

                time_now = time.time()
                print "%f\t%d\t+%f" % (time_now - start, reading, time_now - time_old)
                time_old = time_now

        pi.spi_close(h)

    pi.stop()

I use pigpio library from here

Is this problem due to limitations of RPi HW, Raspbian OS, Python, library, or my code itself?

1
  • You need to tweak Rasbpian to enable high priority on the code and apply it to C or bash scripting. Problem is with event driven environment. I think you will find using Arduino will solve the problem or try RiscOS but I hear its not always great solutions for everything.
    – Piotr Kula
    Nov 20, 2014 at 21:17

1 Answer 1

1

As the author of pigpio I would only use the pigpio Python SPI, I2C, and serial functions if you are using pigpio over a network.

The pigpio Python module uses the socket interface to the pigpio daemon.

You should use the Python smbus module if you are running Python on the local Pi.

The pigpio C bit bang functions will be required if you need >10K samples per second.

4
  • Thanks. I've searched smbus and found that it is used for I2C. Not for SPI?
    – Jeon
    Jul 26, 2014 at 5:07
  • Yes, that was a mistake on my part. As I said only use the pigpio SPI, I2C, serial functions if you are working over a network. Use the normal Python modules for SPI (spidev), I2C (smbus) and serial (serial) if you are working locally. How many readings do you need to make per second? You will be wasting your own time (and ours) if you are trying the wrong type of solutions for the number of readings per second you require.
    – joan
    Jul 26, 2014 at 5:39
  • 250 readings per second has been done with smbus, thanks. But, the higher rate is the better
    – Jeon
    Jul 28, 2014 at 8:04
  • You could try C. I would expect an improvement. Personally I'd just use /dev/spidev0.0 and see what speeds were achieved. If that wasn't high enough I'd wonder if bit banging might work. There must be lots of standard C code for reading this device. Google?
    – joan
    Jul 28, 2014 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.