Experiment 35: Stepper Motor Experiment
From Diustou Wiki
Contents
Arduino
Experimental Phenomenon
- The motor is rotating slowly.
Circuit Connection
Reference Program
#include <Arduino.h> #define A1 2 //Pin naming #define B1 3 #define C1 4 #define D1 5 void setup() { pinMode(A1,OUTPUT); /Set pins as output pins pinMode(B1,OUTPUT); pinMode(C1,OUTPUT); pinMode(D1,OUTPUT); } void loop() { Phase_A(); //Set phase A delay(5); //Changing the delay changes the rotation speed Phase_B(); //Set phase B delay(5); Phase_C(); //Set phase C delay(5); Phase_D(); //Set phase D delay(5); } void Phase_A() { digitalWrite(A1,HIGH); //Set A1 pin high digitalWrite(B1,LOW); digitalWrite(C1,LOW); digitalWrite(D1,LOW); } void Phase_B() { digitalWrite(A1,LOW); digitalWrite(B1,HIGH); //Set B1 pin high digitalWrite(C1,LOW); digitalWrite(D1,LOW); } void Phase_C() { digitalWrite(A1,LOW); digitalWrite(B1,LOW); digitalWrite(C1,HIGH); //Set C1 pin high digitalWrite(D1,LOW); } void Phase_D() { digitalWrite(A1,LOW); digitalWrite(B1,LOW); digitalWrite(C1,LOW); digitalWrite(D1,HIGH); //Set D1 pin high }
Raspberry Pi
Circuit Connection
Program Execution
Python
- Install the gpiozero library
- You can use the following command to install the library:
sudo apt update sudo apt install python3-gpiozero
- For other systems on the Raspberry Pi, you can use the following command to install the library:
sudo pip3 install gpiozero
- Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
- Set the DIP switch on the ULN2003 driver module to the ON position
- Download the Raspberry Pi reference example, unzip the file, copy it to the user directory, and run it:
cd raspberrypi/35/python_gpiozero python motor.py
- You can now see the Raspberry Pi running the program correctly. To exit, press ctrl+C.
- Command description: LEDBoard(pin, pwm, active_high, initial_value)
- Main parameters:
- pin: GPIO pin number,
- pwm:
- If True, constructs a PWMLED instance for each pin.
- If False (default), constructs a regular LED instance.
- active_high: Internal pull-up/pull-down resistor setting,
- When set to True (default), on() sets the pin to HIGH and off() sets the pin to LOW.
- When set to False, on() sets the pin to LOW and off() sets the pin to HIGH.
- initial_value:
- If False (default), all LEDs are initially off.
- If None, all LEDs are in an unstable initial state.
- If True, all LEDs are initially on.
- Main parameters:
- For more commands, please refer to the gpiozero documentation