[Robot Arm Control Project 2] PWM Servo Motor Control Using Digital Input

· 2019-11-13 · 5
Through this practice, you will understand the relationship between digital input and servo motor operation. Materials: Arduino, 1 servo motor, 2 buttons Content: Using Arduino Uno to control a servo motor with 2 buttons. When power is connected, the servo motor moves to 90 degrees and then enters a waiting state for input. The operation is as follows. - Button 1 input => +15 degree rotation - Button 2 input => -15 degree rotation The circuit connection is as follows. The actual button used consists of 3 pins (VCC, GND, PIN), but since Fritzing does not have a 3-pin format button, the circuit was constructed using a commonly used button. Two buttons were connected to I/O pins 12 and 13, and the servo motor's I/O pin was connected to pin 3. When using a standard button, a pull-down resistor can be used to prevent the floating state of the button. Typically, a 10K resistor is used to connect the point where the I/O pin and servo motor are connected to GND through the resistor. *Floating state: A state where the input voltage of the button is trapped and floating without being discharged. ``` #include //Include header file Servo myservo; //Declare myservo variable int pos = 90; //Set initial angle value void setup() { myservo.attach(3); //Specify servo motor I/O pin as pin 3 pinMode(12,INPUT); //Use pin 12 as digital input for button 1 pinMode(13,INPUT); //Use pin 13 as digital input for button 2 myservo.write(90); //When power is connected, servo motor is set to 90 degrees and waits for input delay(500); Serial.begin(9600); //Use serial monitor } void loop() { if(digitalRead(12) == HIGH) //When button 1 is pressed { pos = pos + 15; //Rotate +15 degrees Serial.println("HIGH12"); //Output HIGH12 on serial monitor delay(50); } if(digitalRead(13) == HIGH) //When button 2 is pressed { pos = pos - 15; //Rotate -15 degrees Serial.println("HIGH13"); //Output HIGH13 on serial monitor delay(50); } if(pos < 0) //When servo motor angle becomes less than 0 degrees pos = 0; //Fix servo motor at 0 degrees if(pos > 180) //When servo motor angle exceeds 180 degrees pos = 180; //Fix servo motor at 180 degrees myservo.write(pos); //Rotate servo motor according to pos value delay(15); } ``` To verify button input on the monitor, the serial monitor was set to display 'HIGH12' and 'HIGH13' according to button input. The serial monitor results are as follows. The operation video is as follows.
5
댓글 0
등록된 댓글이 없습니다.