'I2C'에 해당되는 글 14건

  1. 2019.11.19 Hardware | LCD2004 를 arduino 로 컨트롤 해보기 - 2
  2. 2019.10.12 Hardware | 공기질 측정용 MiCS-6814 센서를 사용해 보자 - 1 26
  3. 2019.10.08 Hardware | LCD2004 를 arduino 로 컨트롤 해보기 - 1
  4. 2019.08.12 Hardware | ADS1115 16bit 4채널 ADC 를 사용해 보자 6
  5. 2019.08.10 Hardware | Digital Compass - HMC5883L 사용기 - 3
  6. 2019.03.18 Hardware | PN523 - RFID / NFC breakout 보드
  7. 2018.11.20 Hardware | RTC DS3231 부품 사용기 - 2
  8. 2018.08.14 Hardware | Raspberry Pi CPU Info screen 구매기 2
  9. 2018.07.24 Hardware | SSD1306 에 로고를 세겨보자
  10. 2018.01.12 Hardware | SSD1309 128x64 1.54" yellow OLED 2

Hardware | LCD2004 를 arduino 로 컨트롤 해보기 - 2

|

이 글은, 먼저 LCD 20x4 를 처음 사용해 본 글의 후편입니다.


* Hardware | LCD2004 를 arduino 로 컨트롤 해보기 - 1

https://chocoball.tistory.com/entry/Hardware-LCD2004-arduino-control-1




1. 이론적 배경


LCD2004 및 HD44780 을 사용하는 LCD 에 대해서는 아래 웹사이트에서 거의 완벽하게 설명하고 있습니다.


* Arduino with HD44780 based Character LCDs

http://www.martyncurrey.com/arduino-with-hd44780-based-lcds/


위의 글은 전반적인 이야기 이고, 아래에 보이는 글 두 개는, 커스텀 글짜에 대한 좀 더 자세한 이야기 입니다.


* How to generate and display self made Custom characters on 16×2 lcd

https://www.engineersgarage.com/knowledge_share/making-custom-characters-on-16x2-lcd/


An internal ​CG-RAM(character generated ram) in which we can generate or place our custom character. CG-RAM size is 64 bytes. We can generate/place 8 characters of size 5×8 at a time in CG-RAM.


* Making and displaying Custom characters on lcd with Arduino Uno and 16×2 lcd

https://www.engineersgarage.com/arduino/making-custom-characters-on-lcd-using-arduino/


CG-RAM is the main component in making custom characters. CG stands for custom generated and RAM you all know random access memory. This CG-RAM stores our custom characters once we declare them in our code. I will come on it later. As you know once we write any type of code we need a memory to store it and a controller to run it. In the 16×2 lcd custom character case its same. We write code(arrays) of character’s which we want to display on lcd. Then we store them in a memory on lcd. In our case this memory is named as CG-RAM(Character generated RAM). 


CG-RAM size is 64 Bytes. You can create 8 characters at a time and load them in cg-ram. Each character occupies 8-bytes. Eight characters each of eight byte (8-characters * 8-Bytes) is equal to 8×8=64 Bytes. CG-RAM address in lcd memory starts from 0x40(Hexadecimal) or 64 in decimal.


위의 글들을 요약하자면 다음과 같습니다.


- HD44780 을 사용하는 LCD 에서는 커스텀 글자를 만들 수 있다.

- 64 Bytes 메모리 한계로, 커스컴 글자는 8개까지만 가능.

- 어드레스는, 0x00~0x07 로 접근 가능.




2. 한땀 한땀 만들어 보기


Custom Character 제작 시, 어떤 형식으로 메모리에 올리는지를 자동으로 소스까지 생성해 주는 페이지 입니다.

이 페이지에서, 8x5 형상에 원하는 모양을 마우스로 클릭해 보면, 대충 감을 잡을 수 있습니다.


* LCD Custom Character Generator

http://maxpromer.github.io/LCD-Character-Creator/


위의 페이지를 따라해 보면, Parallel 연결 + Binary 조합으로 소스 작성은 다음과 같이 됩니다.

대문자 B 로 시작하여, 각 cell 값에 따라 도식처럼 보여줍니다.


이 블로그의 마스코트인 초코볼을 본따 봤습니다. (도트의 한계로 거의 뭐...)



Hexadecimal 로 표현할 때에는, 5자리 2진수를 Hex 코드로 만들어 줍니다. 예를 들면 아래처럼요...


B10111 = 0x17


내 머리로 계산하지 않더라도 Hexadecimal 인 "0x..." 로 표현할 수 있습니다.

코드적으로 보면, 아무래도 Hex 를 사용하는 것이 코드상 더 간결하게 됩니다.



Arduino 연결을 I2C 로 바꾸어 주면,

include 구문과 LiquidCrystal_I2C 를 I2C 접근 주소와 함께 선언할 수 있도록 소스가 추가됩니다.



소스까지 생성해 주니, 이해하기 쉽습니다.

위의 페이지를 통하여, custom character 는 어떻게 해서 생성되는 지를 감각적으로 배워볼 수 있네요.




3. 끝판 왕


한땀 한땀이 아니라, 8개 생성 가능한 메모리를 한번에 만들 수 있는 소개 자료 입니다.


* Alphanumeric LCD HD44780 - big font, big digits generator (excel sheet)

https://www.avrfreaks.net/forum/alphanumeric-lcd-hd44780-big-font-big-digits-generator-excel-sheet



EXCEL 을 이용하여, 마우스 클릭만으로 폰트를 제작할 수 있게 되어 있습니다.


* dblachut/LCD-font-generator

https://github.com/dblachut/LCD-font-generator

LCD-font-generator.xlsm


Custom Character 는 8개까지 가능하니,

all 흑백, all 흰색을 제외하면 7개를 만들고, 추가적으로 하나 더 만들 수 있는 여유가 있습니다.




4. 메모리 들여다 보기


아래 장표는 HD44780 에 기억되어 있는 코드들 입니다.

왼쪽이 아시아 버전 (일본어 가다가나가 미리 들어가 있슴) 과, 오른쪽이 유럽 버전 (뭔가 더 다채로움) 입니다.



그럼, 실제로 커스텀 글짜를 입력해 보고, 메모리에 어떤 문자가 기억되어 있는지 알아 볼 수 있는 소스가 있습니다.

해당 소스를 사용하려면 아래처럼, 관련 Library 를 인스톨 하면 됩니다.


Tools > Manage Libraries... > LiquidCrystal I2C by Frank de Brabander



위의 library 를 인스톨 하면, 예제코드를 받을 수 있습니다.


File > Examples > LiquidCrystal I2C > CustomChars



소스는 다음과 같습니다.


//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include "Wire.h"
#include "LiquidCrystal_I2C.h"

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif

uint8_t bell[8]  = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
uint8_t note[8]  = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
uint8_t duck[8]  = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
uint8_t retarrow[8] = {	0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
  
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {
	lcd.init(); // initialize the lcd
	lcd.backlight();
	
	lcd.createChar(0, bell);
	lcd.createChar(1, note);
	lcd.createChar(2, clock);
	lcd.createChar(3, heart);
	lcd.createChar(4, duck);
	lcd.createChar(5, check);
	lcd.createChar(6, cross);
	lcd.createChar(7, retarrow);
	lcd.home();
	
	lcd.print("Hello world...");
	lcd.setCursor(0, 1);
	lcd.print(" i ");
	lcd.printByte(3);
	lcd.print(" arduinos!");
	delay(5000);
	displayKeyCodes();
}

// display all keycodes
void displayKeyCodes(void) {
	uint8_t i = 0;
	
	while (1) {
		lcd.clear();
		lcd.print("Codes 0x"); lcd.print(i, HEX);
		lcd.print("-0x"); lcd.print(i+16, HEX);
		lcd.setCursor(0, 1);
		for (int j=0; j<16; j++) {
			lcd.printByte(i+j);
		}
		
		i+=16;
		
		delay(4000);
	}
}

void loop() {

}


위의 소스를 arduino 에 올리고, LCD 20x4 와 I2C 연결해서 얻은 결과 입니다.



Custom Character 가 잘 올라가서 표현되었습니다.

0x00 부터 0xff 까지, 내부 글짜를 모두 훑는 결과는 다음과 같습니다.



확실히 아시아 버전임을 알 수 있네요.

버전이 다르지만, 구동은 같은 HD44780 칩 이므로, 해킹을 통하여 비어있는 address 에도 쓰기가 가능할 것 같은데,

복잡할 듯 하여 그만 두기로 합니다.




5. 큰 폰트


여기까지 해 보면, 큰 폰트 사용하여 시계 등을 표현하는 방법을 대강 생각해 낼 수 있습니다.

이 글의 원래 목적이었던, 아래 그림처럼 표현해 보고자 함이었으니, 이제 커스텀 폰트를 만들어 봅니다.



위의 숫자 들을 잘 살펴 보면, 몇 가지 모양을 조합하여 큰 폰트를 만들어 낸 것을 알 수 있습니다.



유니크한 영역을 살펴 보면, 아무것도 적혀있지 않은 모양까지 포함하면, 모두 8개가 됩니다.

이렇게 딱 맞게 만들다니...


열씸히 만들어 보려고 여러가지 검색하던 중, 좋은 소스를 발견합니다.


* 4-Line LCD Big Numbers

http://woodsgood.ca/projects/2015/02/27/4-line-lcd-big-numbers/



Software RTC 를 이용하여, 시간 데이터를 가져오고, custom / big font 로 시계를 표현해 주는 소스 입니다.


//************************************************************
//             BIG FONT (4-line) LCD CHARACTERS 
//                Adrian Jones, February 2015
//************************************************************

// Build 1
//   r1 150214 - initial build with glyphs and big font character table in program memory
//   r2 150227 - added RTC support
//************************************************************
#define build 1
#define revision 2
//************************************************************

#include "avr/pgmspace.h" // for memory storage in program space

#include "Wire.h" 
#include "LiquidCrystal_I2C.h" // library for I@C interface

LiquidCrystal_I2C lcd(0x27, 20, 4);

const char custom[][8] PROGMEM = {
	{0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0f, 0x1f}, // char 1: top left triangle
	{0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x1f}, // char 2: upper block
	{0x00, 0x00, 0x00, 0x00, 0x10, 0x1c, 0x1e, 0x1f}, // char 3: top right triangle
	{0x1f, 0x0f, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00}, // char 4: bottom left triangle
	{0x1f, 0x1e, 0x1c, 0x10, 0x00, 0x00, 0x00, 0x00}, // char 5: bottom right triangle
	{0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00}, // char 6: bottom block
	{0x1f, 0x1f, 0x1e, 0x1c, 0x18, 0x18, 0x10, 0x10}  // char 7: full top right triangle
	                                                  // room for another one!
};

const char bn[][30] PROGMEM = { // organized by row
//         0               1               2               3               4              5               6                7               8               9
    {0x01,0x02,0x03, 0x01,0x02,0xFE, 0x01,0x02,0x03, 0x01,0x02,0x03, 0x02,0xFE,0x02, 0x02,0x02,0x02, 0x01,0x02,0x03, 0x02,0x02,0x02, 0x01,0x02,0x03, 0x01,0x02,0x03},
    {0xff,0xfe,0xff, 0xFE,0xFF,0xFE, 0x01,0x02,0xFF, 0xFE,0x02,0xFF, 0xFF,0x02,0xFF, 0xFF,0x02,0x02, 0xFF,0x02,0x03, 0xFE,0x01,0x07, 0xFF,0x02,0xFF, 0xFF,0xFE,0xFF},
    {0xff,0xfe,0xff, 0xFE,0xFF,0xFE, 0xFF,0xFE,0xFE, 0xFE,0xFE,0xFF, 0xFE,0xFE,0xFF, 0xFE,0xFE,0xFF, 0xFF,0xFE,0xFF, 0xFE,0xFF,0xFE, 0xFF,0xFE,0xFF, 0x04,0x06,0xFF},
    {0x04,0x06,0x05, 0xFE,0x06,0xFE, 0x06,0x06,0x06, 0x04,0x06,0x05, 0xFE,0xFE,0x06, 0x04,0x06,0x05, 0x04,0x06,0x05, 0xFE,0x06,0xFE, 0x04,0x06,0x05, 0xFE,0xFE,0x06}
};

byte col, row, nb=0, bc=0; // general
byte bb[8]; // byte buffer for reading from PROGMEM

#include "RTClib.h"
RTC_Millis RTC;
byte hr, mn, se, osec;

//*********************** INITIAL SETUP ***********************
void setup() {
	randomSeed(analogRead(0));
	RTC.begin(DateTime(__DATE__, __TIME__));
	
	lcd.init(); // initialize the LCD
	lcd.backlight();
	lcd.begin(20, 4);
	
	for (nb=0; nb<7; nb++ ) { // create 8 custom characters
		for (bc=0; bc<8; bc++) bb[bc]= pgm_read_byte( &custom[nb][bc] );
		lcd.createChar( nb+1, bb );
	}
	
	lcd.clear();
	lcd.setCursor(4, 0);
	lcd.print(F("4-Line LARGE"));
	lcd.setCursor(4, 1);
	lcd.print(F("TIME DISPLAY"));
	lcd.setCursor(5, 3);
	lcd.print(F("V"));
	lcd.print(build);
	lcd.print(F("."));
	lcd.print(revision);
	lcd.print(F(" "));
	lcd.print(freeRam());
	lcd.print(F("B"));
	printNum(random(0,10),0);
	printNum(random(0,10),17);
	delay(5000);
	lcd.clear();
}

//************************* MAIN LOOP ************************
void loop() {
	DateTime now = RTC.now();
	hr = now.hour();
	mn = now.minute();
	se = now.second();
	
	if(se != osec) {
		printNum(hr/10,0);
		printNum(hr%10,3);
		printColon(6);
		printNum(mn/10,7);
		printNum(mn%10,10);
		printColon(13);
		printNum(se/10,14);
		printNum(se%10,17);
		osec = se;
	}
	delay(50); // not strictly necessary
}

...


소스가 좀 길어서, SyntaxHighLighter 가 커버하지 못합니다. 따로 소스파일을 올립니다.


4line_LCD_clock.txt


참고로, 사이트에 올라와 있는 소스 그대로 사용할 수가 없습니다.

제작된지 시간도 좀 흘렀고, 사용한 library 가 조금 다른 것이 원인일 듯 하네요.

그래서 소스에서 몇가지 살짝 수정하였습니다.


	lcd.init(); // initialize the LCD
	lcd.backlight();
	lcd.begin(20, 4);


debug 시, 특히 가장 시간을 많이 잡아 먹은 부분은, 위의 LCD 초기화 부분이었습니다.

원 소스에는 위의 부분이 없거나, 실행 위치가 달라 그대로 사용하면 이상한 폰트들을 마구 뿌립니다.

특히, "lcd.init()" 는 custom character 를 메모리에 입히는 전단계에서 실행되어야 합니다.


추가로, Software RTC 라이브러리가 필요합니다.


Tools > Manage Libraries... > RTClib by Adafruit




모든 준비가 완료되면 arduino 에 업로드 합니다.



역시 삽질 끝에 맛보는 행복. 동영상도 올려봅니다.



참고로, 다른 custom character 들은 숫자들 끼리 공유가 되지만, 7 숫자의 중간 꺾이는 부분에 할당된 character 는 7에서만 사용됩니다.

위의 동영상에서 7 나오는 부분을 잘 보시면 됩니다.



7에서만 사용되는 문자 형상은 위와 같습니다. 공유되기 애매한 모양이죠?

메모리 한개가 여유 있으니, 소스 원작자가 여유를 부린 듯 합니다 :-)



사무실 책상 위에, laptop 옆에 위치 시켰더니, 가독성도 좋고 시간 보는 재미가 있습니다.

사진에는 잘 표현되지 못했지만, 검은색 글씨라 눈도 편안합니다.


모두 Happy Arduino~!


And

Hardware | 공기질 측정용 MiCS-6814 센서를 사용해 보자 - 1

|

1. 공기


사람이 건강하게 살아가는데 필요한게 뭘까 라고 생각했을 때, 육체적 관점에서 보면 아래와 같다고 생각합니다.


- 깨끗한 공기

- 깨끗한 물

- 오염되지 않은 먹거리

- 쾌적한 주거 환경

- 충분한 수면

- 충분한 운동


예전에는 당연한 것이였지만, 이 시대에 와서는 가장 돈을 많이 들여야 하는 항목들이 되었습니다.

과거로 회기하는데 드는 비용이죠.


자고로, 산업혁명이 일어나 지구가 오염되기 전 상황으로 되돌아 가는 것이, 육체적인 건강한 삶 되겠습니다. (나의 생각)

나만 잘한다고 되는건 아니라 요원하긴 하지만...


이 arduino 나 sensor 를 가지고 노는 이유도, 전자적인 지식을 습득해 나가는 것에 대한 희열 말고도,

위의 "건강한 삶을 보낼 수 있는 환경" 에 대한 추구가 다른 주된 이유이기도 합니다.


그래서, "깨끗한 공기" 가 목적이지만, 그 이전에 현재 "어떤 공기" 속에서 살고 있는지 알아보고 싶어졌습니다.

일전에 공기질의 척도만 나타내 주는 센서를 가지고 놀아본 적이 있습니다.


* Hardware | ZP07-MP901 공기질 측정 센서

https://chocoball.tistory.com/entry/Hardware-ZP07-MP901-air-quality-sensor


다만 위의 센서는 공기의 성분까지는 알려주지 못해 그닥 쓸모가 없었죠.





2. 센서


기체를 알아보는 sensor 는 꽤 많이 나와 있습니다. 대표적으로는 MQ series 가 있지요.





이 MQ 시리즈를 이용하면 왠만한건 다 잡아 낼 수 있을 것 같습니다.


