七段顯示器( Seven-segment display )常作為顯示數字的電子元件,主要藉由七個發光二極體組合發光來顯示數字,多數的七段顯示器還會附加小數點或冒號的燈光指示,因為七段顯示器可以顯示 A~F 的英文字母,所以也常用來表示十六進位代碼。
每個七段顯示器以七個發光二極體顯示號碼或英文字,使用共陰極或共楊極,一個七段顯示器需用8個接腳,為減少接腳的使用,常採用TM1637晶片或74HC595晶片來驅動,為能順利使用,需先下載相關的函式庫。
此範例中使用arduino-tm1637 函式庫,可至GitHub搜尋及下載。請同學自行練習至GitHub搜尋及下載。
下載後加入Arduino IDE函式庫內。
接線
請依照範例說明
七段顯示器模組 Arduino
Vcc以紅線 接 5V
GND 以黑線 接 GND
CLK 以顏色線接 D4
DIO 以顏色線接 D5
/*
Basic usage example
Demonstrated some of the basic functionality of the library. Initialize the display, set the backlight brightness, print some text, count from 0 to 100 and print on display and blink some text.
Note: make sure to set your serial monitor to line end: NEW LINE!
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentTM1637 display(PIN_CLK, PIN_DIO);
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
display.print("INIT"); // display INIT on the display
delay(1000); // wait 1000 ms
}
// run loop (forever)
void loop() {
display.print("LOOP"); // display LOOP on the display
delay(1000); // wait 1000 ms
display.print("COUNTING SOME DIGITS");// print COUNTING SOME DIGITS
display.clear(); // clear the display
for (uint8_t i=0; i < 100; i++) { // loop from 0 to 100
display.print(i); // display loop counter
delay(100); // wait 100 ms
}
display.clear(); // clear the display
display.print("SUCC"); // print SUCC for success
display.blink(); // blink SUCC
delay(1000); // wait 1000 ms
}