개발자/Arduino

SSD1306 OLED 로 LM35 온도센서 값을 디스플레이

지구빵집 2020. 10. 7. 11:00
반응형

 

아두이노에서 온도 습도와 같은 데이터를 ssd1306 oled에 디스플레이하기 위한 소스코드를 보려고 찾아서 코드를 올려둡니다. 아래 참고 사이트에 기록된 사이트를 번역하여 올려둔 내용임을 알립니다.

 

이 튜토리얼은 Arduino UNO 보드를 SSD1306 OLED 디스플레이 및 LM35 아날로그 온도 센서와 인터페이스 하는 방법을 보여줍니다. 이 프로젝트에서 SSD1306 OLED 디스플레이 (128x64 픽셀)는 섭씨, 켈빈 및 화씨로 환경 온도를 표시하는 데 사용됩니다. Arduino를 SSD1306 OLED 디스플레이와 인터페이스 하는 방법을 보려면 다음 게시물을 방문하십시오. Interfacing Arduino with SSD1306 OLED display 

 

이 프로젝트에서 SSD1306 OLED는 I2C 모드에서 작동하도록 구성되어 있으며 디스플레이가 I2C 모드에서 작동하도록 구성되어 있는지, 일부 디스플레이는 점퍼 배치 또는 일부 납땜이 필요합니다. LM35 온도 센서는 섭씨온도와 선형 적으로 관련된 출력 전압이 있는 3 핀 장치 (VCC, OUT 및 GND)입니다. LM35 출력은 온도에 따라 달라지므로 이 전압을 측정하려면 ADC (Analog-to-Digital Converter) 모듈이 필요합니다. Arduino UNO 마이크로 컨트롤러 (Microchip ATmega328P)에는 10 비트 분해능의 ADC 모듈이 1 개 있습니다. 

 

LM35 출력의 선형 + 10mV / ° C 스케일 팩터는 다음을 의미합니다.

출력 전압 = 10mV —> 온도 = 1 ° C 인 경우

출력 전압 = 100mV —> 온도 = 10 ° C 인 경우

출력 전압 = 200mV —> 온도 = 20 ° C 인 경우

출력 전압 = 370mV —> 온도 = 37 ° C 인 경우

등등. 

 

LM35 Futures (from datasheet): 참고 LM35 temperature sensor Datasheet 

  • Calibrated Directly in ° Celsius (Centigrade)
  • Linear + 10 mV/°C Scale Factor
  • 0.5°C Ensured Accuracy (at +25°C)
  • Rated for Full −55°C to +150°C Range
  • Suitable for Remote Applications
  • Low Cost Due to Wafer-Level Trimming
  • Operates from 4 to 30 V
  • Less than 60-μA Current Drain
  • Low Self-Heating, 0.08°C in Still Air
  • Nonlinearity Only ±¼°C Typical
  • Low Impedance Output, 0.1 Ω for 1 mA Load 

 

ADC 모듈은 아날로그 데이터를 디지털 데이터로 변환합니다. ATmega328P MCU에는 10 비트 ADC 모듈과 1.1V의 내장 기준 전압이 있습니다. 기준 전압을 사용하면 더 나은 결과를 얻을 수 있습니다. 일반적으로 ADC 모듈의 음수 및 양수 참조는 각각 VSS 및 VDD이지만 VDD는 정확히 5.00V와 같지 않으므로 ADC 모듈의 양수 참조로 참조 전압 (1.1V)을 사용해야 합니다. 

 

SSD1306 OLED 디스플레이 및 LM35 온도 센서 회로가 있는 Arduino : 아래 이미지는 프로젝트 회로도를 보여줍니다.

  • LM35 센서에는 3 개의 핀 (왼쪽에서 오른쪽으로)이 있습니다.
  • 핀 1은 Arduino 5V 핀에 연결된 전원 공급 장치 핀입니다.
  • 핀 2 : 출력 핀, Arduino 아날로그 핀 0 (A0)에 연결됨
  • 핀 3 : GND (접지), Arduino GND 핀에 연결됩니다. 

Arduino with SSD1306 OLED display and LM35 temperature sensor circuit:

 

