■ MCP3008은 라즈베리파이나 아두이노 같은 마이크로컨트롤러에서 8채널 10비트 아날로그-디지털 컨버터(ADC) 칩입니다.
▶ SPI 통신 인터페이스를 통해 디지털 데이터를 전송하며, 전위차계, 조이스틱, 온도 센서 등
다양한 아날로그 센서의 값을 읽어 디지털 값(0~1023)으로 변환해주는 역할을 합니다.
1) 가변저항기와 MCP3008 연결
가변저항기 중간 핀을 MCP4008의 1번 핀 CH0에 연결한다
2} 라즈베리파이와 MCP3008 연결
MCP3008 라즈베리파이
16 Vdd 5V
15 Vref 5V
14 AGND ----GND
13 CLK GP11
12 Dout GP9
11 Din GP10
10 CS/SHC GP8
9 GND ---5V
*그라운드, 전원 순서로 연결했을 시에는 가운데 2번 핀을 우측으로 돌리면, 저항을 최소 값 0에서 최소값 1023을 읽을 수 있다.
MCP 칩으로 연결시, SPI 통신 연결 방법
vi Potentiometer_graph.py
import RPi.GPIO as GPIO
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import random
import time
import spidev
spi=spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000
def ReadChannel(channel):
adc=spi.xfer2([1,(8+channel)<<4,0])
data=((adc[1]&3)<<8)+adc[2]
return data
mcp3008_channel=0
fig = plt.figure()
ax = plt.axes(xlim=(0, 50), ylim=(0, 1024))
line, = ax.plot([], [], lw=1, c='blue', marker='d',ms=2)
max_points = 50
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float64)*np.nan, lw=1, c='blue',marker='d',ms=2)
def init():
return line
def animate(i):
y = ReadChannel(mcp3008_channel) // 이부분만 센서값으로 바꾸어주면 된다.
old_y = line.get_ydata()
new_y = np.r_[old_y[1:], y]
line.set_ydata(new_y)
print(new_y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=False)
plt.show()