다만, 이 MQ 시리즈의 단점이 히터를 통해서 기체를 태우고 그에 따른 반응으로 측정을 하는 방법인지라,

가스나 화기를 취급하는 곳에서는 발화의 원인이 될 수 있습니다. 그래서 인화 물질을 취급하는 공장이나 병원에서는 사용되지 못합니다.


그러던 중, MiCS-6814 라는 센서의 존재를 알게 됩니다.



이 센서는 metal oxide = 기체에 의한 금속 산화도 에 따른 저항 값을 가지고 측정하므로, MQ 시리즈보다는 더 안전합니다.


SGX-CMOS-Gas-Sensor-MiCs-6814.pdf

pollutant-visualizer.pdf


잡아낼 수 있는 기체들은 다음과 같다고 하네요.


• Carbon monoxide CO 1 – 1000ppm

• Nitrogen dioxide NO2 0.05 – 10ppm

• Ethanol C2H5OH 10 – 500ppm

• Hydrogen H2 1 – 1000ppm

• Ammonia NH3 1 – 500ppm

• Methane CH4 >1000ppm

• Propane C3H8 >1000ppm

• Iso-butane C4H10 >1000ppm


하나의 센서 chip 으로 많은 성분을 잡아 낼 수 으며, 소형인지라,

MQ 시리즈를 구입해서 실험 해보기 보단, 이놈으로 결정하였습니다.




3. Grove - Multichannel Gas Sensor


MiCS-6814 를 활용하기 위해 가장 좋은 방법은,

MiCS-6814 를 활용한 breakout board 인 Grove 사의 Multichannel Gas Sensor 를 구입하는 것 입니다.


* Grove - Multichannel Gas Sensor

http://wiki.seeedstudio.com/Grove-Multichannel_Gas_Sensor/



Arduino 와의 통신도 I2C로 이루어 지며, 센서의 3개 측정값 - CO, NH3, NO3 - 의 조합으로 다른 기체도 유추해 놓는것 같습니다.

거기에 정확하지는 않지만, 어느정도 calibration 도 잡혀 있을 것이구요.


다만, 항상 그렇 듯, 가격이 문제 입니다. 요즘은 팔지도 않거니와, 실 구매 가격은 약 10만원 정도.

나중에 혹시 PCB 라도 꾸며서 만들 수 있으면 만들어 보고자, 여기에 회로 관련 정보를 올려 놓습니다.


Grove-Multichannel_Gas_Sensor_v1.0_sch.pdf

Grove-Multichannel_Gas_Sensor_v1.0_eagle_files.zip

1143_Datasheet-MiCS-6814-rev-8.pdf


* Firmware (Seeed-Studio/Mutichannel_Gas_Sensor)

https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor




4. 구입


눈물을 뒤로 하고, AliExpress 로 향합니다.


* 1pcs CJMCU- MICS-6814 Air Quality CO VOC NH3 Nitrogen Oxygen Gas Sensor

- https://www.aliexpress.com/item/32762216430.html



저를 위로해 주는 기교적 착한 가격. 거진 3만원돈 이지만, 과감하게 투자해 봅니다. (6개월 고민... ㅠㅠ)



대단한건 아니지만, 도착했을 시 꽤 기뻤습니다. 이제야 이런 고급진 센서 써보는구나~ 라고.



정말 단순한 구조. 레귤레이터도 없어...



뒷면입니다.





5. 연결


어떻게 arduino 와 연결하여, 각 기체의 값을 알아낼까 찾아보던 중 아래 사이트를 발견합니다.


* Wiring MiCs 5524 / 6814 CMOS MEMS Gas Detection Sensor

https://www.14core.com/wiring-mics-5524-6814-cmos-mems-gas-detection-sensor/



걍 analog pin 에 직결하라고 하네요?

제가 구매한 breakout 보드에는 MOSFET 도 없고 그렇지만, 일단 아래 소스를 가지고 돌려 봅니다.


int NH3 = 0;
int NO2 = 1;
int CO = 2;

void setup() {
	pinMode(NH3, OUTPUT);
	pinMode(NO2, OUTPUT);
	pinMode(CO, OUTPUT);
	
	Serial.begin(9600);
	Serial.println("MiCS 6814 simple test");
}
 
void loop() {
	int sense_val_1 = analogRead(NH3);
	int sense_val_2 = analogRead(NO2);
	int sense_val_3 = analogRead(CO);
	
	Serial.print("NH3 : ");
	Serial.print(sense_val_1);
	Serial.print(" \t NO2 : ");
	Serial.print(sense_val_2);
	Serial.print(" \t CO : ");
	Serial.println(sense_val_3);
	
	delay(1000);
}


Serial Monitor 로 값을 확인해 보고, EXCEL 로 그래프를 그려 봅니다.



음... 뭔가 알아보기 힘드네요. 각 기체의 값이 서로 비슷하게 나와버립니다.






6. 저항을 추가하여 연결


아래 사이트에서 이 아저씨가 한 작업은,

측정값이 현실적으로 되려면 analog pin 에 입력되는 값은, 3.3V 를 저항을 거쳐서 연결하라고 합니다.

(이론적으로는 50K ohm 이 좋으나, 제품으로 나오는 저항은 47K ohm 이 제일 가까운 값)


* ESP32, PMS5003, BME280, MICS6814 Sensor Build

http://kstobbe.dk/2019/02/16/esp32-pms5003-bme280-mics6814-sensor-build/



또한, 아래 arduino forum 에서는, 각 단자에 47K ohm 통해 연결하는 전원은 3.3V 보단, 5V 를 먹이라고 하네요.

아마도 MiCS-6814 구동 전압이 5V 임을 감안하면, 동일한 입력 값이 좀더 예민한 값의 도출에 좋다는 이야기 같습니다.

(솔직히 잘 모름)


* CJMCU-6814 adapter board with MICS-6814 CO/NH3/NO2-sensor

https://forum.arduino.cc/index.php?topic=619992.0


Tested the sensor with the code and schematic. Connected 47k to 3 gases and 5v rather than 3v. The results seem to reflect well to CO. Have not tested NO2 and NH3.


바로 따라해 봅니다.



Serial Monitor 에서 측정된 값이구요.



EXCEL 에서 그래프화 해 봤습니다.

오오오오오, 뭔가 나오네요.



가능한 24시간동안 예열하도록 안내가 되어 있듯이, 예열 구간동안 특정 값으로 꾸준히 변화되는 것을 알 수 있습니다.

위의 빨간 표시한 부분은, 시험삼아 숨을 한번 불어본 구간입니다. 변화를 잘 감지하는군요!



하룻밤 측정해본 결과 입니다.

각 기체의 상관 관계와 실내 공기 내에서의 NH3, NO2, CO 의 값은 이정도인 듯 합니다.




7. 15bit ADC + 저항을 추가하여 연결


Arduino 내장 10 bit ADC 를 가지고 측정한 결과 보다는 15 bit ADC 가 훨씬 정확합니다.

아래 포스트 이후로, 향후 모든 측정값은 더 높은 ADC 를 통하라고 배웠습니다.


* Hardware | ADS1115 16bit 4채널 ADC 를 사용해 보자

https://chocoball.tistory.com/entry/Hardware-ADS1115-16bit-4channel-ADC


ADS1115 이 입력값을 받구요.



소스를 ADS1115 용으로 살짝 수정해 줍니다.

#include "Wire.h"
#include "Adafruit_ADS1015.h"
 
Adafruit_ADS1115 ads(0x48);
 
void setup(void) {
    Serial.begin(115200);
    ads.begin();
}
 
void loop(void) {
    int16_t adc1; // CO
    int16_t adc2; // NH3
    int16_t adc3; // NO2
     
    adc1 = ads.readADC_SingleEnded(1);
    adc2 = ads.readADC_SingleEnded(2);
    adc3 = ads.readADC_SingleEnded(3);

    Serial.print("NH3 : ");
    Serial.print(adc2);
    Serial.print(" \t NO2 : ");
    Serial.print(adc3);
    Serial.print(" \t CO : ");
    Serial.println(adc1);
     
    delay(1000);
}


Serial Monitor 에서 확인한 값 들 입니다.

확실히 뭔가 엄청나게 숫자가 불어나 있습니다.



EXCEL 을 통해서 그래프화 시켜 봅니다.



패턴은 47K ohm 저항 꼽고 측정할 때와 비슷하게 나왔습니다.


다만, 높은 ADC 를 통하여 해상도는 좋아졌지만,

어떤 기준값으로 나누어야 할지 알지 못하기 때문에, trend 만 알 수 있지 정확한 수치화까지는 진행하지 못했습니다.




8. Next Step


이런 공기질 측정에서의 수치화는 calibration 인데,

그렇게 하려면 실제 실험실 환경을 통해서, 실제 기체를 가지고 기준을 잡아야 합니다.


위의 방법이 현실적으로 불가능 하기에,

다음에 할 것으로는 Grove - Multichannel Gas Sensor 의 breakout 을 만들어서 (아님 구입해서),

제조사인 SeeedStudio 의 경험이 녹아들어가 있는 firmware 와 소스코드를 가지고 수치화 해보는 것 이겠습니다.


공기 질에 관한 센서는 몇 개가 더 있습니다. 계속 놀아보도록 하지요.

To Be Continued...


And

Hardware | LCD2004 를 arduino 로 컨트롤 해보기 - 1

|

1. 시작


흠흠흠~ 하면서 인터넷을 보던 중, arduino 로 컨트롤 하는 Liquid Crystal Display 를 보게 됩니다.

이게 흔히 말하는 LCD 이죠.


OLED 와 비교하여 부피도 크고, 명암 조절을 수동으로 해줘야 하는 등, 불편한 device 라고 생각하고 있던 중,

LCD 의 푸른색 색감을 보고 반해버렸습니다.


Display 부품을 늘이고 싶지 않았지만, 이제 구입할 때가 된것 같아 구입합니다.


* LCD2004+I2C 2004 20x4 2004A blue screen HD44780 Character LCD /w IIC/I2C Serial Interface Adapter Module For Arduino

https://www.aliexpress.com/item/32831845529.html



결코 싼 가격은 아니지만, 이왕 구입하는 김에 보편적으로 많이 사용하는 녹색과 색감에 반한 푸른색, 두 개를 삽니다.





2. 도착


한 2주만에 재품을 받았습니다.

무료 배송이 점점 없어지고 배송비 책정이 되면서 배송 기간이 점점 짧아지고 있는 듯 합니다.



푸른색, 녹색 두개가 와서 PCB 를 비교해 보니, 서로 동일한 듯 하나 살짝 다른 모습 입니다.



I2C 컨트롤러의 칩 한쪽은 마킹이 너무 얇아 fake 제품처럼 보입니다.

조금이라도 진품처럼 보이는 쪽은 파란색 버전 입니다.



납땜도 녹색보다는 파란색이 잘 되어 있습니다.



PCB 마킹도, 파란색은 동판에 미리 새겨져 있고, 녹색은 프린팅 되어 있네요.



뭐, 생산하다가 점점 원가절감 하면서 변화된 것 일 수도 있습니다.

보통 녹색을 많이 사용하니, 녹색이 원가절감이 더 된 것 일수도 있구요.





3. Pin


Pin 수는 16개로, 아래와 같은 사양을 가집니다.


-------------------------------------------------------------------
|  pin  |                        function                         |
-------------------------------------------------------------------
|  VSS  | connected to ground                                     |
-------------------------------------------------------------------
|  VDD  | connected to a +5V power supply                         |
-------------------------------------------------------------------
|   V0  | to adjust the contrast                                  |
-------------------------------------------------------------------
|   RS  | A register select pin that controls where in the LCD's  |
|       | memory you are writing data to. You can select either   |
|       | the data register, which holds what goes on the screen, |
|       | or an instruction register, which is where the LCD's    |
|       | controller looks for instructions on what to do next.   |
-------------------------------------------------------------------
|  R/W  | A Read/Write pin to select between reading and writing  |
|       | mode                                                    |
-------------------------------------------------------------------
|   E   | An enabling pin that reads the information when HIGH    |
|       | level(1) is received. The instructions are run when the |
|       | signal changes from HIGH level to LOW level.            |
-------------------------------------------------------------------
| D0-D7 | to read and write data                                  |
-------------------------------------------------------------------
|   A   | Pins that control the LCD backlight. Connect A to 3.3V. |
-------------------------------------------------------------------
|   K   | Pins that control the LCD backlight. Connect K to GND.  |
-------------------------------------------------------------------


대충 훑어 보면, back light 전원, 본체 전원, 명암 조절, data 통신과 컨트롤 라인 등이 있습니다.

제가 구매한 제품은 I2C 모듈인 "PCF8574" 컨트롤러가 달린 제품입니다.

그러니, 일일이 모두 연결할 필요 없이 I2C 를 통해, 단 4가닥 선으로 컨트롤 할 수 있습니다.


이 chip 의 생산은 Philips, TI, NXP 등에서 생산되고 있네요. PDF 사양서를 올려 놓습니다.


LCD2004

TC2004A-01.pdf

Systronix_20x4_lcd_brief_data.pdf

RK-10290_410.pdf

50586.pdf


HD44780

HD44780.pdf

LCD Interfacing using HD44780 Hitachi chipset compatible LCD.pdf


PCF8574

PCF8574_PCF8574A.pdf

PCF8574T.pdf

pcf8574.pdf





4. I2C


Arduino 와 연결은 I2C 이므로, 4가닥으로 처리됩니다.



Liquid Crystal I2C 라이브러리가 필요합니다.

일반 Liquid Crystal I2C 라이브러리를 설치해도 되나, 궂이... 궂이 PCF8574 용 라이브러리를 찾아서 설치해 봅니다.

(범용 Liquid Crystal I2C 라이브러리가 아님)



위의 라이브러리를 설치하면 아래처럼 sample code 를 사용할 수 있습니다.


File > Examples > LiquidCrystal_PCF8574 > LiquidCrystal_PCF8574_test



I2C 활용시에는 항상 address 를 확인해 봐야 합니다.

Arduino 와 I2C 로 연결 후, i2cdetect 소스를 돌려 봅니다.



0x27 로 잡혀 있네요.

참고로 다른 I2C 센서들과 연동 시에 address 충돌이 일어나면, 아래 사진의 A0, A1, A2 쇼트 조합으로 주소를 바꿀 수 있습니다.






5. I2C 소스


친절하게도 example 소스가 충분히 잘 만들어져 있는지라, 그냥 돌려 봅니다.


#include "LiquidCrystal_PCF8574.h"
#include "Wire.h"

LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
int show = -1;

void setup() {
	int error;
	
	Serial.begin(115200);
	Serial.println("LCD...");
	
	// wait on Serial to be available on Leonardo
	while (!Serial)
		;
	Serial.println("Dose: check for LCD");
	
	// See http://playground.arduino.cc/Main/I2cScanner how to test for a I2C device.
	Wire.begin();
	Wire.beginTransmission(0x27);
	error = Wire.endTransmission();
	Serial.print("Error: ");
	Serial.print(error);
	
	if (error == 0) {
		Serial.println(": LCD found.");
		show = 0;
		lcd.begin(20, 4); // initialize the lcd
	} else {
		Serial.println(": LCD not found.");
	} // if
} // setup()

void loop() {
	if (show == 0) {
		lcd.setBacklight(255);
		lcd.home();
		lcd.clear();
		lcd.print("Hello LCD");
		delay(1000);
		
		lcd.setBacklight(0);
		delay(400);
		lcd.setBacklight(255);
	} else if (show == 1) {
		lcd.clear();
		lcd.print("Cursor On");
		lcd.cursor();
	} else if (show == 2) {
		lcd.clear();
		lcd.print("Cursor Blink");
		lcd.blink();
	} else if (show == 3) {
		lcd.clear();
		lcd.print("Cursor OFF");
		lcd.noBlink();
		lcd.noCursor();
	} else if (show == 4) {
		lcd.clear();
		lcd.print("Display Off");
		lcd.noDisplay();
	} else if (show == 5) {
		lcd.clear();
		lcd.print("Display On");
		lcd.display();
	} else if (show == 7) {
		lcd.clear();
		lcd.setCursor(0, 0);
		lcd.print("*** first line.");
		lcd.setCursor(0, 1);
		lcd.print("*** second line.");
		lcd.setCursor(0, 2);
		lcd.print("*** third line.");
		lcd.setCursor(0, 3);
		lcd.print("*** forth line.");
	} else if (show == 8) {
		lcd.scrollDisplayLeft();
	} else if (show == 9) {
		lcd.scrollDisplayLeft();
	} else if (show == 10) {
		lcd.scrollDisplayLeft();
	} else if (show == 11) {
		lcd.scrollDisplayRight();
	} else if (show == 12) {
		lcd.clear();
		lcd.print("write-");
	} else if (show > 12) {
		lcd.print(show - 13);
	} // if
	
	delay(1400);
	show = (show + 1) % 16;
} // loop()


Serial Monitor 에서 확인해 보니, 잘 인식 되었네요.



너무 쉽게 되어서 살짝 김이 빠지는 정상적인 결과.



동영상도 올려 봅니다. (동영상은 16x2 버전으로 돌린 결과. 수정된 위의 소스로 20x4 로 변경된 소스를 사용하면 4줄 다 사용.)






6. Direct 연결


여기서 끝내면 너무 재미 없는지라, 한땀한땀 직접 연결해 봅니다.

워낙 오래된 부품이라, 아래 arduino 정식 사이트에서 자세히 소개되어 있습니다.


* TUTORIALS > Examples from Libraries > LiquidCrystal > HelloWorld

https://www.arduino.cc/en/Tutorial/HelloWorld?from=Tutorial.LiquidCrystal


