Raspberry Pi Motion Sensor Control

네오즈 · 2019-09-27 · 3
```json { "time": 1717347250795, "blocks": [ { "id": "qB-RJ3HZai", "type": "headerAlign", "data": { "text": "1. Motion Detector Test Circuit Configuration", "level": 4, "alignment": "left" } }, { "id": "vEJ93hTLzy", "type": "paragraph", "data": { "text": "When motion is detected, the red LED operates; otherwise, the green LED operates." } }, { "id": "O8qVNWwMvf", "type": "simpleImage", "data": { "url": "http://192.168.10.9:3000/img_e4dsmake/ck/1OLT2EB3V6NM2LOSZXU9.png", "caption": "", "withBorder": false, "withBackground": false, "stretched": false } }, { "id": "GXper8PgY7", "type": "paragraph", "data": { "text": "( PIR Motion Detector 1 EA, Red LED, Green LED 1 EA each, 220Ω Resistor 2 EA )" } }, { "id": "AsgrYPV1kh", "type": "paragraph", "data": { "text": "Red LED: Connected to GPIO 23, Green LED: Connected to GPIO 24, Motion Sensor: Connected to GPIO 18" } }, { "id": "Tw8DD7HIyg", "type": "paragraph", "data": { "text": "2. Motion Detector Test Python Code" } }, { "id": "N_pPp-D79r", "type": "paragraph", "data": { "text": "Refer to the \"Raspberry Pi GPIO Port (LED, Button) Control\" guide for code writing and execution methods" } }, { "id": "3Hvt6bZ_6a", "type": "paragraph", "data": { "text": "Code that turns on the red LED when motion is detected, otherwise turns on the green LED." } }, { "id": "uaVILbFwQ8", "type": "code", "data": { "code": "import RPi.GPIO as GPIO # Use functions defined in RPi.GPIO with the name GPIO\n\nGPIO.setmode(GPIO.BCM) # Use BCM nomenclature for GPIO names\n\nGPIO.setup(23, GPIO.OUT) # Set GPIO 23 as output\nGPIO.setup(24, GPIO.OUT) # Set GPIO 24 as output\nGPIO.setup(18, GPIO.IN ) # Set GPIO 18 as input\n\nGPIO.output(23, False) # Output LOW to GPIO 23 ( red LED off )\nGPIO.output(24, False) # Output LOW to GPIO 24 ( green LED off )\n\nprint \"PIR Sensor Test\" # Print message to screen\n\ntry: # try: line and below except KeyboardInterrupt: can be omitted\n while True: # Infinite loop section - equivalent to while(1) in C language\n\n if GPIO.input(18) == 0: # If GPIO 18 input (sensor output) is LOW\n GPIO.output(23, False) # Output LOW to GPIO 23 ( red LED off )\n GPIO.output(24, True ) # Output HIGH to GPIO 24 ( green LED on )\n\n else: # If GPIO 18 input (sensor output) is HIGH\n GPIO.output(23, False) # Output HIGH to GPIO 23 ( red LED on )\n GPIO.output(24, True ) # Output LOW to GPIO 24 ( green LED off )\n\nexcept KeyboardInterrupt: # When Ctrl-C is pressed\n GPIO.cleanup() # Clear GPIO-related settings\n" } }, { "id": "B1779-cz63", "type": "paragraph", "data": { "text": "\n3. Motion Detector Test C Code" } }, { "id": "p1Fe-QwPLU", "type": "code", "data": { "code": "#include // Include stdio.h file ( to use printf() )\n#include // Include wiringPi.h file\n\n#define PIR 1 // Definition for using PIR instead of pin 1 (GPIO 18)\n#define LED1 4 // Definition for using LED1 instead of pin 4 (GPIO 23)\n#define LED2 5 // Definition for using LED2 instead of pin 5 (GPIO 24)\n\nint main(void)\n{\n printf(\"PIR Sensor Test\\n\"); // Print message to screen\n\n wiringPiSetup( ); // Enable wiringPi library usage\n // ( to use pinMode(), digitalWrite(), etc. )\n pinMode(PIR, INPUT ); // Set pin 1 (GPIO 18) as input\n pinMode(LED1, OUTPUT); // Set pin 4 (GPIO 23) as output\n pinMode(LED2, OUTPUT); // Set pin 5 (GPIO 24) as output\n\n while(1) // Infinite loop section\n {\n if( digitalRead(PIR) == 0 ) // If pin 1 (GPIO 18) input is LOW - sensor output is LOW\n {\n digitalWrite(4, 0); // Output LOW to pin 4 (23) ( red LED off )\n digitalWrite(5, 1); // Output HIGH to pin 5 (24) ( green LED on )\n }\n else // If sensor output is HIGH\n {\n digitalWrite(4, 1); // Output HIGH to pin 4 (23) ( red LED on )\n digitalWrite(5, 0); // Output LOW to pin 5 (24) ( green LED off )\n }\n }\n\n return 0;\n}\n" } } ], "version": "2.30.5" } ```
3
댓글 0
등록된 댓글이 없습니다.