这几天想做一个给金鱼自动喂食的装置,所以学习了下如何操控步进电机,现在做个记录。
使用Pretues仿真Arduino的话,可以参考:http://www.geek-workshop.com/thread-26325-1-1.html。
步进电机的话,是通过给不同的相通电,形成电磁场,来驱动中间的转子。转子越多,每一步就可以越小,电机就越精确。
ULN2004A是供电模块,通过电平信号的配置来输出电压,通过配置1,2,3,4输入,来使得13,14,15,16输出电压驱动电机。
PS:如果要电机反转的话,就反序配置引脚。参见:http://www.elecfans.com/dianlutu/187/20170116475004.html。
仿真结果:
代码:
/* Main.ino file generated by New Project wizard * * Created: Thu Sep 5 2013 * Processor: ATmega328P * Compiler: Arduino AVR */ #define LED ( 13 ) #define MOTOR_PIN0 ( 0 ) #define MOTOR_PIN1 ( 1 ) #define MOTOR_PIN2 ( 2 ) #define MOTOR_PIN3 ( 3 ) void setup() { // put your setup code here, to run once: pinMode( LED, OUTPUT ); pinMode( MOTOR_PIN0, OUTPUT ); pinMode( MOTOR_PIN1, OUTPUT ); pinMode( MOTOR_PIN2, OUTPUT ); pinMode( MOTOR_PIN3, OUTPUT ); } void loop() { // put your main code here, to run repeatedly: /*digitalWrite( LED, HIGH ); delay( 500 ); digitalWrite( LED, LOW ); delay( 500 );*/ static int i = 3; switch( i-- ) { case 0: digitalWrite( MOTOR_PIN0, HIGH ); digitalWrite( MOTOR_PIN1, LOW ); digitalWrite( MOTOR_PIN2, LOW ); digitalWrite( MOTOR_PIN3, LOW ); digitalWrite( LED, HIGH ); break; case 1: digitalWrite( MOTOR_PIN0, LOW ); digitalWrite( MOTOR_PIN1, HIGH ); digitalWrite( MOTOR_PIN2, LOW ); digitalWrite( MOTOR_PIN3, LOW ); digitalWrite( LED, LOW ); break; case 2: digitalWrite( MOTOR_PIN0, LOW ); digitalWrite( MOTOR_PIN1, LOW ); digitalWrite( MOTOR_PIN2, HIGH ); digitalWrite( MOTOR_PIN3, LOW ); digitalWrite( LED, HIGH ); break; case 3: digitalWrite( MOTOR_PIN0, LOW ); digitalWrite( MOTOR_PIN1, LOW ); digitalWrite( MOTOR_PIN2, LOW ); digitalWrite( MOTOR_PIN3, HIGH ); digitalWrite( LED, LOW ); break; default: break; } delay(500); if( 0 > i ) i = 3; }