각 Pin 의 정보는 다음과 같습니다.



실제 arduino 와 연결은 다음과 같습니다.


--------------------------------
| LCD2004 |    Arduino Nano    |
--------------------------------
|   VSS   |         GND        |
|   VDD   |         5V         |
|   V0    | S of potentiometer |
|   RS    |         D12        |
|   R/W   |         GND        |
|   E     |         D11        |
|  D0-D3  |    no connected    |
|   D4    |         D5         |
|   D5    |         D4         |
|   D6    |         D3         |
|   D7    |         D2         |
|   A     |         3.3V       |
|   K     |         GND        |
--------------------------------


Diagram 으로도 그려 봤습니다. (Digital pin 은 실제와 살짝 틀림)






7. Potentiometer


여기서 필요한 것이 potentiometer 입니다. 가변 저항이죠.

다이얼을 돌려서 저항값을 변화시키는 부품이죠. 구입을 미뤄 오다가, 이번 기회에 사용하고자 구입 했습니다.


* 5 PCS/Lot Potentiometer Resistor 1K 10K 20K 50K 100K 500K Ohm 3 Pin Linear Taper Rotary Potentiometer for Arduino with Cap

https://www.aliexpress.com/item/32948875673.html



구입한 버전은 10K Ohm.



글의 문맥상 뜬금 없지만, 도착샷 입니다. 빠질 수 없죠.



요런 모양 입니다.




8. Direct 연결 구동


직접 연결하는 방법은 Arduino IDE 에서 기본 제공합니다.



소스는 다음과 같구요.

기본 16x2 버전으로 되어 있으니, 20x4 버전으로 살짝 수정해 줍니다.


/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */
 
// include the library code:
#include "LiquidCrystal.h"
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.setCursor(0, 0);
  lcd.print(" Chocoball's Tech");
  lcd.setCursor(0, 2);
  lcd.print(" chocoball.tistory");
}
 
void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 3);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}


전부 연결하면 아래와 같습니다.



동영상도 올려 봅니다.



hello, world! 를 저의 블로그명으로 바꿔 봤습니다.



단순한 표시 방법과 오랜 기간 수정된 라이브러리 덕으로, 정말 스트레스 없이 구동 시켰습니다.




9. I2C 와 Direct 연결시 명령어 차이


I2C 를 통한 함수와, 직접 연결했을 시에 사용할 수 있는 명령어 구성이 살짝 다릅니다.

고맙게도 아래 사이트에서 잘 정리된 테이블이 있어서 여기에 올려 봅니다.


* LCD Display Tutorial for Arduino and ESP8266

https://diyi0t.com/lcd-display-tutorial-for-arduino-and-esp8266/






10. 커스텀 폰트


명령어 차이를 자세하게 올려준 사이트에서 폰트 커스텀에 대해서도 내용이 있어서 따라해 봤습니다.

이 때, 사용하는 LiquidCrystal_I2C 라이브러리는 범용을 사용하는 지라, 아래 라이브러리를 설치합니다.



소스는 아래와 같습니다. 폰트라기 보단 캐릭터 입니다.


#include "Wire.h"
#include "LiquidCrystal_I2C.h"

// Set the LCD address to 0x27 in PCF8574 by NXP and Set to 0x3F in PCF8574A by Ti
LiquidCrystal_I2C lcd(0x27, 20, 4);

byte customChar1[] = {
  B01110,
  B01110,
  B00100,
  B11111,
  B00100,
  B01110,
  B01010,
  B01010
};

byte customChar2[] = {
  B01110,
  B01110,
  B10101,
  B01110,
  B00100,
  B00100,
  B01010,
  B10001
};

void setup() {
  lcd.init();
  lcd.backlight();
}

void loop() {
  lcd.createChar(0, customChar1);
  lcd.home();
  lcd.write(0);
  delay(1000);
  lcd.createChar(0, customChar2);
  lcd.home();
  lcd.write(0);
  delay(1000);
}


Bitmap 을 이용해서 만들었고, 화면 refresh 를 통해서 움직이는 것 처럼 구성했네요.



동영상도 올려 봅니다.



참고로, 이 Liquid Crystal 은 Hitachi HD44780 를 사용하거나 호환품들이라 아래 폰트를 기본 제공합니다.


HD44780.pdf






11. 아...


인터넷에서 보지 말아야 할 것을 봐버렸습니다.



이렇게 이쁘게도 커스터마이징 할 수 있는거군요.

다음은, 커스텀 폰트를 가지고 놀아보겠습니다.


And

Hardware | ADS1115 16bit 4채널 ADC 를 사용해 보자

|

1. 16 bit ADC


ADC 는 Analog to Digital Converter 의 약자로서, 입력받는 값에 대해 digital 로 표현해 줍니다.

Arduino 에는 이 ADC 가 장착되어 있어서 analog input 에 입력받은 신호에 대해 digital 로 leveling 을 해서 보여줍니다.

즉, analog 값을 digital 로 변환해서 보여주는 것이죠.


참고로, arduino nano 에는 10 bit ADC 가 장착되어 있어서 10 bit (0 ~ 1023) 값으로 표현해 줍니다.


다 좋은데, 민감한 sensor 를 다룰 때에는, 이 10 bit ADC 가 아쉬울 때가 있습니다.

좀더 정밀한 값을 들여다 보고 싶은데, 값과 값의 사이값을 알수가 없는거죠.


이 때 등장하는 것이 외부 ADC 모듈 입니다.

AliExpress 와 arduino 를 사랑하는 사람들의 blog 를 보니 ADS1115 라는 것을 많이 사용하는 군요.


ADS1115 는 Texas Instruments 사의 chip 을 사용한 16 bit ADC 입니다.


ads1115.pdf


- Resolution: 16 Bits

- Programmable Sample Rate: 8 to 860 Samples/Second

- Power Supply/Logic Levels: 2.0V to 5.5V

- Low Current Consumption: Continuous Mode: Only 150µA Single-Shot Mode: Auto Shut-Down

- Internal Low-Drift Voltage Reference

- Internal Oscillator

- Internal PGA: up to x16

- I2C Interface: 4-Pin-Selectable Addresses

- Four Single-Ended or 2 Differential Inputs

- Programmable Comparator


아래 link 의 제품이 적당해 보이네요. 구매합니다.


* I2C ADS1115 16 Bit ADC 4 channel Module with Programmable Gain Amplifier 2.0V to 5.5V for Arduino RPi

https://www.aliexpress.com/item/32850495005.html






2. 도착


그간 업무로 정신 없었는데, 어느샌가 도착했습니다.



블로그 내용을 부풀리기 위해서라도 항상 도착샷을 올리는건 필수 입니다.



저렇코롬 생겼구요.



ALERT 와 ADDR 에 pinheader 는 남겨 놓고 납땜하기로 합니다. 그 덕에 2 pinheader 하나 득템.

그 이유는 이 밑에 설명.





3. Addressing


이 ADS1115 는 I2C 통신을 하는데, 하나의 arduino 와 4개까지 연결할 수 있다 보니, I2C 접근 주소가 겹치지 않게 할 수 있습니다.

방법은 ADDR pin 을 어디로 연결하느냐로 address 를 결정할 수 있습니다.


Adafruit 4-Channel ADC Breakouts

https://learn.adafruit.com/adafruit-4-channel-adc-breakouts


- 0x48 (1001000) ADR -> GND

- 0x49 (1001001) ADR -> VDD

- 0x4A (1001010) ADR -> SDA

- 0x4B (1001011) ADR -> SCL


회로를 꾸밀 때 마다, address 정의를 위한 연결을 해도 되지만, 귀찮겠죠?

또한, addressing 을 위해 I2C 용 핀이나, VCC 로 연결하면 왠지 껄끄럽습니다.


그래서 ground 로 연결하여, 기본 0x48 을 가지게 합니다.

또한, 아래 새다리님의 블로그를 보면, 이 연결을 가장 깔끔하게 처리하셨더군요. 따라쟁이는 바로 따라해 봅니다.


* 16비트, 4채널 ADC ADS1115 아두이노 Test

https://m.blog.naver.com/twophase/220801664646


예전에 파손된 멀티탭 전원선에서 동선 한가닥을 짧게 잘라 내어 아래와 같이 납땜 해 주섰습니다.

캡톤 테이프로 혹시 모를 쇼트를 방지했구요.



i2cdetect 로 addressing 이 잘 되었나 확인해 봅니다.


     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --


흠흠. 잘 되었네요.




4. Layout


* Arduino ADS1115 Module Getting Started Tutorial

http://henrysbench.capnfatz.com/henrys-bench/arduino-voltage-measurements/arduino-ads1115-module-getting-started-tutorial/


위의 tutorial 사이트에서 보면 ADS1115 를 활용하여, arduino 자체 3.3V 출력을 세밀하게 검증해 보는 소스가 있습니다.

따라쟁이는 당연 따라서 검증해 봅니다.


연결은 I2C 용 연결 2가닥과 VCC/GND 그리고, 입력용에 arduino 3.3V output 을 연결합니다.


 ADS1115 | Arduino Nano
------------------------
    VCC  |      5V
    GND  |      GND
    SCL  |      A5
    SDA  |      A4
    A0   |      3.3V
------------------------


그림으로 그려보면 다음과 같습니다.






5. Sketch


이미 관련한 library 가 나와 있기 때문에, library 를 설치합니다.

역시 God Adafruit. 없는게 없습니다.



ads1115 로 검색하면 나오지 않고, ads1x 로 검색해야 나옵니다.


이제 HENRY'S BENCH 에서 Henry 아저씨가 arduino 3.3V output 에 대해, ADS1115 를 검증해 놓은 소스를 사용해 봅니다.


#include "Wire.h"
#include "Adafruit_ADS1015.h"

Adafruit_ADS1115 ads(0x48);
float Voltage = 0.0;

void setup(void) {
	Serial.begin(9600);
	ads.begin();
}

void loop(void) {
	int16_t adc0;	// we read from the ADC, we have a sixteen bit integer as a result
	
	adc0 = ads.readADC_SingleEnded(0);
	Voltage = (adc0 * 0.1875)/1000;
	
	Serial.print("AIN0: ");
	Serial.print(adc0);
	Serial.print("\tVoltage: ");
	Serial.println(Voltage, 7);
		
	delay(1000);
}


이 소스에서 가장 중요한 것은 PGA (Programmable Gain Amplifier) 값 입니다.

이 ADS1115 의 default output 최대값이 6.144V 이므로, 이를 15 bit (16 bit 이지만, 부호를 표시하는 1 bit 를 빼면 15 bit 만 활용 가능) 인 32767 로 나누면, 출력 1 에 대해 0.1875mV 라는 계산이 나옵니다.


In the default mode, the setting is +/-6.144 volts.

Thus the value of 32767 would represent a value of 6.144 volts.

Dividing 6.144 volts by 32767 yields a scale factor of 0.1875 mV per bit.   This is a significant improvement over the Arduino ADC which resolution of approximately 5 mV per bit.  In fact, its about 26 times better!


위의 로직이 소스에 활용되었습니다.





6. 결과


지금까지 구성한 layout 과 위의 소스를 가지고 실행해 보면 다음과 같이 결과가 나옵니다.



3.3V 이지만, 미세하게 값이 변하고 있다는 것을 알 수 있습니다.

이게 USB 를 통해서 연결하지 않고 Power source 를 통해서 입력 받으면 좀더 정확하고 잘 변하지 않는 3.3V 를 얻을 수 있다고 해요.





7. 비교


Arduino nano 의 자체 3.3V 를 16 bit ADC 를 거치지 않은 채로, anlogRead (10 bit ADC) 를 하면 어떨까?


참고로 Arduino 의 AnalogReference 의 정의는 다음과 같습니다.

--------------------------------

Arduino AVR Boards (Uno, Mega, Leonardo, etc.)

- DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)

- INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328P and 2.56 volts on the ATmega32U4 and ATmega8 (not available on the Arduino Mega)

- INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)

- INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)

- EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.

--------------------------------


AnalogReference(DEFAULT) 를 사용하여, 5V 기준으로 입력값을 leveling 하게 했으며,

arduino nano 에는 PGA 가 없으므로, 단순히 5V 를 10 bit ADC 해상도를 감안하여, 1024 로 나누어, 한 level 당, 0.0049 V 로 계산하도록 했습니다.



최종 소스는 다음과 같습니다.


#include "Wire.h"
#include "Adafruit_ADS1015.h"
 
Adafruit_ADS1115 ads(0x48);
float Voltage = 0.0;
float Voltage2 = 0.0;	// analogRead(A2)
 
void setup(void) {
    analogReference(DEFAULT);
    Serial.begin(9600);
    ads.begin();
}
 
void loop(void) {
    int16_t adc0;   // we read from the ADC, we have a sixteen bit integer as a result
     
    adc0 = ads.readADC_SingleEnded(0);
    Voltage = (adc0 * 0.1875)/1000;
     
    Serial.print("AIN0: ");
    Serial.print(adc0);
    Serial.print("\tVoltage: ");
    Serial.print(Voltage, 7);

	// analogRead(A2) start
    int16_t adc2;
    adc2 = analogRead(A2);
    Voltage2 = (adc2 * 0.0049);
    
    Serial.print("\tAIN2: ");
    Serial.print(adc2);
    Serial.print("\tVoltage2: ");
    Serial.println(Voltage2, 7);
    // analogRead(A2) end
         
    delay(1000);
}


결과값은 이렇게 나왔네요. 많이 부정확 합니다.


그 원인으로는,

- 5V reference 전압이 USB 를 통해 공급받으며, USB 전원은 불안하게 공급받습니다.

- PGA 가 없이, 단순히 5V reference 전압을 10 bit 로 나눈 값을 기준값으로 정했습니다.

- 16 bit 하고는 비교도 안되는 10 bit 해상도 차이가 납니다.



ADS1115 16 bit ADC 를 이용한 센서값 입력은 보다 정확한 값을 보장해 주네요.

향후 자주 사용해야 겠습니다.


And

Hardware | Digital Compass - HMC5883L 사용기 - 3

|

이 글은 전편 2개가 있습니다.


* Hardware | Digitial Compass - HMC5883L 사용기 - 1

https://chocoball.tistory.com/entry/Hardware-Digital-Compass-HMC5883L-1


* Hardware | Digital Compass - HMC5883L 사용기 - 2

https://chocoball.tistory.com/entry/Hardware-Digital-Compass-HMC5883L-2





1. Why calibration?


이 HMC5883L 이나, 기타 digital compass 는 영점조정이 필요합니다.

Calibration 을 하기 전은 정확성에 있어서 사용할 수 있는 물건이 아니라고도 할 정도 입니다.


또한 지구상에서 내가 어디에 있느냐에 따라 자력의 방향과 세기가 달라지므로, 공장에서 만들어 졌던 들,

이 영점 조정은 꼭 필요하게 된다는 이야기가 됩니다.


현재 위치하고 있는 지리적 장소에 따라 변하는 자력을 계산해 주는 사이트도 있습니다.


* NCEI Geomagnetic Calculators

https://www.ngdc.noaa.gov/geomag/calculators/magcalc.shtml






2. Yury Matselenak


아래 그림처럼 직각 육면체 (박스) 를 이용하여 영점 조정한 사람이 있습니다.


* Advanced hard and soft iron magnetometer calibration for dummies

https://diydrones.com/profiles/blogs/advanced-hard-and-soft-iron-magnetometer-calibration-for-dummies



영점 조절을 위한 arduino code, Visual Studio 2010 C# 을 이용한 검증 어플 및 계산 어플까지 all set 를 혼자 만드신 분이죠.

위의 블로그에서 가져온 소스를 여기에 올려 놓습니다.


MagMaster.rar


위의 압축파일에 모든게 들어 있습니다만,

한가지 아쉬운 것은, 2014년에 만들어진 코드라, 최신 Arduino IDE 에서는 제대로 동작하지 않습니다.

동작만 한다면, 이 분이 안내하고 만들어진 프로그램을 사용하여 영점 조정이 가능하나, 그러지 못했네요.


시간이 생기면 소스를 분석하고, geomagnetic 에 대해 공부하여 소스 개선을 좀 해주고 싶으나, 현재 그럴 여유는 없습니다.


Yuri 아저씨의 방식을 잠깐 설명해 보면, HMC5883L 센서는, PCB에 프린팅 된 XYZ 축의 모양에 따라 다음과 같은 축을 가집니다.



이 센서를 가지고 다음과 같이, 북쪽을 0도로 정하고 3차원 공간에서 XYZ 값을 가져오면, 틀어진 중심점을 알 수 있으니,

측정시에 그 틀어진 만큼을 빼주거나 더해주면 된다는 이론 입니다.



모두 12가지를 측정하여, 그 값들을 가지고 틀어진 중심점을 구하는 방식 입니다.





3. Sequential Quadratic Programming


측정된 값을 가지고 SQP (Sequential Quadratic Programming) 을 통해 최적의 영점을 잡아주는 방식도 있습니다.

(보다 기술적인 것은 잘 모름)


본 방식은, 아래 블로그에서 소개되었습니다.


* 電子コンパスHMC5883Lのキャリブレーションに挑戦

http://tomoto335.hatenablog.com/entry/arduino/HMC5883L


결과적으로 SQP 를 실행해야 하는데, 이를 Octave (상용 MatLab 의 GNU 버전) 을 통해서 답을 구할 수 있다고 하네요.

Yuri 아저씨 방식이 안되니, 이 방식으로 진행해 봅니다.





4. Octave


SQP 계산을 위해, 일단 Octave 를 설치해 봅니다.


* GNU Octave

https://www.gnu.org/software/octave/



사용하는 OS 에 맞게 다운로드 하구요.



인스톨 실행파일을 실행 시킵니다.



