ESP32控制8个步进电机同时运动(74HC595)
连线图+程序+仿真
https://wokwi.com/projects/333693682987303506
通过使用74HC595
模块进行IO扩展,实现esp32
使用3个引脚控制8个步进电机同时运动
Arduino程序
所用到的库:
#include <Arduino.h>
#include <ShiftRegister74HC595.h>
// https://github.com/Simsso/ShiftRegister74HC595
const int dataPin = 5; /* DS */
const int clockPin = 19; /* SHCP */
const int latchPin = 18; /* STCP */
ShiftRegister74HC595<2> sr(dataPin, clockPin, latchPin);
// 电机正转
void forward(unsigned long STEPS_PER_REV)
{
unsigned int delayTime = 800;
// 设置电机顺时针旋转
// digitalWrite(dirPin, LOW);
sr.set(6, LOW);
sr.set(4, LOW);
sr.set(2, LOW);
sr.set(0, LOW);
sr.set(14, LOW);
sr.set(12, LOW);
sr.set(10, LOW);
sr.set(8, LOW);
// 电机慢速旋转
for (int x = 0; x < STEPS_PER_REV; x++)
{
// digitalWrite(stepPin, HIGH);
sr.set(7, HIGH);
sr.set(5, HIGH);
sr.set(3, HIGH);
sr.set(1, HIGH);
sr.set(15, HIGH);
sr.set(13, HIGH);
sr.set(11, HIGH);
sr.set(9, HIGH);
delayMicroseconds(delayTime);
// digitalWrite(stepPin, LOW);
sr.set(7, LOW);
sr.set(5, LOW);
sr.set(3, LOW);
sr.set(1, LOW);
sr.set(15, LOW);
sr.set(13, LOW);
sr.set(11, LOW);
sr.set(9, LOW);
delayMicroseconds(delayTime);
}
}
void setup()
{
}
void loop()
{
forward(200);
delay(1000);
}
74HC595串联连线
8位串行输入并行输出(SIPO)移位寄存器 使用74HC595移位寄存器扩展微控制器上的输出引脚数量。
引脚名称
Pin | Description |
---|---|
DS | Serial input |
SHCP | Serial clock |
STCP | Storage (latch) pin |
OE | Output enable, active low. Connect to GND if not used. |
Q0…Q7 | Parallel output |
Q7S | Serial output* |
MR | Reset (clear), active low. Connect to VCC if not used |
GND | Ground |
VCC | Supply voltage |
使用 Q7S 将多个 74HC595 单元链接在一起 : 将 Q7S 连接到下一个 74HC595 芯片的 DS 引脚。
注:SHCP、STCP也需连接