Hi Folks,
We are back in action with the Pi Pico project. Now let’s add a stepper motor to our project and ask ChatGPT to generate some code that will work with our current project (Project Three).
My ChatGPT query:
Now add a dc motor, include the code, the electronic components needed, and how to connect the motor to the current code.
ChatGPT Response:
Integrating a 5V DC stepper motor with five wires into your Raspberry Pi Pico setup involves using a stepper motor driver. These types of motors are typically unipolar stepper motors. Here’s a detailed guide on the electronic components needed, how to connect the stepper motor, and the necessary code changes.
Components Needed
5V DC Stepper Motor: With five wires, typically a unipolar stepper motor.
ULN2003 Stepper Motor Driver: Suitable for driving unipolar stepper motors.
Power Supply: A 5V power supply for the stepper motor.
Jumper Wires: For connections.
Breadboard: For prototyping connections.
Circuit Connections
Connecting the Stepper Motor to ULN2003 Driver:
The ULN2003 driver is designed for easy connection with a unipolar stepper motor.
Connect the motor wires to the driver board in the order specified in the motor’s datasheet or according to the colors typically matched on the driver board.
Connections from ULN2003 Driver to Raspberry Pi Pico:
Motor Driver IN Pins: Connect these to GPIO pins on the Pico.
VCC (5V) and GND: Connect to the 5V and ground on the Pico or an external 5V supply with a common ground.
Circuit Schematic:
Connect the stepper motor wires to the driver.
Connect the driver input pins (IN1, IN2, IN3, IN4) to Pico GPIO pins (e.g., GPIO 6, GPIO 7, GPIO 8, GPIO 9).
Connect the
VCC
of the ULN2003 to the Pico's 5V pin.Connect
GND
of the ULN2003 to the Pico's ground.
Updated Code
Here’s the updated code with stepper motor control added:
import machine
import utime
from i2c_lcd import I2cLcd
# Define the I2C address of the LCD (use the address detected by the I2C scanner)
I2C_ADDR = 0x27 # Replace with your LCD's I2C address
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# Initialize I2C on bus 1 with SDA on Pin 2 and SCL on Pin 3
i2c = machine.I2C(1, scl=machine.Pin(3), sda=machine.Pin(2), freq=100000)
# Initialize the LCD
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Define GP0 as an output pin for the LED
led_pin = machine.Pin(0, machine.Pin.OUT)
# Define GP1 as an input pin with a pull-up resistor for the button
button_pin = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_UP)
# Stepper motor control pins
stepper_pins = [
machine.Pin(6, machine.Pin.OUT), # IN1
machine.Pin(7, machine.Pin.OUT), # IN2
machine.Pin(8, machine.Pin.OUT), # IN3
machine.Pin(9, machine.Pin.OUT) # IN4
]
# Stepper motor steps for one revolution
step_sequence = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1]
]
# Variables to track button states
button_state = button_pin.value()
prev_button_state = button_state
# Function to display LED and motor state on the LCD
def update_lcd(led_state, motor_state):
lcd.clear() # Clear the LCD before writing new text
utime.sleep_ms(100) # Give a small delay to ensure clearing completes
if led_state:
lcd.move_to(0, 0)
lcd.putstr("LED: ON")
else:
lcd.move_to(0, 0)
lcd.putstr("LED: OFF")
if motor_state:
lcd.move_to(0, 1)
lcd.putstr("Motor: RUNNING")
else:
lcd.move_to(0, 1)
lcd.putstr("Motor: STOPPED")
# Function to control stepper motor steps
def step_motor(steps, direction=1, delay=10):
for _ in range(steps):
for step in step_sequence[::direction]:
for pin, value in zip(stepper_pins, step):
pin.value(value)
utime.sleep_ms(delay)
# Display initial states
update_lcd(led_pin.value(), False)
# Main loop
while True:
# Read the state of the button
button_state = button_pin.value()
# Check if the button state has changed (active low)
if button_state != prev_button_state:
# Debounce delay
utime.sleep_ms(20)
# Confirm the state change
if button_state == button_pin.value():
# If the button is pressed
if button_state == 0:
# Toggle the LED
led_pin.toggle()
# Update the LCD with the new LED state
motor_running = led_pin.value() == 1
update_lcd(led_pin.value(), motor_running)
# If LED is ON, run the stepper motor
if motor_running:
step_motor(512, direction=1, delay=5) # Run motor for 512 steps (one revolution)
else:
for pin in stepper_pins:
pin.value(0) # Stop the motor by setting all pins to low
# Update the previous button state
prev_button_state = button_state
# Short delay to reduce CPU usage
utime.sleep_ms(50)
Notes
Power Supply: Ensure the motor driver and motor are powered correctly. If using the Pico's 5V output, ensure it can supply sufficient current. A separate 5V power supply might be needed for the motor, with common ground.
Stepper Motor Steps: Adjust the
steps
parameter in thestep_motor
function to match the number of steps per revolution of your stepper motor.Delay Adjustment: The
delay
parameter instep_motor
can be adjusted to control the speed of the motor. Reducing the delay will increase the motor speed.
This setup provides a way to control a 5V unipolar stepper motor, adding stepper motor functionality to the existing LED and LCD system.
Alright, let’s give this a shot and see what happens.
Videos:
It works!
Another successful ChatGPT generated code for our project.
Fritzing Schematic:
Links: