📌 사용 전 핵심 주의사항
| 항목 | 설명 |
|---|---|
| 전압 | Pico WH GPIO는 3.3V 기준입니다. 5V 신호를 GPIO에 직접 넣지 마세요. |
| 공통 GND | 센서, 모터드라이버, 외부 전원은 Pico WH와 GND를 반드시 공통으로 연결합니다. |
| ADC 핀 | 아날로그 입력은 GP26, GP27, GP28을 사용합니다. |
| I2C 기본 예시 | SDA=GP0, SCL=GP1 기준으로 작성했습니다. |
| 라이브러리 | OLED, BMP280, MPU6050, RFID 등은 별도 MicroPython 라이브러리 파일이 필요할 수 있습니다. |
🔗 프로젝트 바로가기
PROJECT 01
내장 LED 깜빡이기
from machine import Pin
import time
led = Pin("LED", Pin.OUT)
while True:
led.value(1)
time.sleep(0.5)
led.value(0)
time.sleep(0.5)
PROJECT 02
외부 LED 켜고 끄기
from machine import Pin
import time
led = Pin(15, Pin.OUT)
while True:
led.on()
time.sleep(1)
led.off()
time.sleep(1)
PROJECT 03
버튼 입력으로 LED 제어
from machine import Pin
import time
button = Pin(14, Pin.IN, Pin.PULL_UP)
led = Pin(15, Pin.OUT)
while True:
if button.value() == 0:
led.on()
else:
led.off()
time.sleep(0.05)
PROJECT 04
버튼 누른 횟수 세기
from machine import Pin
import time
button = Pin(14, Pin.IN, Pin.PULL_UP)
count = 0
old = 1
while True:
now = button.value()
if old == 1 and now == 0:
count += 1
print("누른 횟수:", count)
time.sleep(0.2)
old = now
PROJECT 05
부저로 삐 소리 내기
from machine import Pin
import time
buzzer = Pin(16, Pin.OUT)
while True:
buzzer.on()
time.sleep(0.2)
buzzer.off()
time.sleep(0.8)
PROJECT 06
PWM 부저 음계 만들기
from machine import Pin, PWM
import time
buzzer = PWM(Pin(16))
notes = [262, 294, 330, 349, 392, 440, 494, 523]
for n in notes:
buzzer.freq(n)
buzzer.duty_u16(30000)
time.sleep(0.3)
buzzer.duty_u16(0)
PROJECT 07
LED 밝기 조절
from machine import Pin, PWM
import time
led = PWM(Pin(15))
led.freq(1000)
while True:
for duty in range(0, 65535, 2000):
led.duty_u16(duty)
time.sleep(0.03)
for duty in range(65535, 0, -2000):
led.duty_u16(duty)
time.sleep(0.03)
PROJECT 08
RGB LED 색 바꾸기
from machine import Pin, PWM
import time
r = PWM(Pin(13)); g = PWM(Pin(14)); b = PWM(Pin(15))
for x in (r, g, b):
x.freq(1000)
colors = [(65535,0,0), (0,65535,0), (0,0,65535), (65535,65535,0), (0,65535,65535)]
while True:
for cr, cg, cb in colors:
r.duty_u16(cr); g.duty_u16(cg); b.duty_u16(cb)
time.sleep(1)
PROJECT 09
가변저항 값 읽기
from machine import ADC
import time
pot = ADC(26)
while True:
value = pot.read_u16()
print("가변저항:", value)
time.sleep(0.2)
PROJECT 10
가변저항으로 LED 밝기 조절
from machine import Pin, ADC, PWM
import time
pot = ADC(26)
led = PWM(Pin(15))
led.freq(1000)
while True:
value = pot.read_u16()
led.duty_u16(value)
print(value)
time.sleep(0.05)
PROJECT 11
조도센서 값 읽기
from machine import ADC
import time
light = ADC(26)
while True:
value = light.read_u16()
print("밝기값:", value)
time.sleep(0.5)
PROJECT 12
어두우면 LED 켜기
from machine import Pin, ADC
import time
light = ADC(26)
led = Pin(15, Pin.OUT)
while True:
value = light.read_u16()
if value < 25000:
led.on()
else:
led.off()
print(value)
time.sleep(0.2)
PROJECT 13
토양 습도 센서 읽기
from machine import ADC
import time
soil = ADC(27)
while True:
value = soil.read_u16()
print("토양 습도 원시값:", value)
time.sleep(1)
PROJECT 14
자동 물주기 알림
from machine import Pin, ADC
import time
soil = ADC(27)
buzzer = Pin(16, Pin.OUT)
while True:
value = soil.read_u16()
if value > 45000:
buzzer.on()
print("흙이 건조합니다!")
else:
buzzer.off()
print("습도 적당:", value)
time.sleep(1)
PROJECT 15
온도 센서 LM35 읽기
from machine import ADC
import time
sensor = ADC(26)
while True:
raw = sensor.read_u16()
voltage = raw * 3.3 / 65535
temp_c = voltage * 100
print("온도:", temp_c, "°C")
time.sleep(1)
PROJECT 16
Pico 내부 온도 측정
from machine import ADC
import time
sensor = ADC(4)
conversion = 3.3 / 65535
while True:
voltage = sensor.read_u16() * conversion
temp = 27 - (voltage - 0.706) / 0.001721
print("내부 온도:", temp)
time.sleep(1)
PROJECT 17
DHT11 온습도 읽기
from machine import Pin
import dht
import time
sensor = dht.DHT11(Pin(15))
while True:
sensor.measure()
print("온도:", sensor.temperature(), "°C")
print("습도:", sensor.humidity(), "%")
time.sleep(2)
PROJECT 18
DHT22 정밀 온습도
from machine import Pin
import dht
import time
sensor = dht.DHT22(Pin(15))
while True:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
print("온도:", t, "습도:", h)
time.sleep(2)
PROJECT 19
초음파 거리 측정
from machine import Pin, time_pulse_us
import time
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
while True:
trig.low()
time.sleep_us(2)
trig.high()
time.sleep_us(10)
trig.low()
duration = time_pulse_us(echo, 1)
distance = duration * 0.0343 / 2
print("거리:", distance, "cm")
time.sleep(1)
PROJECT 20
거리 가까우면 경고음
from machine import Pin, time_pulse_us
import time
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
buzzer = Pin(16, Pin.OUT)
while True:
trig.low(); time.sleep_us(2)
trig.high(); time.sleep_us(10)
trig.low()
duration = time_pulse_us(echo, 1)
distance = duration * 0.0343 / 2
buzzer.value(distance < 20)
print(distance)
time.sleep(0.2)
PROJECT 21
PIR 인체 감지
from machine import Pin
import time
pir = Pin(14, Pin.IN)
led = Pin("LED", Pin.OUT)
while True:
if pir.value():
print("움직임 감지!")
led.on()
else:
led.off()
time.sleep(0.2)
PROJECT 22
기울기 센서
from machine import Pin
import time
tilt = Pin(14, Pin.IN, Pin.PULL_UP)
while True:
if tilt.value() == 0:
print("기울어짐")
else:
print("정상")
time.sleep(0.5)
PROJECT 23
진동 센서 감지
from machine import Pin
import time
vibration = Pin(14, Pin.IN)
while True:
if vibration.value():
print("진동 발생!")
time.sleep(0.1)
PROJECT 24
자석 리드 스위치
from machine import Pin
import time
door = Pin(14, Pin.IN, Pin.PULL_UP)
while True:
if door.value():
print("문 열림")
else:
print("문 닫힘")
time.sleep(0.5)
PROJECT 25
서보모터 각도 제어
from machine import Pin, PWM
import time
servo = PWM(Pin(15))
servo.freq(50)
def angle(a):
min_duty = 1638
max_duty = 8192
duty = int(min_duty + (max_duty - min_duty) * a / 180)
servo.duty_u16(duty)
while True:
for a in [0, 45, 90, 135, 180]:
angle(a)
time.sleep(1)
PROJECT 26
가변저항으로 서보 제어
from machine import Pin, PWM, ADC
import time
pot = ADC(26)
servo = PWM(Pin(15))
servo.freq(50)
while True:
raw = pot.read_u16()
duty = int(1638 + raw * (8192 - 1638) / 65535)
servo.duty_u16(duty)
print(raw, duty)
time.sleep(0.05)
PROJECT 27
릴레이 제어
from machine import Pin
import time
relay = Pin(15, Pin.OUT)
while True:
relay.on()
print("릴레이 ON")
time.sleep(2)
relay.off()
print("릴레이 OFF")
time.sleep(2)
PROJECT 28
7세그먼트 숫자 표시
from machine import Pin
import time
pins = [Pin(i, Pin.OUT) for i in range(2, 9)]
nums = {
0:[1,1,1,1,1,1,0], 1:[0,1,1,0,0,0,0],
2:[1,1,0,1,1,0,1], 3:[1,1,1,1,0,0,1],
4:[0,1,1,0,0,1,1], 5:[1,0,1,1,0,1,1],
6:[1,0,1,1,1,1,1], 7:[1,1,1,0,0,0,0],
8:[1,1,1,1,1,1,1], 9:[1,1,1,1,0,1,1]
}
while True:
for n in range(10):
for p, v in zip(pins, nums[n]):
p.value(v)
time.sleep(1)
PROJECT 29
I2C 주소 검색
from machine import Pin, I2C
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
print("I2C 주소:", i2c.scan())
PROJECT 30
OLED에 글자 출력
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(0, sda=Pin(0), scl=Pin(1))
oled = SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.text("Pico WH", 0, 0)
oled.text("MicroPython", 0, 16)
oled.show()
PROJECT 31
OLED에 센서값 표시
from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
import time
i2c = I2C(0, sda=Pin(0), scl=Pin(1))
oled = SSD1306_I2C(128, 64, i2c)
pot = ADC(26)
while True:
value = pot.read_u16()
oled.fill(0)
oled.text("ADC Value", 0, 0)
oled.text(str(value), 0, 20)
oled.show()
time.sleep(0.3)
PROJECT 32
BMP280 기압 센서
from machine import Pin, I2C
import time
import bmp280
i2c = I2C(0, sda=Pin(0), scl=Pin(1))
bmp = bmp280.BMP280(i2c)
while True:
print("온도:", bmp.temperature)
print("기압:", bmp.pressure)
time.sleep(1)
PROJECT 33
MPU6050 가속도 읽기
from machine import Pin, I2C
import time
from mpu6050 import MPU6050
i2c = I2C(0, sda=Pin(0), scl=Pin(1))
mpu = MPU6050(i2c)
while True:
acc = mpu.read_accel_data()
print(acc)
time.sleep(0.5)
PROJECT 34
RFID 카드 UID 읽기
from machine import Pin, SPI
import mfrc522
import time
reader = mfrc522.MFRC522(spi_id=0, sck=18, miso=16, mosi=19, cs=17, rst=20)
while True:
stat, tag_type = reader.request(reader.REQIDL)
if stat == reader.OK:
stat, uid = reader.SelectTagSN()
if stat == reader.OK:
print("카드 UID:", uid)
time.sleep(0.2)
PROJECT 35
UART GPS 데이터 읽기
from machine import UART, Pin
import time
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
while True:
if uart.any():
line = uart.readline()
print(line)
time.sleep(0.1)
PROJECT 36
블루투스 HC-05 UART
from machine import UART, Pin
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
while True:
if uart.any():
msg = uart.readline()
print("받음:", msg)
uart.write(b"OK\n")
PROJECT 37
네오픽셀 LED 8개 제어
from machine import Pin
import neopixel
import time
np = neopixel.NeoPixel(Pin(15), 8)
while True:
for i in range(8):
np.fill((0, 0, 0))
np[i] = (30, 0, 0)
np.write()
time.sleep(0.1)
PROJECT 38
소리 센서 감지
from machine import ADC
import time
sound = ADC(26)
while True:
value = sound.read_u16()
print("소리 크기:", value)
time.sleep(0.1)
PROJECT 39
화염 센서 경보
from machine import Pin
import time
flame = Pin(14, Pin.IN)
buzzer = Pin(16, Pin.OUT)
while True:
if flame.value() == 0:
print("화염 감지!")
buzzer.on()
else:
buzzer.off()
time.sleep(0.2)
PROJECT 40
가스 센서 MQ-2 읽기
from machine import ADC
import time
gas = ADC(26)
while True:
value = gas.read_u16()
print("가스 센서값:", value)
time.sleep(1)
PROJECT 41
빗물 감지 센서
from machine import ADC
import time
rain = ADC(27)
while True:
value = rain.read_u16()
print("빗물 감지값:", value)
time.sleep(1)
PROJECT 42
라인 트레이서 센서
from machine import Pin
import time
line = Pin(14, Pin.IN)
while True:
if line.value():
print("흰색 또는 반사 많음")
else:
print("검정선 감지")
time.sleep(0.2)
PROJECT 43
IR 장애물 감지
from machine import Pin
import time
ir = Pin(14, Pin.IN)
led = Pin("LED", Pin.OUT)
while True:
detected = ir.value() == 0
led.value(detected)
print("장애물:", detected)
time.sleep(0.2)
PROJECT 44
미니 DC 모터 제어
from machine import Pin
import time
motor = Pin(15, Pin.OUT)
while True:
motor.on()
print("모터 ON")
time.sleep(2)
motor.off()
print("모터 OFF")
time.sleep(2)
PROJECT 45
DC 모터 속도 조절
from machine import Pin, PWM
import time
motor = PWM(Pin(15))
motor.freq(1000)
while True:
for speed in range(0, 65535, 5000):
motor.duty_u16(speed)
time.sleep(0.2)
motor.duty_u16(0)
time.sleep(1)
PROJECT 46
Wi-Fi 연결 확인
import network
import time
ssid = "YOUR_WIFI"
password = "YOUR_PASSWORD"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
print("연결 중...")
time.sleep(1)
print("연결 완료:", wlan.ifconfig())
PROJECT 47
웹서버로 LED 제어
import network
import socket
from machine import Pin
import time
ssid = "YOUR_WIFI"
password = "YOUR_PASSWORD"
led = Pin("LED", Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(1)
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print("접속 주소:", wlan.ifconfig()[0])
while True:
cl, addr = s.accept()
request = cl.recv(1024).decode()
if "/on" in request:
led.on()
if "/off" in request:
led.off()
html = "<h1>Pico WH LED</h1><a href='/on'>ON</a><br><a href='/off'>OFF</a>"
cl.send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
cl.send(html)
cl.close()
PROJECT 48
웹페이지에 온습도 표시
import network, socket, time
from machine import Pin
import dht
ssid = "YOUR_WIFI"
password = "YOUR_PASSWORD"
sensor = dht.DHT11(Pin(15))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(1)
s = socket.socket()
s.bind(("0.0.0.0", 80))
s.listen(1)
print("IP:", wlan.ifconfig()[0])
while True:
cl, addr = s.accept()
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
page = f"<h1>Pico WH Weather</h1><p>Temp: {t} C</p><p>Humidity: {h}%</p>"
cl.recv(1024)
cl.send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
cl.send(page)
cl.close()
PROJECT 49
IFTTT 웹훅 알림
import network, urequests, time
ssid = "YOUR_WIFI"
password = "YOUR_PASSWORD"
event = "pico_alert"
key = "YOUR_IFTTT_KEY"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(1)
url = f"https://maker.ifttt.com/trigger/{event}/with/key/{key}"
data = {"value1": "Pico WH", "value2": "Sensor Alert", "value3": "ON"}
response = urequests.post(url, json=data)
print(response.text)
response.close()
PROJECT 50
ThingSpeak로 센서값 전송
import network, urequests, time
from machine import ADC
ssid = "YOUR_WIFI"
password = "YOUR_PASSWORD"
api_key = "YOUR_THINGSPEAK_WRITE_KEY"
sensor = ADC(26)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(1)
while True:
value = sensor.read_u16()
url = f"https://api.thingspeak.com/update?api_key={api_key}&field1={value}"
r = urequests.get(url)
print("전송:", value, r.text)
r.close()
time.sleep(20)