esp32 devkitc-1 驱动sg90舵机的问题

Moderator: ESP_ZT

cqf123
Posts: 6
Joined: Sun Jun 12, 2022 8:02 am

esp32 devkitc-1 驱动sg90舵机的问题

Postby cqf123 » Thu Jul 14, 2022 6:25 am

开发环境:arduino
想用自带的LEDPWD来驱动sg90 发现没有反应
  1. int freq = 50;    
  2. int channel = 7;  
  3. int resolution = 8;
  4. const int led = 16;
  5.  
  6. int servo_max=180,servo_min=90;
  7.  
  8. int calculatePWM(int degree)
  9. {
  10.   const float deadZone = 6.4;
  11.   const float max = 32;
  12.   if (degree < 0)
  13.     degree = 0;
  14.   if (degree > 180)
  15.     degree = 180;
  16.   return (int)(((max - deadZone) / 180) * degree + deadZone);
  17. }
  18.  
  19. void setup()    
  20. {  
  21.   Serial.begin(9600);  
  22.   ledcSetup(channel, freq, resolution); // 设置通道
  23.   ledcAttachPin(led, channel);          // 将通道与对应的引脚连接
  24. }
  25.  
  26. void loop()
  27. {  
  28.   ledcWrite(channel, calculatePWM(90));
  29.   delay(1000);
  30.   ledcWrite(channel, calculatePWM(180));
  31.   delay(1000);
  32. }

HF CHENG
Posts: 2
Joined: Thu Jul 14, 2022 1:41 pm

Re: esp32 devkitc-1 驱动sg90舵机的问题

Postby HF CHENG » Thu Jul 14, 2022 2:15 pm

:D 巧啊,我昨天也尝试相同的方法驱动舵机,可能你的占空比设置的不太正确,可以试一下我改的示例程序,看看有没有动起来
  1. #define LEDC_CHANNEL_0     0// use first channel of 16 channels (started from zero)
  2. #define LEDC_TIMER_13_BIT  13// use 13 bit precission for LEDC timer
  3. #define LEDC_BASE_FREQ     50// use 50 Hz as a LEDC base frequency
  4. #define LED_PIN            14// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
  5. int brightness = 700;    // how bright the LED is
  6. int fadeAmount = 1;    // how many points to fade the LED by
  7.  
  8. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 8191) {// Arduino like analogWrite// value has to be between 0 and valueMax
  9.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);// calculate duty, 8191 from 2 ^ 13 - 1
  10.   ledcWrite(channel, duty);// write duty to LEDC
  11. }
  12.  
  13. void setup() {
  14.   ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);// Setup timer and attach timer to a led pin  
  15.   ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
  16. }
  17.  
  18. void loop() {
  19.   ledcAnalogWrite(LEDC_CHANNEL_0, brightness);// set the brightness on LEDC channel 0  
  20.   brightness = brightness + fadeAmount;// change the brightness for next time through the loop:  
  21.   if (brightness <= 700 || brightness >= 899) {// reverse the direction of the fading at the ends of the fade:
  22.     fadeAmount = -fadeAmount;
  23.   }
  24.   delay(100);// wait for 30 milliseconds to see the dimming effect
  25. }

cqf123
Posts: 6
Joined: Sun Jun 12, 2022 8:02 am

Re: esp32 devkitc-1 驱动sg90舵机的问题

Postby cqf123 » Sat Jul 16, 2022 11:23 am

感谢 还是不行

Li Junru
Posts: 11
Joined: Thu Jul 21, 2022 4:03 am

Re: esp32 devkitc-1 驱动sg90舵机的问题

Postby Li Junru » Thu Jul 21, 2022 6:43 am

可以试一下:ServoESP32 这个arduion库

Who is online

Users browsing this forum: No registered users and 16 guests