본문 바로가기

Raspberry Pi Pico C 프로그래밍 - 온보드 온도 센서

webnautes 2024. 8. 4.
반응형

Raspberry Pi Pico에서 보드에 장착된 온도 센서의 측정 결과를 터미널에 문자열로 전송하는 예제입니다. 



2024. 8. 4 최초작성



진행하기 전에 다음 포스트를 먼저 진행해야 합니다. 



Windows에 Raspberry Pi Pico C 개발 환경 만들기

 

https://webnautes.tistory.com/2092




1.다음 파일을 다운로드하여 앞에서 진행한 pico 폴더의 다음 위치에 압축을 풀어둡니다.

폴더 이름을 onboard_temperature라고 이름을 바꾸었습니다.

 

https://github.com/webnautes/nudapeu/blob/master/project1.zip 

 




2. 다음 위치에 있는 onboard_temperature.c를 복사하여 

 



다음 위치에 붙여넣기합니다.

 

D:\work\pico\onboard_temperature

 




3. CMakeLists.txt 파일을 열어서 다음 부분을 수정합니다. 

 

cmake_minimum_required(VERSION 3.12)
include(pico_sdk_import.cmake)

set(projname "onboard_temperature")

project(${projname} C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

pico_sdk_init()

#include(example_auto_set_url.cmake)

add_executable(${projname}
        onboard_temperature.c
        )

target_link_libraries(${projname} pico_stdlib  hardware_adc)


# enable usb output, disable uart output
pico_enable_stdio_usb(${projname} 1)
pico_enable_stdio_uart(${projname} 0)

pico_add_extra_outputs(${projname})






4. 윈도우 키를 누른 후, “ Developer Command”을 입력하여 Developer Command Prompt for VS 2022를 실행합니다. 



5. onboard_temperature 폴더에 있는 build 폴더로 이동합니다. 

 

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools>d:

 

D:\>cd work\pico\onboard_temperature\build

 

D:\work\pico\onboard_temperature\build>




6. 다음 두 명령을 차례로  실행합니다.

 

cmake -G "NMake Makefiles" ..  

 

nmake 




7. 윈도우 탐색기로  onboard_temperature 아래에 있는 build 폴더로 이동하면  onboard_temperature.uf2 파일을 찾을 수 있습니다.  

 



이 파일을 앞에서 한 방식대로 pico로 복사하면 됩니다. 

 

pico의 BOOTSEL 버튼을 누른 채 PC에 연결합니다.

 hello.uf2를 pico를 연결해 추가된 외장 저장공간으로 복사합니다. 




8. 장치관리자에서 확인해보면 COM포트가 추가된 것을 볼 수 있습니다. 

 




9. 시리얼 터미널에서 확인해보면 측정된 온도가 일정한 간격으로 출력됩니다. 

 




소스코드입니다.

 

/**
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"

/* Choose 'C' for Celsius or 'F' for Fahrenheit. */
#define TEMPERATURE_UNITS 'C'

/* References for this implementation:
* raspberry-pi-pico-c-sdk.pdf, Section '4.1.1. hardware_adc'
* pico-examples/adc/adc_console/adc_console.c */
float read_onboard_temperature(const char unit) {
   
    /* 12-bit conversion, assume max value == ADC_VREF == 3.3 V */
    const float conversionFactor = 3.3f / (1 << 12);

    float adc = (float)adc_read() * conversionFactor;
    float tempC = 27.0f - (adc - 0.706f) / 0.001721f;

    if (unit == 'C') {
        return tempC;
    } else if (unit == 'F') {
        return tempC * 9 / 5 + 32;
    }

    return -1.0f;
}

int main() {
    stdio_init_all();
#ifdef PICO_DEFAULT_LED_PIN
    gpio_init(PICO_DEFAULT_LED_PIN);
    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
#endif

    /* Initialize hardware AD converter, enable onboard temperature sensor and
    *   select its channel (do this once for efficiency, but beware that this
    *   is a global operation). */
    adc_init();
    adc_set_temp_sensor_enabled(true);
    adc_select_input(4);

    while (true) {
        float temperature = read_onboard_temperature(TEMPERATURE_UNITS);
        printf("Onboard temperature = %.02f %c\n", temperature, TEMPERATURE_UNITS);

#ifdef PICO_DEFAULT_LED_PIN
        gpio_put(PICO_DEFAULT_LED_PIN, 1);
        sleep_ms(10);

        gpio_put(PICO_DEFAULT_LED_PIN, 0);
#endif
        sleep_ms(990);
    }
}



반응형

시간날때마다 틈틈이 이것저것 해보며 블로그에 글을 남깁니다.

블로그의 문서는 종종 최신 버전으로 업데이트됩니다.
여유 시간이 날때 진행하는 거라 언제 진행될지는 알 수 없습니다.



영화,책, 생각등을 올리는 블로그도 운영하고 있습니다.
https://freewriting2024.tistory.com


제가 쓴 책도 한번 검토해보세요 ^^

댓글