Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Raspberry Pi Pico controls stepper motor through serial port commands with STONE HMI display

Thu May 27, 2021 9:36 am

This project is implemented by using Raspberry Pi Pico under Thonny compiler with MicroPython language

The work that needs to be prepared before the start of the project is as follows:

hardware:

1. A Raspberry Pi pico board (from:https://www.raspberrypi.org/products/raspberry-pi-pico/)

2. 28byj-48 stepper motor one (from: https://components101.com/motors/28byj-48-stepper-motor)

3. A stepping motor drive circuit

4. STONE serial port HMI display (Customized Arduino 7-inch display) (from: https://www.stoneitech.com/)

5. Several connecting lines

Compile and debug software: Thonny 3.3.5
Last edited by Dylanli on Fri May 28, 2021 12:59 am, edited 2 times in total.

Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:37 am

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 Files, one of which is the official website link, we click on it.
Image

Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:37 am

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

Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:38 am

Below we determine the connection of each part according to Pico’s pin diagram:
Image
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.


Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:39 am

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:
Image

Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:40 am

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

Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:40 am

Finally, attach the program code:

from machine import UART, Pin
import time

Code: Select all

#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()


Dylanli
Posts: 28
Joined: Mon May 24, 2021 6:03 am

Re: Raspberry Pi Pico controls stepper motor through serial port commands

Thu May 27, 2021 9:41 am

The program demonstration effect is: when the switch 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.
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.

Return to “General”