나에게 맞게 설정하구요.

저는 바탕화면이 지저분하게 되는게 싫어서 "Install for all users" 만 선택했습니다.

BLAS library 는 뭔지 모르니 그냥 OpenBLAS 로 놔뒀습니다.



Windows 10을 새로 깔았더니만 JRE 가 없네요. Octave 는 JRE 가 필요하다 합니다. 깔아 줍니다.



자 이제 다음으로 넘어갑니다.





5. 연결


우선 arduino 와 HML5883L 을 다음과 같이 연결합니다.


 HMC5883L | Arduino Nano
-------------------------
    VCC   |      5V
    GND   |      GND
    SCL   |      A5
    SDA   |      A4
-------------------------


그리고 I2CScanner / I2Cdetect 를 이요하여 address 0x1E 를 통해서 잘 인식 되었는지 확인합니다.



     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --


Hardware 준비는 끝났으니, arduino source 준비로 넘어갑니다.





6. 값을 입력받자


HMC5883L 을 구동시켜 주는 몇 가지 Library 들을 확인해 봤는데, Adafruit 에서 만든 Unified library 가 가장 짱인 것 같습니다.



OS 를 싹 밀었더니만 예전 library 가 다 날라갔네요. 깔끔하게 다시 설치해 줍니다.



바로 실행하면, Octave 에서 계산을 방해하는 문자들이 들어가니 아래와 같이 단순한게 XYZ 값만 입력받게 합니다.


#include "Wire.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_HMC5883_U.h"

/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);


void setup(void) 
{
  Serial.begin(9600);
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print(event.magnetic.x); Serial.print(",");
  Serial.print(event.magnetic.y); Serial.print(",");
  Serial.println(event.magnetic.z);
}


그러면 다음과 같은 결과들을 얻을 수 있습니다.


...
-21.82,-9.36,-41.94
-16.45,-8.73,-47.24
-16.45,-8.73,-47.24
-16.45,-8.73,-47.24
-9.64,-6.91,-51.43
-9.64,-6.91,-51.43
-9.64,-6.91,-51.43
-3.55,-4.00,-53.27
-3.55,-4.00,-53.27
-3.55,-4.00,-53.27
2.55,-1.27,-54.18
...


Serial Monitor 의 값들을 카피하여 txt 파일로 하나 만들어 놓습니다. 이게 Octave 에서 사용될 소스 값입니다.


참고로 Yuri 아저씨가 만들어 주신 MagMaster 프로그램 중에, MagViewer 라는게 있습니다.

실행시키면, 입력받을 Serial Port 를 물어보는데, arduino 와 연결된 port 를 지정하면 값들을 읽어와서 3차원 공간에 뿌려 줍니다.



훗, 확실히 틀어져 있군요.



동영상으로도 올려 봅니다. Graph Fetish 인지라, 이런거 보면 사족을 못 씁니다.

마우스로 요리조리 움직일 수 있어서 3차원 공간에서 어떻게 값들이 찍히는지 실시간으로 알 수 있습니다.





7. 영접을 찾아 보자


이제 Octave 에서 SQP 를 시켜 볼 차례 입니다.

Serial Monitor 에서 받은 값들을 파일로 만든 다음, Octave 내에서 아래 command 를 실행 시킵니다.


global points
points = csvread("HMC5883L_org_uncal.txt");
scatter3(points(:,1), points(:,2), points(:,3));


느낌이 예전 GNU Plot 을 사용하는 것이랑 매우 비슷하네요. Command 도 비슷하고.



짜잔~! 3차원으로 찍힌 그림을 그려줍니다. 이는 아까 MegViewer 로 본 모습이기도 합니다.



값들이 모두 입력 되었으니, 틀어진 값을 찾기 위해 SQP 해 봅니다.


function retval = sphere_errors(p, x)
  retval = sqrt( (p(:,1).-x(1)).^2+(p(:,2).-x(2)).^2+(p(:,3).-x(3)).^2).-x(4)
endfunction

function retval = sphere_error(x)
  global points
  retval = sum(sphere_errors(points, x).^2)
endfunction

x0 = [mean(points(:,1)), mean(points(:,2)), mean(points(:,3)), 100]

result = sqp(x0, @sphere_error)


그러면 한참만에 아래 값들이 도출됩니다. 모든 값들에 대해 mash 형태로 계산을 하다 보니, 꽤 시간이 걸립니다.



계산에 의하면, 중심점은 (12.1, -5.9, -7.2) 반경 47.85 라고 나옵니다.

이 값들이 지금까지 찾았던 영점값 되겠습니다.





8. 영접값 적용하여 확인


이제 다시 arduino source 로 돌아와, 위에서 구한 값들을 입력받는 source 에 반영해 줍니다.



중심점이 벗어나 있으니, 그만큼 가감해서, 입력 받을 때, 자동으로 계산되게 해주면 됩니다.


MegViewer 를 통해 어떻게 변했나 확인해 볼까요?



오호이~. 적용이 되어서 둥그런 원 모양과 중심축에 붙어서 값들이 표현되었습니다.

이게 calibration 의 힘이란 말인가... (대박)





9. Further More


사실 저는 이 결과가 썩 마음에 들지 않습니다.


일단, 값을 입력받는 방식 차제를 아래 그림들 처럼 고정된 상태에서 선의 걸리적 거림 없이,

6면체의 각 면과 같이 회전하면서 값들을 받아야, 입력값을 신뢰할 수 있을 것 같았습니다.


Arduino GY-273 HMC5883L Magnetometer Compass Tutorial

http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-gy-273-hmc5883l-magnetometer-compass-tutorial/



위와 같이 하려면, 아래 장치 처럼, 무선모듈을 추가해야 하고, 배터리로 구동시켜야 합니다.


* Tutorial: How to calibrate a compass (and accelerometer) with Arduino

https://thecavepearlproject.org/2015/05/22/calibrating-any-compass-or-accelerometer-for-arduino/



저는 이렇게 연결하고 윙윙 휘둘렀습니다.

그래서 몇 개씩 값이 튀기도 하고...



혹시... 혹시 나중에 기회가 되면, 그땐 위의 전문가들 처럼 해보고 싶네요.


And

Hardware | PN523 - RFID / NFC breakout 보드

|

이 글은, 아래 포스트에서 예고 했듯이, RFID / NFC 를 arduino 를 이용하여 tag를 인식시켜 보는 글 입니다.


* Book | 훤히 보이는 RFID/USN - Get to know RFID/USN

https://chocoball.tistory.com/entry/Book-Get-to-know-RFID-USN





1. 대응 가능한 chip


RFID / NFC 를 읽을 수 있는 chip 중에 PN532 가 FeliCa 도 인식할 수 있으며, 대중적으로 구입 가능하다는 것을 알게 되었습니다. (범용)


* RFID Selection Guide - Adafruit Industries

https://cdn-shop.adafruit.com/datasheets/rfid+guide.pdf

rfid+guide.pdf



PN5xx 시리즈 중에서 시중에서 구입 가능한, 그리고 xx 부분의 숫자가 큰 것으로는 PN532 가 있더군요.

가장 우수한 chip 으로는 PN544 입니다만, 관련 breakout 은 5만원 이상이었습니다.


저렴하게 AliExpress 에서 골라서 구입합니다.


* 1Set GREATZT PN532 NFC RFID Wireless Module V3 User Kits Reader Writer Mode IC S50 Card PCB Attenna I2C IIC SPI HSU For Arduino

https://www.aliexpress.com/item/1Set-GREATZT-PN532-NFC-RFID-Wireless-Module-V3-User-Kits-Reader-Writer-Mode-IC-S50-Card/32859551116.html



- User manual : PN532_Manual_V3.pdf


[Features]

1. Gilt PCB and Small dimension and easy to embed into your project

2. Support I2C, SPI and HSU (High Speed UART), Change between those modes

3. Support RFID reading and writing

  1) SupportP2P communication with peers

  2) Support NFC with Android phone


4. Typical Operating Distance have been updated to 5cm~7cm reading distance

5. Work in NFC Mode or RFID reader/writer Mode

6. RFID reader/writer supports:

  1) 1k, 4k, Ultralight, and DesFire cards

  2) ISO/IEC 14443-4 cards such as CD97BX, CD light, Desfire, P5CN072 (SMX)

  3) Innovision Jewel cards such as IRT5001 card

  4) FeliCa cards such as RCS_860 and RCS_854


7. Plug and play, for compatible

8. Built in PCB Antenna, with 4cm~6cm communication distance

9. On-board level shifter, Standard 5V TTL for I2C and UART, 3.3V TTL SPI

10. Work as RFID reader/writer

11. Work as 1443-A card or a virtual card

12. Exchange data with other NFC devices such as smartphone



[Package Included]

1 x1PCS*PN532 NFC RFID Module

1x 2.54mm spacing 4pin Cable

1xMifare One S50 White Card

1xMifare One S50 Key Card

1x12P bended male pins


사양을 보면 FeliCa 도 읽을 수 있다고 되어 있습니다.

FeliCa 는 일본 지하철 / 국철에서 사용할 수 있는 Suica / PASMO 카드에 사용된 기술입니다.

마침 일본에서 사용했던 Suica / Pasmo 카드를 가지고 있으니, FeliCa 대응 가능한 이 breakout 을 이용할 수 있겠네요.


다만, fake 제품은 읽을 수 없다고 합니다. (나중에 안 사실)

AliExpress 에서 구매할 수 있는 저가품이다 보니, 아마 불가능할 것 같다는 느낌은 듭니다.





2. 도착


배송에 한달정도 소요되었습니다.



구성품은 다음과 같습니다.

Tag 종류가 둥그런 것과 카드형식, 두가지가 들어 있네요.



Breakout 보드의 확대 사진입니다.



뒷면입니다. I2C 용 pin head 와 SPI 용이 따로 구분되어 있습니다.



Arduino 와 연결하기 위해서 pin head 들을 납땜 했습니다.

납땜 팁이 오래 쓰면서 산화되어 버려 이제는 납볼이 잘 생성되지 않았지만, 어떻게든 이쁘게 된것 같네요.







3. Library 설치


이 보드에 관한 제작 / 판매하는 사이트를 따라가다 보면 Seeed Studio 라는 회사가 떠오릅니다.

관련한 source 들은 아래 GitHub 에서 공유되어 있습니다.


* elechouse/PN532

https://github.com/elechouse/PN532


위의 사이트에서 설명되어 있기론, 아래 두 파일을 Arduino libraries 폴더에 압축을 풀어서 copy 하라고 합니다.

결국 위의 GitHub 의 파일과, 추가로 Don 이라는 사람이 만든 NDEF 파일을 Arduino > libraries 에 설치하면 준비는 끝납니다.


PN532-PN532_HSU.zip

NDEF-master.zip


위에서 시키는 대로 하면, PN532 directory 가 많아지므로, 구분을 위해 prefix "elechouse" 를 붙여서 아래처럼 저장했어요.



다른 source 로는, 가장 유명한 adafruit 에서 나온 PN532 library 를 설치하면 됩니다.


* adafruit/Adafruit-PN532

https://github.com/adafruit/Adafruit-PN532


위에서 파일을 다운로드 받아 libraries 에 copy 해도 되고, 아래처럼 Library Manager 를 이용하여 install 해도 됩니다.



다만, adafruit 소스에는 HSU (High Speed UART) 연결방식이 지원되지 않습니다.

그러니 HSU 를 사용하고 싶으면, 처음에 소개된 elechouse source 가 필요합니다.





4. I2C 연결


이제 소스를 올리고 RFID 인식을 시켜 봅니다.

Arduino 와 연결 방식은 I2C / SPI / HSU 가 있으니, 먼저 가장 단순한 I2C 를 이용해 봅니다.


아래처럼 DIP switch 를 I2C 방식으로 변경합니다.



문제 없이 I2C 통신이 이루어 지는지 I2C detect 소스로 확인해 봅니다.

방법은 예전에 올렸던 아래 포스트에서 확인해 보세요.


* Hardware | Gyroscope GY-521 MPU-6050 을 사용해 보자

https://chocoball.tistory.com/entry/Hardware-Gyroscope-GY521-MPU6050



PN532 breakout 의 측정된 주소로 "0x24" 가 나왔네요.

연결은 다음과 같이 합니다.


   PN531  | Arduino Nano
-------------------------
    VCC   |     5V
    GND   |     GND
    SDA   |     A4
    SDL   |     A5
-------------------------


연결 layout 은 다음과 같습니다.

구매한 breakout 보드와 동일한 fritzing 파트를 찾을 수 없어서 adafruit 에서 나온 것을 사용하였습니다.



여타 I2C 연결이 그러하듯 동일합니다.



아래 sample source 를 arduino 에 로드합니다. iso14443a_uid 가 처음 시작하기에 가장 평범한 소스라고 하네요.


File > Examples > elechouse_PN532 > iso14443a_uid



Serial Monitor 에서 확인하면 다음과 같이 인식합니다!



위의 소스의 단점은 준비 상태로 되는 것과 카드를 태킹하면 인식에 시간이 좀 걸린다는 것 입니다.

실생활에 전혀 사용할 수 없는 수준이네요.


그럼 이번에는 Adafruit 의 동일한 소스를 사용해 봅니다.


File > Examples > Adafruit PN532 > iso14443a_uid


adafruit 소스는 먼저번 소스와는 다르게, IRQ 와 RESET (RSTO) 를 추가로 연결하는 부분이 존재합니다.


// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a SPI connection:
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


위의 소스의 일부분에서 보여주는 것 처럼 SPI 부분을 주석처리 하고, I2C 부분을 활성화 시킵니다.

두개의 pin 연결이 아래처럼 추가되었습니다.


   PN531  | Arduino Nano
-------------------------
    VCC   |     5V
    GND   |     GND
    SDA   |     A4
    SDL   |     A5
    IRQ   |     D2
   RSTO   |     D3
-------------------------


결과는 인식률과 인식 속도가 엄청 빨라졌습니다.

결국 IRQ / RESET 핀이 준비상태 및 인식 처리를 추가로 담당한다는 것을 예상할 수 있습니다.

Serial Monitor 결과는 다음과 같습니다.



참고로 위의 소스로 신용카드 (버스카드) 를 인식 시키면 "Mifare Classic" 으로 읽어보라고 메시지가 뜹니다.


File > Examples > Adafruid PN532 > readMifareClassic 을 로드 시켜 봅니다.



뭔가 정보를 더 많이 뿌려줌과 동시에, "Mifare Classic" 이라고 이야기 해 줍니다.


조금 벗어난 이야기 이지만,

RFID / NFC 분야도 존재하는 규격이 많아서 chip 제조사로서는 골치가 아플 듯 합니다.


이것도 결국 기술 로열티와 표준 제정 이권싸움의 결과겠죠.

Thunderbolt 도, 결국은 Thunderbolt 3 = USB Type-C 로 통합되듯, 언젠가 RFID / NFC 도 통합이 되었으면 좋겠습니다.





5. SPI 연결


이제 SPI 연결을 시도해 봅니다. 역시 많은 데이터 교환은 I2C 보다는 SPI 방식입니다.

먼저, Software SPI 연결법 입니다.


   PN531  | Arduino Nano
-------------------------
    VCC   |     5V
    GND   |     GND
    SCK   |     D2
    MISO  |     D5
    MOSI  |     D3
    SS    |     D4
-------------------------


소스는 adafruit 의 것을 이용해 봅니다. (elechouse 것도 상관 없슴)


File > Examples > Adafruit PN532 > readMifare


이미 소스에서 SCK / MOSI / SS / MISO 의 pin 번호를 정희해 놨으므로, 그에 맞게 arduino 와 연결해 줍니다.



TIMEOUT! 이 뜨긴 합니다만, 결과는 아래와 같이 잘 나옵니다.

아무래도 Software SPI 여서 그런 듯 합니다.



역시 SPI 는 Hardware SPI 죠. Hardware SPI 법으로 구동해 봅니다.


   PN531  | Arduino Nano
-------------------------
    VCC   |     5V
    GND   |     GND
    SCK   |     D13
    MISO  |     D12
    MOSI  |     D11
    SS    |     D4
-------------------------


SS pin 은 어느 digital IO pin 이나 상관 없습니다.

이미 PN532_SS 를 4 번 pin 으로 정의해 놨으니, 그 pin 을 그대로 사용합니다.



나머지 pin 들은 각각의 arduino 에 맞게 연결하면 됩니다.

참고로 arduino nano 는 위의 주석에 설명되어 있는 것처럼 SCK = 13, MOSI = 11, MISO = 12 로 맞추면 됩니다.

이는 아래 그림처럼 실제 nano 의 pin out 과 동일합니다.



결과는 다음과 같이 나옵니다. TIMEOUT! 도 없고 인식도 가장 빠른것 같아요.



동영상도 올려 봅니다.






6. FeliCa 인식


FeliCa 가 된다고 하니, electhouse 소스의 FeliCa_Card_Read 를 실행해 봅니다.


File > Examples > elechouse_PN532 > FeliCa_Card_Read


실망스럽게도 PASMO 는 인식되지 않았습니다.

당연하게도 Mifare (ISO14443A) 카드들에게는 전혀 반응하지 않았구요.


단, 희한하게도 일본에서 사용했던 Times (한국의 SOCAR 같은 서비스) 카드는 이 소스로 읽혔습니다.



FeliCa 도 여러 종류가 있는 듯 합니다.

아쉽게도 지하철에 사용되는 FeliCa 인, 일본의 PASMO 와 싱가포르의 EZ-Link 는 어떤 소스에도 읽히지 않았습니다.





7. High Speed UART 연결


특이하게 HSU 라는 연결 방법을 제공합니다. 이는 High Speed UART 의 약자.

