1. 程式人生 > >arduino中的變頻

arduino中的變頻

主要內容

  1. Arduino輸出模擬訊號,也就是可變電壓輸出。
  2. 電機型號和規格,常見的電機驅動和控制電路
  3. 介紹常見的電晶體,以及電晶體電路的基本應用與設計方式

一、 調節電壓變化

  1. 改變電路中的電壓。
  • 缺點:體積增大,電能變成熱能
  1. 省電又環保的PWM變頻技術
  • 稱為:脈衝寬度調製(Pulse Width Modulation)
  • 通過改變脈衝寬度,將能模擬模擬電壓高低變化的效果。
  • 公式:
    =
    ×
    = 模擬輸出電壓 = 脈衝寬度 \times 高電平值 \Rightarrow \frac {輸出電壓}{高電平值} = 開啟時間百分比

二、 模式輸出(PWM)指令和預設頻率

指令

analogWrite(埠號,模擬數值) 

說明:埠號可能為3,5,6,9,10, 11
模擬數值:0~255

例如:

analogWrite(5, 168); 

上面的指令將使第5引腳輸出3.3V電壓,因為 168 255 × 5 3.3 \frac {168}{255}\times {5} \approx 3.3

預設頻率

Arduino控制板預設採用1kHz 和 500Hz兩組不同的PWM輸出頻率

  • 引腳5、6:976.5625Hz
  • 引腳3、11、9、10:490.196Hz
  • 若PWM頻率太低,電機會抖動,LED會閃爍
  • 人耳可以感受20kHz以內的頻率,為避免聽到電機的抖動音,應調高PWM的輸出頻率

改變PWM的輸出頻率

Arduino的PWM輸出頻率是由ATmega微處理器內部的三個系統定時器(Timer0~Timer2)決定的,Timer0(5、6)、Timer1(9、10)、Timer2(3、11),通過改變定時器的設定,來調整PWM的輸出頻率。
Arduino的delay()、millis()、micros()等函式的基準時間,來自Timer0,若調整此定時器,將導致這些函式的延遲時間錯亂。

下面的程式語句將Timer1(9、10埠輸出)的PWM頻率調整成31250Hz。

void setup() {
	TCCR1B = TCCR1B & 0b11111000 | 0x01;
}

關於引數的詳細說明以及頻率對照表

三、任務

任務1:調光器

  1. 實驗說明: 利用可變電阻的輸入訊號變化來調整LED亮度
  2. 實驗材料:
物品 數量
LED 1個
10kΩ可變電阻 1個
  • Arduino 的模擬輸入(analogRead)的範圍值介於0~1023之間,而模擬輸出(analogWrite)介於0 ~ 255。
  1. 實驗程式
byte potPin = A0;
byte ledPin = 11;
int potSpeed = 0;
byte val = 0;
void setup() {
	pinMode(ledPin OUTPUT);
}

void loop() {
	potSpeed = analogRead(potPin);
	val = map(potSpeed, 0, 1023, 0, 255);
	analogWrite(ledPin, val);
}

任務2:隨機數字與燭光效果

實驗說明: 隨機, 通過隨機調整接在數字11埠的LED亮度,以及隨機持續時間來模擬燭光效果。

隨機指令:

random

byte rnd = random(200); //從0~199之間挑選一個數字,存入rnd
byte rnd = random(20, 50); //從20~49之間挑選一個數字,存入rnd

然而,Arduino每次挑選的數字並不是那麼隨意,為了提高不重複的比率,在每次執行random()函式之前,先執行randomSeed()函式,

實驗程式:

byte ledPin = 11;
void setup() {
	pinMode(ledPin, OUTPUT);
	randomSeed(analogRead(A5);  //空接埠的讀取值很不穩定,適合於做randomSeed()引數。
}

void loop() {
	analogWrite(ledPin, random(135) + 120);
	delay(random(200));
}

任務3: 通過串列埠調整燈光亮度

實驗說明: 接收使用者輸入的0~255數值來改變接在11引腳的LED亮度。

使用者通過按鍵的輸入值是字串格式,而模擬輸出指令所需要的引數是數字格式。有兩種方法把字元或字串轉換成數字。

  • 將ASCII碼減掉48。數字0的十進位制ASCII碼是48
    ‘2’ - ‘0’ \Rightarrow 50 - 48 \Rightarrow 2
    ‘2’ - 48 \Rightarrow 50 -48 \Rightarrow 2
  • 使用atoi()函式指令,把字串轉換成數字。

比如:Arduino收到字串 ‘168\n’

  1. 處理第一個字:pwm = pwm * 10 + (_in - '0')
  2. 處理第二個字:pwm = pwm * 10 + (_in - '0')
  3. 處理第三個字:pwm = pwm * 10 + (_in - '0')
  4. 讀取到字元’\n’,轉換完畢!
    由於每次傳遞的數字長度不確定(如: ‘3’ 和‘168’),為了讓接收端確認一串數字的結尾,在傳送資料的後面加上‘\n’字元。
    實驗程式:
byte ledPin = 11;
void setup() {
	Serial.begin(9600);
}

void loop() {
	int pwm = 0;
	byte _in;
	if (Serial.available()) {
		_in = Serial.read();
		while (_in != '\n') {
			if (_in >= '0' && _in <='9') {
				pwm = pwm * 10 + (_in - '0');
			}
			_in = Serial.read();
		}
		if (pwm >255) pwm = 255;
		analogWrite(ledPin, pwm);
	}
}	

活動4:使用atoi()轉換字串成數字

實驗說明: 先宣告一個空字串變數,每次收到的新字元,存入字串變數

附錄

1. random()

Description

The random function generates pseudo-random numbers.

Syntax

random(max)
random(min, max)

Parameters

min - lower bound of the random value, inclusive (optional)

max - upper bound of the random value, exclusive

Returns

A random number between min and max-1 (long) .

Example Code

The code generates random numbers and displays them.

long randNumber;

void setup(){
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}

void loop() {
  // print a random number from 0 to 299
  randNumber = random(300);
  Serial.println(randNumber);

  // print a random number from 10 to 19
  randNumber = random(10, 20);
  Serial.println(randNumber);

  delay(50);
}

Notes and Warnings

If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.

Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This can be accomplished by calling randomSeed() with a fixed number, before starting the random sequence.

The max parameter should be chosen according to the data type of the variable in which the value is stored. In any case, the absolute maximum is bound to the long nature of the value generated (32 bit - 2,147,483,647). Setting max to a higher value won’t generate an error during compilation, but during sketch execution the numbers generated will not be as expected.