Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[47] 230307 Ch. 5 ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด GPIO ๋ฐ ์„ผ์„œ ํ™œ์šฉํ•˜๊ธฐ: 6. I²C(Inter-Integrated Circuit) ํ†ต์‹  [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 47์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Raspberry Pi, Linux, Electronics Basics

[47] 230307 Ch. 5 ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด GPIO ๋ฐ ์„ผ์„œ ํ™œ์šฉํ•˜๊ธฐ: 6. I²C(Inter-Integrated Circuit) ํ†ต์‹  [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 47์ผ]

yjyuwisely 2023. 3. 7. 09:06

230307 Tue 47th class

Ch. 5 ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด GPIO ๋ฐ ์„ผ์„œ ํ™œ์šฉํ•˜๊ธฐ
์ง„๋„: p. 156 ~ (๊ต์žฌ: IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„, ์ €์ž: ์ตœ์ฃผํ˜ธ , ๊น€์žฌ๋ฒ” , ์ •๋™์ง„)
๊ต์žฌ ์ฝ”๋“œ ๊นƒํ—ˆ๋ธŒ: https://github.com/codingspecialist/RaspberryPi4-Book-Example
๊ต์žฌ ์†Œ์Šค ๋ฐ PPT: http://jspstudy.co.kr/main/main.jsp
์ €์ž ์นดํŽ˜: https://cafe.naver.com/metacoding

 

IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ | ์ตœ์ฃผํ˜ธ - ๊ต๋ณด๋ฌธ๊ณ 

IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ | ใ€ŽIoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ใ€์€ ์™•์ดˆ๋ณด๋„ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ IoT๋ฅผ ์ง์ ‘ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ „๊ธฐ์ „์ž ๊ธฐ์ดˆ ์›๋ฆฌ๋ถ€ํ„ฐ ์„ผ

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


Chapter 05 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด GPIO ๋ฐ ์„ผ์„œ ํ™œ์šฉํ•˜๊ธฐ

์ฑ…์˜ ๋ชฉ์ฐจ
01 _ GPIO ์ œ์–ด ๋ฐ ํ…Œ์ŠคํŠธ
01-1 LED ์‹ค์Šต
01-2 ํ‘ธ์‹œ ๋ฒ„ํŠผ ์Šค์œ„์น˜ ์‹ค์Šต(Polling ๋ฐฉ์‹)
01-3 ํ‘ธ์‹œ ๋ฒ„ํŠผ ์Šค์œ„์น˜ ์‹ค์Šต(Event ์•Œ๋ฆผ ๋ฐฉ์‹)
01-4 ํ‘ธ์‹œ ๋ฒ„ํŠผ ์ž…๋ ฅ์œผ๋กœ LED ์‹ค์Šต

02 _ PWM ์ œ์–ด
02-1 PWM์œผ๋กœ LED ์‹ค์Šต
02-2 PWM์œผ๋กœ ๋ถ€์ € ์‹ค์Šต
02-3 PWM์œผ๋กœ ์„œ๋ณด๋ชจํ„ฐ ์‹ค์Šต

03 _ ์ดˆ์ŒํŒŒ์„ผ์„œ(HC-SR04) ์‹ค์Šต

04 _ PIR ์„ผ์„œ(HC-SR501) ์‹ค์Šต

05 _ ์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ์™€ SPI ํ†ต์‹ 
05-1 ์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ๋ฅผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋กœ ์ฝ์–ด์˜ค๊ธฐ
05-2 SPI ํ†ต์‹ ์˜ ์ดํ•ด
05-3 SPI ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์—์„œ ์„ค์ •ํ•˜๊ธฐ
05-4 ์•„๋‚ ๋กœ๊ทธ ์ „์••์„ MCP3008๋กœ ์ฝ์–ด์˜ค๊ธฐ
05-5 LDR ์„ผ์„œ ์‹ค์Šต
05-6 ์กฐ์ด์Šคํ‹ฑ(Joystick) ์‹ค์Šต

06 _ I²C ํ†ต์‹ 
06-1 I²C ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์—์„œ ์„ค์ •ํ•˜๊ธฐ
06-2 BMP180(๋Œ€๊ธฐ์•• ์„ผ์„œ) ์‹ค์Šต
06-3 OLED ๋””์Šคํ”Œ๋ ˆ์ด ์‹ค์Šต

6 I²C (Inter-Integrated Circuit) ํ†ต์‹  p. 161

์‹œ๋ฆฌ์–ผ ํ†ต์‹ (์ง๋ ฌ ํ†ต์‹ )
2๊ฐœ์˜ ํ†ต์‹ ๋ผ์ธ(SDA, SCL)
  • SDA(Serial Data) : ์‹œ๋ฆฌ์–ผ ๋ฐ์ดํ„ฐ๋กœ ํ†ต์‹ ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ์œ„์˜ ์„ค๋ช…์ฒ˜๋Ÿผ SCL์ด LOW์ผ ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฐ๊ฐ์˜ ์ „์†ก์—๋Š” ACK๊ฐ€ ํฌํ•จ๋˜๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ๊ฒƒ์ผ์ง€ ์“ธ ๊ฒƒ์ผ์ง€์— ๋”ฐ๋ผ ๊ทœ๊ฒฉ์ด ๋‹ค๋ฅด๋‹ค.
  • SCL(Serial Clock) : ์‹œ๋ฆฌ์–ผ ํด๋Ÿญ์œผ๋กœ ๋งˆ์Šคํ„ฐ์™€ ์Šฌ๋ ˆ์ด๋ธŒ ๊ฐ„์˜ ํด๋Ÿญ์„ ๋งž์ถฐ ๋ฐ์ดํ„ฐ๋ฅผ ์†ก์ˆ˜์‹  ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ๊ธฐ๋ณธ ๊ฐ’ HIGH์ผ ๋•Œ๋งŒ SDA๊ฐ€ HIGH -> LOW ๋กœ ๊ฐ€๋ฉด START, LOW -> HIGH๋กœ ๊ฐ€๋ฉด END๋ฅผ ๋œปํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹ ์€ SCL์ด LOW์ผ ๋•Œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
IC2 ํ†ต์‹  ๋ชจ๋ธ

์ฐธ๊ณ : https://gudgud.tistory.com/28

์ €์†์˜ ํ†ต์‹ ์†๋„

I²C ํ†ต์‹  ์ƒ์„ธ ๋ถ„์„ ์ฐธ๊ณ : https://www.hardcopyworld.com/?p=2790

PULL UP ์ €ํ•ญ์€ ์ด๋ฏธ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด์— ์žฅ์ฐฉ๋˜์–ด ์žˆ์Œ


6.1 I²C ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์—์„œ ์„ค์ •ํ•˜๊ธฐ


6.2 BMP180(๋Œ€๊ธฐ์•• ์„ผ์„œ) ์‹ค์Šต

BMP180: Barometric Pressure

BMP180์€ ๋ณด์‰ฌ(Bosch)์‚ฌ์—์„œ ์ œ์ž‘ํ•œ ์„ผ์„œ๋กœ ์ •๋ฐ€ํ•œ ๊ธฐ์•• ๋ฐ ์˜จ๋„๋ฅผ ์ธก์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค๊ณ ๋„ (ALTitude)๋Š” ๊ธฐ์••์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ๊ธฐ์••๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ๊ณ ๋„๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

[1] BMP_180.py p. 163

#-*- coding: utf-8 -*-

#ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. (BMP85/180)
import Adafruit_BMP.BMP085 as BMP085

#BMP180์„ผ์„œ์˜ ์ธ์Šคํ„ด์Šค sensor ์ƒ์„ฑ
sensor = BMP085.BMP085()
#์˜จ๋„, ์••๋ ฅ, ๊ณ ๋„ ๊ฐ’์„ ์ฝ์–ด์„œ ๋ณ€์ˆ˜์— ์ €์žฅ
temp = sensor.read_temperature()
pressure = sensor.read_pressure()
ALTitude = sensor.read_ALTiture()
#์ธก์ •๊ฐ’์„ ์ถœ๋ ฅ
print('Temp = {0:0.2f} *C'.format(temp))
print('Pressure = {0:0.2f} Pa'.format(pressure))
print('ALTitude = {0:0.2f} m'.format(ALTitude))


6.3 OLED ๋””์Šคํ”Œ๋ ˆ์ด ์‹ค์Šต

I²C ํ†ต์‹ 
OLED ๋””์Šคํ”Œ๋ ˆ์ด ๋ชจ๋“ˆ
ํ•ด์ƒ๋„๋Š” 128X64 ํ”ฝ์…€
SSD1306 ๋“œ๋ผ์ด๋ฒ„

I²์ฃผ์†Œ(Address) ํ™•์ธ

ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅ

i2cdetect -y 1

I²์ฃผ์†Œ๋ฅผ ๋ณด๋ฉด ์•ž์—์„œ ํ™•์ธํ–ˆ์—ˆ๋˜ BMP180 ๋ชจ๋“ˆ์˜ ์ฃผ์†Œ 0x77์— ๋”ํ•ด์„œ OLED ๋ชจ๋“ˆ์˜ 0x3c ๊ฐ€ ํ‘œ์‹œ๋จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(3) ํŒŒ์ด์ฌ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ 
[2] Oled.py p. 165 - p. 166
์‹ค์ŠตํŒŒ์ผ: /home/pi/webapps/ch05/Oled.py

#-*-coding:utf-8-*-

# ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. 
import time
import Adafruit_SSD1306
from PIL import Image, ImageDraw, ImageFont
import Adafruit_BMP.BMP085 as BMP085

# OLED 128X64 ( i2c_address = ์—ฐ๊ฒฐ๋œ OLED ์ฃผ์†Œ ) ์ธ์Šคํ„ด์Šค disp ์ƒ์„ฑ
disp = Adafruit_SSD1306.SSD1306_128_64(rst=None, i2c_address=0x3C)

# disp ์ดˆ๊ธฐํ™” 
disp.begin()

# ํ™”๋ฉด ํด๋ฆฌ์–ด 
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image
draw.rectangle((0,0,width,height), outline=0, fill=0)

# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

# ๋””ํดํŠธ ํฐํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ 
font = ImageFont.load_default()
# BMP180์„ผ์„œ์˜ ์ธ์Šคํ„ด์Šค sensor ์ƒ์„ฑ 
sensor = BMP085.BMP085()

while True:
    # ์˜จ๋„, ์••๋ ฅ, ๊ณ ๋„ ๊ฐ’์„ ์ฝ์–ด์„œ ๋ณ€์ˆ˜์— ์ €์žฅ 
    temp = sensor.read_temperature()
    pressure = sensor.read_pressure()
    altitude = sensor.read_altitude()

	# ์ธก์ •๊ฐ’์„ ์ถœ๋ ฅ (ํ„ฐ๋ฏธ๋„) 
    print('Temp = {0:0.2f} *C'.format(temp))
    print('Pressure = {0:0.2f} Pa'.format(pressure))
    print('Altitude = {0:0.2f} m'.format(altitude))

	# OLED์— ํ™”๋ฉด ํ‘œ์‹œ ๋‚ด์šฉ 
    draw.text((x,top),   'Temp = {0:0.2f} *C'.format(temp), font=font, fill=255)
    draw.text((x,top+8), 'Pressure = {0:0.2f} Pa'.format(pressure),font=font, fill=255)
    draw.text((x,top+16),'Altitude = {0:0.2f} m'.format(altitude), font=font, fill=255)
    
    # ํ™”๋ฉด ํ‘œ์‹œ 
    disp.image(image)
    disp.display()
    
    # ๋”œ๋ ˆ์ด ์‹œ๊ฐ„ 2์ดˆ 
    time.sleep(2)

์„ผ์„œ BMP180์˜ ์œ„์น˜๋‚˜ ์˜จ๋„๊ฐ€ ๋ณ€ํ•  ๋•Œ OLED ๋””์Šคํ”Œ๋ ˆ์ด์— 2์ดˆ๋งˆ๋‹ค ํ™•์ธํ•ด ๋ณธ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

'๐ŸŒณBootcamp Revision 2023โœจ > Raspberry Pi, Linux, Electronics Basics' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[46] 230306 Ch. 5 ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด GPIO ๋ฐ ์„ผ์„œ ํ™œ์šฉํ•˜๊ธฐ: 3. ์ดˆ์ŒํŒŒ์„ผ์„œ(HC-SR04) ์‹ค์Šต, 4. PIR(Passive Infrared Sensor) ์„ผ์„œ(HC-SR501) ์‹ค์Šต, 5. ์•„๋‚ ๋กœ๊ทธ ์‹ ํ˜ธ์™€ SPI(Serial Peripheral Interface) ํ†ต์‹  [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 4..  (0) 2023.03.06
[45] 230303 Ch. 5 ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด GPIO ๋ฐ ์„ผ์„œ ํ™œ์šฉํ•˜๊ธฐ: 1. GPIO(General Purpose Input Output) ์ œ์–ด ๋ฐ ํ…Œ์ŠคํŠธ, 2. PWM(Pulse Width Modulation) ์ œ์–ด [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 45์ผ]  (0) 2023.03.03
[45] 230303 Ch. 4 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ์ „์ž ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 1. ์•Œ๊ธฐ ์‰ฌ์šด ์ „์žํšŒ๋กœ, 2. ๋ฐ์ดํ„ฐ์‹œํŠธ์™€ ํšŒ๋กœ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ „์ž ๋ถ€ํ’ˆ ์•Œ์•„๋ณด๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 45์ผ]  (0) 2023.03.03
[43~44] 230228, 230302 ๋ฆฌ๋ˆ…์Šค Ch. 2 ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด๋ฅผ ์œ„ํ•œ ๋ฆฌ๋ˆ…์Šค ๊ธฐ์ดˆ ๋ฐฐ์šฐ๊ธฐ: 1. ์šด์˜ ์ฒด์ œ, 2. ์›๊ฒฉ ์ ‘์†์„ ์œ„ํ•œ VNC์™€ Putty, 3. ๋ฆฌ๋ˆ…์Šค ํŒŒ์ผ ์‹œ์Šคํ…œ, 4. ๋ฆฌ๋ˆ…์Šค ๋ช…๋ น์–ด [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 43~44์ผ]  (0) 2023.02.28
Comments