037 - MicroPython TechNotes: Servo Motor
Introduction
In this article, I will talk about SERVO MOTOR with ESP32 using MicroPython.
Bill Of Materials
- ESP32 development board.
- Gorillacell ESP32 shield.
- Servo Motor
Pinout
- brown wire – for the ground pin.
- red wire – for the servo motor supply voltage.
- orange wire – for the servo motor control signal.
Hardware Instruction
- First, attach the ESP32 board on top of the GorillaCell ESP32 shield and make sure that both USB ports are on the same side.
- Next, attach the servo motor wires to ESP32 shield by the following color coding:
- servo motor brown wire – shield black pin headers.
- servo motor red wire – shield red pin headers.
- servo motor orange wire – shield yellow pin headers.
For this lesson, I choose GPIO 25 to serve as the control signal pin to the servo motor.
- Next, power the GorillaCell ESP32 shield with an external 5V power supply with a type-USB connector.
- Last, connect the ESP32 to the computer through a micro-USB cable.
Software Instruction
For the software part, copy the sample program from the SOURCE CODE section.
You may modify and adapt it according to your needs, and most of all enjoy.
Video Demonstration
Call To Action
For any concern, write your message in the comment section.
You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.
Thank you and have a good days ahead.
See you,
– George Bantique | tech.to.tinker@gmail.com
Source Code
1. Example # 1, exploring the basics of controlling a servo motor:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5from machine import PWM
6from time import sleep_ms
7
8def map(x, in_min, in_max, out_min, out_max):
9 return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
10
11class GORILLACELL_SERVO:
12 def __init__(self, signal_pin):
13 self.pwm = PWM(Pin(signal_pin), freq=50, duty=0)
14
15 def rotate(self, angle):
16 self.pwm.duty(map(angle, 0, 180, 23, 124))
17
18
19servo = GORILLACELL_SERVO(signal_pin=25)
20
21# The following lines of codes can be tested using the REPL:
22# # To rotate the servo motor to 0 degrees
23# servo.rotate(0)
24#
25# # To rotate the servo motor to 90 degrees
26# servo.rotate(90)
27#
28# # To rotate the servo motor to 180 degrees
29# servo.rotate(180)
30#
31# # To rotate the servo from 0 to 180 degrees
32# # by 10 degrees increment
33# for angle in range(0, 181, 10):
34# servo.rotate(angle)
35# print(angle)
36# sleep_ms(500)
37# # To rotate the servo from 180 to 0 degrees
38# # by 10 degrees decrement
39# for angle in range(180, -1, -10):
40# servo.rotate(angle)
41# print(angle)
42# sleep_ms(500)
References And Credits
-
Purchase your Gorillacell ESP32 development kit at: https://gorillacell.kr
-
MG90S Timing diagram from: https://components101.com/motors/mg90s-metal-gear-servo-motor
Posts in this series
- 049 - MicroPython TechNotes: MP3 Player
- 048 - MicroPython TechNotes: Analog Touch Sensor
- 047 - MicroPython TechNotes: E108 GPS
- 046 - MicroPython TechNotes: RF433 Transceivers
- 045 - MicroPython TechNotes: Infrared Transmitter
- 044 - MicroPython TechNotes: Infrared Receiver
- 043 - MicroPython TechNotes: ESP12E WiFi | External WiFi module
- 042 - MicroPython TechNotes: JDY-32 | Bluetooth Low Energy BLE
- 041 - MicroPython TechNotes: Bluetooth HC-06
- 040 - MicroPython TechNotes: Relay
- 039 - MicroPython TechNotes: Electromagnet
- 038 - MicroPython TechNotes: Buzzer
- 036 - MicroPython TechNotes: Stepper Motor
- 035 - MicroPython TechNotes: Dual Motor Driver
- 034 - MicroPython TechNotes: DC Motors | Gear Motor and Fan Motor
- 033 - MicroPython TechNotes: TCS34725 RGB Color Sensor
- 032 - MicroPython TechNotes: BMP280 Sensor
- 031 - MicroPython TechNotes: TOF Distance Sensor
- 030 - MicroPython TechNotes: DS3231 RTC
- 029 - MicroPython TechNotes: HC-SR04 Ultrasonic Sensor
- 028 - MicroPython TechNotes: DHT11 Temperature and Humidity Sensor
- 027 - MicroPython TechNotes: Rotary Encoder
- 026 - MicroPython TechNotes: Light Dependent Resistor (LDR)
- 025 - MicroPython TechNotes: Joystick
- 024 - MicroPython TechNotes: Slider Switch
- 023 - MicroPython TechNotes: Continuous Rotation Potentiometer
- 022 - MicroPython TechNotes: Potentiometer | Reading an Analog Input
- 021 - MicroPython TechNotes: Color Touch Sensor
- 020 - MicroPython TechNotes: Touch Sensor
- 019 - MicroPython TechNotes: Switch Module
- 018 - MicroPython TechNotes: Button | Reading an Input
- 017 - MicroPython TechNotes: LASER Module
- 016 - MicroPython TechNotes: RGB LED Matrix
- 015 - MicroPython TechNotes: Neopixel 16
- 014 - MicroPython TechNotes: 8x8 Dot Matrix Display (I2C)
- 013 - MicroPython TechNotes: 8x16 Dot Matrix Display (SPI)
- 012 - MicroPython TechNotes: 8x8 Dot Matrix Display (SPI)
- 011 - MicroPython TechNotes: 1.3 OLED Display
- 010 - MicroPython TechNotes: 0.96 OLED Display
- 009 - MicroPython TechNotes: 7 Segment Display
- 008 - MicroPython TechNotes: 16x2 LCD
- 007 - MicroPython TechNotes: RGB LED
- 006 - MicroPython TechNotes: Traffic Light LED Module
- 005 - MicroPython TechNotes: Gorilla Cell LED | MicroPython Hello World
- 004 - MicroPython TechNotes: Gorilla Cell I/O Devices
- 003 - MicroPython TechNotes: Gorillacell ESP32 Shield
- 002 - MicroPython TechNotes: Introduction for Gorillacell ESP32 Dev Kit
- 001 - MicroPython TechNotes: Get Started with MicroPython
- 000 - MicroPython TechNotes: Unboxing Gorillacell ESP32 Development Kit
No comments yet!