반응형

토양 수분 센서를 라즈베리 파이와 함께 사용해 보자.

 

라즈베리 파이는 아날로그 신호를 입력 받을 수 없기 때문에 ADC(MCP3008)를 사용해야 한다.

 

Analog-Digital Converter

 

라즈베리 파이 - ADC - 토양 수분 센서를 연결한다.

 

 

ADC            -       Raspberry Pi

VDD                                5V
VREF                               5V
AGND                            Ground
CLK                           GPIO11(SCLK)
DOUT                         GPIO9(MISO)
DIN                            GPIO10(MOSI)
CS/SHDN                    GPIO8(CE0)
DGND                            Ground

 

Soil Moisture Sensor   -   ADC(Raspberry Pi)

VCC                                       (5V)

GND                                    (Ground)

A0                                         CH0

 

라즈베리 파이에서 raspi-config를 실행한다.

 

 

3 Interface Options를 선택한다.

 

I4 SPI를 선택한다.

 

YES를 선택한다.

 

SPI가 활성화 된다.

 

 

git이 설치되어 있지 않다면 설치한다.

 

py-spidev를 다운 받는다.

 

다운받은 py-spidev 디렉토리로 이동하고 설치한다

 

수분량을 감지하는 소스를 입력하고 저장한다.

 

 

소스를 실행하면 센서에서 읽은 값이 표시된다.

※ 참고

Python Spidev

 

반응형
Posted by J-sean
:
반응형

숫자(1~99,999)와 디스플레이 크기(1~10) s를 입력하면 [(2s+3)행 X (s+2)열] 크기의 디지털 숫자로 표시하는 프로그램을 만들어 보자. 각 숫자 사이에는 1줄의 공백이 있다.


숫자 5,238을 s=1의 크기로 표시한 예.


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
 
using namespace std;
 
int main()
{
    const int MAX_DIGIT = 5;
    const int MAX_SCALE = 10;
 
    char hor_bar[2][MAX_SCALE + 1= { {"          "}, {"----------"} };
    char ver_bar[2][2= { {" "}, {"|"} };
 
    char hor_dig[10][3= { {101}, {000}, {111}, {111}, {010},
        {111}, {111}, {100}, {111}, {111} };    // 0~9
    char ver_dig[10][4= { {1111}, {0101}, {0110}, {0101}, {1101},
        {1001}, {1011}, {0101}, {1111}, {1101} };    // 0~9
    //  -
    // | |
    //  -
    // | |
    //  -
    // 1 digit = 가로선 3개(3줄), 세로선 4개(2줄)
    //             최소 5행 3열
 
    char digits[5][(MAX_SCALE + 3* MAX_DIGIT] = { '\0', };    // 각 숫자 사이에 빈칸 + 마지막 개행 문자 = MAX_SCALE + 3.
                                                                // null문자 '\0'으로 초기화.
    char number[MAX_DIGIT + 1];
    cout << "Enter number(1~99,999): ";
    cin >> number;
 
    int scale;
    cout << "Enter scale(1~10): ";
    cin >> scale;
 
    // scale에 맞게 가로선 길이 조정.
    hor_bar[0][scale] = '\0';
    hor_bar[1][scale] = '\0';
 
    int count_number = strlen(number);
 
    for (int i = 0; i < count_number; i++)
    {
        int real_number = number[i] - '0';    // 문자를 숫자로 변환.
 
        // 두 번째 숫자부터 각 숫자마다 빈 칸 삽입.
        if (i > 0)
            for (int j = 0; j < 5; j++)
                strcat_s(digits[j], sizeof(digits[j]), " ");
 
        // 첫 번째 줄 삽입.
        strcat_s(digits[0], sizeof(digits[0]), " ");
        strcat_s(digits[0], sizeof(digits[0]), hor_bar[hor_dig[real_number][0]]);
        strcat_s(digits[0], sizeof(digits[0]), " ");
 
        // 두 번째 줄 삽입.
        strcat_s(digits[1], sizeof(digits[1]), ver_bar[ver_dig[real_number][0]]);
        strcat_s(digits[1], sizeof(digits[1]), hor_bar[0]);
        strcat_s(digits[1], sizeof(digits[1]), ver_bar[ver_dig[real_number][1]]);
 
        // 세 번째 줄 삽입.
        strcat_s(digits[2], sizeof(digits[2]), " ");
        strcat_s(digits[2], sizeof(digits[2]), hor_bar[hor_dig[real_number][1]]);
        strcat_s(digits[2], sizeof(digits[2]), " ");
 
        // 네 번째 줄 삽입.
        strcat_s(digits[3], sizeof(digits[3]), ver_bar[ver_dig[real_number][2]]);
        strcat_s(digits[3], sizeof(digits[3]), hor_bar[0]);
        strcat_s(digits[3], sizeof(digits[3]), ver_bar[ver_dig[real_number][3]]);
 
        // 다섯 번째 줄 삽입.
        strcat_s(digits[4], sizeof(digits[4]), " ");
        strcat_s(digits[4], sizeof(digits[4]), hor_bar[hor_dig[real_number][2]]);
        strcat_s(digits[4], sizeof(digits[4]), " ");
    }
 
    // 첫 번째 줄 출력
    cout << digits[0<< endl;
 
    // 두 번째 줄 출력
    for (int i = 0; i < scale; i++)
        cout << digits[1<< endl;
 
    // 세 번째 줄 출력
    cout << digits[2<< endl;
 
    // 네 번째 줄 출력
    for (int i = 0; i < scale; i++)
        cout << digits[3<< endl;
 
    // 다섯 번째 줄 출력
    cout << digits[4<< endl;
 
    return 0;
}


잘못된 입력에 대한 처리는 생략되어 있다.


실행 결과.



반응형
Posted by J-sean
: