dhhagan / py-opc

Python wrapper for the Alphasense OPC-N2 built around py-spidev
MIT License
30 stars 26 forks source link

Add a one second sleep time before reading Alphasense values in Raspberry pi #57

Closed H60gh closed 6 years ago

H60gh commented 6 years ago

Hi As I experienced, Raspberry pi has some problems during reading data from Alphasense sensor. If you manually test a connected sensor using terminal and see that alphasense.ping() returns a value, but running your code returns a reading error, firstly check your wiring, After that just simply add one second delay (using sleep command) before calling alphasense.histogram() function. As an example, see following code:

import spidev
import opc
from time import sleep

spi = spidev.SpiDev()
spi.open(0, 0)
spi.mode = 1
spi.max_speed_hz = 500000

alphasense = opc.OPCN2(spi)

# Turn the opc ON
alphasense.on()

# Add one second delay before reading histogram values
sleep(1.0)

# Read the histogram
print (alphasense.histogram())

# Turn the opc OFF
alphasense.off()