2014年3月22日 星期六

電路及程式



步進馬達驅動需要較大電流,可用2D掃瞄器的驅動IC及電源,故將UNO的控制信號加在驅動IC的信號輸入端可





UNO程式如下


const int motorPin1 = 11;     
const int motorPin2 = 10;     
const int motorPin3 = 13;     
const int motorPin4 = 12;     
                               
int motorSpeed=1000;            // set stepper speed
float err=0;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop(){

 for(int i=0;i<2;i++) cw();

 delay(1000); 
}


//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
void ccw (){
  // 1
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delayMicroseconds(motorSpeed);
  // 2
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delayMicroseconds(motorSpeed);
  // 3
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delayMicroseconds(motorSpeed);
  // 4
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delayMicroseconds(motorSpeed);
  // 5
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delayMicroseconds(motorSpeed);
  // 6
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, HIGH);
  delayMicroseconds(motorSpeed);
  // 7
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delayMicroseconds(motorSpeed);
  // 8
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delayMicroseconds(motorSpeed);
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
void cw(){
  // 1
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delayMicroseconds(motorSpeed);
  // 2
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delayMicroseconds(motorSpeed);
  // 3
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delayMicroseconds(motorSpeed);
  // 4
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, LOW);
  delayMicroseconds(motorSpeed);
  // 5
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, LOW);
  delayMicroseconds(motorSpeed);
  // 6
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, HIGH);
  delayMicroseconds(motorSpeed);
  // 7
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, HIGH);
  delayMicroseconds(motorSpeed);
  // 8
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, HIGH);
  delayMicroseconds(motorSpeed);

}

2014年3月8日 星期六

安裝測試

安裝測試


一、確認連接埠

控制台->系統->硬體->裝置管理員





二、點選連接埠(COMLPT),記住COM4(每個機器可能不同),這是Arduino的連接埠地址





三、開啓Arduino

四、成功進入arduino


五、設定連接埠

Tools->Serial Port->COM4(要與裝置管理員顯示的一致)




六、設定後會出現打勾



七、打開範例程式—LED

File->Open->c:Program Files\Arduinp\examples\01Basics\Blink\Blink.ino


/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;    //LED的長腳接PIN13,短腳接GND

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

八、接好硬體

LED的長腳接PIN13,短腳接GND


九、按File->Upload就開始上傳執行碼





十、上傳完,可見LED一閃一閃

安裝驅動程式

安裝驅動程式

uno接上電腦




出現安裝驅動程式畫面
不,現在不要,按下一步


從清單或特定位置安裝,按下一步


瀏覽,選Arduino安裝時的目錄下的driver子目錄,按下一步



完成





安裝arduino開發環境

安裝arduino開發環境

一、至http://arduino.cc/en/Main/Software下載相對作業系統的版本
       如果是WINDOWS,建議下載INSTALLER,另存在桌面


 二、執行下載檔案



三、”I Agree”



”Next”


”Install”  (要記得安裝的目錄)



完成



2014年3月3日 星期一

Stepper Motors code, circuits, & construction

摘錄自 http://www.tigoe.net/pcomp/code/circuits/motors/stepper-motors/

Stepper Motors
A stepper motor is a motor controlled by a series of electromagnetic coils. The center shaft has a series of magnets mounted on it, and the coils surrounding the shaft are alternately given current or not, creating magnetic fields which repulse or attract the magnets on the shaft, causing the motor to rotate.
This design allows for very precise control of the motor: by proper pulsing, it can be turned in very accurate steps of set degree increments (for example, two-degree increments, half-degree increments, etc.). They are used in printers, disk drives, and other devices where precise positioning of the motor is necessary.
There are two basic types of stepper motors, unipolar steppers and bipolar steppers.
Unipolar Stepper Motors(一般有56條接線)
The unipolar stepper motor has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole.



