Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Raspberry Pi Pico controls stepper motor through serial port commands

Status
Not open for further replies.

Justinli

Member level 4
Member level 4
Joined
Apr 29, 2021
Messages
76
Helped
0
Reputation
0
Reaction score
2
Trophy points
8
Activity points
413
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. A serial port HMI display (Customized Arduino 7-inch display)

5. Several connecting lines

Compile and debug software: Thonny 3.3.5
--- Updated ---

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.
a3vyv4ponp4ix9h9s9i5afs042pna5qg.png

--- Updated ---

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.
xhsi76c5j9mcudrlzf1oobccutozi0hl.png

--- Updated ---

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

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

Actual connection:
7xfamxv721je7vl3937g43eqjuo5pnkk.png

s39cknbrj7oepkug8ahvtxo7duqyzzge.png

bkxyi1bpgm57g8kx63oe4iknoqddjnpj.png

--- Updated ---

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:
b7q1g9y8qbt4ffsl1zthxqtmstd4abxr.png

--- Updated ---

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.
c6rh73iihmqbr4pl1ysjvd7d50dnk33x.png

--- Updated ---

Finally, attach the program code:

from machine import UART, Pin
import time
Code:
#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()
--- Updated ---

2d6zkvh882jk6duvl0mnd9qt0ios2hwa.png

--- Updated ---

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.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top