- Assignment
Output devices.
Group assignment is
here
Task. Program the xiao-esp32s3 microcontroller to control the servo motor.
For this, I will use xiao-esp32s3 and servo sg90.
What is servo sg90
Servo drives are a family of devices consisting of a motor and a negative feedback control system. The main distinguishing feature of such drives is the ability to precisely control motion parameters, for example, speed, force or shaft position. In the hobby, servos usually control the last parameter - the position of the shaft.
The logic of the servo drive is quite simple. A control PWM signal is supplied to the input, which is compared with the signal generated by the feedback system. And if the feedback signal pulse duration is shorter than the PWM signal pulse duration, then the motor rotates in one direction, and if it is longer, then in the opposite direction. If the signal pulses coincide, then the engine remains motionless.
The important point here is that the duration of the feedback pulses is regulated by a built-in potentiometer, the position of the shaft of which is set by the motor shaft through a gear drive: where the motor shaft goes, so does the potentiometer handle. Thus, any movement of the motor shaft will be followed by a change in the length of the feedback signal pulses. This coupling forms feedback. You can see it in the servomotor diagram above.
Step 1. Conect board to Arduino IDE
Add this link to Arduino's board manager (File --> Preferences --> Additional board manager URL's)
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json"
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
Step 2. Board manager: ESP32, Version 2.x. Version 3 is producing error messages with the servo libraries.
Resist when your Arduino IDE prompts you to update the board information!
At first, I did not pay attention to the fact that the board library must be version 2.x.x. (ie older). Therefore, this caused a conflict with the library. To fix this, you need to go to the board manager and select the previous version next to the installed library.
It is also worth paying attention that when re-downloading the sketches to the xiao board, you should hold the boot button on the board.
Step 3: Software: Install the Library
Download the servo library from github.
Open the Arduino IDE, Sketch --> Include Library --> Add .ZIP Library --> choose ZIP file
Step 4: Software: the Sweep Sketch
It is important that you specify the GPIO number for input/output in the ESP32-S3 program.
The for-loop is for sweeping the servo from 30° to 150°. Increasing the delay would slow down the movement.
To connect the servo motor to the board, you need to use the following scheme
WARNING! You need to use a servo with a rotation angle of 0 - 180 degrees. The first time I connected a servo with a 0-360 degrees turn angle and my servo went crazy. It perceives commands to turn to a certain angle as commands for the speed of rotation in a circle. that is, it simply rotates with greater or lesser speed around the axis.
after compiling and downloading the sketch got the result. the servo rotates
for (int i = 30; i <= 150; i++) {
servo1.write(i);
delay(22);
}
for (int i = 150; i >= 30; i--) {
servo1.write(i);
delay(22);
}
This part of the code is responsible for moving the servo up to 150 degrees and back at intervals of 30.
Added a laser pointer to the servo motor. To do this, you need to connect the laser diode to the power and control diode pins. Red wire to power, blue to pin 2.
To limit the current flowing through the diode, a resistor is usually placed in series with the diode.I predicted this on the board I developed in week 8 (link at the bottom of the page). Calculations of the resistor rating are also given there.