MicroPython is installed in ESP32 in advance in the above.
This time I decided to connect it to the G2 pin. See Pins and GPIOs (https://micropython-docs-ja.readthedocs.io/ja/latest/esp32/quickref.html#pins-and-gpio) for available pins. Since the required voltage of the servo I bought was 4.8V ~ 6V, it is connected to 5V.
How to use PuTTY is described in the postscript of the above.
Enter the command line by line in PuTTY and press Enter to check the range of motion of the servo.
Declaration to use PWM pulse and GPIO pin
from machine import PWM, Pin
I named it servo1 with the frequency of pin 2 set to 50.
It seems that the rotation speed is determined by the strength of the frequency.
servo1 = PWM(Pin(2), freq = 50)
Changed the duty ratio of the servo. With this, adjust the angle many times and check the range of motion. Fine-tune in the range of 30 to 120. When the frequency is set to 100, it seems to be 60 to 240.
servo1.duty(30)
servo1.duty(0)
When I checked it while turning it, the minimum and maximum limits of my servo were 25 to 130.
I named it main.py and wrote the following simple code and saved it on my desktop. sleep is important. Without it, an infinite loop would occur and the ESP32 could not be overwritten with new code. If you can't overwrite it, you need to erase the flash and re-insert MicroPython.
main.py
# -*- coding: utf-8 -*-
from machine import PWM, Pin
import time
servo1 = PWM(Pin(2), freq = 50)
#Repeat angle 30 (minimum) and angle 120 (maximum) every 2 seconds
while True:
servo1.duty(30)
time.sleep_ms(2000)
servo1.duty(120)
time.sleep_ms(2000)
Install the tool to send the code from the command prompt
pip install adafruit-ampy
Ampy command confirmation
ampy
You can confirm by hitting.
I saved main.py to my desktop so I moved it to the bottom of my desktop
cd Desktop
Send the main.py file to the ESP32. Change the COM3 part to the port you are connected to.
ampy -p COM3 put main.py
It worked safely.
▼ ESP32 parts for Fritzing https://qiita.com/XZ_Manj/items/8bbdef59eeac4286e4f8
▼ Pin and GPIO https://micropython-docs-ja.readthedocs.io/ja/latest/esp32/quickref.html#pins-and-gpio
▼ Hada memo Move the servo by adjusting the pulse width https://scrapbox.io/hada/ESP32でサーボモータ[2018/10/29]_22:09:36
Recommended Posts