이 HSU 는 Hardware Serial (Serial1) 을 바탕으로 소스가 만들어졌습니다.



다만, 위의 표에서 보이듯이, Hardware Serial 를 사용하는 지라, 일부 arduino 에서는 Serial Monitor 를 열어서 확인할 수 없게 됩니다.

Arduino Nano 도 Hardware Serial 은 USB 통신에 점유되어 있어서 "Serial1" 을 사용할 수 없었습니다.



하늘이 무너져도 솟아날 구멍은 있다던가요, 가지고있는 arduino micro 에서는 사용 가능했습니다.

그럼 아래 source 를 arduino micro 에 업로드 해봅니다.


File > Examples > electhouse_PN532 > iso14443a_uid


PN532_HSU 쪽을 활성화면서 "Serial1" 을 사용하게 합니다.



Arudino micro 와의 pin 연결은 다음과 같습니다.


   PN531  | Arduino Micro
--------------------------
    VCC   |     5V
    GND   |     GND
    SDA   |     RX
    SDL   |     TX
--------------------------


잊지 말아야 할 것은, DIP switch 를 HSU 으로 설정해 둬야 합니다.



Arduino micro 의 RX / TX 와 연결하여 HSU 인겁니다.



오오오오! 느낌적으로 SPI 보다 더 빠른 듯 합니다. 이게 가장 빠르네요.




결과는 이렇게 보이구요.



동영상도 올려 봅니다.



그럼 arduino nano 처럼 Hardware Serial 여유가 없는 arduino 는 안되는거냐!

찾아보니 방법을 GitHub 의 설명에서 친절하게 알려주고 있었습니다.


아래 소스처럼 "SoftwareSerial.h" 를 이용하여 구현이 가능합니다.

우선 바로 아래는 Hardware Serial 로 구현된 부분을...


#include "PN532_HSU.h"
#include "PN532.h"

PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);

void setup(void)
{
	nfc.begin();
	//...
}


아래처럼 SoftwareSerial.h 를 추가하고 관련된 pin 을 정의해 주면 됩니다.

뭐, 관련된 함수를 "PN532_SWHSU.h" 에서 구현해 줘서 가능한 것이지만 말입니다.


#include "SoftwareSerial.h"
#include "PN532_SWHSU.h"
#include "PN532.h"

SoftwareSerial SWSerial( 10, 11 ); // RX, TX

PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );

void setup(void)
{
	nfc.begin();
	//...
}


최종적으로 Hardware Serial 관련 부분을 주석처리 하고, SoftwareSerial 을 활성화 하는 코드를 추가하면 됩니다.



Pin 연결은 위에서 정의한 D10 과 D11 에 각각 연결하면 됩니다.

참고로, SDA 는 TX 이고, SDL 은 RX 이므로, SDA(TX) <--> D10 (RX), SDL(RX) <--> D11(TX) 가 됩니다.


   PN531  | Arduino Nano
-------------------------
    VCC   |     5V
    GND   |     GND
    SDA   |     D10
    SDL   |     D11
-------------------------


잘 구동합니다만, 뭔가 타이밍이 맞지 않은지 authentication fail 이 뜹니다.

소스코드에서 수정해야 할 부분이 있는듯 보입니다만, 확인이 어느정도 되었으니 패스.






8. 확인한 RFID / NFC 카드들


마지막으로, 본 포스트에서 확인용으로 사용된 카드들을 소개합니다.



위는 PN532 breakout board 를 구입하면 기본으로 딸려오는 tag 들 입니다. 하나는 원형, 하나는 카드 모양입니다.



위는 싱가포르 출장때 구입해서 사용했던 지하철 패스카드 입니다. 충전식이죠.

아쉽지만, 구입한 PN532 가 짝퉁이라서 못 읽는 것인지 모든 소스와 연결 방법에서 읽기를 실패했습니다.



마찬가지 FeliCa 인식에서 실패한 일본 PASMO 입니다. 일본에서 거주할때 신세를 졌었죠.



저의 회사 출입 카드 입니다. 5년전에 찍은 거라 얼굴이 지금보다 젊어 보이네요. ㅠㅠ



버스카드 겸용인 신용카드 입니다. Mifare Classic 입니다. 잘 읽힙니다.

전용 어플을 이용하면 RFID 정보도 덮어 씌기가 될 듯 한데, 이번에는 도전하지 않았습니다.



유일하게 읽힌 FeliCa 카드 입니다!

다른 소스에서는 전혀 읽히지 않았고, FeliCa Read 소스에서만 유일하게 읽힌 놈입니다.

일본에서 자가용을 운용할 여유가 안되어서, 잘 빌려서 타고 다녔습니다. (SOCAR 같은 서비스)





9. FIN


역시 아쉬운 점은 지하철용 FeliCa 를 읽을 수 없었다는 점 입니다.

뿌듯한건 모든 인터페이스 - HSU, Software HSU, I2C, I2C with RST, Hardware SPI, Software SPI - 모두를 확인해 봤다는 점 입니다.


기회가 되면, 아래 스샷처럼 NXP 에서 나온 어플을 가지고 완벽하게 debugging 을 해보고 싶습니다.

다만, PN544 breakout 보드가 5만원 이상이라는 것 때문에, 일단 여기서 멈춥니다.



And

Hardware | RTC DS3231 부품 사용기 - 2

|

이 포스트는 앞전에 DS3231 을 사용해 보면서, 못다한 이야기를 하기 위해 구성했습니다.


* Hardware | RTC DS3231 부품 사용기 - 1

http://chocoball.tistory.com/entry/Hardware-RTC-usning-DS3231-1



1. AT24C32 (EEPROM) I2C address


DS3231 에 붙어있는 EEPROM 은 ATMEL 사의 AT24C32 라는 칩 입니다.

32bit = 4kByte EEPROM 의 I2C 기본 주소값은 0x57 입니다.


 

 A0

A1

A2 

0x57

0

0

0

0x56

1

0

0

0x55

0

1

0

0x54

1

1

0

0x53

0

0

1

0x52

1

0

1

0x51

0

1

1

0x50

1

1

1

* 0 = open, 1 = short


참고로 0x57 은 16진수 이므로, 10진수로 표시하고자 할 때에는 87 이라고 입력해도 됩니다.


* Hex to Decimal converter

https://www.rapidtables.com/convert/number/hex-to-decimal.html






2. 유령 I2C address - 0x5f


제가 사용한 fake DS3231 은, 사용되지 않는 "0x5f" 라는 I2C 주소가 존재합니다.



모르고 지나가기엔 너무 궁금합니다.

여러 방법으로 찾아 봤는데, 가장 신뢰성 있는 설명은 다음 LINK 인 듯 합니다.


* I2C response to "Ghost Address" 0x5F

https://electronics.stackexchange.com/questions/397569/i2c-response-to-ghost-address-0x5f



즉, 정품에 포함되어 있는 32k EEPROM 은 ATMEL 문자로 시작하는데,

이 fake 제품에 다른 chip 이 있는 것이 아니라, ATMLH745 는 address 가 두개 있는 듯 합니다.


왜냐하면, 위의 첫번째 댓글에 A2 단자를 short 시켰을때 EEPROM 주소가 바뀌는데,

이 ghost address 도 연동해서 바뀐다는 테스트 결과가 나왔거든요.


A0, A1, A2 all open (default)
--------------------------------------------
|   chip  |    DIGIT   |   HEX  |  DECIMAL |
--------------------------------------------
| AT24C32 | 0b01010111 |  0x57  |    87    |
--------------------------------------------
|  ghost  | 0b01011111 |  0x5f  |    95    |
--------------------------------------------

A0, A1 open, A2 short
--------------------------------------------
|   chip  |    DIGIT   |   HEX  |  DECIMAL |
--------------------------------------------
| AT24C32 | 0b01010011 |  0x53  |    83    |
--------------------------------------------
|  ghost  | 0b01011011 |  0x5b  |    91    |
--------------------------------------------


그럼 AT24C32 주소를 0x5f 로 해보면 어떨까요?

아래처럼 소스를 조금 바꿔 봤습니다.


#include "Wire.h"
#define AT24C32_I2C_ADDRESS 0x5f //I2C address of AT24C32

byte seconds, minutes, hours, day, date, month, year;

void setup() {
	Serial.begin(9600);
	delay(1000);
	
	Wire.begin();
}

void loop() {
	getAT24C32Data();
	
	Serial.print("seconds : "); Serial.println(seconds, BIN);
	Serial.print("minutes : "); Serial.println(minutes, BIN);
	Serial.print("hours   : "); Serial.println(hours, BIN);
	Serial.print("day     : "); Serial.println(day, BIN);
	Serial.print("date    : "); Serial.println(date, BIN);
	Serial.print("month   : "); Serial.println(month, BIN);
	Serial.print("year    : "); Serial.println(year, BIN); 
	Serial.println("");
	
	delay(1000);
}

void getAT24C32Data() {
	// send request to receive data starting at register 0
	Wire.beginTransmission(AT24C32_I2C_ADDRESS);
	Wire.write(0x00); // start at register 0
	Wire.endTransmission();
	Wire.requestFrom(AT24C32_I2C_ADDRESS, 7); // request seven bytes
	
	if(Wire.available()) {
		seconds = Wire.read(); // get seconds
		minutes = Wire.read(); // get minutes
		hours   = Wire.read(); // get hours
		day     = Wire.read();
		date    = Wire.read();
		month   = Wire.read(); //temp month
		year    = Wire.read();
	} else {
		//oh noes, no data!
	}
}


원래 AT24C32 의 주소인 0x57 를 넣으면 아래처럼 나옵니다만,

0x5f 로 하면 data 는 오는데, 전부 255 값을 갖습니다.


0x57 로 할 때



0x5f 로 할 때



엄한 어드레스인 0x83 로 할 때



값이 동일하지 않아서, 완벽히 동일한 값에 접근하는것 같지는 않습니다.

그래도 뭔가 있기는 한것 같네요.


소설을 써 보자면, 중국에서 fake 칩을 만들면서 다른 기능도 넣지 않았을까...

혹시 어떤 다른 정보와 관여하는 기능이 있는것이 아닌지?





3. Battery 의 종류


밧데리는 지금 CR2032 을 끼워서 사용하고 있습니다.

시간 유지만으로 사용되면 1uA 정도 사용되므로, 200mAh 라고 한다면 약 20년은 사용 가능하다고 하네요.


* RTCモジュール DS3231+AT24C32 ZS-042

https://blogs.yahoo.co.jp/dascomp21/68145713.html



다만, 사양적으로는 충방전이 되는 LIR2032 을 사용하라고 하네요.



실제로 약 1년정도 사용하다가 베터리가 터진 케이스가 인터넷에 보고되었습니다.


회로 구성도를 봐도, 충전이 되게끔 만들어진 회로라서 rechargeable battery 를 사용하지 않으면,

지속적으로 건전지에 부담이 가게끔 되어 있다고 합니다.



굳이 CR2025 을 장기간 사용하려 한다면, 위의 그림처럼 방충전 회로와 연결되는 패턴을 끊어주면 된다 합니다.


잠깐 쓰고 빼 놓거나, 잔량이 거의 없는거라면 괜찮겠죠?

저는 체중계에서 다 쓴 건전지를 사용하고 있어서 그냥 낑궈 놓으려고 합니다.


이 정보는 아래 사이트를 참고하였습니다.


* RTC DS3231/DS1302を調べて見ました

https://blogs.yahoo.co.jp/hobbyele/64900109.html





4. BCD


EEPROM 에 집어 넣고 빼는 값의 포맷은 decimal 이지만, 실제로 저장되는 값은 binary 나열값 입니다.


더 나아가, 이는 아래 그림처럼 EEPROM 에 저장되는 방식이 얼핏 보기에는 실제의 값을 단순히 binary 로 바꾼 값처럼 보이지만,

이 binary 값은 기능적으로 입력되어 있을 뿐, 실존하는 값과는 다른 값 입니다.


결국, 입출력은 decimal, 저장된 것은 binary, 더욱이 이 decimal / binary 가 실제의 값을 표현하지는 않다는 것이죠.


그래서 값 치환이 3번 일어나게 됩니다.

- decimal 실제 값을 EEPROM 에 저장될 binary 를 상정하여 변경 (변경 1)

- 이를 decimal 로 치환하여 EERPOM 에 전송 (변경 2)

- EEPROM 에 보내면 알아서 다시 binary 의 나열로 저장 (변경 3)


값의 format 변경은, decimal --> binary --> decimal --> binary 로 되겠죠.


왜이리 복잡하게 하는가를 생각해 보면,

EEPROM 의 address 는 직접 접근 가능하지만, 각 address 의 bit 값은 따로따로 접근을 못하며,

거기에 각 bit 가 기능적으로 정의되어 있어서, "저장된 값 = 실존하는 값" 공식이 성립하지 않기 때문 입니다.



그럼 실제 source code 에서 10의 자리와 1의 자리의 연산식이 이떻게 이루어 지는지를 확인해 보죠.

초 단위의 식은 아래와 같습니다.


seconds = (((seconds & B11110000)>>4)*10 + (seconds & B00001111)); // convert BCD to decimal


상위 bit 부분 - 10의 자리 수 - "((seconds & B11110000)>>4)*10" 부분만을 띄어서 보면 다음과 같은 항목이 됩니다.

왼쪽부터,

- EEPROM 에서 읽어온 값 (decimal) 인 seconds

- seconds 값을 AND 연산 하면서 자동으로 binary 로 변환

- 4 bit 를 왼쪽으로 shift (날림) 를 통해 10의 자리만을 건짐

- 10 을 곱하면서 decimal 로 변경


위의 과정의 경과를 살펴보면 아래와 같겠죠.


| seconds | (BIN)seconds | seconds & B11110000 | (BIN)(seconds & B11110000) | (seconds & B11110000)>>4 | *10 |


0 | 0 | 0 | 0 | 0 | 0
1 | 1 | 0 | 0 | 0 | 0
2 | 10 | 0 | 0 | 0 | 0
3 | 11 | 0 | 0 | 0 | 0
4 | 100 | 0 | 0 | 0 | 0
5 | 101 | 0 | 0 | 0 | 0
6 | 110 | 0 | 0 | 0 | 0
7 | 111 | 0 | 0 | 0 | 0
8 | 1000 | 0 | 0 | 0 | 0
9 | 1001 | 0 | 0 | 0 | 0
16 | 10000 | 16 | 10000 | 1 | 10
17 | 10001 | 16 | 10000 | 1 | 10
18 | 10010 | 16 | 10000 | 1 | 10
19 | 10011 | 16 | 10000 | 1 | 10
21 | 10101 | 16 | 10000 | 1 | 10
22 | 10110 | 16 | 10000 | 1 | 10
23 | 10111 | 16 | 10000 | 1 | 10
24 | 11000 | 16 | 10000 | 1 | 10
25 | 11001 | 16 | 10000 | 1 | 10
32 | 100000 | 32 | 100000 | 2 | 20
33 | 100001 | 32 | 100000 | 2 | 20
34 | 100010 | 32 | 100000 | 2 | 20
35 | 100011 | 32 | 100000 | 2 | 20
36 | 100100 | 32 | 100000 | 2 | 20
37 | 100101 | 32 | 100000 | 2 | 20
38 | 100110 | 32 | 100000 | 2 | 20
39 | 100111 | 32 | 100000 | 2 | 20
40 | 101000 | 32 | 100000 | 2 | 20
41 | 101001 | 32 | 100000 | 2 | 20
48 | 110000 | 48 | 110000 | 3 | 30
49 | 110001 | 48 | 110000 | 3 | 30
50 | 110010 | 48 | 110000 | 3 | 30
51 | 110011 | 48 | 110000 | 3 | 30
52 | 110100 | 48 | 110000 | 3 | 30
53 | 110101 | 48 | 110000 | 3 | 30
54 | 110110 | 48 | 110000 | 3 | 30
55 | 110111 | 48 | 110000 | 3 | 30
56 | 111000 | 48 | 110000 | 3 | 30
57 | 111001 | 48 | 110000 | 3 | 30
64 | 1000000 | 64 | 1000000 | 4 | 40
65 | 1000001 | 64 | 1000000 | 4 | 40
66 | 1000010 | 64 | 1000000 | 4 | 40
67 | 1000011 | 64 | 1000000 | 4 | 40
68 | 1000100 | 64 | 1000000 | 4 | 40
69 | 1000101 | 64 | 1000000 | 4 | 40
71 | 1000111 | 64 | 1000000 | 4 | 40
72 | 1001000 | 64 | 1000000 | 4 | 40
73 | 1001001 | 64 | 1000000 | 4 | 40
80 | 1010000 | 80 | 1010000 | 5 | 50
81 | 1010001 | 80 | 1010000 | 5 | 50
82 | 1010010 | 80 | 1010000 | 5 | 50
83 | 1010011 | 80 | 1010000 | 5 | 50
84 | 1010100 | 80 | 1010000 | 5 | 50
85 | 1010101 | 80 | 1010000 | 5 | 50
86 | 1010110 | 80 | 1010000 | 5 | 50
87 | 1010111 | 80 | 1010000 | 5 | 50
88 | 1011000 | 80 | 1010000 | 5 | 50
89 | 1011001 | 80 | 1010000 | 5 | 50


결과적으로 10 --> 20 --> 30 --> 40 --> 50 순으로 값을 변경한다는 것을 알 수 있습니다.

보고 있으면 오묘하죠? 숫자놀이의 향연이라고 할 수 있겠습니다.






5. 알람 설정


아래는 EEPROM 에 저장된 기능별 주소록 입니다.


- datasheet : DS3231.pdf



저 위의 표를 염두에 두면서 DS3231.h 파일을 열어보면 알람의 설정방법에 대해 기술되어 있습니다.


