RP2040의 PWM를 이용해 DC 모터 속도를 제어하는 방법을 다룹니다. H-브리지 드라이버(L9110S, TB6612FNG 등)와 결합해 방향 제어도 가능합니다.
하드웨어 구성:
- Pico 보드, 5V DC 모터, 모터 드라이버(TB6612FNG 예), 외부 전원(모터용)
- PWM 핀 예: GP14 (PWM 슬라이스 7 채널 A)
배선 예시(TB6612FNG):
- PWMA -> GP14(PWM), AIN1->GP16, AIN2->GP17, VM->모터전원, GND 공통
예제 코드 (C, Pico SDK):
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#define PWM_PIN 14
#define IN1 16
#define IN2 17
void motor_dir(bool forward){
gpio_put(IN1, forward);
gpio_put(IN2, !forward);
}
int main(){
gpio_init(IN1); gpio_set_dir(IN1, GPIO_OUT);
gpio_init(IN2); gpio_set_dir(IN2, GPIO_OUT);
gpio_set_function(PWM_PIN, GPIO_FUNC_PWM);
uint slice = pwm_gpio_to_slice_num(PWM_PIN);
pwm_config cfg = pwm_get_default_config();
pwm_config_set_clkdiv(&cfg, 4.0f); // PWM 주파수 설정용
pwm_init(slice, &cfg, true);
while(true){
motor_dir(true);
for(int duty=0; duty<=255; duty++){
pwm_set_gpio_level(PWM_PIN, duty*257); // 16bit 스케일
sleep_ms(5);
}
sleep_ms(500);
motor_dir(false);
for(int duty=255; duty>=0; duty--){
pwm_set_gpio_level(PWM_PIN, duty*257);
sleep_ms(5);
}
sleep_ms(500);
}
}
설명: pwm_set_gpio_level은 16비트 레벨을 받으므로 8비트 듀티를 257배로 확장해 사용했습니다. 외부 전원과 GND 공통, 드라이버 최대 전류를 반드시 확인하세요.
이 글에서는 RP2040(Pico)로 USB HID 키보드를 구현하는 방법을 소개합니다. Pico SDK의 TinyUSB 스택을 사용하며, 버튼 입력을 USB 키코드로 전송합니다.
하드웨어 구성:
- Raspberry Pi Pico 보드
- 푸시버튼 1~3개 (GPIO 2,3,4 예시)
- 풀다운 저항 또는 내부 풀다운 사용
CMakeLists.txt에 TinyUSB 활성화:
- pico_sdk_init() 이후 tinyusb_device 추가
- target_link_libraries에 tinyusb_device, tinyusb_board 링크
예제 코드 (C, Pico SDK + TinyUSB):
#include "pico/stdlib.h"
#include "bsp/board.h"
#include "tusb.h"
#define BTN1 2
#define BTN2 3
void send_key(uint8_t code) {
uint8_t keycode[6] = {0};
keycode[0] = code; // 한 개 키만 전송
tud_hid_keyboard_report(0, 0, keycode);
sleep_ms(10);
tud_hid_keyboard_report(0, 0, NULL); // 키 릴리즈
}
int main() {
board_init();
tusb_init();
gpio_init(BTN1); gpio_set_dir(BTN1, GPIO_IN); gpio_pull_down(BTN1);
gpio_init(BTN2); gpio_set_dir(BTN2, GPIO_IN); gpio_pull_down(BTN2);
while (true) {
tud_task();
if (tud_mounted()) {
if (gpio_get(BTN1)) send_key(HID_KEY_A);
if (gpio_get(BTN2)) send_key(HID_KEY_ENTER);
}
}
}
빌드/업로드:
- pico-sdk 예제 구조로 cmake .. && make
- UF2 드라이브로 복사하여 플래싱
설명: TinyUSB를 통해 HID 디스크립터와 리포트를 자동 처리하므로, 키코드 전송만 구현하면 간단히 테스트 키보드를 만들 수 있습니다.
RP2040는 Raspberry Pi 재단이 설계한 듀얼코어 Arm Cortex-M0+ 마이크로컨트롤러로, 133MHz까지 동작하며 264KB SRAM과 유연한 I/O, 그리고 PIO(Programmable I/O)라는 독특한 주변장치를 제공합니다.
주요 특징:
- CPU: 듀얼코어 Arm Cortex-M0+ (최대 133MHz)
- 메모리: 264KB SRAM, 외부 QSPI 플래시(일반적으로 2MB~16MB)
- 주변장치: PIO x2(각각 4 SM), PWM, I2C, SPI, UART, USB 1.1 Device/Host, ADC(12-bit, 5채널)
- 전력: 저전력 모드 지원, 온칩 온도 센서
- 개발 생태계: Pico SDK(C/C++), MicroPython, CircuitPython 등 다수
간단 예제: 온보드 LED 깜박이기 (Pico SDK C)
#include <stdio.h>
#include "pico/stdlib.h"
int main() {
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while (true) {
gpio_put(LED_PIN, 1);
sleep_ms(500);
gpio_put(LED_PIN, 0);
sleep_ms(500);
}
}
설명: 듀얼코어 구조와 PIO 덕분에, RP2040은 저가 보드임에도 고성능 I/O 타이밍 제어와 멀티태스킹이 가능해 교육, 취미, 상용 프로토타이핑까지 폭넓은 용도에 적합합니다.