這幾天想作一個給金魚自動餵食的裝置,因此學習了下如何操控步進電機,如今作個記錄。html
使用Pretues仿真Arduino的話,能夠參考:http://www.geek-workshop.com/thread-26325-1-1.html。git
步進電機的話,是經過給不一樣的相通電,造成電磁場,來驅動中間的轉子。轉子越多,每一步就能夠越小,電機就越精確。oop
ULN2004A是供電模塊,經過電平信號的配置來輸出電壓,經過配置1,2,3,4輸入,來使得13,14,15,16輸出電壓驅動電機。學習
PS:若是要電機反轉的話,就反序配置引腳。參見:http://www.elecfans.com/dianlutu/187/20170116475004.html。ui
仿真結果:spa
代碼:3d
/* 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; }