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

  1. GND – for the ground pin.
  2. VCC – for the supply voltage.
  3. INA – for the motor terminal pin A.
  4. INB – for the motor terminal pin B.

Bill Of Materials

  1. ESP32 development board.
  2. ESP32 shield.
  3. 3-pin female-female dupont wires.
  4. DC Motors: Gear Motor and Fan Motor modules.

Hardware Instruction

  1. Attach the ESP32 board on top of the ESP32 shield and make sure that the USB port are on the same side.
  2. 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.
  3. 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.
  4. 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.
  5. Connect the ESP32 to the computer by attaching a micro-USB cable.

Software Instruction

  1. Copy the sample source code below and paste it to your Thonny IDE.
  2. Run it then modify according to your needs.
  3. 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

  1. Purchase your copy of Gorillacell ESP32 Development kit at: https://gorillacell.kr

  2. Pulse Width Modulation (PWM) reference: https://docs.micropython.org/en/latest/esp32/quickref.html https://docs.micropython.org/en/latest/esp8266/tutorial/pwm.html

  3. 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



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.