SSD1306 OLED 디스플레이는 다음과 같이 Arduino UNO 보드에 연결됩니다.

  • SSD1306 OLED GND는 Arduino GND (접지)로 이동합니다.
  • SSD1306 OLED VDD-Arduino 5V,
  • SSD1306 OLED SDA 핀 (직렬 데이터)-핀 A4 (Arduino UNO 하드웨어 I2C SDA 핀),
  • SSD1306 OLED SCK 핀 (직렬 클럭)-핀 A5 (Arduino UNO 하드웨어 I2C SCL 핀),
  • SSD1306 OLED RES 핀 (리셋)을 Arduino 핀 4로 연결합니다. 

SSD1306 OLED 디스플레이 DC 핀은 VDD에 연결되어 장치의 I2C 슬레이브 주소가 0x3D임을 의미합니다. DC 핀이 접지 (GND)에 연결된 경우 I2C 슬레이브 주소는 0x3C가 됩니다. 보통 DC핀이 없다면 주소는 3C가 맞을 겁니다.

 

코드에는 총 3 개의 라이브러리가 있으며 다음과 같이 코드에 포함됩니다.

 

#include <Wire.h>              // include Arduino wire library (required for I2C devices)
#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_SSD1306.h>  // include Adafruit SSD1306 OLED display driver

 

SSD1306 OLED 디스플레이 재설정 핀 및 LM35 아날로그 온도 센서 핀 연결은 아래 표시된 코드에 정의되어 있습니다.

 

#define OLED_RESET  4    // define display reset pin
Adafruit_SSD1306 display(OLED_RESET);
 
// define LM35 data pin connection
#define LM35_PIN   A0

 

ADC를 사용하여 전압 량을 읽으면 0에서 1023 (10 비트 분해능) 사이의 숫자가 제공되고, 0V는 0으로 표시되고 1.1V는 1023으로 표시됩니다 (ADC 양의 기준은 1.1V). ADC 디지털 값을 다시 변환하는 것은 쉽습니다. 다음 방정식을 사용할 수 있습니다.

전압 (볼트 단위) = ADC 판독 * 1.1 / 1023

이전 결과에 100을 곱하면 (LM35 배율은 10mV / ° C = 0.01V / ° C) 실제 온도가 됩니다.

온도 (° C) = ADC 판독 값 * 0.1075,

또는 온도 (° C) = ADC 판독 값 / 9.3

여기서 : 0.1075 = 100 * 1.1 / 1023 및 9.3 = 1 / 0.1075

내부 1.1V 레퍼런스를 사용하기 위해 다음 명령을 사용했습니다. analogReference (INTERNAL);

 

이 예에서는 소수점 뒤에 하나의 숫자를 사용했습니다. 온도에 10을 곱하면 온도가 섭씨 10 분의 1도 ( "274"의 출력 값이 27.4 ° C와 같음)가 되므로 최종 결과는 다음과 같습니다.

 

tCelsius = 10 * analogRead(LM35_pin) / 9.3;

 

화씨 10 도의 온도 = (섭씨 10도) x 9/5 +320 (이유 : ° F = ° Cx9 / 5 + 32) 및 1/10 켈빈의 온도 = (섭씨 10도) + 2732 (이유 : K = ° C + 273.16).

 

각 수량의 실제 값을 얻으려면 10으로 나누어야 합니다. 아래 선은 켈빈 온도의 예를 보여줍니다.

 

sprintf(_buffer, "%03u.%1u K", tKelvin / 10, tKelvin % 10);

 

10 분의 1 값을 10으로 나누어 처음 3 자리를 얻고 실제 온도 값의 10 분의 1 숫자 (소수점 뒤의 숫자)는 해당 구분 (10 분의 값 % 10)을 상기시켜줍니다. 이 온도계의 분해능은 0.1 ° C입니다. 

 

전체 소스코드는 아래와 같습니다.

/*
 * Arduino with LM35 analog temperature sensor and SSD1306 OLED display.
 * This is a free software with NO WARRANTY.
 * https://simple-circuit.com/
 */
 
