2016年1月24日 星期日

Arduino的教學10- 利用DHT11抓取溫度和濕度,並顯示於2x16 I2C LCD上

一、學習目標:
學習使用DHT11溫溼度感應器來測量溫度,並顯示於 I2C 2X16

二、重點說明
DHT11功能說明:
1、濕度測量範圍:20~90%RH;
2、濕度測量精度:±5%RH;
3、溫度測量範圍:0~50℃
4、溫度測量精度:±2℃
5、電源供應範圍: 3~5V
6、頻率不可超過:0.5Hz (每2秒一次)

三、接線圖
"+" vcc 極接5V,v
"-" Gnd 極接GND,
data pin接上要輸入的Pin腳=>接在2

四、程式碼
DHT11的程式碼1

#include <dht.h>    
#define dht_dpin 2 //定義訊號要從Pin 2 進來  
 
dht DHT; 
  
void setup(){   
Serial.begin(9600);   
delay(300);             //Let system settle   
Serial.println("Humidity and temperature\n\n");   
delay(700);             //Wait rest of 1000ms recommended delay before   
                        //accessing sensor   
}
  
void loop(){   
DHT.read11(dht_dpin);   //去library裡面找DHT.read11  
Serial.print("Humidity = ");   
Serial.print(DHT.humidity);   
Serial.print("% ");   
Serial.print("temperature = ");   
Serial.print(DHT.temperature);   
Serial.println("C ");   
delay(1000);  //每1000ms更新一次  
}

DHT11 + I2C LCD 程式碼
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#include <dht.h>    
#define dht_dpin 2 //定義訊號要從Pin 2 進來  
dht DHT; 

int no=0;//計數器
void setup()
{
  lcd.init();                      // initialize the lcd 
Serial.begin(9600);
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("I2C+DHT11");
  Serial.println("Humidity and temperature\n\n");   
  delay(1000);  
}

void loop()
{
  no++;
  DHT.read11(dht_dpin);   //去library裡面找DHT.read11 
  //序列埠輸出
  Serial.print("Humidity = ");   
  Serial.print(DHT.humidity);   
  Serial.print("% ");   
  Serial.print("temperature = ");   
  Serial.print(DHT.temperature);   
  Serial.println("C "); 

  //資料顯示在LCD
  lcd.setCursor(0, 0);
  lcd.print("No.");
  lcd.print(no);
  lcd.setCursor(8, 1);
  lcd.print("H=");
  lcd.print(DHT.humidity);   
  lcd.setCursor(0, 1);
  lcd.print("T=");
  lcd.print(DHT.temperature);   
  delay(500);            //每500ms更新一次  
  lcd.clear();
}
五、參考資料: http://ming-shian.blogspot.tw/2014/05/arduino19dht11.html

2015年11月28日 星期六

Arduino的教學09-使用I2C 2X16 lcd


一、學習目標:學習使用I2C 2X16 lcd
i2c為一種通訊界面,2x16代表lcd為二行,每行可以顯示16個字母。

二、接線圖


SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V

三、參考資料:
http://coopermaa2nd.blogspot.tw/2012/09/i2c-16x2-lcd.html

四、程式碼
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);//第1列,空1格開始
  lcd.print("Hello, world!");
  lcd.setCursor(2, 1);//第2列,空2格開始
  lcd.print("second line");
}

void loop()
{
}
練習一:如何每秒測量一次,利出
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,16,2);  
 
int no=0;//計數器
void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);//第1列,空1格開始
  lcd.print("Hello, world!");
  lcd.setCursor(2, 1);//第2列,空2格開始
  lcd.print("sceond line");
  delay(3000);//等3秒
}
 
void loop(){
 no++;
 
   //資料顯示在LCD
  lcd.setCursor(0, 0);
  lcd.print("No.");
  lcd.print(no);
  delay(500);            //每500ms更新一次  
  lcd.clear();
 
}
五、作業:
如何將 HC-SR04 超聲波模組測量結果,以lcd顯示。

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x27,16,2);


int number=0;
#define TrigPIN 13
#define EchoPIN 12

void setup() {
  pinMode(TrigPIN, OUTPUT);
  pinMode(EchoPIN, INPUT);
    lcd.init();  // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);//第1列,空1格開始
  lcd.print("Hello,HC_SR04!");

  //序列埠除錯用
  Serial.begin(9600);
  Serial.println("Hello, HC_SR04!");
  delay(1000);
}
void loop() {
  float duration, distance;//時間、距離
  digitalWrite(TrigPIN, HIGH);
  delayMicroseconds(1000);
  digitalWrite(TrigPIN, LOW);
  duration = pulseIn (EchoPIN, HIGH);//pulseIn ( ) :讀取一個針腳的脈衝時間(HIGH或LOW)
  distance = (duration/2)/29;

  number = number +1;

  lcd.setCursor (0,0);
  lcd.print("No.");
  lcd.print (number);
  lcd.setCursor(0, 1);  
  lcd.print ("Dis.");   
  lcd.print(distance);
  lcd.print ("cm");
  
  
  Serial.print("No.");
  Serial.print (number);
  Serial.print (" Dis. ");   
  Serial.print(distance);
  Serial.println (" cm");//換行
  
  delay(1000);
  lcd.clear();
}
 

畫愛心的方法
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};



void setup() {

  Serial.begin(9600);
  lcd.init();                      // initialize the lcd

  // Print a message to the LCD.
  lcd.backlight();


  lcd.createChar(0, heart);
}

void loop() {

lcd.printByte(0);

delay(1000);
  lcd.clear();

}

2015年10月3日 星期六

Arduino的教學08-利用Tera Term 來收集序列埠資料

一、教學目標:利用Tera Term 來收集序列埠(com port )資料

