香橙派orangepi c# iot .net 控制ULN2003驱动板步进电机 代码实例
设备:
1、香橙派
2、ULN2003驱动板,28BYJ-48步进电机
刚接触的时候,我也是看到不太懂,看懂了之后现在总结一下
28BYJ-48步进电机名称含义:
28:表示步进电机的有效最大外径为28毫米
B: 表示步进电机“步”字汉语拼音首字母
Y: 表示永磁式“永”字汉语拼音首字母
J: 表示减速型“减”字汉语拼音首字母
BYJ: 组合即为永磁式减速步进电机
48:表示四相八拍
5V:表示额定电压为5V,且为直流电压
查看说明书内容

减速比为1/64的意思是电机内部转64圈,电机外部才相应地转1圈,而这个步进电机是四相八拍的,八拍中一拍就是一个脉冲信号,完成一个内循环要8个脉冲信号。
内部 64圈=外部1圈
内部1圈=8个脉冲信号
所以看到电机转一圈,是要发送 64*8=512个脉冲信

ULN2003驱动板上面的IN1,IN2,IN3,IN4的功能作用如下:


- using System.Device.Gpio;//nuget中下载
- /// <summary>
- /// //步进电机28byj48+uln2003驱动板4相5线
- /// </summary>
- public static class Device_Stepper_uln2003_Service
- {
- static int pin_19 = 19; //接In1
- static int pin_26 = 26;//接In2
- static int pin_16 = 16;//接In3
- static int pin_20 = 20;//接In4
-
- /// <summary>
- /// 调用示例
- /// </summary>
- public static void test()
- {
- while (true)
- {
- Console.WriteLine("输入b逆时针转,输入f顺时针转");
- //控制台中输入内容
- string key = Console.ReadLine();
- if (!string.IsNullOrWhiteSpace(key))
- {
- run(key);
- }
- }
- }
-
- public static void run(string runType)
- {
-
- GpioController gpioController = new GpioController();
- gpioController.OpenPin(pin_19, PinMode.Output);
- gpioController.OpenPin(pin_26, PinMode.Output);
- gpioController.OpenPin(pin_16, PinMode.Output);
- gpioController.OpenPin(pin_20, PinMode.Output);
- if (runType == "b")
- { //# 逆时针
- setBack(gpioController);
- }
- else if (runType == "f")
- {
- //# 顺时针
- setForward(gpioController);
- }
- }
- public static void setValue(GpioController GPIO, int w1, int w2, int w3, int w4)
- {
- GPIO.Write(pin_19, w1);
- GPIO.Write(pin_26, w2);
- GPIO.Write(pin_16, w3);
- GPIO.Write(pin_20, w4);
-
- }
- //# 逆时针
- public static void setBack(GpioController GPIO)
- {
- Console.WriteLine("开始逆时针转");
- //256半圈
- for (int i = 0; i < 256; i++)
- {
- int delay = 3;//毫秒
- setValue(GPIO, 0, 0, 0, 1);
- Thread.Sleep(delay);
- setValue(GPIO, 0, 0, 1, 0);
- Thread.Sleep(delay);
- setValue(GPIO, 0, 1, 0, 0);
- Thread.Sleep(delay);
- setValue(GPIO, 1, 0, 0, 0);
- Thread.Sleep(delay);
-
- }
- }
- //#顺时针
- public static void setForward(GpioController GPIO)
- {
- Console.WriteLine("开始顺时针转");
- //256半圈
- for (int i = 0; i < 256; i++)
- {
- int delay = 2;//毫秒
- setValue(GPIO, 1, 0, 0, 0);
- Thread.Sleep(delay);
- setValue(GPIO, 0, 1, 0, 0);
- Thread.Sleep(delay);
- setValue(GPIO, 0, 0, 1, 0);
- Thread.Sleep(delay);
- setValue(GPIO, 0, 0, 0, 1);
- Thread.Sleep(delay);
- }
- }
- }
部署到香橙派中即可

代码在:https://gitee.com/yihong-lin/OrangePIWiringOPforCsharp
代码里面还包括 香橙派 c# 引用 wiringOP 类库, 操作其它设备的代码 ,包括串口,i2c 等操作