/* Retrieves everything you could want to know about alarm
 * one. 
 * A1Dy true makes the alarm go on A1Day = Day of Week,
 * A1Dy false makes the alarm go on A1Day = Date of month.
 *
 * byte AlarmBits sets the behavior of the alarms:
 *	Dy	A1M4	A1M3	A1M2	A1M1	Rate
 *	X	1		1		1		1		Once per second
 *	X	1		1		1		0		Alarm when seconds match
 *	X	1		1		0		0		Alarm when min, sec match
 *	X	1		0		0		0		Alarm when hour, min, sec match
 *	0	0		0		0		0		Alarm when date, h, m, s match
 *	1	0		0		0		0		Alarm when DoW, h, m, s match
 *
 *	Dy	A2M4	A2M3	A2M2	Rate
 *	X	1		1		1		Once per minute (at seconds = 00)
 *	X	1		1		0		Alarm when minutes match
 *	X	1		0		0		Alarm when hours and minutes match
 *	0	0		0		0		Alarm when date, hour, min match
 *	1	0		0		0		Alarm when DoW, hour, min match
 */


자세히 보면, Alarm 1 이 초단위까지 자세하게 설정할 수 있으며,

Alarm 2 는 최소단위가 분단위임을 알 수 있습니다.

이 차이는 Alarm 1에 5개의 address 가 할당되어 있고, Alarm 2 에 4개의 address 가 할당되어 있는게 그 차이 입니다.


"DS3231.h" 에는 setA1Time, setA2TimeturnOnAlarm() 펑션이 있습니다.


File > Examples > DS3231 > DS3231_set 의 일부분이 다음과 같습니다.


		// Test of alarm functions
		// set A1 to one minute past the time we just set the clock
		// on current day of week.
		Clock.setA1Time(DoW, Hour, Minute+1, Second, 0x0, true, false, false);
		// set A2 to two minutes past, on current day of month.
		Clock.setA2Time(Date, Hour, Minute+2, 0x0, false, false, false);
		// Turn on both alarms, with external interrupt
		Clock.turnOnAlarm(1);
		Clock.turnOnAlarm(2);


이걸 이용하여 알람을 설정할 수 있습니다.

위의 source 는 특정 시간 설정 후, 1분과 2분 경과 후에 알람이 뜨도록 되어 있네요.


알람 확인은 기본으로 제공되는 다음 소스에서 확인할 수 있습니다.


File > Examples > DS3231 > DS3231_test


print 부분의 소스가 좀 부실해서 아주 쬐끔 수정 후, 표시된 내용이 아래 입니다.


2018 11 19 2 15 56 58 24h T=24.25 O+
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0

2018 11 19 2 15 56 59 24h T=24.25 O+
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0

2018 11 19 2 15 57 0 24h T=24.25 O+ A1!
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0

2018 11 19 2 15 57 2 24h T=24.25 O+
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0

......

2018 11 19 2 15 57 59 24h T=24.25 O+
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0

2018 11 19 2 15 58 0 24h T=24.25 O+ A2!
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0

2018 11 19 2 15 58 1 24h T=24.25 O+
Alarm 1: 2 DoW 15 57 0 enabled
Alarm_1 bits: 0
Alarm 2: 19 Date 15 58 enabled
Alarm_2 bits: 0


시간 설정 1분 후와 2분 후에 A1 과 A2 알람이 표시된 것을 확인할 수 있습니다.


알람 설정은, 시간 설정/표시 보다 훨씬 복잡합니다.

BCD 및 EEPROM 값에 대한 완벽한 이해가 있어야 하더군요.


소스를 새로 짤 수 있으나 너무 힘을 들이는 듯 해서,

지금까지 본것 중에 최고의 source 를 만들어 놓은 분의 website를 대신해서 기록해 봅니다.


* Arduino real time clock with alarm and temperature monitor using DS3231

https://simple-circuit.com/arduino-ds3231-real-time-clock-alarm-temperature/


이걸 만든 양반은 실제 상용으로 사용해도 될만큼 시간설정 및 알람 설정이 가능하도록 만들었습니다.

EEPROM 의 각 값들이 어떻게 사용되는지에 대한 완벽한 code 가 포함되어 있습니다.


또한 편한 library 를 사용하지 않고, 오로지 Wire.h 라이브러리만을 이용하여 직접 모든 것을 컨트롤 했습니다.


/* Arduino real time clock and calendar with 2 alarm functions and temperature monitor using DS3231
   Read DS3231 RTC datasheet to understand the code
   Time & date parameters can be set using two push buttons connected to pins 9 (B1) & 10 (B2).
   Alarm1 and alarm2 can be set using two push buttons connected to 11 (B3) & 10 (B2).
   Pin 12 becomes high when alarm occurred and button B2 returns it to low and
   turns the occurred alarm OFF.
   DS3231 interrupt pin is connected to Arduino external interrupt pin 2.
*/

// include LCD library code
#include "LiquidCrystal.h"
// include Wire library code (needed for I2C protocol devices)
#include "Wire.h"

// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(3, 4, 5, 6, 7, 8);

const int button1   =  9;                   // button1 pin number
const int button2   = 10;                   // button1 pin number
const int button3   = 11;                   // button1 pin number
const int alarm_pin = 12;                   // Alarms pin number

void setup() {
  pinMode(9,  INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  digitalWrite(alarm_pin, LOW);
  // set up the LCD's number of columns and rows
  lcd.begin(20, 4);
  Wire.begin();                                 // Join i2c bus
  attachInterrupt(digitalPinToInterrupt(2), Alarm, FALLING);
}

// Variables declaration
bool alarm1_status, alarm2_status;
char Time[]     = "  :  :  ",
     calendar[] = "      /  /20  ",
     alarm1[]   = "A1:   :  :00", alarm2[]   = "A2:   :  :00",
     temperature[] = "T:   .   C";
byte  i, second, minute, hour, day, date, month, year,
      alarm1_minute, alarm1_hour, alarm2_minute, alarm2_hour,
      status_reg;

void Alarm(){
  digitalWrite(alarm_pin, HIGH);
}
void DS3231_read(){                             // Function to read time & calendar data
  Wire.beginTransmission(0x68);                 // Start I2C protocol with DS3231 address
  Wire.write(0);                                // Send register address
  Wire.endTransmission(false);                  // I2C restart
  Wire.requestFrom(0x68, 7);                    // Request 7 bytes from DS3231 and release I2C bus at end of reading
  second = Wire.read();                         // Read seconds from register 0
  minute = Wire.read();                         // Read minuts from register 1
  hour   = Wire.read();                         // Read hour from register 2
  day    = Wire.read();                         // Read day from register 3
  date   = Wire.read();                         // Read date from register 4
  month  = Wire.read();                         // Read month from register 5
  year   = Wire.read();                         // Read year from register 6
}
void alarms_read_display(){                     // Function to read and display alarm1, alarm2 and temperature data
  byte control_reg, temperature_lsb;
  char temperature_msb;
  Wire.beginTransmission(0x68);                 // Start I2C protocol with DS3231 address
  Wire.write(0x08);                             // Send register address
  Wire.endTransmission(false);                  // I2C restart
  Wire.requestFrom(0x68, 11);                   // Request 11 bytes from DS3231 and release I2C bus at end of reading
  alarm1_minute = Wire.read();                  // Read alarm1 minutes
  alarm1_hour   = Wire.read();                  // Read alarm1 hours
  Wire.read();                                  // Skip alarm1 day/date register
  alarm2_minute = Wire.read();                  // Read alarm2 minutes
  alarm2_hour   = Wire.read();                  // Read alarm2 hours
  Wire.read();                                  // Skip alarm2 day/date register
  control_reg = Wire.read();                    // Read the DS3231 control register
  status_reg  = Wire.read();                    // Read the DS3231 status register
  Wire.read();                                  // Skip aging offset register
  temperature_msb = Wire.read();                // Read temperature MSB
  temperature_lsb = Wire.read();                // Read temperature LSB
    // Convert BCD to decimal
  alarm1_minute = (alarm1_minute >> 4) * 10 + (alarm1_minute & 0x0F);
  alarm1_hour   = (alarm1_hour   >> 4) * 10 + (alarm1_hour & 0x0F);
  alarm2_minute = (alarm2_minute >> 4) * 10 + (alarm2_minute & 0x0F);
  alarm2_hour   = (alarm2_hour   >> 4) * 10 + (alarm2_hour & 0x0F);
    // End conversion
  alarm1[8]     = alarm1_minute % 10  + 48;
  alarm1[7]     = alarm1_minute / 10  + 48;
  alarm1[5]     = alarm1_hour   % 10  + 48;
  alarm1[4]     = alarm1_hour   / 10  + 48;
  alarm2[8]     = alarm2_minute % 10  + 48;
  alarm2[7]     = alarm2_minute / 10  + 48;
  alarm2[5]     = alarm2_hour   % 10  + 48;
  alarm2[4]     = alarm2_hour   / 10  + 48;
  alarm1_status = bitRead(control_reg, 0);      // Read alarm1 interrupt enable bit (A1IE) from DS3231 control register
  alarm2_status = bitRead(control_reg, 1);      // Read alarm2 interrupt enable bit (A2IE) from DS3231 control register
  if(temperature_msb < 0){
    temperature_msb = abs(temperature_msb);
    temperature[2] = '-';
  }
  else
    temperature[2] = ' ';
  temperature_lsb >>= 6;
  temperature[4] = temperature_msb % 10  + 48;
  temperature[3] = temperature_msb / 10  + 48;
  if(temperature_lsb == 0 || temperature_lsb == 2){
    temperature[7] = '0';
    if(temperature_lsb == 0) temperature[6] = '0';
    else                     temperature[6] = '5';
  }
  if(temperature_lsb == 1 || temperature_lsb == 3){
    temperature[7] = '5';
    if(temperature_lsb == 1) temperature[6] = '2';
    else                     temperature[6] = '7';
  }
  temperature[8]  = 223;                        // Put the degree symbol
  lcd.setCursor(10, 0);
  lcd.print(temperature);                       // Display temperature
  lcd.setCursor(0, 2);
  lcd.print(alarm1);                            // Display alarm1
  lcd.setCursor(17, 2);
  if(alarm1_status)  lcd.print("ON ");          // If A1IE = 1 print 'ON'
  else               lcd.print("OFF");          // If A1IE = 0 print 'OFF'
  lcd.setCursor(0, 3);
  lcd.print(alarm2);                            // Display alarm2
  lcd.setCursor(17, 3);
  if(alarm2_status)  lcd.print("ON ");          // If A2IE = 1 print 'ON'
  else               lcd.print("OFF");          // If A2IE = 0 print 'OFF'
}
void calendar_display(){                        // Function to display calendar
  switch(day){
    case 1:  strcpy(calendar, "Sun   /  /20  "); break;
    case 2:  strcpy(calendar, "Mon   /  /20  "); break;
    case 3:  strcpy(calendar, "Tue   /  /20  "); break;
    case 4:  strcpy(calendar, "Wed   /  /20  "); break;
    case 5:  strcpy(calendar, "Thu   /  /20  "); break;
    case 6:  strcpy(calendar, "Fri   /  /20  "); break;
    case 7:  strcpy(calendar, "Sat   /  /20  "); break;
    default: strcpy(calendar, "Sat   /  /20  ");
  }
  calendar[13] = year  % 10 + 48;
  calendar[12] = year  / 10 + 48;
  calendar[8]  = month % 10 + 48;
  calendar[7]  = month / 10 + 48;
  calendar[5]  = date  % 10 + 48;
  calendar[4]  = date  / 10 + 48;
  lcd.setCursor(0, 1);
  lcd.print(calendar);                          // Display calendar
}
void DS3231_display(){
  // Convert BCD to decimal
  second = (second >> 4) * 10 + (second & 0x0F);
  minute = (minute >> 4) * 10 + (minute & 0x0F);
  hour = (hour >> 4) * 10 + (hour & 0x0F);
  date = (date >> 4) * 10 + (date & 0x0F);
  month = (month >> 4) * 10 + (month & 0x0F);
  year = (year >> 4) * 10 + (year & 0x0F);
  // End conversion
  Time[7]     = second % 10  + 48;
  Time[6]     = second / 10  + 48;
  Time[4]     = minute % 10  + 48;
  Time[3]     = minute / 10  + 48;
  Time[1]     = hour   % 10  + 48;
  Time[0]     = hour   / 10  + 48;
  calendar_display();                           // Call calendar display function
  lcd.setCursor(0, 0);
  lcd.print(Time);                              // Display time
}
void Blink(){
  byte j = 0;
  while(j < 10 && (digitalRead(button1) || i >= 5) && digitalRead(button2) && (digitalRead(button3) || i < 5)){
    j++;
    delay(25);
  }
}
byte edit(byte x, byte y, byte parameter){
  char text[3];
  while(!digitalRead(button1) || !digitalRead(button3));    // Wait until button B1 is released
  while(true){
    while(!digitalRead(button2)){                           // If button B2 is pressed
      parameter++;
      if(((i == 0) || (i == 5)) && parameter > 23)          // If hours > 23 ==> hours = 0
        parameter = 0;
      if(((i == 1) || (i == 6)) && parameter > 59)          // If minutes > 59 ==> minutes = 0
        parameter = 0;
      if(i == 2 && parameter > 31)                          // If date > 31 ==> date = 1
        parameter = 1;
      if(i == 3 && parameter > 12)                          // If month > 12 ==> month = 1
        parameter = 1;
      if(i == 4 && parameter > 99)                          // If year > 99 ==> year = 0
        parameter = 0;
      if(i == 7 && parameter > 1)                           // For alarms ON or OFF (1: alarm ON, 0: alarm OFF)
        parameter = 0;
      lcd.setCursor(x, y);
      if(i == 7){                                           // For alarms ON & OFF
        if(parameter == 1)  lcd.print("ON ");
        else                lcd.print("OFF");
      }
      else{
        sprintf(text,"%02u", parameter);
        lcd.print(text);
      }
      if(i >= 5){
        DS3231_read();                          // Read data from DS3231
        DS3231_display();                       // Display DS3231 time and calendar
      }
      delay(200);                               // Wait 200ms
    }
    lcd.setCursor(x, y);
    lcd.print("  ");                            // Print two spaces
    if(i == 7) lcd.print(" ");                  // Print space (for alarms ON & OFF)
    Blink();                                    // Call Blink function
    lcd.setCursor(x, y);
    if(i == 7){                                 // For alarms ON & OFF
      if(parameter == 1)  lcd.print("ON ");
      else                lcd.print("OFF");
    }
    else{
      sprintf(text,"%02u", parameter);
      lcd.print(text);
    }
    Blink();
    if(i >= 5){
      DS3231_read();
      DS3231_display();}
    if((!digitalRead(button1) && i < 5) || (!digitalRead(button3) && i >= 5)){
      i++;                                      // Increment 'i' for the next parameter
      return parameter;                         // Return parameter value and exit
    }
  }
}

void loop() {
  if(!digitalRead(button1)){                    // If B1 button is pressed
      i = 0;
      hour   = edit(0, 0, hour);
      minute = edit(3, 0, minute);
      while(!digitalRead(button1));             // Wait until button B1 released
      while(true){
        while(!digitalRead(button2)){           // If button B2 button is pressed
          day++;                                // Increment day
          if(day > 7) day = 1;
          calendar_display();                   // Call display_calendar function
          lcd.setCursor(0, 1);
          lcd.print(calendar);                  // Display calendar
          delay(200);
        }
        lcd.setCursor(0, 1);
        lcd.print("   ");                       // Print 3 spaces
        Blink();
        lcd.setCursor(0, 1);
        lcd.print(calendar);                    // Print calendar
        Blink();                                // Call Blink function
        if(!digitalRead(button1))               // If button B1 is pressed
          break;
      }
      date = edit(4, 1, date);                  // Edit date
      month = edit(7, 1, month);                // Edit month
      year = edit(12, 1, year);                 // Edit year
      // Convert decimal to BCD
      minute = ((minute / 10) << 4) + (minute % 10);
      hour = ((hour / 10) << 4) + (hour % 10);
      date = ((date / 10) << 4) + (date % 10);
      month = ((month / 10) << 4) + (month % 10);
      year = ((year / 10) << 4) + (year % 10);
      // End conversion
      // Write time & calendar data to DS3231 RTC
      Wire.beginTransmission(0x68);             // Start I2C protocol with DS3231 address
      Wire.write(0);                            // Send register address
      Wire.write(0);                            // Reset sesonds and start oscillator
      Wire.write(minute);                       // Write minute
      Wire.write(hour);                         // Write hour
      Wire.write(day);                          // Write day
      Wire.write(date);                         // Write date
      Wire.write(month);                        // Write month
      Wire.write(year);                         // Write year
      Wire.endTransmission();                   // Stop transmission and release the I2C bus
      delay(200);
    }
    if(!digitalRead(button3)){                  // If B3 button is pressed
      while(!digitalRead(button3));             // Wait until button B3 released
      i = 5;
      alarm1_hour   = edit(4,  2, alarm1_hour);
      alarm1_minute = edit(7,  2, alarm1_minute);
      alarm1_status = edit(17, 2, alarm1_status);
      i = 5;
      alarm2_hour   = edit(4,  3, alarm2_hour);
      alarm2_minute = edit(7,  3, alarm2_minute);
      alarm2_status = edit(17, 3, alarm2_status);
      alarm1_minute = ((alarm1_minute / 10) << 4) + (alarm1_minute % 10);
      alarm1_hour   = ((alarm1_hour   / 10) << 4) + (alarm1_hour % 10);
      alarm2_minute = ((alarm2_minute / 10) << 4) + (alarm2_minute % 10);
      alarm2_hour   = ((alarm2_hour   / 10) << 4) + (alarm2_hour % 10);
      // Write alarms data to DS3231
      Wire.beginTransmission(0x68);               // Start I2C protocol with DS3231 address
      Wire.write(7);                              // Send register address (alarm1 seconds)
      Wire.write(0);                              // Write 0 to alarm1 seconds
      Wire.write(alarm1_minute);                  // Write alarm1 minutes value to DS3231
      Wire.write(alarm1_hour);                    // Write alarm1 hours value to DS3231
      Wire.write(0x80);                           // Alarm1 when hours, minutes, and seconds match
      Wire.write(alarm2_minute);                  // Write alarm2 minutes value to DS3231
      Wire.write(alarm2_hour);                    // Write alarm2 hours value to DS3231
      Wire.write(0x80);                           // Alarm2 when hours and minutes match
      Wire.write(4 | alarm1_status | (alarm2_status << 1));      // Write data to DS3231 control register (enable interrupt when alarm)
      Wire.write(0);                              // Clear alarm flag bits
      Wire.endTransmission();                     // Stop transmission and release the I2C bus
      delay(200);                                 // Wait 200ms
    }
    if(!digitalRead(button2) && digitalRead(alarm_pin)){         // When button B2 pressed with alarm (Reset and turn OFF the alarm)
      digitalWrite(alarm_pin, LOW);               // Turn OFF the alarm indicator
      Wire.beginTransmission(0x68);               // Start I2C protocol with DS3231 address
      Wire.write(0x0E);                           // Send register address (control register)
      // Write data to control register (Turn OFF the occurred alarm and keep the other as it is)
      Wire.write(4 | (!bitRead(status_reg, 0) & alarm1_status) | ((!bitRead(status_reg, 1) & alarm2_status) << 1));
      Wire.write(0);                              // Clear alarm flag bits
      Wire.endTransmission();                     // Stop transmission and release the I2C bus
    }
    DS3231_read();                                // Read time and calendar parameters from DS3231 RTC
    alarms_read_display();                        // Read and display alarms parameters
    DS3231_display();                             // Display time & calendar
    delay(50);                                    // Wait 50ms
}
// End of code


이렇게 긴 source 는 붙여넣기 하면 이쁘지 않지만, 너무 잘 짠 코드라 여기에 남깁니다.





6. Square Wave - 정현파 만들기


RTC에 왠 정현파 재조기 인가 하는데, 시간은 정확한 oscillator 를 사용하므로,

square wave 를 정확하게 만들 수 있는 기능이 있습니다.


만들 수 있는 주파수는 다음과 같습니다.

- 1.024kHz

- 4.096kHz

- 8.192kHz

- 32.768kHz


source 는 Example 에 올라와 있는 내용을 그대로 사용해 봤습니다.


File > Examples > DS3231 > DS3231_oscillator_test


		// Oscillator functions

		void enableOscillator(bool TF, bool battery, byte frequency); 
			// turns oscillator on or off. True is on, false is off.
			// if battery is true, turns on even for battery-only operation,
			// otherwise turns off if Vcc is off.
			// frequency must be 0, 1, 2, or 3.
			// 0 = 1 Hz
			// 1 = 1.024 kHz
			// 2 = 4.096 kHz
			// 3 = 8.192 kHz (Default if frequency byte is out of range);
		void enable32kHz(bool TF); 
			// Turns the 32kHz output pin on (true); or off (false).
		bool oscillatorCheck();;
			// Checks the status of the OSF (Oscillator Stop Flag);.
			// If this returns false, then the clock is probably not
			// giving you the correct time.
			// The OSF is cleared by function setSecond();.


마침 DSO150 이라는 DIY oscilloscope 를 만들어 놓은 것이 있네요.


* Hardware | DSO150 Oscilloscope

http://chocoball.tistory.com/entry/HardwareDSO150Oscilloscope


SQW 에 DSO150 을 연결해서 확인해 봅니다.

신기하게도 정현파가 잘 뜨네요.



단, 32kHz 는 높은 주파수라서 그런지 32K 단자에서 따로 검출할 수 있습니다.






7. 시간 표시를 2 digit 로 바꾸기


시간, 분, 초의 표시가 10자리 밑이면 한자리 값으로만 나옵니다.

6초면 "06"이 아니라 "6" 으로 표시되면서 자리 위치가 자꾸 바뀌는게 마음에 들지 않습니다.



값을 EEPROM 에서 리턴 받으면 10 이하일 경우 "0" 을 삽입해 주는 코드를 추가하여 아래와 같이 수정했습니다.


#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
 
Adafruit_SSD1306 display = Adafruit_SSD1306();
  
#define DS3231_I2C_ADDRESS 104
 
// SCL - pin A5
// SDA - pin A4
// To set the clock, run the sketch and use the serial monitor.
// Enter T1124154091014; the code will read this and set the clock. See the code for full details.
 
byte seconds, minutes, hours, day, date, month, year;
char weekDay[4];
 
byte tMSB, tLSB;
float temp3231;

String T_seconds, T_minutes, T_hours, D_date, D_month, D_year;
 
void setup() {
  Serial.begin(9600);
   
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.clearDisplay();
  display.display();
  delay(1000);
 
  Wire.begin();
}
 
void loop() {
  watchConsole();
   
  get3231Date();
   
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.setTextSize(1);

  if (seconds < 10) { T_seconds = "0" + String(seconds, DEC);} else {T_seconds = String(seconds, DEC);}
  if (minutes < 10) { T_minutes = "0" + String(minutes, DEC);} else {T_minutes = String(minutes, DEC);}
  if (hours < 10) { T_hours = "0" + String(hours, DEC);} else {T_hours = String(hours, DEC);}
  if (date < 10) { D_date = "0" + String(date, DEC);} else {D_date = String(date, DEC);}
  if (month < 10) { D_month = "0" + String(month, DEC);} else {D_month = String(month, DEC);}
  if (year < 10) { D_year = "0" + String(year, DEC);} else {D_year = String(year, DEC);}
  
  display.print("DATE : "); display.print(weekDay); display.print(", "); display.print(D_date); display.print("/"); display.print(D_month); display.print("/"); display.println(D_year);
  display.print("TIME : "); display.print(T_hours); display.print(":"); display.print(T_minutes); display.print(":"); display.println(T_seconds);
  display.print("TEMP : "); display.println(get3231Temp());
  display.display();
   
  delay(1000);
}
 
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val) {
  return ( (val/10*16) + (val%10) );
}
 
