Raspberry Pi Servo Motor (SG90) Control

네오즈 · 2019-09-27 · 3
```json { "time": 1717347241075, "blocks": [ { "id": "eIg-DAJK4n", "type": "paragraph", "data": { "text": "1. SG90 Test Circuit Configuration" } }, { "id": "ETCkwaJh5T", "type": "simpleImage", "data": { "url": "http://192.168.10.9:3000/img_e4dsmake/ck/HSZKZ14PADOHO7K39GXW.png", "caption": "", "withBorder": false, "withBackground": false, "stretched": false } }, { "id": "H9MYSUnVyp", "type": "paragraph", "data": { "text": "( SG90 Servo Motor 1EA, Red Power Line (+) / Brown Ground (-) / Orange (Data) : Connected to GPIO 18 )" } }, { "id": "68N74CfEJp", "type": "paragraph", "data": { "text": "Refer to the \"; Raspberry Pi GPIO Port ( LED, Button ) Control \"; guide for code writing and execution methods" } }, { "id": "1In0uTMya7", "type": "paragraph", "data": { "text": "2. SG90 Test Python Code" } }, { "id": "eQOoZCCasO", "type": "code", "data": { "code": "import RPi.GPIO as GPIO # Use the features defined in RPi.GPIO with the name GPIO\nimport time # time module\n\npin = 18 # Use Python(BCM) pin 18\n\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(pin,GPIO.OUT)\n\nP = GPIO.PWM(pin,50) # PWM pulse width modulation, frequency set to 50\np.start(0)\n\ntry:\n while True:\n var = raw_input(\"Enter L/R/C : \")\n\n if var == 'R' or var == 'r' : # When R or r is entered, adjust angle to Right\n print \"Right\"\n p.ChangeDutyCycle(2.5) # Execute with Dutycycle as argument during runtime\n time.sleep(0.5)\n elif var == 'L' or var == 'l' : # When L or l is entered, adjust angle to Left\n print \"Left\"\n p.ChangeDutyCycle(12.5)\n time.sleep(0.5)\n elif var == 'C' or var == 'c' : # When C or c is entered, adjust angle to Center\n print \"Center\"\n p.ChangeDutyCycle(7.5)\n time.sleep(0.5)\n\nexcept KeyboardInterrupt:\n p.stop()\n GPIO.cleanup()" } }, { "id": "jQ-vQI2OOM", "type": "paragraph", "data": { "text": "\n3. SG90 Test C Code" } }, { "id": "R2LDGjv-_F", "type": "code", "data": { "code": "#include // Include stdio.h file (to use printf())\n#include // Include wiringPi.h file\n#include \n\n#define SERVO 1 // wiringPi GPIO 1 = Python(BCM) 18\n\nint main()\n{\n char str;\n\n If(wiringPiSetup() == -1)\n Return 1;\n softPwmCreate(SERVO,0,200); // When the period is set to 200, it has a 20ms period,\n // and since signals are generated every 20ms,\n // it can be viewed as 50Hz\n while(1)\n {\n fputs(\"select c(Center)/r(Right)/l(Left)/q(Quit) : \", stdout);\n scanf(\"%c\", &str);\n getchar();\n if(str == 'C' || str == 'c') \n softPwmWrite(SERVO,15); // 0 degree\n else if(str == 'R' || str=='r') \n softPwmWrite(SERVO,24); // 90 degree\n else if(str == 'L' || str=='l') \n softPwmWrite(SERVO,5); // -90 degree\n else if(str == 'Q' || str == 'q') \n return 0;\n }\nreturn 0;\n}\n" } } ], "version": "2.30.5" } ```
3
댓글 0
등록된 댓글이 없습니다.