Bipolar stepper motors(一般僅有4條接線)
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center connection. They have two independent sets of coils instead. You can distinguish them from unipolar steppers by measuring the resistance between the wires. You should find two pairs of wires with equal resistance. If you’ve got the leads of your meter connected to two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance (or no continuity).
Like other motors, stepper motors require more power than a microcontroller can give them, so you’ll need a separate power supply for it. Ideally you’ll know the voltage from the manufacturer, but if not, get a variable DC power supply, apply the minimum voltage (hopefully 3V or so), apply voltage across two wires of a coil (e.g. 1 to 2 or 3 to 4) and slowly raise the voltage until the motor is difficult to turn. It is possible to damage a motor this way, so don’t go too far. Typical voltages for a stepper might be 5V, 9V, 12V, 24V. Higher than 24V is less common for small steppers, and frankly, above that level it’s best not to guess.
To control the stepper, apply voltage to each of the coils in a specific sequence. The sequence would go like this:
Step
wire 1
wire 2
wire 3
wire 4
1
High
low
high
low
2
low
high
high
low
3
low
high
low
high
4
high
low
low
high
To control a unipolar stepper, you use a Darlington Transistor Array. The stepping sequence is as shown above. Wires 5 and 6 are wired to the supply voltage.

To control a bipolar stepper motor, you give the coils current using to the same steps as for a unipolar stepper motor. However, instead of using four coils, you use the both poles of the two coils, and reverse the polarity of the current.
The easiest way to reverse the polarity in the coils is to use a pair of H-bridges. The L293D dual H-bridge has two H-bridges in the chip, so it will work nicely for this purpose.

Once you have the motor stepping in one direction, stepping in the other direction is simply a matter of doing the steps in reverse order.
Knowing the position is a matter of knowing how many degrees per step, and counting the steps and multiplying by that many degrees. So for examples, if you have a 1.8-degree stepper, and it’s turned 200 steps, then it’s turned 1.8 x 200 degrees, or 360 degrees, or one full revolution.
Two-Wire Control
Thanks to Sebastian Gassner for ideas on how to do this.
In every step of the sequence, two wires are always set to opposite polarities. Because of this, it’s possible to control steppers with only two wires instead of four, with a slightly more complex circuit. The stepping sequence is the same as it is for the two middle wires of the sequence above:
Step
wire 1
wire 2
1
low
high
2
high
high
3
high
low
4
low
low
The circuits for two-wire stepping are as follows:
Unipolar stepper two-wire circuit:

Biolar stepper two-wire circuit:

Programming the Microcontroller to Control a Stepper
Because both unipolar and bipolar stepper motors are controlled by the same stepping sequence, we can use the same microcontroller code to control either one. In the code examples below, connect either the Darlington transistor array (for unipolar steppers) or the dual H-bridge (for bipolar steppers) to the pins of your microcontroller as described in each example. There is a switch attached to the microcontroller as well. When the switch is high, the motor turns one direction. When it’s low, it turns the other direction.
The examples below use the 4-wire stepping sequence. A two-wire control program is shown for the Wiring/Arduino Stepper library only.
Wire pins 9-12 of the BX-24 to inputs 1-4 of the Darlington transistor array, respectively. If you’re using the PicBasic Pro code, it’s designed for a PIC 40-pin PIC such as the 16F877 or 18F452. Use pins PORTD.0 through PORTD.3, respectively. If you’re using a smaller PIC, you can swap ports, as long as you use the first four pins of the port.
Note that the wires read from left to right. Their numbers don’t correspond with the bit positions. For example, PORTD.3 would be wire 1, PORTD.2 would be wire 2, PORTD.1 would be wire 3, and PORTD.0 would be wire 4. On the BX-24, pin 9 is wire 1, pin 10 is wire 2, and so forth.
Wiring Code (for Arduino board):

This example uses the Stepper library for Wiring/Arduino. It was tested using the 2-wire circuit. To change to the 4-wire circuit, just add two more motor pins, and change the line that initalizes the Stepper library like so:

Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);
/*
 Stepper Motor Controller
 language: Wiring/Arduino

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 and 9 of the Arduino.

 The motor moves 100 steps in one direction, then 100 in the other.

 Created 11 Mar. 2007
 Modified 7 Apr. 2007
 by Tom Igoe

 */

// define the pins that the motor is attached to. You can use
// any digital I/O pins.

#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
                           // per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define ledPin 13

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);

void setup() {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);

  // Initialize the Serial port:
  Serial.begin(9600);

  // set up the LED pin:
  pinMode(ledPin, OUTPUT);
  // blink the LED:
  blink(3);
}

void loop() {
  // Step forward 100 steps:
  Serial.println("Forward");
  myStepper.step(100);
  delay(500);

  // Step backward 100 steps:
  Serial.println("Backward");
  myStepper.step(-100);
  delay(500);

}

// Blink the reset LED:
void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
  }
}