2016年4月22日 星期五

ds18b20溫度感測器

詳細教學可參考阿簡生物筆記
利用防水的溫度感測計可進行有關熱量變化的實驗耶,會考後讓9年級玩玩。

下載函式庫

OneWire(https://goo.gl/DMRdxi)

Arduino-Temperature-Control-Library

解壓縮後分別放到Arduinosketchbook下的libraries目錄裡。

DS18B20
藍線或黃線接D2
紅線接5V
黑線或灰接GND
還要在紅線藍線之間再加一個4.7k的電阻








  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
 // Serial.print("Requesting temperatures...");不列出
  sensors.requestTemperatures(); // Send the command to get temperatures
  //Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
 // Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));  

}

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  //Serial.println("Dallas Temperature IC Control Library Demo");
  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,Timer,Temperature,val,");
  // Start up the library
  sensors.begin();
}
/*
 * Main function, get and show the temperature
 */
void loop(void)
{
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println(sensors.getTempCByIndex(0));
  Serial.print("DATA,TIME");
  Serial.print(",");
  Serial.print("TIMER");
  Serial.print(",");
  Serial.println(sensors.getTempCByIndex(0));
  delay(100);
}

ds18b20並聯8支(第9支就會出現負數)應該是極限
放進溫水中
將2支改放入熱水
抽離也可測氣溫
PLX-DAQ收集數據




2016年4月8日 星期五

Arduino/並聯photocell的應用


從數據的變化可計算平均速率與運動變化


Arduino/讀取光敏電阻數值

使用範例/01Basics/AnalogReadSerial

CODE

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:

  int sensorValue = analogRead(A0);

  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

數值會與亮度成正比
從序列埠間視窗可以看到數值的變化,也可從serial plotter繪出圖表,當然也能用PLX-DAQ匯出資料到excel。

CODE


int analogPin = 0;

int RED = 0;

void setup(){
  Serial.begin(9600);
  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,Timer,RED");
      
}
void loop(){
      RED = analogRead(analogPin);
     
  Serial.print("DATA,TIME"); 
  Serial.print(",");
  Serial.print("TIMER"); 
  Serial.print(",");
  Serial.print(RED);
  Serial.print(",");
     
       delay(100);
}



ReadAnalogvoltage/讀取電壓值電阻值


        手中的電阻到底幾歐姆,顏色代號背了就忘,也可以用Arduino來測量,順便解釋串聯電阻與電壓的關聯性。

首先使用範例



線路圖(已知電阻1000歐姆,X為未知)
黃色A0 ,黑色GND,紅色5V
量測電壓


測量電阻(串聯電壓與電阻成正比用來改變公式)

從序列埠監視窗就可以看到結果了





2016年4月7日 星期四

PLX-DAQ 收集和紀錄DATA

下載PLX-DAQ



紅字是將LM35得到的資料送進PLX-DAQ的關鍵參考簡大師
注意PORT的選擇要和Arduino的一樣

#define sensor A0     

int tempC=0;

void setup()                                      //  初始化

{

  Serial.begin(9600);

 Serial.println("CLEARDATA");

 Serial.println("LABEL,Time,Timer,Temperature");

}

void loop()

{               

      tempC = analogRead(sensor);           //read the value from the sensor

      tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature

                         // 讀取溫度並顯示

   Serial.print("DATA,TIME"); 

   Serial.print(",");

  Serial.print("TIMER"); 

  Serial.print(",");

  Serial.println((byte)tempC);

       

      delay(1000);                                         // 休息 1 秒

}





LM35溫度探測與變色燈


程式碼在這





2016年4月6日 星期三

LM35溫度感測器

紅色(5v)/黑色(接地)/綠色(A0)
程式
int potPin = A0 ;//接收腳A0
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  int val;
  int dat;

  val = analogRead(potPin);//讀取

  dat = (125*val)>>8 ;//轉換成溫度
 // Serial.print("Tep : ") ;
  Serial.println(dat) ;
 // Serial.println("C");
  delay(500);
}

從工具/打開序列埠監視窗,就可以看到溫度的顯示,也可從serial plotter匯出圖形

RGB2

網路上找到的聰明代碼,就不用那麼累了。

使用三色全彩LED製造
模組有3個輸出
1. R,紅色輸出,
2. G,綠色輸出,
3. B,藍色輸出.

 3組信號輸出,可通過單片機程式設計實現RGB三種顏色的混合達到全彩的效果,