二、Tera Term 可以在底下的網址取得
http://ttssh2.sourceforge.jp/

三、參考資料:
http://coopermaa2nd.blogspot.tw/2012/01/tera-term.html

四、學生作業:
1.請利用tera term來收集
的資料

Arduino的教學07-利用HC-SR04 超音波模組測量距離

一、教學目標:HC-SR04 超音波模組測量距離-led閃爍


Working Voltage: DC 5 V 
Working Current: 15mA 
Working Frequency: 40Hz 
Max Range: 4m 
Min Range: 2cm 
MeasuringAngle: 15 degree 
Trigger Input Signal: 10uS TTL pulse 
Echo Output Signal: Input TTL lever signal and the range in proportion 
Dimension: 45*20*15mm

二、佈線圖



三、程式碼

#define TrigPIN 13
#define EchoPIN 12
#define LED1 8
#define LED2 9

void setup() {
  pinMode(TrigPIN, OUTPUT);
  pinMode(EchoPIN, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
    float duration, distance;
  digitalWrite(TrigPIN, HIGH);
  delayMicroseconds(1000);
  digitalWrite(TrigPIN, LOW);
  duration = pulseIn (EchoPIN, HIGH);//pulseIn ( ) :讀取一個針腳的脈衝時間(HIGH或LOW)
  distance = (duration/2)/29;

  
  if (distance <= 100) {
    digitalWrite(LED1, HIGH);
    delay(distance*1.5 + 10);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(distance*1.5 + 10);
    digitalWrite(LED2, LOW);
  }
  
  delay(100);
}


四、作業
1.請將此系統改裝成車雷達(小於一段距離時)產生聲音

五、參考資料
https://eportal.stust.edu.tw/eshare/EshareFile/2014_5/2014_5_c185d20a.pdf
http://blog.lyhdev.com/2012/10/arduino-1hc-sr04.html

2015年8月12日 星期三

Arduino的教學06-利用蜂鳴器來發音

一、教學目標:蜂鳴器來演奏簡單音樂
https://www.arduino.cc/en/Tutorial/Tone
二、佈線圖

以tone(腳位,頻率,時間)函式播放
第一個參數是腳位
第二個參數是音符的頻率
第三個參數代表播放時間長度(ms)。

tone(6,224,500);



練習01-發出簡單三個音
#define tone_pin 8 //發聲的pin
#define NOTE_    0//

#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494


int melody[] = {
  NOTE_C4  , NOTE_D4  , NOTE_E4   
};

int noteDurations[] = {
  6, 16, 8
};


//函式,傳入歌曲的資料,進行播放。
void play(int *melody, int *noteDurations, int num){
  for(int note = 0; note < num; note++){
    int noteDuration = 3000 / noteDurations[note];
    tone(tone_pin, melody[note], noteDuration);

    delay(noteDuration * 1.30);
  }
}
void setup () {
  }

void loop(){
  play(melody, noteDurations, sizeof(melody) / sizeof(int));
  
  delay(2000);
}



三、程式碼
#define tone_pin 8 //發聲的pin
#define NOTE_    0//
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978


int melody[] = {
  NOTE_G4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, 
  NOTE_E4, NOTE_D4, NOTE_C4, NOTE_A3, NOTE_G3, NOTE_A3, NOTE_G3, 
  NOTE_A3, NOTE_A3, NOTE_C4, NOTE_A3, NOTE_C4, NOTE_D4, NOTE_E4, 
  NOTE_D4, NOTE_D4, NOTE_G4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, 
};

int noteDurations[] = {
  6, 16, 8, 8, 8, 8, 4, 
  6, 16, 8, 8, 8, 8, 4, 
  6, 16, 8, 8, 8, 8, 4,
  6, 16, 8, 8, 8, 8, 4,
};


//函式,傳入歌曲的資料,進行播放。
void play(int *melody, int *noteDurations, int num){
  for(int note = 0; note < num; note++){
    int noteDuration = 3000 / noteDurations[note];
    tone(tone_pin, melody[note], noteDuration);

    delay(noteDuration * 1.30);
  }
}
void setup () {
  }

void loop(){
  play(melody, noteDurations, sizeof(melody) / sizeof(int));
  
  delay(2000);
}

四、作業
1.請加入一個按鈕,按下時發出Do、Re、Mi的音,放開時發出Mi、Re、Do
2.請調整出一首兒歌或校歌

五、參考資料
http://yehnan.blogspot.tw/2012/02/arduinoloudspeaker.html

2015年8月6日 星期四

Arduino的教學05-可變電阻類比輸入與LED燈調速

一、教學目標:將LED燈依可變電阻調速
https://www.arduino.cc/en/Tutorial/AnalogInput

二、接線圖


/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);                  
}

練習01-如何將可變電阻的測量值-顯示除錯(Serial Monitor Debug)
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
 
void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}
 
void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   
   Serial.println(sensorValue);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);
}

2015年8月5日 星期三

Arduino的教學04-霹靂燈與陣列宣告練習

一、教學目標:將LED燈依序點亮
http://ming-shian.blogspot.tw/2013/05/ardunioled_5.html

二、接線圖

三、程式碼

const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 5;    // the number of LEDs
int ledPins[] = { 
7, 8,9,10,11};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }
}

void loop() {
// read the potentiometer:
 // int sensorReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  //int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(100);
    digitalWrite(ledPins[thisLed], LOW);
  } 

  for (int thisLed = ledCount-1; thisLed >= 0; thisLed--) {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(100);
    digitalWrite(ledPins[thisLed], LOW);

    if (thisLed ==0){
      digitalWrite(ledPins[thisLed], HIGH );
      delay(100);
    }
  }
}


四、作業練習:
如何利用可變電阻來調整霹靂燈速?