• Author
    Posts
  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Now that the above conditions are ready, let’s start the experiment:

    Before connecting, we need to flash the firmware for Pico. The method is to press and hold the bootsel button on the Pico board to connect to the computer through the USB port. At this time, the Pico will be recognized as a removable hard disk. After opening, there are two File, one of which is the official website link, we click on it

    Raspberry Pi Pico controls stepper motor through serial port commands

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Raspberry Pi Pico controls stepper motor through serial port commands

    Then find the Download UF2 file to download the MicroPython firmware, after downloading it, copy it to the mobile hard disk RPI-RP2, Pico will automatically restart and the mobile hard disk will pop up, which means that the firmware has been successfully flashed, and it is best to reconnect at this time USB cable.

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Below we determine the connection of each part according to Pico’s pin diagram:

    I use a five-wire four-phase motor, so I need to connect four gpio lines to the Pico. Here I choose IN1, IN2, IN3, and IN4 of the drive circuit to correspond to GP9, GP10, GP11, and GP12 on the Pico. , And then the drive circuit also needs a voltage of 5-12V, so here I use the VBUS on the Pico board to provide it with a 5v voltage, and then connect the negative pole of the drive circuit to the GND of the Pico, then this part of the connection is finished.

    The following is the connection to the serial screen. There are four sets of UART provided on the Pico board. Choose one of them for connection. Connect the serial screen. The TXP of the serial screen is connected to the RX of the Pico, the RXP of the serial screen is connected to the TX of Pico, and the GND is connected to GND.

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Actual connection:

    Raspberry Pi Pico controls stepper motor through serial port commands

    Raspberry Pi Pico controls stepper motor through serial port commands

    Raspberry Pi Pico controls stepper motor through serial port commands

     

     

     

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Then there is the software debugging part:

    Open Thonny and select Raspberry Pi Pico in the lower right corner. When the shell command window prompts as follows, it means that Thonny and Pico are successfully connected:

    Raspberry Pi Pico controls stepper motor through serial port commands

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    When the following prompt appears, it usually means that Pico is running a program. You can press Ctrl+C to interrupt the program according to the prompt.

    Raspberry Pi Pico controls stepper motor through serial port commands

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Finally, attach the program code:

    from machine import UART,Pin
    import time

    #Define the serial port
    uart1 = UART(1, baudrate = 115200, tx = Pin(4), rx = Pin(5))

    #Define five-wire four-phase motor pins
    motor1a = Pin(9,Pin.OUT)
    motor1b = Pin(10,Pin.OUT)
    motor1c = Pin(11,Pin.OUT)
    motor1d = Pin(12,Pin.OUT)

    def forward(delay, maxspeed): #Stepper motor forward function, define stepper motor as double four-shot drive mode
    for i in range(0, maxspeed):
    motor1a.high()
    motor1b.high()
    motor1c.low()
    motor1d.low()
    time.sleep(delay)
    motor1a.low()
    motor1b.high()
    motor1c.high()
    motor1d.low()
    time.sleep(delay)
    motor1a.low()
    motor1b.low()
    motor1c.high()
    motor1d.high()
    time.sleep(delay)
    motor1a.high()
    motor1b.low()
    motor1c.low()
    motor1d.high()
    time.sleep(delay)

    def backward(delay, maxspeed): #Stepper motor reversal function
    for i in range(0, maxspeed):
    motor1a.low()
    motor1b.low()
    motor1c.high()
    motor1d.high()
    time.sleep(delay)
    motor1a.low()
    motor1b.high()
    motor1c.high()
    motor1d.low()
    time.sleep(delay)
    motor1a.high()
    motor1b.high()
    motor1c.low()
    motor1d.low()
    time.sleep(delay)
    motor1a.high()
    motor1b.low()
    motor1c.low()
    motor1d.high()
    time.sleep(delay)

    def stop(): #Motor stop function
    motor1a.low()
    motor1b.low()
    motor1c.low()
    motor1d.low()

    def dianji():
    forward(0.005, 512)
    stop()
    time.sleep(3)
    backward(0.005, 512)
    stop()
    time.sleep(3)

    def zrun():
    forward(0.005, 512)

    def frun():
    backward(0.005, 512)

    #counti = 0
    #list2 = []
    motor_off = [‘S’, ‘T’, ‘<‘, ‘\x10’, ‘\x10’, ‘\x00’, ‘\x07’, ‘s’, ‘w’, ‘i’, ‘t’, ‘c’, ‘h’, ‘\x00’, ‘>’, ‘E’, ‘T’]#Serial screen command, you can modify it by yourself
    motor_on = [‘S’, ‘T’, ‘<‘, ‘\x10’, ‘\x10’, ‘\x00’, ‘\x07’, ‘s’, ‘w’, ‘i’, ‘t’, ‘c’, ‘h’, ‘\x01’, ‘>’, ‘E’, ‘T’]
    time.sleep(1)

    while True:
    counti = 0
    list2 = []
    try:
    rxdata = uart1.read(40)
    list1 = list(rxdata)
    except BaseException as e:
    print(‘串口没有接收到数据’)
    time.sleep(3)
    else:
    #list1 = list(jieshou)
    for item in list1:
    list2.append(chr(item))
    counti += 1
    if item == 62: #The serial port returns the decimal value of the ASCII code of the end sign’>’ in the command data
    for item2 in range(2): #When the’>’ is read, two characters will be read later to display the complete command, but there are actually two check digit                                                    #characters behind
    counts = list1[counti]
    list2.append(chr(counts))
    counti += 1
    break

    print(list2)
    if list2 == motor_on:
    zrun()
    elif list2 == motor_off:
    stop()
    uart1.sendbreak()

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62
  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    The program demonstration effect is: when the swtich on the screen is pressed to open, the motor rotates forward, and when it is pressed to close, the motor rotates backward, which can be modified as needed.

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    Another point to note is that the above process is debugged and run in the Thonny environment. If you want to download the program to the pico board, just use the save button of Thonny to rename the python file to main.py and save it in the internal space of pico. That’s it

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    In the next chapter, I will introduce a project demonstration that uses pico’s dual-threaded parallel and asynchronous processing tasks.

  • Clay GradyClay Grady
    Participant
    @grady
    Post count: 7

    hello,

    Maybe it’s a simple question, but I’d like to ask what does the try except do?

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    ‘try except else’ is a fixed usage, to judge whether there is data in the buffer, if it is not added, an error will be reported, because uart received None at the beginning of the serial port

    • Clay GradyClay Grady
      Participant
      @grady
      Post count: 7

      Thank you very much, I get it.

  • 前史蒂夫·威尔金森前史蒂夫·威尔金森
    Participant
    @formerly_steve_wilkinson
    Post count: 3

    Hi,

    I tried your program, the program works in the compiler, but when pico executes the program alone, it doesn’t work.

    Can you tell me why?

  • bowei zhangbowei zhang
    Participant
    @zbw3366
    Post count: 62

    I have encountered this situation many times, usually because your grammar has logical problems, you can try to check and modify the grammar, or provide your code, I will help you to see

  • retnavairretnavair
    Participant
    @retnavair
    Post count: 16

    Hi,

    Why it doesn’t work with uart. readline()?

You must be logged in to reply to this topic.