實驗代碼:
int ledPin = 13; // LED is connected to digital pin 13
int redPin = 11;  // R petal on RGB LED module connected to digital pin 11
int greenPin = 9;  // G petal on RGB LED module connected to digital pin 9
int bluePin = 10;  // B petal on RGB LED module connected to digital pin 10
     
void setup()   
{  
         pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
         pinMode(redPin, OUTPUT); // sets the redPin to be an output
         pinMode(greenPin, OUTPUT); // sets the greenPin to be an output
         pinMode(bluePin, OUTPUT); // sets the bluePin to be an output
}   
     
void loop()  // run over and over again 
{   
          // Basic colors: 
          color(255, 0, 0); // turn the RGB LED red 
          delay(1000); // delay for 1 second 
          color(0,255, 0); // turn the RGB LED green 
          delay(1000); // delay for 1 second 
          color(0, 0, 255); // turn the RGB LED blue 
          delay(1000); // delay for 1 second 
 
          // Example blended colors: 
          color(255,255,0); // turn the RGB LED yellow 
          delay(1000); // delay for 1 second 
          color(255,255,255); // turn the RGB LED white 
          delay(1000); // delay for 1 second 
          color(128,0,255); // turn the RGB LED purple 
          delay(1000); // delay for 1 second 
          color(0,0,0); // turn the RGB LED off 
          delay(1000); // delay for 1 second 
}    
    
void color (unsigned char red, unsigned char green, unsigned char blue)     // the color generating function 
{   
          analogWrite(redPin, 255-red);  
          analogWrite(bluePin, 255-blue);
          analogWrite(greenPin, 255-green);

}     

RGB1

程式碼
analogWrite(pin,v)  //電壓0~5V,0~255表示
控制燈的強弱調出自己的顏色。可以利用在三原色的實驗。
底下是自己亂寫的(反正不要短路就好了,勇敢的亂寫)


















int redPin = 9;  // R petal on RGB LED module connected to digital pin 9
int greenPin = 10;  // G petal on RGB LED module connected to digital pin 10
int bluePin = 11;  // B petal on RGB LED module connected to digital pin 11

void setup()  
{
  pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
  pinMode(redPin, OUTPUT); // sets the redPin to be an output
  pinMode(greenPin, OUTPUT); // sets the greenPin to be an output
  pinMode(bluePin, OUTPUT); // sets the bluePin to be an output
}  

void loop()  // run over and over again
{  
analogWrite(redPin, 124);//紅
delay(500);
analogWrite(redPin, 0);//關燈
delay(500);

analogWrite(greenPin,50);//橙
analogWrite(redPin, 255);
delay(500);
analogWrite(greenPin, 0);
analogWrite(redPin, 0);
delay(500);

analogWrite(greenPin, 100);//黃
analogWrite(redPin, 255);
delay(500);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
delay(500);

analogWrite(greenPin, 125); //綠
delay(500);
analogWrite(greenPin, 0);
delay(500);

analogWrite(bluePin, 125);//藍
delay(500);
analogWrite(bluePin, 0);
 delay(500);

  analogWrite(redPin, 150);//靛
  analogWrite(bluePin, 80);
 delay(500);
 analogWrite(redPin, 0);
 analogWrite(bluePin, 0);
 delay(500);

   analogWrite(redPin, 64);//紫
   analogWrite(bluePin, 255);
delay(500);
analogWrite(redPin, 0);
   analogWrite(bluePin, 0);
 
delay(500);
analogWrite(greenPin,125);//白
   analogWrite(redPin,125);
   analogWrite(bluePin,125);
   delay(500);
   analogWrite(greenPin, 0);
   analogWrite(redPin, 0);
   analogWrite(bluePin, 0);
   delay(500);
}  

Arduino/閃爍的紅綠燈

       將Blink 稍作修改,依樣畫葫蘆增加一個pinMode(7,OUTPUT);
當digitalWrite(13, HIGH);digitalWrite(, LOW);類似13通電,7斷電。
改變delay(1000)數字,就會改變閃爍的變化了。


Arduino/Blink

看了學校簡大師的專研課程使用了Arduino,令人嘆為觀止,同事老王科展也利用Arduino控制LED以毫秒時間閃爍,忍不住也到書局買一片來玩玩。

1首先下載安裝軟體(在這裡)



2 arduino連接pc


Usb串連線­→arduino連接pc.選擇不要自動搜尋,然後點擊「Next」.選擇從列表或指定位置安裝(Install from a list or specific location)點擊瀏覽Arduin\drivers\ftdi usb drivers 路徑Finish檢查 COM port  (開始--裝置印表機)   Arduino port(COM3)








3中文化

4選擇板子與序列埠







5第一個程式BLINK