Package: haas-python-uln2003
HaaS Python介绍HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架，兼容MicroPython编程规范，依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力，从而解决了物联网应用开发难的问题。有了Python轻应用框架，物联网编程不再局限于专业软件开发人员，一般的技术员也可以快速实现复杂的物联网需求。
更多HaaS Python介绍和开发资料见HaaS Python官网,创意案例,硬件积木HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements.
For more HaaS Python introduction and development materials, seeHaaS Python official website,Creative Case,Hardware Building Blocks安装方法在HaaS Python官网下载开发板对应的固件，并完成固件烧录Download the firmware corresponding to the development board atHaaS Python websiteand complete the firmware burning在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题，欢迎在github上向我们提交issue，我们的工程师会及时解答Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us ongithub, Our engineers will answer in timeimport upip
upip.install("haas-python-uln2003")
import haas-python-uln2003步进电机驱动模组 - ULN2003A一、产品简介ULN2003A，是一种高压大电流的共发射极达林顿晶体管数组集成电路，内含7组达林顿对管，每组对管的电流容量是500mA，输出的电压最高50V。该集成电路还集成了7只共阴极形式连接的续流二极管，用于电感性负载的开关动作的电流续流，本驱动是通过ULN2003A控制5线4相步进电机。引脚定义VCC: 5-12VIN1: host GPIOIN2: host GPIOIN3: host GPIOIN4: host GPIOGND: GND二、技术参数钳位二极管输出高电压输出:50V可兼容各类逻辑的输入三、软件接口ULN2003A步进电机驱动HaaS Python驱动：下载地址ULN2003(A, A_, B, B_) - 创建ULN2003驱动对象函数原型:motorObj = ULN2003(a, a_, b, b_)参数说明:参数类型必选参数？说明aGPIO是调用此函数前需确保a对象已经处于open状态a_GPIO是调用此函数前需确保a_对象已经处于open状态bGPIO是调用此函数前需确保b对象已经处于open状态b_GPIO是调用此函数前需确保b_对象已经处于open状态返回值:
创建对象成功，返回ULN2003对象；ULN2003A对象创建失败，抛出Exception示例代码:fromuln2003import*# ULN2003驱动库A=GPIO()A.open("uln2003_a")A_=GPIO()A_.open("uln2003_a_")B=GPIO()B.open("uln2003_b")B_=GPIO()B_.open("uln2003_b_")motorDev=ULN2003(A,A_,B,B_)输出:
无motorCw(speed) - 步进电机正向旋转函数功能:
驱动步进电机正向旋转函数原型:ULN2003.motorCw(speed=4)参数说明:
speed: 单位ms，步进电机旋转速度，speed越小，速度越快返回值:
无motorCcw(speed) - 步进电机反向旋转函数功能:
驱动步进电机反向旋转函数原型:ULN2003.motorCcw(speed=4)参数说明:
speed: 单位ms，步进电机旋转速度，speed越小，速度越快返回值:
无motorStop() - 停止步进电机旋转函数功能:
停止步进电机旋转函数原型:ULN2003.motorStop()参数说明:
无返回值:
无示例:fromuln2003import*# MQ3温湿度传感器驱动库A=GPIO()A.open("uln2003_a")A_=GPIO()A_.open("uln2003_a_")B=GPIO()B.open("uln2003_b")B_=GPIO()B_.open("uln2003_b_")motorDev=ULN2003(A,A_,B,B_)while1:motorDev.motorCw()输出:
无四、接口案例此使用实例在board.json中定义了名为uln2003的gpio数据类型，并控制步进电机正向旋转。代码:#board.json配置：{"name":"board-name","version":"1.0.0","io":{"uln2003_a":{"type":"GPIO","port":19,"dir":"output","pull":"pullup"},"uln2003_a_":{"type":"GPIO","port":27,"dir":"output","pull":"pullup"},"uln2003_b":{"type":"GPIO","port":0,"dir":"output","pull":"pullup"},"uln2003_b_":{"type":"GPIO","port":2,"dir":"output","pull":"pullup"}},"debugLevel":"ERROR","repl":"disable"}# Python代码print("Testing uln2003  ...")A=GPIO()A.open("uln2003_a")A_=GPIO()A_.open("uln2003_a_")B=GPIO()B.open("uln2003_b")B_=GPIO()B_.open("uln2003_b_")motorDev=ULN2003(A,A_,B,B_)while1:motorDev.motorCw()A.close()A_.close()B.close()B_.close()delmotorDevprint("Test uln2003 done!")输出：
无五、通信协议主控芯片通过GPIO控制步进电机的正转反转。正转序列序号功能说明命令字宏定义1GPIO A 输出高电平CMD_PINA_OUT_HIGH2GPIO A_ 输出低电平CMD_PINA__OUT_LOW3GPIO B 输出低电平CMD_PINB_OUT_LOW4GPIO B_ 输出低电平CMD_PINB__OUT_LOW5delay xx msCMD_DELAY_XX_MS6GPIO A 输出低电平CMD_PINA_OUT_LOW7GPIO A_ 输出高电平CMD_PINA__OUT_HIGH8GPIO B 输出低电平CMD_PINB_OUT_LOW9GPIO B_ 输出低电平CMD_PINB__OUT_LOW10delay xx msCMD_DELAY_XX_MS11GPIO A 输出低电平CMD_PINA_OUT_LOW12GPIO A_ 输出低电平CMD_PINA__OUT_LOW13GPIO B 输出高电平CMD_PINB_OUT_HIGH14GPIO B_ 输出低电平CMD_PINB__OUT_LOW15delay xx msCMD_DELAY_XX_MS16GPIO A 输出低电平CMD_PINA_OUT_LOW17GPIO A_ 输出低电平CMD_PINA__OUT_LOW18GPIO B 输出低电平CMD_PINB_OUT_LOW19GPIO B_ 输出高电平CMD_PINB__OUT_HIGH20delay xx msCMD_DELAY_XX_MS反转序列序号功能说明命令字宏定义1GPIO A 输出低电平CMD_PINA_OUT_LOW2GPIO A_ 输出低电平CMD_PINA__OUT_LOW3GPIO B 输出低电平CMD_PINB_OUT_LOW4GPIO B_ 输出高电平CMD_PINB__OUT_HIGH5delay xx msCMD_DELAY_XX_MS6GPIO A 输出低电平CMD_PINA_OUT_LOW7GPIO A_ 输出低电平CMD_PINA__OUT_LOW8GPIO B 输出高电平CMD_PINB_OUT_HIGH9GPIO B_ 输出低电平CMD_PINB__OUT_LOW10delay xx msCMD_DELAY_XX_MS11GPIO A 输出低电平CMD_PINA_OUT_LOW12GPIO A_ 输出高电平CMD_PINA__OUT_HIGH13GPIO B 输出低电平CMD_PINB_OUT_HIGH14GPIO B_ 输出低电平CMD_PINB__OUT_LOW15delay xx msCMD_DELAY_XX_MS16GPIO A 输出高电平CMD_PINA_OUT_HIGH17GPIO A_ 输出低电平CMD_PINA__OUT_LOW18GPIO B 输出低电平CMD_PINB_OUT_LOW19GPIO B_ 输出低电平CMD_PINB__OUT_HIGH20delay xx msCMD_DELAY_XX_MS停止序列序号功能说明命令字宏定义1GPIO A 输出低电平CMD_PINA_OUT_LOW2GPIO A_ 输出低电平CMD_PINA__OUT_LOW3GPIO B 输出低电平CMD_PINB_OUT_LOW4GPIO B_ 输出低电平CMD_PINB__OUT_LOW控制电机流程
可以通过控制MCU的gpio 来输出正向旋转序列、反向旋转序列或停止序列来控制步进电机的正转、反转或者停止，并且可以通过控制正向序列或反向序列之间的延时间隔来控制步进电机的速度。六、工作原理步进电机是将电脉冲信号转变为角位移或线位移的开环控制元件, 通过GPIO控制脉冲变化从而触发磁场周期性变化，即可牵引转子周期性的变化，而ULN2003A能与TTL和CMOS电路直接相连，可以直接处理原先需要标准逻辑缓冲期来处理的数据。
如下是ULN2003内部的框图，1-7是ULN2003的输入段，经过达林顿管的电流放大，9-16进行输出。参考文献及购买链接[1]ULN2003A驱动器[2]购买链接