Introduction
Servo is a type of geared motor that can only rotate 180 degrees. It is controlled by sending electrical pulses from your microcontroller. These pulses tell the servo what position it should move to.
A servo has three wires, the brown wire is GND, the red one is VCC, and the orange one is signal line, as shown below.
Components
– 1*Raspberry Pi
– 1*Breadboard
– 1*Network cable (or USB wireless network adapter)
– 1*Servo
– Several jumper wires
Experimental Principle
Servo consists of shell, circuit board, non-core motor, gear and location detection. Its working principle is as follow: Raspberry Pi controller sends PWM signal to servo motor, and then this signal is processed by IC on circuit board to calculate rotation direction to drive motor, and then this driving power is transferred to swing arm by reduction gear. At the same time, position detector returns location signal to judge whether set location is reached or not.
Experimental Procedures
Step 1: Connect the circuit
Servo Raspberry Pi
Orange line————————————— GPIO1
Red line —————————————- +5V
Brown line—————————————- GND
Step 2: Edit and save the code (see path/Rpi_BasicKit/08_servo/servo.py)
Step 3: Modify the file permissions
chmod +x servo.py |
Step 4: Run the program
python servo.py |
Press Enter, you can see the servo motor rotate 180 degrees. And then rotate in opposite direction.
Code
#include <wiringPi.h> #include <wiringSerial.h> #include <stdio.h> #include <unistd.h> int main(void) { int fd; unsigned char pos = 0; unsigned char buf[4] = {0}; unsigned char *buf1 = “hello\n”; fd = serialOpen(“/dev/ttyAMA0”, 9600); unsigned char tmp; while(1){ // scanf(“%d”, &pos); // snprintf(buf, 4, “%d”, pos); //printf(“*** %s\n”,buf); serialPrintf(fd, “120\r\n”); sleep(1); serialPrintf(fd, “20\r\n”); sleep(1); serialPrintf(fd, “150\r\n”); sleep(1); delay(2500); } serialClose(fd); return 0; } |