• 香橙派c# iot .net 控制ULN2003驱动板步进电机 代码实例


    香橙派orangepi c# iot .net 控制ULN2003驱动板步进电机 代码实例

    设备:

    1、香橙派

    2、ULN2003驱动板,28BYJ-48步进电机

     

    一、原理说明

    刚接触的时候,我也是看到不太懂,看懂了之后现在总结一下

    28BYJ-48步进电机名称含义:
    28:表示步进电机的有效最大外径为28毫米
    B: 表示步进电机“步”字汉语拼音首字母
    Y: 表示永磁式“永”字汉语拼音首字母
    J: 表示减速型“减”字汉语拼音首字母
    BYJ: 组合即为永磁式减速步进电机
    48:表示四相八拍
    5V:表示额定电压为5V,且为直流电压

    查看说明书内容

    9e7f332955ce4a94b652535abeacba1d.png

     

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

    内部 64圈=外部1圈

    内部1圈=8个脉冲信号

    所以看到电机转一圈,是要发送 64*8=512个脉冲信

     

    5231c7fa6e874e5394feaec26a63f9e3.png

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

    bcba32affbe84fda8898be696341a101.png

    9e6f2a38d30d4db987f7e3ff9cf1d0c3.jpg

     

    二、完整代码

    1. using System.Device.Gpio;//nuget中下载
    2. /// <summary>
    3. /// //步进电机28byj48+uln2003驱动板45线
    4. /// </summary>
    5. public static class Device_Stepper_uln2003_Service
    6. {
    7. static int pin_19 = 19; //In1
    8. static int pin_26 = 26;//In2
    9. static int pin_16 = 16;//In3
    10. static int pin_20 = 20;//In4
    11. /// <summary>
    12. /// 调用示例
    13. /// </summary>
    14. public static void test()
    15. {
    16. while (true)
    17. {
    18. Console.WriteLine("输入b逆时针转,输入f顺时针转");
    19. //控制台中输入内容
    20. string key = Console.ReadLine();
    21. if (!string.IsNullOrWhiteSpace(key))
    22. {
    23. run(key);
    24. }
    25. }
    26. }
    27. public static void run(string runType)
    28. {
    29. GpioController gpioController = new GpioController();
    30. gpioController.OpenPin(pin_19, PinMode.Output);
    31. gpioController.OpenPin(pin_26, PinMode.Output);
    32. gpioController.OpenPin(pin_16, PinMode.Output);
    33. gpioController.OpenPin(pin_20, PinMode.Output);
    34. if (runType == "b")
    35. { //# 逆时针
    36. setBack(gpioController);
    37. }
    38. else if (runType == "f")
    39. {
    40. //# 顺时针
    41. setForward(gpioController);
    42. }
    43. }
    44. public static void setValue(GpioController GPIO, int w1, int w2, int w3, int w4)
    45. {
    46. GPIO.Write(pin_19, w1);
    47. GPIO.Write(pin_26, w2);
    48. GPIO.Write(pin_16, w3);
    49. GPIO.Write(pin_20, w4);
    50. }
    51. //# 逆时针
    52. public static void setBack(GpioController GPIO)
    53. {
    54. Console.WriteLine("开始逆时针转");
    55. //256半圈
    56. for (int i = 0; i < 256; i++)
    57. {
    58. int delay = 3;//毫秒
    59. setValue(GPIO, 0, 0, 0, 1);
    60. Thread.Sleep(delay);
    61. setValue(GPIO, 0, 0, 1, 0);
    62. Thread.Sleep(delay);
    63. setValue(GPIO, 0, 1, 0, 0);
    64. Thread.Sleep(delay);
    65. setValue(GPIO, 1, 0, 0, 0);
    66. Thread.Sleep(delay);
    67. }
    68. }
    69. //#顺时针
    70. public static void setForward(GpioController GPIO)
    71. {
    72. Console.WriteLine("开始顺时针转");
    73. //256半圈
    74. for (int i = 0; i < 256; i++)
    75. {
    76. int delay = 2;//毫秒
    77. setValue(GPIO, 1, 0, 0, 0);
    78. Thread.Sleep(delay);
    79. setValue(GPIO, 0, 1, 0, 0);
    80. Thread.Sleep(delay);
    81. setValue(GPIO, 0, 0, 1, 0);
    82. Thread.Sleep(delay);
    83. setValue(GPIO, 0, 0, 0, 1);
    84. Thread.Sleep(delay);
    85. }
    86. }
    87. }

     

    部署到香橙派中即可

    51f73f165ecf4fdb904ed4e713e1643d.jpg

     

    代码在:https://gitee.com/yihong-lin/OrangePIWiringOPforCsharp

     

    代码里面还包括 香橙派 c# 引用 wiringOP 类库, 操作其它设备的代码 ,包括串口,i2c  等操作

    f9384e040ce2478fa12e690b3f99cb38.png

     

     

     

  • 相关阅读:
    『第八章』进击的雨燕:Combine 框架
    ubuntu创建账号和samba共享目录
    git版本回退
    CentOS系统环境搭建(二十)——CentOS7安装chat GPT进阶
    Docker发布镜像时报错denied: requested access to the resource is denied解决办法
    SpringBoot很熟?那手撕一下自定义启动器吧
    [Codeforces] number theory (R1200) Part.9
    Proteus仿真--基于51单片机的LED模拟交通灯仿真(仿真文件+程序)
    PaddleSeg学习3——使用PP-LiteSeg模型对道路进行分割
    [附源码]计算机毕业设计springboot停车场管理系统
  • 原文地址:https://blog.csdn.net/qq_16005627/article/details/126948784