CircuitPython exercise to control servo motor, run on ESP32-S3-Zero/CircuitPython 9.0.0-beta.0.

CircuitPython exercise to control servo motor SG90, run on ESP32-S3-Zero/CircuitPython 9.0.0-beta.0.


cpyS3Zero-servo.py
"""
CircuitPython exercise to control servo motor
run on ESP32-S3-Zero/CircuitPython 9.0.0-beta.0

ref:
https://learn.adafruit.com/circuitpython-essentials/circuitpython-servo
https://learn.adafruit.com/using-servos-with-circuitpython/circuitpython

lib needed:
- adafruit_motor folder

Connection:
(a external 5V power source is used for servo motor)
Ext.5V      Servo          ESP32-S3-Zero
----------------------------------------
GND         GND(Brown)     GND
+5V         V+(Red)
            Signal(Yellow) IO7
"""
import os, sys
import time
import board
import pwmio
from adafruit_motor import servo

pin_signal = board.IO7
# create a PWMOut object on Pin GPIO7.
pwm = pwmio.PWMOut(pin_signal, duty_cycle=2**15, frequency=50)

# Create a servo object, my_servo.
my_servo = servo.Servo(pwm, min_pulse=500, max_pulse=2500)

print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
       'run on ' + os.uname()[4]
print(info)
print("=================================================")
print(servo.__name__, servo.__version__)

def convAngleToBar(ang):
    bar = ""
    for a in range(int(ang/5)):
        bar = bar + "="
    for a in range(int((180-ang)/5)):
        bar = bar + " "
    return bar

while True:
    for angle in range(0, 180, 5):  # 0 - 180 degrees, 5 degrees at a time.
        my_servo.angle = angle
        print("{:03}".format(angle), convAngleToBar(angle), end="\r")
        time.sleep(0.1)
    for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
        my_servo.angle = angle
        print("{:03}".format(angle), convAngleToBar(angle), end="\r")
        time.sleep(0.1)



next:
~ Radar-like distance scanner on ESP32-S3/CircuitPython 9, using SG90 servo motor, VL53L0X distance sensor and SSD1306 OLED.

Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module