void watchConsole() {
   
  if (Serial.available()) { // Look for char in serial queue and process if found
    if (Serial.read() == 84) { //If command = "T" Set Date
      set3231Date();
      get3231Date();
      Serial.println(" ");
    }
  }
}
  
void set3231Date() {
  //T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year)
  //T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99)
  //Example: 02-Feb-09 @ 19:57:11 for the 3rd day of the week -> T1157193020209
  // T1124154091014
  seconds = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
  minutes = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hours   = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  day     = (byte) (Serial.read() - 48); // set day of week (1=Sunday, 7=Saturday)
  date    = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month   = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year    = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0x00);
  Wire.write(decToBcd(seconds));
  Wire.write(decToBcd(minutes));
  Wire.write(decToBcd(hours));
  Wire.write(decToBcd(day));
  Wire.write(decToBcd(date));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}
 
void get3231Date() {
  // send request to receive data starting at register 0
  Wire.beginTransmission(DS3231_I2C_ADDRESS); // 104 is DS3231 device address
  Wire.write(0x00); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes
   
  if(Wire.available()) {
    seconds = Wire.read(); // get seconds
    minutes = Wire.read(); // get minutes
    hours   = Wire.read(); // get hours
    day     = Wire.read();
    date    = Wire.read();
    month   = Wire.read(); //temp month
    year    = Wire.read();

    seconds = (((seconds & B11110000)>>4)*10 + (seconds & B00001111)); // convert BCD to decimal
    minutes = (((minutes & B11110000)>>4)*10 + (minutes & B00001111)); // convert BCD to decimal
    hours   = (((hours & B00110000)>>4)*10 + (hours & B00001111)); // convert BCD to decimal (assume 24 hour mode)
    day     = (day & B00000111); // 1-7
    date    = (((date & B00110000)>>4)*10 + (date & B00001111)); // 1-31
    month   = (((month & B00010000)>>4)*10 + (month & B00001111)); //msb7 is century overflow
    year    = (((year & B11110000)>>4)*10 + (year & B00001111));
  } else {
    //oh noes, no data!
  }
   
  switch (day) {
    case 1:
      strcpy(weekDay, "Sun");
      break;
    case 2:
      strcpy(weekDay, "Mon");
      break;
    case 3:
      strcpy(weekDay, "Tue");
      break;
    case 4:
      strcpy(weekDay, "Wed");
      break;
    case 5:
      strcpy(weekDay, "Thu");
      break;
    case 6:
      strcpy(weekDay, "Fri");
      break;
    case 7:
      strcpy(weekDay, "Sat");
      break;
  }
}
 
float get3231Temp() {
  //temp registers (11h-12h) get updated automatically every 64s
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0x11);
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 2);
   
  if(Wire.available()) {
    tMSB = Wire.read(); //2's complement int portion
    tLSB = Wire.read(); //fraction portion
     
    temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
    temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8
  } else {
    //oh noes, no data!
  }
  return temp3231;
}


10보다 작은 수가 오면 자동으로 "0" 을 붙여주게 되었습니다.



요일 값을 year / month / date 값을 이용하여 자동으로 연산하여 넣어줄 수 있도록 할 수도 있습니다만,

너무 복잡해 지므로 관련해서 코드를 짠 분의 site 를 링크해 놓습니다.


* Day of the Week calculator

https://www.hackster.io/erikuchama/day-of-the-week-calculator-cde704






8. DS3231 내부 온도 센서와 BME280 센서와 비교


예전에 온도 전용 센서인 BME280 을 이용하여 측정해 본 경험이 있습니다.


* Hardware | BME280 sensor

http://chocoball.tistory.com/entry/HardwareBME280


DS3231 내부 온도센서의 정확성 비교를 위해 하룻저녁 두개를 같이 측정해 봤습니다.



꽤나 근접하네요.


DS3231 의 결과값에 일괄적으로 +0.25 를 했더니만 이제 좀 비슷해 진것 같습니다.

그 결과 그래프가 아래 그림입니다. 최종적으로는 +0.3 정도가 가장 적당해 보이네요.



DS3231 내부 온도센서의 정확성을 위해,

추출한 값을 100 곱한 다음, 마지막에 100 으로 나누는 방식을 채용하여 측정하였습니다.


* How to read DS3231 Internal temperature sensor, example code

http://forum.arduino.cc/index.php?topic=262986.15


아래 참고한 사이트를 보면 DS3231 을 가지고 온갖 할 수 있는 일을 다하는 사람의 글 입니다.


* Using a $1 DS3231 Real-time Clock Module with Arduino

https://thecavepearlproject.org/2014/05/21/using-a-cheap-3-ds3231-rtc-at24c32-eeprom-from-ebay/


최종 비교를 위해 사용된 sketch 는 다음과 같습니다.


#include "Wire.h"
#include "SPI.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_BME280.h"

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

Adafruit_BME280 BME;
   
const int DS3231_RTC_ADDR = 0x68;
const int DS3231_TEMP_MSB = 0x11;
union int16_byte {
  int i;
  byte b[2];
} rtcTemp;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  BME.begin();
}

void loop() {
   Wire.beginTransmission(DS3231_RTC_ADDR);
   Wire.write(DS3231_TEMP_MSB);
   Wire.endTransmission();
   Wire.requestFrom(DS3231_RTC_ADDR, 2);
   rtcTemp.b[1] = Wire.read(); 
   rtcTemp.b[0] = Wire.read();
   long tempC100 = (rtcTemp.i >> 6) * 25;    //degrees celsius times 100
   Serial.print( tempC100 / 100 );
   Serial.print( '.' );
   Serial.print( abs(tempC100 % 100) );
   
   Serial.print("\t");
   Serial.println(BME.readTemperature());

   delay(1000);
}





FIN


이번에 DS3231 을 가지고 놀면서 대학때 배운 BCD 도 다시 해보고,

EEPROM 에 대한 address 방식 등에 대해서도 배울 수 있어서 좋았습니다....

만, 배울게 너무 많아서 힘들었습니다.


당연히 지금껏 사용해본 sensor 중에는 활용도와 배울 점으로는 단연 top 입니다.

관련해서 전문가들도 온갖 기술을 구현해 놨고... 정말 변태같은 sensor 인듯 합니다.

(datasheet 를 보면 뭔가 더 많은데, 여기서 그만 하려구요.)


And

Hardware | Raspberry Pi CPU Info screen 구매기

|

1. Headless


Raspberry Pi 3 를 사용하고 있습니다.


* Hardware | PiAware 로 항공기 추적하기

http://chocoball.tistory.com/entry/Hardware-PiAware-FlightAware


* Hardware | Cross cable 로 MediaWiki 서버 연결해 보기

http://chocoball.tistory.com/entry/Hardware-connect-MediaWiki-with-Cross-cable


* Hardware | Raspberry Pi 3 model B 의 RPC 와 UK 생산지 차이를 비교해보자

http://chocoball.tistory.com/entry/Hardware-Raspberry-Pi-3-model-B-RPC-UK-compare


* Linux | Ubuntu-Mate 를 원격 데스크탑으로 사용해 보자

http://chocoball.tistory.com/entry/Linux-UbuntuMate-remote-desktop


* Hardware | Raspberry Pi 3 model B+ unboxing

http://chocoball.tistory.com/entry/Hardware-Raspberry-Pi-3-model-B-plus-unboxing


다만, desktop 환경이 아니라 서버처럼 사용하고 있죠.

이런 사용을 headless 라고 부릅니다.


연결도 SSH 를 통해서 연결하므로, 키보드 / 마우스 / 모니터가 필요 없어서

유지하기에 깔끔한 형태로 사용할 수 있습니다.


다만 문제가 되는건, SSH 를 통한 원격 연결시 사용되는 Raspberry Pi 의 IP가 dynamic 발급이 되므로,

가끔씩 접속하려는 IP를 확인해야 할 필요가 있습니다.


이때는 키보드 / 모니터 연결하여 "ifconfig" 를 때려봐야 알 수 있죠.


그렇습니다. 매우 귀찮습니다.





2. Raspberry Pi 용 간단 모니터를 구입하자


터치패드가 되며, 칼라는 바라지도 않습니다.

단순한 정보만 표시해주는 제품을 찾아 봅니다.


* Raspberry Pi 3 Model B CPU Info LCD Screen 1.6 inch 84x48 with Backlight Switch Compatible Pi2/1 / Orange Pi

https://www.aliexpress.com/item/Raspberry-Pi-3-Model-B-CPU-Info-LCD-Screen-1-6-inch-84x48-with-Backlight-Switch/32822409671.html



상품소개의 사진을 보니 IP 도 표시해주는 sample code 도 존재하나 봅니다.

세상 편해졌습니다. 바로 구매합니다.





3. 도착


1만원이 넘는 제품이라 그런지 2주만에 도착했습니다.



포장은 잘 되어 있네요.


안에 들어있는 박스에 제품이 들어있습니다.



짜잔~... 흠?!!!



뭔가 좀 이상합니다.


그렇습니다. 소개 사진과 다른 제품입니다.

업자 말로는 업그레이드 버전이라고 하는데, 제가 보기에는 다운그레이드 제품입니다.





4. 다운그래이드인 이유


아래는 업자가 개시하고 있는 사진입니다.



우선 앞면.

백라이트 on/off 스위치가 작고 간결하며 딱 저 위치가 사용하기 편합니다.



배달된 제품은 백라이트 스위치가 뒷면에 있고, 조작 버튼도 딱딱합니다. 한마디로 불편함.



또한 I2C pinout 이 구비되어 있습니다. 도착한건 생략되어 있습니다.

이 pinout 이 있으면, arduino 프로젝트에도 그대로 사용할 수 있습니다.

물론 GP pin 이 있으므로 그쪽으로 연결하면 되겠지만, 이렇게 깔끔하게 해놓은 것을 사용하고 싶었죠.



저는 Raspberry Pi 에 MediaWiki 를 집어 넣고, 개인정보 관리를 하고 있습니다.

그래서 portable 성이 중요한데...

저 pin 들 때문에 이 screen shield 는 가지고 다니면 손등이 뚫리겠어요.


업자는 V4.2 로 올라가면서 더 좋아졌다고 하지만, 저는 아직 dispute 를 풀지 않았습니다.





5. Python 으로 동작


우찌 되었든, 일단 동작이 되는지 확인해 봅니다.

다음 링크는, 이 제품컨셉을 처음 만든 회사의 사이트처럼 보입니다.


* SUNFOUNDER

http://wiki.sunfounder.cc/index.php?title=Raspberry_Pi_5110_Mini_LCD_84*48_PCD8544_Usage


우선 git 을 통해 repository 를 다운로드 받고, 실행시키면 됩니다.

git clone https://github.com/sunfounder/Adafruit_Nokia_LCD.git



그 다음 필요한 python 파일을 인스톨 해줍니다.

cd Adafruit_Nokia_LCD
sudo apt-get install python-dev
sudo python setup.py install
sudo apt-get install python-imaging



그런 다음, 해당 디렉토리로 가서 py 파일을 실행시키면 됩니다.

cd examples
sudo python image.py



자주보던 사진을 실재로 눈앞에서 보게 되네요.

동작에는 문제가 없어 보입니다.





6. C compiler 를 통한 실행파일로 동작


이제 IP 를 표시해주는 어플로 동작시켜 봅니다.

아래 사이트에서 cpu_show_v1.zip / cpu_show_v2.zip / cpu_show_v3.zip 을 다운로드 받아서 해동합니다.


