반응형


아래 명령을 사용하여 우선 I2C를 활성화시킵니다.

sudo raspi-config  


8 Advanced Options을 선택하면 I2c를 활성화 시키는 옵션을 찾을 수 있습니다.



그리고나서 다음 파일을 수정하여 

sudo nano /etc/modules


다음 2줄을 추가해줍니다.

i2c-bcm2708

i2c-dev


이제 재부팅을 합니다.



부팅이 완료되면 i2c-tools 패키지를 설치합니다.

$sudo apt-get install i2c-tools




 아두이노에 다음 코드를 올려줍니다. 
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
#include <Wire.h>  
  
//I2C 주소 지정    
#define SLAVE_ADDRESS 0x04  
int number = 0;  
int state = 0;  
int temp;  
char retMSG[1024= "";  
  
const int ledPIN = 13;  
const int cdsPIN = 0;  
   
void setup() {  
   pinMode( ledPIN, OUTPUT);  
     
   //슬레이브로써 I2C를 초기화한다.  
   Wire.begin(SLAVE_ADDRESS);  
     
   //I2C 통신을 위한 콜백함수를 지정한다.  
   Wire.onReceive(receiveData); //데이터 수신용  
   Wire.onRequest(sendData);    //데이터 전송  
}  
  
   
void loop() {  
   delay(100);  
   temp = analogRead(cdsPIN);  
}  
   
  
void receiveData(int byteCount){  
   
 while(Wire.available()) {  
    number = Wire.read();  
     
    if (number == 1){  
         if (state == 0){  
             digitalWrite( ledPIN, HIGH ); // set the LED on  
             state = 1;  
             strcpy( retMSG, "Trun on LED\n");  
         } else{  
             digitalWrite(13, LOW); // set the LED off  
             state = 0;  
             strcpy(retMSG, "Turn off LED\n");  
           }  
     }     
    else if(number==2) {  
         sprintf( retMSG, "Light = %d\n", temp );  
    }  
    else{  
        sprintf( retMSG, "Invaild command\n" );  
    }  
      
      
   }  
}  
  
void sendData(){  
    Wire.write( retMSG, strlen(retMSG) );  
}  
 
 
cs



그리고나서 sudo i2cdetect -y 1명령으로 아두이노와 연결 전후를 확인해보면 차이가 있음을 알 수 있습니다.


라즈베리파이2와 아두이노는 아래처럼 연결해주면 됩니다. 

raspberry pi 2/3         Arduino UNO 

(I2C1 SCL)5      ------  A5(SCL)

(I2C1 SDA)3      ------  A4(SDA)

        GND6      ------  GND


 




라즈베리파이에서사용한 코드입니다. 아두이노에 명령을 줘서 LED를 껐다 켠다든가.. cds 값을 가져올 수 있습니다. 


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
#include <string.h>  
#include <unistd.h>  
#include <errno.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <linux/i2c-dev.h>  
#include <sys/ioctl.h>  
#include <fcntl.h>  
#include <unistd.h>  
  
   
//i2c address  
#define ADDRESS 0x04  
   
//I2C bus  
static const char *deviceName = "/dev/i2c-1";  
  
   
int main(int argc, char** argv)   
{  
    if (argc == 1)   
    {  
        printf"Usage:\n");  
        printf"%s 1 : led control\n", argv[0] );  
        printf"%s 2 : get temperature\n", argv[0] );  
        exit(1);  
    }  
  
  
    int file;  
   
    if ((file = open( deviceName, O_RDWR ) ) < 0)   
    {  
        fprintf(stderr, "I2C: Failed to access %s\n", deviceName);  
        exit(1);  
    }  
    printf("I2C: Connected\n");  
  
   
    printf("I2C: acquiring buss to 0x%x\n", ADDRESS);  
    if (ioctl(file, I2C_SLAVE, ADDRESS) < 0)   
    {  
        fprintf(stderr, "I2C: Failed to acquire bus access/talk to slave 0x%x\n", ADDRESS);  
        exit(1);  
    }  
  
   
    int arg;  
   
    for (arg = 1; arg < argc; arg++)   
    {  
        int val;  
        unsigned char cmd[16];  
   
        if (0 == sscanf(argv[arg], "%d", &val)) {  
            fprintf(stderr, "Invalid parameter %d \"%s\"\n", arg, argv[arg]);  
            exit(1);  
        }  
   
        printf("Sending %d\n", val);  
   
        cmd[0= val;  
        if (write(file, cmd, 1== 1)   
        {  
             
            usleep(10000);  
   
            char buf[1024], buf2[1024];  
            read( file, buf, 1024 );        
  
            int i;  
            for(i=0; i<1024; i++ )  
            {  
                buf2[i] = buf[i];  
  
                if ( buf[i] == '\n' )   
                {  
                    buf2[i]='\0';  
                    break;  
                }  
            }  
  
            printf"[%s]\n", buf2 );  
        }  
 
        usleep(10000);  
    }  
   
    close(file);  
  
    return 0;  
}  
cs






반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


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

+ Recent posts