SG90 Servo
Module Source
Purchase link: https://detail.tmall.com/item.htm?abbucket=0&id=615779197448&ns=1&spm=a21n57.1.0.0.6f6f523cXTHwFh Materials download: https://pan.baidu.com/s/1QsTIKnoQsOTCkeYLLTTjTA?pwd=8889 Extraction code: 8889
Specifications
Drive voltage: 3V~7.2V Drive current: Operating torque: 1.6 kg·cm Control method: PWM Rotation angle: 180 degrees
View Materials
When purchasing, you need to clearly distinguish whether your servo can rotate 180 degrees or 360 degrees. A 360-degree servo cannot control the angle; it can only control the rotation speed.
The rotation speed of the SG90 servo is not very fast — generally 0.22 s/60° or 0.18 s/60° — so if you change the width of the angle control pulse too quickly, the servo may not be able to react in time. If you need faster response, you need a higher rotation speed.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_sg90.c and bsp_sg90.h, and the folder name to SG90.
Write Code
In the file bsp_sg90.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#include "bsp_sg90.h"
unsigned int Servo_Angle = 0;// Servo angle
/******************************************************************
* Function Name: SG90_Init
* Function Description: PWM configuration
* Function Parameters: pre timer clock prescaler value per period
* Function Return: None
* Author: LC
* Notes: PWM frequency = 80,000,000 / ((pre+1) * (per+1))
******************************************************************/
void SG90_Init(void)
{
// Prepare and apply the PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = PWM_MODE, // Low speed mode
.timer_num = PWMA_TIMER, // Timer source of the channel Timer 0
.duty_resolution = LEDC_DUTY_RES, // Set duty cycle resolution to 12 bits
.freq_hz = LEDC_FREQUENCY, // Set output frequency to 50 kHz
.clk_cfg = LEDC_AUTO_CLK // Set the LED PWM clock source to automatic
//LEDC_AUTO_CLK = When starting the timer, the source clock will be automatically selected based on the given resolution and duty cycle parameters
};
ledc_timer_config(&ledc_timer);
// Prepare and apply the LEDC1 PWM channel configuration
ledc_channel_config_t ledc_channel = {
.speed_mode = PWM_MODE, // Low speed mode
.channel = SG90_CHANNEL, // Channel 0
.timer_sel = PWMA_TIMER, // Timer source Timer 0
.intr_type = LEDC_INTR_DISABLE, // Disable interrupt
.gpio_num = GPIO_SIG, // Output pin GPIO1
.duty = 0, // Set duty cycle to 0
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}
/******************************************************************
* Function Name: Set_Servo_Angle
* Function Description: Set the angle
* Function Parameters: angle = angle to set, range 0-180
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Set_Servo_Angle(unsigned int angle)
{
unsigned int ServoAngle = 0;
float min = 101.375;
float max = 510.875;
// Angle set exceeds 180 degrees
if( angle > 180 )
{
return ;
}
// Save the set angle
Servo_Angle = angle;
// Convert angle
ServoAngle = (unsigned int)( min + ( (float)angle * 2.2777 ));
printf("ServoAngle = %d \r\n",ServoAngle);
ledc_set_duty(PWM_MODE, SG90_CHANNEL, ServoAngle);
ledc_update_duty(PWM_MODE, SG90_CHANNEL);
}
/******************************************************************
* Function Name: Read the current angle
* Function Description: Get_Servo_Angle
* Function Parameters: None
* Function Return: Current angle
* Author: LC
* Notes: Before use, you must ensure that the angle has been set before using
void Set_Servo_Angle(unsigned int angle)
******************************************************************/
unsigned int Get_Servo_Angle(void)
{
return Servo_Angle;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
In the file bsp_sg90.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#ifndef _BSP_SG90_H
#define _BSP_SG90_H
#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "driver/spi_common.h"
#include "hal/gpio_types.h"
#include "driver/ledc.h"
#include "driver/mcpwm.h"
#include "string.h"
#define GPIO_SIG 1
#define PWMA_TIMER LEDC_TIMER_0 // Timer 0
#define SG90_CHANNEL LEDC_CHANNEL_0 // Use LEDC channel 0
#define PWM_MODE LEDC_LOW_SPEED_MODE // Low speed mode
#define LEDC_DUTY_RES LEDC_TIMER_12_BIT // LEDC resolution set to 12 bits
#define LEDC_FREQUENCY (50) // Frequency unit is Hz. Set frequency to 50Hz
void SG90_Init(void);
void Set_Servo_Angle(unsigned int angle);
unsigned int Get_Servo_Angle(void);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Porting Verification
In the main function of your project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_sg90.h"
#include "string.h"
#include "esp_private/esp_task_wdt.h"
#include "esp_private/esp_task_wdt_impl.h"
int app_main(void)
{
int i = 0;
esp_task_wdt_deinit();
SG90_Init();
printf("SG90 Start......\r\n");
while(1)
{
Set_Servo_Angle(i++);
printf("i = %d \r\n",i);
if( i >= 180 )
{
i = 0;
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
File Download
📌 Materials Download Center (Click to Jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.