* Raspberry Pi Viet Nam

https://raspberrypi.vn/thu-thuat-raspberry-pi/huong-dan-cai-dat-raspberry-pi-cpuinfo-screen-3315.pi


- cpu_show_v1.zip : cpu_show_v1.zip

- cpu_show_v2.zip : cpu_show_v2.zip

- cpu_show_v3.zip : cpu_show_v3.zip



처음에 "curl -O" 명령어를 사용했더니만 redirection 되는거 모르고 엄한 파일만 해동하려 했습니다.

가능하면 wget 사용하세요.


각 버전별로 보여주는 내용이 살짝 다릅니다.

v2 는 온도를 추가로 보여주고, v3 는 실행파일까지 만들어져 있네요.


v3 의 cpushow 실행파일을 실행시켜 보면 다음과 같습니다.



taobao 이메일 쓰는 5iPi 누구야~.

입맛에 맞게 화면을 수정하려면, "pcd8544_rpi.c" 파일의 프로그램을 조금 수정해야 할 필요가 있습니다.


일단 compile 할 수 있도록 필요한 페키지를 인스톨 합니다.


cd /home
git clone git://git.drogon.net/wiringPi
cd wiringPi
sudo ./build



warning 이 뜨고 그렇지만 사용하는데는 문제가 없어 보이는군요.



마지막에 wiringPi 페키지가 인스톨 되면, 컴파일시 "-lwiringPi" 옵션을 꼭 붙이라고 합니다.

이 주의문구 무시했다가 2시간가량 삽질했습니다.



wiringPi 가 잘 인스톨 되었는지 확인하려면 "gpio -v" 를 통해서 GPIO 통신을 시도합니다.

위의 화면처럼 나오면 정상적으로 인스톨 되었습니다.


자... 아까의 taobao 및 IP 를 해결해 봅시다.



소스를 보니 저 부분에서 수정해주면 되겠네요.

IP는 eth0 로 정의되어 있지만 device 리스트에는 enxb... 로 시작하는 device 명이니, 그걸로 수정합니다.



위와같이 하니, 온도 및 IP 가 모두 정상적으로 표시되었습니다.



문자가 들어갈 칸 수가 모자라, 제일 위의 문구는 빼버리고 IP 정보를 표시하게 했습니다.


IP octet 이 길어서, 마지막 숫자가 넘어가버렸네요.

대충 5라고 알수는 있는데... 10번 시도해서 알수 있는 부분이니 그냥 넘기기로 합니다.

(이제 막 귀찮아지기 시작)


아... 컴파일은 아래 명령어 입니다.


gcc -o cpushow pcd8544_rpi.c PCD8544.c -L/home/wiringPi/wiringPi/ -lwiringPi


최종적으로 잘 동작하는 동영상 입니다.



시작시 자동으로 프로그램이 back ground 로 실행될 수 있으며,

logging / 화면으로 결과를 나타내지 않도록, 아래 줄을 "/etc/rc.local" 파일의 "exit 0" 앞에 추가해 줍니다.


/home/cpu_info/cpushow >/dev/null 2>&1 &






FIN


cross cable 로 연결된 기기는 PC 가 IP를 자동 발급하고, arp 명령어를 통해서 알수 있다는걸 깜빡 했습니다.



나, 이거 왜산거야?


And

Hardware | SSD1306 에 로고를 세겨보자

|

1. 이제 때가 되었군


그렇습니다.

OLED 를 사용하다 보면, 커스텀 로고 새기는 방법을 익혀야 하는 경우가 몇번 있었습니다만,

대세에 지장이 없어서 무시하고 왔습니다.


그러나 UV Meter 를 가지로 놀려고 아래 링크를 따라해 보니,

커스텀 로고를 새기는 방법을 익혀야 할 때가 된걸 느꼈습니다.


* Arduino UV Meter using the UV31A Ultraviolet Sensor

http://www.electronics-lab.com/project/arduino-uv-meter-using-uv30a-ultraviolet-sensor/


아래 그림처럼, 처음 시작을 커스텀 로고로 시작되는 것을 볼 수 있습니다.



또한, 예전에 VU Meter 를 만들 때, 제작자가 한번 언급한 경우도 있었습니다.

이때는 그냥 제작자 코드를 따라하기만 하면 되는거였죠.


* Hardware | SSD1306 monochrome OLED 를 가지고 VU meter 를 만들어보자

 - http://chocoball.tistory.com/entry/Hardware-VU-meter-using-SSD1306-monochrome-OLED


자 그럼, 한번 시작해 볼까요?





2. Arduino micro 의 I2C 연결


여기서 잠깐.

평소 사용하는 Arduino Nano 와는 다르게, 요즘 자주 사용하고 있는 Arduino Micro 의 I2C pinout 이 달라 조금 헤매었습니다.



OLED 의 SCL / SDA 연결을 위해서는 D3 / D2 pin 을 이용해야 합니다.


 SSD1306  | Arduino Micro
--------------------------
   VCC    |     3.3V
   GND    |     GND
   SDL    |     D3
   SDA    |     D2
--------------------------


연결은 다음과 같아요.






3. OLED 의 너비와 높이를 구해보자


우선 사용할 OLED 의 가로, 세로 pixel 수를 구해야 합니다.

이는 최종적으로 만들 그림의 크기를 정하기 위한거죠.


연결한 OLED 에 가로, 세로 pixel 수를 알아보기 위해 아래 code 를 사용합니다.


#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
	display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
	display.clearDisplay();
	display.display();
}

void loop() {
	display.clearDisplay();
	
	display.setTextColor(WHITE);
	display.setCursor(0,0);
	display.setTextSize(2);
	display.println(display.width());
	display.print(display.height());
	
	display.display();
}


결과는 다음과 같습니다.



SSD1306 이지만 128x64 가 아닌, 128x32 군요.

높이가 일반적인 OLED 보다 반쪽이라서 그림을 신경써야 합니다.





4. 필요한 그림파일 찾기


UV Meter 를 위해서는, 우선 태양 그림이 필요합니다.

Googling 하여 태양 그림을 찾아 봅니다.

키워드는 "sun shining icon image" 로 검색했습니다.


주르륵 뜨는군요. 적당한 것을 하나 골라 봅니다.







5. 흑백 및 적당한 크기로


그림을 생성하기 위해 Paint.net 이라는 어플을 사용합니다.


* paint.net

https://www.getpaint.net/


우선 OLED 의 크기가 128x32 이므로, 그림 크기를 OLED 크기에 맞게 생성해 줍니다.



태양 그림 파일을 불러와서 크기를 줄여줍니다.

OLED 가 32 이므로, 32로 하면 그림이 다 들어가겠지만, 그렇게 되면 너무 작아 보여,

64x64 로 줄여줍니다.



또한 OLED 에 표시하기 위해서는 on/off, 0/1, white/black 으로 표시해야 합니다.

최대한 근접하게 우선 Brightness / Contrast 를 설정해 줍니다.



전체적으로 다음과 같이 꾸며 봅니다.


Contrast 를 최대로 높히면 Bitmap 생성시 완전한 흑백으로 근접하게 만들 수 있습니다.



마지막으로 Windows 에 기본으로 들어 있는,

mspaint ( %windir%\system32\mspaint.exe ) 를 사용하여,

최종적으로 Monochrome BMP 파일로 생성해 줍니다.


Save as Monochrome Bitmap 으로 하면서 완전히 white/black 으로 변경됩니다.






6. LCD Assistant


이제 C 코드용 bitmap 을 만들어주는 어플을 다운로드 받아서 실행합니다.


* LCD Assistant

http://en.radzio.dxp.pl/bitmap_converter/



실행한 후, 위에서 만든 파일을 load 합니다.



이제 "Save output" 으로 최종적으로 text 파일로 export 합니다.



Output 된 text 파일을 열어보면 아래와 같이 생성되어 있습니다.



Monochrome Bitmap 으로만 잘 만들면,

LCD Assistant 를 이용하여 C code map 을 만드는데 쉽게 진행됩니다.





7. OLED 에 커스텀 코드를 올려보자


C code map 을 array 로 올린 다음,

display.drawBitmap 을 이용하여 표현할 수 있는 준비가 되었습니다.


최종 코드는 다음과 같습니다.


#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

static const unsigned char PROGMEM UVMeter[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x18, 0x03, 0x18, 0x01, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x1C, 0x03, 0x18, 0x03, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x1C, 0x03, 0x1C, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x07, 0xC0, 0x07, 0x00, 0x00, 0x1C, 0x03, 0x0C, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xC0, 0x0F, 0xC0, 0x0E, 0x00, 0x00, 0x1C, 0x03, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xE0, 0x0F, 0xE0, 0x3E, 0x00, 0x00, 0x1C, 0x03, 0x0E, 0x06, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xF0, 0x0F, 0xF0, 0x7E, 0x00, 0x00, 0x1C, 0x03, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFC, 0x0F, 0xF8, 0xFE, 0x00, 0x00, 0x1C, 0x03, 0x06, 0x0C, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xDF, 0xFD, 0xFE, 0x00, 0x00, 0x1C, 0x03, 0x07, 0x0C, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x1C, 0x03, 0x03, 0x0C, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x1C, 0x03, 0x03, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x1C, 0x03, 0x03, 0x98, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7F, 0xE0, 0x03, 0xFF, 0x00, 0x00, 0x1C, 0x07, 0x01, 0x98, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7F, 0x03, 0xE0, 0x7F, 0x00, 0x00, 0x0C, 0x06, 0x01, 0xB0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFC, 0x3F, 0xFE, 0x1F, 0x3F, 0xF0, 0x0E, 0x0E, 0x01, 0xF0, 0x00, 0x00, 0x00, 0x00,
0x06, 0x0F, 0xF8, 0xFF, 0xFF, 0x8F, 0xFF, 0xC0, 0x07, 0xFC, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00,
0x07, 0xFF, 0xF1, 0xFF, 0xFF, 0xC7, 0xFF, 0x80, 0x03, 0xF8, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xE7, 0xFF, 0xFF, 0xF3, 0xFF, 0x00, 0x00, 0x00, 0x60, 0x18, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xCF, 0xFF, 0xFF, 0xF9, 0xFF, 0x00, 0x00, 0x00, 0x70, 0x38, 0x00, 0x20, 0x00, 0x00,
0x01, 0xFF, 0x9F, 0xFF, 0x7F, 0xFC, 0xFF, 0x00, 0x00, 0x00, 0x70, 0x38, 0x00, 0x20, 0x00, 0x00,
0x00, 0xFF, 0x9F, 0xF0, 0x07, 0xFC, 0xFE, 0x00, 0x00, 0x00, 0x58, 0x28, 0x38, 0x78, 0x38, 0x48,
0x00, 0x7F, 0x3F, 0xC0, 0x01, 0xFE, 0x7E, 0x00, 0x00, 0x00, 0x58, 0x68, 0x6E, 0x78, 0xCC, 0x7C,
0x00, 0x3E, 0x7F, 0x80, 0x00, 0xFF, 0x3E, 0x00, 0x00, 0x00, 0x48, 0x48, 0xC6, 0x20, 0x84, 0x60,
0x00, 0x1E, 0x7F, 0x00, 0x00, 0x7F, 0x3C, 0x00, 0x00, 0x00, 0x4C, 0xC8, 0xC2, 0x21, 0x86, 0x40,
0x00, 0x1E, 0x7E, 0x00, 0x00, 0x3F, 0x3C, 0x00, 0x00, 0x00, 0x4C, 0x88, 0xFE, 0x21, 0xFE, 0x40,
0x00, 0x3C, 0xFE, 0x00, 0x00, 0x3F, 0x9F, 0x00, 0x00, 0x00, 0x46, 0x88, 0x80, 0x21, 0x80, 0x40,
0x00, 0x7C, 0xFC, 0x00, 0x00, 0x1F, 0x9F, 0xF0, 0x00, 0x00, 0x47, 0x88, 0xC0, 0x21, 0x80, 0x40,
0x00, 0xFC, 0xFC, 0x00, 0x00, 0x1F, 0x9F, 0xFC, 0x00, 0x00, 0x43, 0x08, 0x42, 0x30, 0xC0, 0x40,
0x03, 0xFC, 0xFC, 0x00, 0x00, 0x1F, 0x9F, 0xFE, 0x00, 0x00, 0x43, 0x08, 0x7E, 0x1C, 0x7C, 0x40,
0x07, 0xFC, 0xFC, 0x00, 0x00, 0x1F, 0x9F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};


void setup() {
	display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
	display.clearDisplay();
	display.display();
}

void loop() {
	display.clearDisplay();
	
	display.drawBitmap(0, 0, UVMeter, 128, 32, WHITE);

	display.display();
}


짜잔~!!!

잘 나오는군요.






FIN


사실 이렇게까지 만드는데 거진 8시간정도 걸렸습니다.

Arduino micro 의 I2C pinout 이나, 순수한 Monochrome Bitmap 을 만드는 방법을 찾는데 많이 걸렸네요.

본 포스트가 다른 분들에게 많이 도움이 되었으면 합니다.


And

Hardware | SSD1309 128x64 1.54" yellow OLED

|

1. OLED 들


지금까지 3가지 OLED 를 가지고 놀아 봤습니다.


* SSD1306 128x64 0.96" monochrome OLED

http://chocoball.tistory.com/entry/Hardware-SSD1306-128x64-monochrome-OLED



* SSD1331 96x64 0.95" full color OLED

http://chocoball.tistory.com/entry/Hardware-SSD1331-96x64-full-color-OLED



* SSD1306 128x64 1.3" monochrome OLED

http://chocoball.tistory.com/entry/Hardware-Adafruit-SSD1306-128x64-13inch-monochrome-OLED


그러다 Spot Welder 를 만들 생각을 구상하던 중, 기존 DIY 된 것들을 보면 뭔가 아쉬운 점들이 눈에 보였습니다.

그것은 바로 !!! 상태창의 크기 !!!


그렇습니다. 모두 창태창의 크기가 본체들과 비교해 너무 작게 보였던 것이였어요.



당장 필요한건 아니지만 향후 Spot Welder 제작 준비단계로, 조금 큰 OLED 를 찾아봅니다.




2. 구매


OLED 를 키워드로 찾고 크기순으로 분류해 보니, 너무 큰걸 빼고 간단한 회로와 연결시키는 OLED 중에는 1.54 inch 가 있었습니다.

또한 White 와 Yellow 가 있었는데, 조금 더 비싸지만 Yellow 가 있어보였습니다.


조금 더 비싼거라 적당한 가격에 파는곳이 그리 많지 않네요.

결국 아래 link 에 걸려있는 업자것을 구매 결정합니다.


* 1.54" 1.54 inch Yellow OLED Display Module 128x64 SPI IIC I2C Interface OLED Screen Board 3.3-5V For Arduino AVR STM32 8051

https://ko.aliexpress.com/item/1-54-1-54-inch-Yellow-OLED-Display-Module-128x64-SPI-IIC-I2C-Interface-OLED-Screen/32834248792.html


흠흠... 괜찮은 선택 같습니다.




사양은 다음과 같습니다.


* Product Introduction:

 1. Size : 1.54 inch

 2. Resolution : 128*64

 3. Light Color : Yellow

 4. Driving IC : SSD1309 (compatible with SSD1306)

 5. Voltage : 3.3V-5V

 

* SPI Interface Definition:

 1. GND : power ground

 2. VCC : power positive

 3. SCL : clock wire

 4. SDA : data wire

 5. RES : reset wire

 6. DC : data/command

 7. CS : chip selection (if not used, it can be directly connect with ground)

  

* Package included:

1*1.54inch OLED Display Module SPI Interface 3.3-5V SSD1309(Yellow)


노란색 OLED 는 어떤 분위기 일까~ 하면서 기다렸습니다.




3. 도착


요즈음은 알리 배송이 대략 2주인것 같습니다.

초기의 기본 한달보다는 많이 짧아진것 같습니다.



뽁뽁이로 잘 쌓여져 있구요.



정전기 방지 비닐로 잘 포장되어 왔습니다... 만!!!



핀 부분이 역력하게 힘으로 눌린 흔적이 보입니다.

이런 종류는 몇번 감는 뽁뽁이 보다는, 핀의 높이를 커버해 주는 스펀지가 제품 파손 방지에 도움이 더 될듯 합니다.



SPI 와 I2C 모두 대응하고 있습니다.

배송된 상태는 기본 SPI 로 동작하게끔 설정되어 있네요.

I2C 로 변경하기 위해서는 jumper 두곳을 쇼트시켜야 합니다.


SPI 가 반응 속도면에서 우수하므로, 그대로 사용하려 합니다.

다만, 기존 spot welder DIY 를 보면, 대부분 I2C 로 연결되어 있는듯 합니다.

실제로 만들 때에는 SPI 로 동작하게끔 소스 수정해야겠습니다.



윗부분 입니다. 크기가 큰 만큼 뭔가 있어 보입니다.



기존에 가지고 있던 0.95 / 0.96 inch 와의 비교샷 입니다.

확실히 크지요?




4. 동작시켜 보기


얼른 보고싶네요.

배선 및 소스는 1.3 inch 를 가지고 놀던 내용을 그대로 사용하였습니다.


* SSD1306 128x64 1.3" monochrome OLED

http://chocoball.tistory.com/entry/Hardware-Adafruit-SSD1306-128x64-13inch-monochrome-OLED



아... 노란색 이쁩니다.



뭔가 더 사이버틱 해졌습니다.



동영상 입니다.

작은 OLED 들의 고질적인 현상인 깜빡거림은 어쩔 수 없네요.

이 가격에 너무 많이 바랄 수는 없겠죠?




FIN


Spot Welder 제작을 위해 어서 링코어를 구해야겠습니다.

And
prev | 1 | 2 | next