#include <Wire.h>              // include Arduino wire library (required for I2C devices)
#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_SSD1306.h>  // include Adafruit SSD1306 OLED display driver
 
#define OLED_RESET  4    // define display reset pin
Adafruit_SSD1306 display(OLED_RESET);
 
// define LM35 data pin connection
#define  LM35_pin  A0
 
void setup(void)
{
  delay(1000);  // wait a second
  analogReference(INTERNAL);  // set positive reference voltage to 1.1V
 
  // initialize the SSD1306 OLED display with I2C address = 0x3D
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
 
  // clear the display buffer.
  display.clearDisplay();
 
  display.setTextSize(1);   // text size = 1
  display.setTextColor(WHITE, BLACK);  // set text color to white and black background
  display.setCursor(15, 0);            // move cursor to position (15, 0) pixel
  display.print("LM35 TEMPERATURE:");
  display.display();        // update the display
  display.setTextSize(2);   // text size = 2
}
 
int tKelvin, tCelsius, tFahrenheit;
char _buffer[8];
 
void loop()
{    
  // read analog voltage ( = tenths degree Celsius)
  //   9.3 = 1023/(1.1*100) 
  tCelsius = 10 * analogRead(LM35_pin) / 9.3;
  
  tKelvin     = tCelsius + 2732;       // convert tenths °C to tenths Kelvin
  tFahrenheit = tCelsius * 9/5 + 320 ; // convert tenths °C to tenths °Fahrenheit
 
  // print temperature in degree Celsius
  if (tCelsius >= 1000)    // if temperature >= 100.0 °C
    sprintf(_buffer, "%03u.%1u C", tCelsius / 10, tCelsius % 10);
  else
    sprintf(_buffer, " %02u.%1u C", tCelsius / 10, tCelsius % 10);
  display.setCursor(23, 10);
  display.print(_buffer);
    
  // print temperature in degree Fahrenheit
  if (tFahrenheit >= 1000)     // if temperature >= 100.0 °F
    sprintf(_buffer, "%03u.%1u F", tFahrenheit / 10, tFahrenheit % 10);
  else
    sprintf(_buffer, " %02u.%1u F", tFahrenheit / 10, tFahrenheit % 10);
  display.setCursor(23, 30);
  display.print(_buffer);
    
  // print temperature in Kelvin
  sprintf(_buffer, "%03u.%1u K", tKelvin/10, tKelvin%10);
  display.setCursor(23, 50);
  display.print(_buffer);
 
  // print degree symbols ( ° )
  display.drawCircle(88, 12, 2, WHITE);
  display.drawCircle(88, 32, 2, WHITE);
 
  // update the display
  display.display();
 
  delay(1000);  // wait a second
 
}
 
// end of code.

 

아래 그림은 결과 화면을 보여줍니다. 

 

온도 습도 디스플레이 결과 화면 https://simple-circuit.com/arduino-lm35-ssd1306-i2c-oled/

 

명희 선배와 종섭이가 양재로 놀러 나오라고 전화 왔는데 안 나가고 일하는 중이다. 노는 게 좋아 몰려다니며 잘 노는 네가 웬일이니? 갖고 싶은 게 생긴 거니? 아주 많이 돈 벌려고 그래? 

 

아니, 그냥 일이 많아. 일이 갑자기 좋아졌어. 딱히 할 일도 없고 말이지.

 

-끝-

 

참고 사이트

Arduino with SSD1306 OLED display and LM35 temperature sensor 

 

Proteus simulation file download (for version 8.6 and higher):
Arduino + SSD1306 OLED I2C

 

Arduino projects with SSD1306 OLED display:
Arduino with SSD1306 OLED and DHT11/DHT22 sensor
Arduino real time clock using DS1307 and SSD1306 OLED
Real time clock using NodeMCU, DS3231 and SSD1306 OLED
Arduino with DS18B20 sensor and SSD1306 OLED display
Arduino with SSD1306 OLED display and LM35 temperature sensor
Arduino with BMP280 pressure & temperature sensor and SSD1306 OLED
Arduino weather station with SSD1306 OLED and BME280 sensor

 

 

 

반응형