034 - MicroPython TechNotes: DC Motors | Gear Motor and Fan Motor
Introduction
In this article, we will talk about the DC motors with ESP32 using MicroPython.
Pinout
- GND – for the ground pin.
- VCC – for the supply voltage.
- INA – for the motor terminal pin A.
- INB – for the motor terminal pin B.
Bill Of Materials
- ESP32 development board.
- ESP32 shield.
- 3-pin female-female dupont wires.
- DC Motors: Gear Motor and Fan Motor modules.
Hardware Instruction
- Attach the ESP32 board on top of the ESP32 shield and make sure that the USB port are on the same side.
- Attach the dupont wires to the modules by following the color coding which is black for the ground, red for the VCC, and yellow and the following colors for the signal pins.
- Attach the other end of the dupont wires to the ESP32 shield by matching the colors of the wires to the colors of the pin headers that is black to black, red to red, yellow and the following colors to the yellow pin headers.
- Power the ESP32 shield with an external power supply through a type-C USB cable and make sure that the slide switch is set to ON state.
- Connect the ESP32 to the computer by attaching a micro-USB cable.
Software Instruction
- Copy the sample source code below and paste it to your Thonny IDE.
- Run it then modify according to your needs.
- Enjoy and happy learning.
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 DC motor:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5
6class GORILLACELL_DCMOTORS:
7 def __init__(self, pinA, pinB):
8 self.pinA = Pin(pinA, Pin.OUT)
9 self.pinB = Pin(pinB, Pin.OUT)
10
11 def rotate(self, direction='cw'):
12 if direction=='cw':
13 self.pinA.value(1)
14 self.pinB.value(0)
15 else: # ccw
16 self.pinA.value(0)
17 self.pinB.value(1)
18
19 def stop(self):
20 self.pinA.value(0)
21 self.pinB.value(0)
22
23fan = GORILLACELL_DCMOTORS(25, 26)
24gear = GORILLACELL_DCMOTORS(16, 17)
2. Example # 2, PWM control of DC 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
6
7def map(x, in_min, in_max, out_min, out_max):
8 return int((x - in_min) * (out_max - out_min) /
9 (in_max - in_min) + out_min)
10
11class GORILLACELL_DCMOTORS:
12 def __init__(self, pinA, pinB):
13 self.pinA = PWM(Pin(pinA, Pin.OUT))
14 self.pinB = PWM(Pin(pinB, Pin.OUT))
15 self.pinA.freq(500)
16 self.pinB.freq(500)
17 self.pinA.duty(0)
18 self.pinB.duty(0)
19
20 def rotate(self, direction='cw', speed=40):
21 if direction=='cw':
22 self.pinA.duty(map(speed,0,99,0,1023))
23 self.pinB.duty(0)
24 else: # ccw
25 self.pinA.duty(0)
26 self.pinB.duty(map(speed,0,99,0,1023))
27
28 def stop(self):
29 self.pinA.duty(0)
30 self.pinB.duty(0)
31
32fan = GORILLACELL_DCMOTORS(25, 26)
33gear = GORILLACELL_DCMOTORS(16, 17)
3. Example # 3, simple application controlling a DC motor using a potentiometer:
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 machine import ADC
7from time import ticks_ms
8
9def map(x, in_min, in_max, out_min, out_max):
10 return int((x - in_min) * (out_max - out_min) /
11 (in_max - in_min) + out_min)
12
13class GORILLACELL_DCMOTORS:
14 def __init__(self, pinA, pinB):
15 self.pinA = PWM(Pin(pinA, Pin.OUT))
16 self.pinB = PWM(Pin(pinB, Pin.OUT))
17 self.pinA.freq(500)
18 self.pinB.freq(500)
19 self.pinA.duty(0)
20 self.pinB.duty(0)
21
22 def rotate(self, direction='cw', speed=40):
23 if direction=='cw':
24 self.pinA.duty(map(speed,0,99,0,1023))
25 self.pinB.duty(0)
26 else: # ccw
27 self.pinA.duty(0)
28 self.pinB.duty(map(speed,0,99,0,1023))
29
30 def stop(self):
31 self.pinA.duty(0)
32 self.pinB.duty(0)
33
34fan = GORILLACELL_DCMOTORS(25, 26)
35pot = ADC(Pin(32, Pin.IN))
36pot.atten(ADC.ATTN_11DB)
37
38t_start = ticks_ms()
39pot_new = 0
40pot_old = 0
41
42while True:
43 # check if 500ms has elapsed
44 if ticks_ms() - t_start > 499:
45 # Read the potentiometer value
46 pot_new = pot.read()
47 # Make sure that the value has change
48 if pot_new != pot_old:
49 fan.rotate('ccw', speed=map(pot_new,0,4095,0,99))
50 pot_old = pot_new
51 print('ADC Value: ', pot_new)
52 t_start = ticks_ms()
References And Credits
-
Purchase your copy of Gorillacell ESP32 Development kit at: https://gorillacell.kr
-
Pulse Width Modulation (PWM) reference: https://docs.micropython.org/en/latest/esp32/quickref.html https://docs.micropython.org/en/latest/esp8266/tutorial/pwm.html
-
Analog to Digital Converter (ADC) reference: https://docs.micropython.org/en/latest/esp32/quickref.html https://docs.micropython.org/en/latest/esp8266/quickref.html#adc-analog-to-digital-conversion
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
- 037 - MicroPython TechNotes: Servo Motor
- 036 - MicroPython TechNotes: Stepper Motor
- 035 - MicroPython TechNotes: Dual Motor Driver
- 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!