I'm new in the forum and apologize in advance for my bad English.
I've been trying for the last days to get digital read and interrupt working by using a PIR sensor and some others, but I always have the same problem.
Here's my setup: I have an Edison with the Sparkfun Console Block and the GPIO Block, and I have to use Python, so I can use mraalib to program it.
I want to use a PIR sensor (parallax PIR sensor 555-28027): when it detects motion a green LED lights up, otherwise it remains on the red.
I connected VDD and GND of the PIR, and output signal directly to pin GP13 of the Sparkfun GPIO block, that is mraa number 14.
Here is my code. When I try to read the sensor's output, I just get the high value, never the low.
Can someone help me and tell me where I'm wrong?
#!/usr/bin/python # Import required Python libraries import mraa import time # Map GPIO block pins to MRAA pin numbers pin_GP13 = 14 pin_GP15 = 48 pin_GP14 = 36 # Pin setup pir = mraa.Gpio(pin_GP13) greenLed = mraa.Gpio(pin_GP15) redLed = mraa.Gpio(pin_GP14) # Variables pirCalibrationPeriod = 45.0 # 45 sec # Function that toggle the led ON/OFF LED_ON = 1 LED_OFF = 0 def Toggle(led, state): led.write(state) def main(): # Define pin direction pir.dir(mraa.DIR_IN) greenLed.dir(mraa.DIR_OUT) redLed.dir(mraa.DIR_OUT) print "\nPIR Module Test (CTRL+C to exit)\n" # Time to calibrate the sensor's output print "Waiting for PIR to settle: don't move anything in front of it ...\n" startCalibration = time.time() pirCalibrationTime = startCalibration + pirCalibrationPeriod # Wait for PIR calibration while (time.time() < pirCalibrationTime): continue print "Ready to work!" print "Move the hand in front of the sensor to detect motion.\n" # Turn redLed ON Toggle(redLed, LED_ON) triggers = 0 while True: # Read and print PIR's state #print "IN =", pir1.read() <-------------------------------- I ONLY READ 1 HERE! ********************* #time.sleep(5.0) pirState = 0 pirState = pir.read() <-------------------------------- I ONLY READ 1 HERE! ********************* if (pirState == 1): triggers = triggers + 1 print "PIR ALARM! Record number:", triggers, "- greenLed ON" # Turn OFF the red led and turn ON the green one for a second Toggle(redLed, LED_OFF) Toggle(greenLed, LED_ON) time.sleep(1.0) # 1 sec delay Toggle(greenLed, LED_OFF) # Pause for 2 sec time.sleep(2.0) Toggle(redLed, LED_ON) else: print "No motion detected - redLed ON" time.sleep(3.0) if __name__ == "__main__": try: main() except KeyboardInterrupt: # Turn OFF leds Toggle(greenLed, LED_OFF) Toggle(redLed, LED_OFF) print "\n\nExit pir.py - Bye!\n"
Again, if I try to use interupt to detect the low-high transition, nothing works. Here's the code, only the main function change.
class State(): state = 0 pir_state = State() # ISR function of the PIR def Motion(args): print "Motion detected!" # Read the sensor output pir_state.state = pir.read() print "Sensor output value: " + str(pir_state.state) # Turn greenLed ON Toggle(greenLed, LED_ON) def main(): # Define pin direction pir.dir(mraa.DIR_IN) greenLed.dir(mraa.DIR_OUT) redLed.dir(mraa.DIR_OUT) pir1.edge(mraa.EDGE_RISING) <--- IS IT NECESSARY? ****** # ISR for pir1 pir.isr(mraa.EDGE_RISING, Motion, Motion) <------------------ NOTHING WORKS HERE! ********************* # Delay time.sleep(500.0)
Thank you all in advance.