Raspberry Pi Pico(9): Control ULN2003APG stepper motor using joystick

From this article we will study how the control motors, and learn how to use Pi Pico (using MicroPython language) to control motors, steppers and servo motors. When I started to learn Raspberry Pi in the early days, I used ULN2003APG, which was inserted on the breadboard to connect the Raspberry Pi and the motor. For the implementation and stepper motor related knowledge, please refer to: Raspberry Pi Notes (12): Controlling the stepper motor . This article will implement how to determine whether the joystick is rocking left and right or up and down. By reading the analog signal value of the joystick, it can control the forward and reverse rotation of the stepping motor and the number of rotation steps.

[Material]

  • Raspberry Pi Pico x1
  • 28BYJ-48 stepping motor (ULN2003APG) module x1
  • Joystick x1
  • Breadboard x1
  • Breadboard power module x1
  • Wires X N

[Wiring diagram]

The 5V power supplied by the breadboard is directly connected to the positive and negative poles of the ULN2003APG module, and the other pins are as follows:
Pi Pico接腳ULN2003APG模組搖桿
Pin 36(3.3V)-VCC
Pin 38(GND)-GND
Pin 1(GP0)IN1-
Pin 2(GP1)IN2-
Pin 4(GP2)IN3-
Pin 5(GP3)IN4-
Pin 21(GP16)-Button/SCL
Pin 31(GP26)-VRy
Pin 32(GP27)-VRx



[Code]

This program uses a library: Stepper

It has two important functions in this library:
  • Number of turning steps: step(count, direction=1)
        count is the number of steps to be rotated, derection is the direction of rotation, 1 is forward rotation, -1 is reverse rotation
  • Rotate to a specific angle: angle(r, direction=1)
        r is a value from 0 to 360 degrees, which allows the stepping motor to rotate to a specified angle.

The joystick is connected to the two pins 26 and 27 of the analog signal ADC. The range of the analog signal value is 2^12, that is, the signal value is between 0 and 65535. When the joystick value is judged to be less than 5000, it means the control step When the motor rotates forward, when it is greater than 60000, the motor is controlled to reverse.

from machine import Pin, ADC
import Stepper
import utime

s1 = Stepper.create(Pin(0,Pin.OUT),Pin(1,Pin.OUT),Pin(2,Pin.OUT),Pin(3,Pin.OUT), delay=2)

xAxis = ADC(Pin(27))  #Read analog signal
yAxis = ADC(Pin(26))
button = Pin(16,Pin.IN, Pin.PULL_UP)

while True:
    xValue = xAxis.read_u16()
    yValue = yAxis.read_u16()
    buttonValue = button.value()

    print(xValue, end = '')   #Don't skip the line and add end=''
    print('    ', end = '')
    print(yValue)
    
    if xValue ≤ 5000 or yValue ≤ 5000:      #Move left or up
         s1.step(5)
    elif xValue ≥ 60000 or yValue ≥ 60000: #Move right or down
         s1.step(5,-1)
    utime.sleep(0.1)     

[Result]




[Reference]

Post a Comment

Previous Post Next Post