Tutorial VI: PWM Servo Control Demo

From Waveshare Wiki
Jump to: navigation, search

Modules Usage Tutorial

PWM Servo Control

The General Driver for Robots board has a module for sending PWM signals. The PWM servo can receive the PWM signal by connecting the IO port on the board, so as to control the PWM servo by the driver board. The following provides a simple rotation demo for the PWM servo.

Demo

Upload Demo

After downloading the dependency library ESP32Servo, unzip the downloaded file into the folder with path C:\Users\Username\AppData\Local\Arduino15\libraries.
PWM Servo Control Demore.png
After downloading the zip file, open pwmServo.ino, connect the multifunction driver board to the computer with a USB cable (the USB Type-C port of the multifunction driver board is inserted here), click "Tools" → "Ports", and then click "COM". Click on the newly appeared COM.
UGV1 doenload03EN.png
In Arduino IDE, click "Tools" → "Development Board" → "ESP32" → "ESP32 Dev Module". Upload the demo after selecting the board and port. After uploading the demo, connect the PWM servo and PWM signal servo interface, connect the XH2.54 power supply interface to the power supply, and run the demo, you can see the PWM servo will swing back and forth.

Demo Analysis

#include <ESP32_Servo.h>
Servo pwmServo;    //Create pwm object

#define PSERVO_PIN 4    //Set servo pin

int pwmServoChannel = 7;    //Set servo channel
int pwmServoInitPos = 90;    //Set the initial position of the servo

void pwmServoInit(){
  pwmServo.attach(PSERVO_PIN);    //Binding servo pins to pwm objects  
}

void setup() {
  pwmServoInit();    //Initialize the servo
}

void loop() {
  pwmServo.write(50);    //Set the servo position to 50 degrees
  delay(1000);
  pwmServo.write(90);    //Set the servo position to 90 degrees
  delay(1000);
}

Resource