Skip to content

Commit

Permalink
servo
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplFerris committed Nov 10, 2024
1 parent c9f8f94 commit b7871b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/servo/servo-pico.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ const PWM_DIV_INT: u8 = 64;
const PWM_TOP: u16 = 46_874;

const TOP: u16 = PWM_TOP + 1;
const MIN_DUTY: u16 = (TOP as f64 * (2.5 / 100.)) as u16;
const HALF_DUTY: u16 = (TOP as f64 * (7.5 / 100.)) as u16;
// 0.5ms is 2.5% of 20ms; 0 degrees in servo
const MIN_DUTY: u16 = (TOP as f64 * (2.5 / 100.)) as u16;
// 1.5ms is 7.5% of 20ms; 90 degrees in servo
const HALF_DUTY: u16 = (TOP as f64 * (7.5 / 100.)) as u16;
// 2.4ms is 12% of 20ms; 180 degree in servo
const MAX_DUTY: u16 = (TOP as f64 * (12. / 100.)) as u16;
```

We multiply the TOP value by a duty cycle percentage to determine the appropriate pulse width for each position of the servo.
We multiply the TOP value by a duty cycle percentage to determine the appropriate pulse width for each position of the servo. You might need to adjust the percentage based on your servo.

6 changes: 6 additions & 0 deletions src/servo/servo.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ They are widely used in applications requiring precise motion, such as robotics,

In our exercise, we'll be using the hobby server (Micro Servo SG90)

<img style="display: block; margin: auto;" alt="pico2" src="./images/sg90-servo-motor.jpg"/>

### How does it work?

A servo motor is controlled by sending a series of pulses through its signal line. The signal has a frequency of 50Hz, with a pulse every 20 milliseconds. The width of the pulse determines the servo's position. Typically, a servo can rotate 180 degrees.
Expand All @@ -19,3 +21,7 @@ The position of a servo motor is controlled by sending a pulse with a specific d
However, from my experiment, I found that not all servos follow these exact timings. For example, with my servo, the pulse duration for 0 degrees was 0.5ms, 1.5ms for 90 degrees, and approximately 2.4ms for 180 degrees. I had to experiment and adjust to get it right. If you're unsure, you can use tools like an oscilloscope to fine-tune it, or simply test different values to find what works best for your specific servo.

The example I'll provide in this exercise is based on my servo's configuration, you might need to adjust the values depending on the servo you're using.

### Reference
- [Learn to Control Servo motor using PWM - Wokwi Style](https://blog.wokwi.com/learn-servo-motor-using-wokwi-logic-analyzer/)

0 comments on commit b7871b7

Please sign in to comment.