arduino 習作工坊#2 - 動力之夜150114
TRANSCRIPT
Arduino 習作計畫
MakerBar Taipei Workshop2015.01.14
Arduino 可以做些什麼?
• 二輪平衡車segway
• 按讚機器人
http://lab.cavedu.com/arduino
馬達的種類
• 直流馬達 DC Motors
• 交流馬達 AC Motors
• 步進馬達 Step Motors
• 伺服馬達 RC Servo Motors
• 線性馬達 Linear Motors
RC 伺服機
數位輸出? 類比輸出?
Pulse Width Modulation (PWM)脈衝寬度調變
• 以數位訊號模擬類比訊號的技術
PWM
• 數位輸出可以控制訊號的開和關,開和關同
時意味著通電與斷電
• 如果我們可以進一步控制通電的時間比例,
就能讓輸出的訊號產生變化,例如LED燈通
電時間為50%,就可以控制LED燈只有50%
的亮度
電壓VS感應值(類比與數位差別)
電壓值
5V
0
數位輸入
數位輸出
1
0
類比輸入
1023
0
類比輸出
255
0
2.5V 1 約512 約128
(PWM~腳位)
PWM 使用方法
• IDE code 為 analogWrite()
• 格式:analogWrite(pin, value)
• 在uno板子中,pin 可以是3/5/9/10/11腳位
• Value: duty cycle,介於0-255,0為0%,127為50%,255為100%
拿程式範例修改File => Example => Basic => Fade
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
http://pcbheaven.com/
1. 控制器(即控制電路)會將輸入的PWM訊號轉換成相對的參考電壓(解
碼,decoding),不同的參考電壓會對應到伺服馬達轉軸的不同位置。
2. 控制器藉由量測電位計的分壓得知目前伺服馬達轉軸的位置。
3. 控制器比較此兩電壓的差異(也就是位置的差異),並開始轉動,直到輸
入的參考電壓與實際的電位計分壓相同為止。
4. 因此,伺服馬達只要通電就會鎖死,無法由外力轉動,因為其會不斷地比
較位置變化(當然會有一個初始值),並嘗試修正到目標位置。
伺服機(Servo)範例(File >> Example >> Servo >> Sweap)
程式 - Sweep
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
伺服機(Servo)範例(File >> Example >> Servo >> Knob)
程式-Knob#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer
(value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value
between 0 and 180)
myservo.write(val); // sets the servo position according to the
scaled value
delay(15); // waits for the servo to get there
}
程式-Knob#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
println(val);
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
DC直流馬達
正反轉怎麼辦?
H 橋電路
• 一個典型的 H 橋 IC 主要是由四個電晶體組成,透過電晶體的開關控制電流流動的方向,因此可以用於馬達正反轉的控制上
原理說明
TA7279P
電路 D8
D10
馬達5V
馬達GND
14 電池盒正電(紅)
阻抗值
煞車
逐漸停止
程式
• File => Example => Basic => Blink
int input1 = 8;
int input2 = 10;
void setup() {
pinMode(input1, OUTPUT);
pinMode(input2, OUTPUT);
}// the loop routine runs over and over again forever:
void loop() {
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
delay(1000);// wait for a second
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
delay(1000);// wait for a second
}
步進馬達
外側為電磁鐵的定子,內為NS交互磁化的永磁轉子(無齒型)
依轉子的構造來分
http://www.engineersgarage.com/
http://ming-shian.blogspot.tw/2013/05/blog-post_8.html
雙極就是驅動馬達的電流是雙向的,驅動控制時,需要改變電流方向。而單極的馬達,其電流就只需提供一個方向就好,改變提供的順序就可以達到驅動控制。
激磁方式
四相單極性步進馬達(五線)
http://robocraft.ru/
ULN2003APG
http://forum.allaboutcircuits.com/
http://www.instructables.com/
Stepper(int steps, pin1, pin2, pin3, pin4)建立一個步進馬達的物件。其中step是指轉一圈所需的步數,假使馬達定義每步的角度,用360去除,就會得到步數。例如:Stepper myStepper(100, 8, 9, 10, 11);
表示每一步為3.6度,轉一圈總共100步。
Stepper.setSpeed(long rpms)設定步進馬達每分鐘轉速 (RPMs) ,需為正數。這個函式並不會讓馬達轉動,只是設定好轉速,當呼叫Step()函式時才會開始轉動。
Stepper.step(int steps)啟動馬達行進steps步數。setSpeed()定義速度,正的表示一個方向, 負數表示反方向。
http://atceiling.blogspot.tw/
<Stepper.h>
程式Stepmotor_step
#include <Stepper.h>// initialize the stepper library on pins 8 through 11:
Stepper myStepper(200, 8,9,10,11);
//表示每一步為1.8度,轉一圈總共200步。
void setup() {
// nothing to do inside the setup
}
void loop() {
myStepper.setSpeed(50);
//轉速為50rpm(revolution per minutes每分鐘可以轉50圈)
myStepper.step(1); //一次走一步}
程式Stepmotor2_control
#include <Stepper.h>
const int stepsPerRevolution = 200;
// change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0); // map it to a range
from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution/100);
}
}
Thank you!