Arduino筆記(53):4位數7段LED顯示器呈現目前溫度

這次實做的主角是4位數的7段LED顯示器,跟單一個7段顯示器一樣,分成共陽極和共陰極兩種。如果是共陽極,共陽的腳位接到電壓端(5V),如果是共陰極,共陰的腳位接到 GND。四位數顯示器的4個共陰或共陽是用來控制要顯示那個數字,顯示方式是同一時間僅能顯示一個數字,跟看電影一樣,採用視覺暫留的原理,讓每個數字以極短的時間輪流顯示,錯覺會讓眼睛看起來同時亮了4個數字。


4位數7段顯示器的接腳如下圖:

   

[材料]

• Arduino UNO R3 x 1
• 4位數7段LED顯示器 x 1
• DHT-11 温濕度感測器 x 1
• 麵包板 x 1
• 連接線 x n


[接線圖]

Arduino Pin 與4位數7段顯示器的接腳對應關係:
Pin 2 -> A
Pin 3 -> B
Pin 4 -> C
Pin 5 -> D
Pin 6 -> 1
Pin 7 -> F
Pin 8 -> G
Pin 9 -> 4
Pin 10 -> 3
Pin 11 -> 2
Pin 12 -> E



[程式]

以下是共陽極的顯示器程式,若為共陰極要將電位 HIGH 改為 LOW,LOW 改為 HIGH。
#include <Adafruit_Sensor.h>
#include <DHT.h>

#define DHT11_PIN A0    // 連接DHT11 的Pin
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHT11_PIN, DHTTYPE);
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 12;
int f = 7;
int g = 8;
int GND1 = 6;
int GND2 = 11;
int GND3 = 10;
int GND4 = 9;
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int Temp;
int Humi;
int i;
int j;
void setup()
{
  dht.begin();
  // 設定 Pin 2-12 爲 output
  for(int x = 2; x < 13; x++) {
    pinMode(x,OUTPUT);
  }
}
void loop()
{
  // 顯示温度 
  Temp = dht.readTemperature();
  for (int i = 1; i <= 500; i++)
  {
    dig1 = Temp / 10;
    dig2 = Temp - (dig1 * 10);
    dig3 = 10;
    dig4 = 11;
  
    digitalWrite( GND4, HIGH);    //數字 4
    DisplayNumber(dig4);
    delay(4);
    digitalWrite( GND4, LOW);

    digitalWrite( GND3, HIGH);    //數字 3
    DisplayNumber(dig3);
    delay(4);
    digitalWrite( GND3, LOW);

    digitalWrite( GND2, HIGH);   //數字 2
    DisplayNumber(dig2);
    delay(4);
    digitalWrite( GND2, LOW);
    digitalWrite( GND1, HIGH);   //數字 1
    DisplayNumber(dig1);
    delay(4);
    digitalWrite( GND1, LOW);

 }   
  // 顯示濕度
  Humi = dht.readHumidity();
  for (int j = 1; j <= 500; j++)
  {
    dig1 = Humi / 10;
    dig2 = Humi - (dig1 * 10);
    dig3 =  12;
    dig4 = 12;
   
    digitalWrite( GND4, HIGH);    //數字 4
    DisplayNumber(dig4);
    delay(4);
    digitalWrite( GND4, LOW);

    digitalWrite( GND3, HIGH);    //數字 3
    DisplayNumber(dig3);
    delay(4);
    digitalWrite( GND3, LOW);

    digitalWrite( GND2, HIGH);   //數字 2
    DisplayNumber(dig2);
    delay(4);
    digitalWrite( GND2, LOW);

    digitalWrite( GND1, HIGH);   //數字 1
    DisplayNumber(dig1);
    delay(4);
    digitalWrite( GND1, LOW);
}
}
void DisplayNumber(int x) {
  switch (x) {
    case 0: zero(); break;
    case 1: one(); break;
    case 2: two(); break;
    case 3: three(); break;
    case 4: four(); break;
    case 5: five(); break;
    case 6: six(); break;
    case 7: seven(); break;
    case 8: eight(); break;
    case 9: nine(); break;
    case 10: o(); break;
    case 11: C(); break;
    case 12: oo(); break;
  }
}

void zero()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, LOW);
  digitalWrite( f, LOW);
  digitalWrite( g, HIGH);
}

void one()
{
  digitalWrite( a, HIGH);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, HIGH);
  digitalWrite( e, HIGH);
  digitalWrite( f, HIGH);
  digitalWrite( g, HIGH);
}

void two()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, HIGH);
  digitalWrite( d, LOW);
  digitalWrite( e, LOW);
  digitalWrite( f, HIGH);
  digitalWrite( g, LOW);
}

void three()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, HIGH);
  digitalWrite( f, HIGH);
  digitalWrite( g, LOW);
}

void four()
{
  digitalWrite( a, HIGH);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, HIGH);
  digitalWrite( e, HIGH);
  digitalWrite( f, LOW);
  digitalWrite( g, LOW);
}

void five()
{
  digitalWrite( a, LOW);
  digitalWrite( b, HIGH);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, HIGH);
  digitalWrite( f, LOW);
  digitalWrite( g, LOW);
}

void six()
{
  digitalWrite( a, LOW);
  digitalWrite( b, HIGH);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, LOW);
  digitalWrite( f, LOW);
  digitalWrite( g, LOW);
}

void seven()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, HIGH);
  digitalWrite( e, HIGH);
  digitalWrite( f, HIGH);
  digitalWrite( g, HIGH);
}

void eight()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, LOW);
  digitalWrite( f, LOW);
  digitalWrite( g, LOW);
}

void nine()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, HIGH);
  digitalWrite( f, LOW);
  digitalWrite( g, LOW);
}
void o()
{
  digitalWrite( a, LOW);
  digitalWrite( b, LOW);
  digitalWrite( c, HIGH);
  digitalWrite( d, HIGH);
  digitalWrite( e, HIGH);
  digitalWrite( f, LOW);
  digitalWrite( g, LOW);
}

void C()
{
  digitalWrite( a, LOW);
  digitalWrite( b, HIGH);
  digitalWrite( c, HIGH);
  digitalWrite( d, LOW);
  digitalWrite( e, LOW);
  digitalWrite( f, LOW);
  digitalWrite( g, HIGH);
}
void oo()
{
  digitalWrite( a, HIGH);
  digitalWrite( b, HIGH);
  digitalWrite( c, LOW);
  digitalWrite( d, LOW);
  digitalWrite( e, LOW);
  digitalWrite( f, HIGH);
  digitalWrite( g, LOW);
}


[實作結果]

最上方的照片為顯示溫度,下圖為顯示濕度。

[參考資料]

Great Projects: Temperature and Humidity Displayed on 4 Digit 7 segment

Post a Comment

較新的 較舊