2015년 7월 15일 수요일

[4th Week] Sensor Test

1) Simple Test
First, we can send sensor data from arduino to RPi with serial connection. I tested this with an temperature sensor in Arduino kit.


Here's circuit figure of temperature sensor and Arduino board. Resistor is 10k Ohm. Here's sketch code on Arduino below.

#define analogPin A0
#define beta 4090
#define resistance 10
void setup(){
  serial.begin(9600);
}
void loop(){
  long a = analogRead(analogPin);
  float tempC = beta/(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 173.0;
  Serial.println(tempC);
  delay(200);
}

Here's the result of serial output. Unit of temperature is Celsius. I could check the temperature changes when I touch the sensor. We can read these data on RPi with same method used in serial checking.

2) WiringPi Library
We used python to get data from Arduino on RPi. We can also get data from Arduino on RPi with WiringPi Library on C language. Here's link about instruction for installing the library.
http://wiringpi.com/download-and-install/

Here's sketch code on Arduino below.

#define analogPin A0
#define beta 4090
#define resistance 10
void setup(){
  serial.begin(9600);
}
void loop(){
  long a = analogRead(analogPin);
  float tempC = beta/(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 173.0;
  Serial.print(tempC);
  delay(2000);
}

Here's sample C code on RPi below.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>

int main(){
  int fd;
  char data[6];
  double temperature;
  int i;

  if((fd = serialOpen("/dev/ttyACM0",9600))<0){
    fprintf(stderr, "Unable to open serial device : %s\n", strerror(errno));
    return 1;
  }
  while(1){
    for(i=0;i<6;i++){
      data[i] = '\0';
    }
    for(i=0;i<5;i++){
      data[i] = serialGetchar(fd);
    }
    temperature = atof(data);
    printf("Arduino > RPi = %.2lf\n", temperature);
    ffluch(stdout);
  }
  return 0;
}

Example of compile command is like gcc sertest1.c -o prog -lwiringPi.
Here's a screenshot of result.
This result is also from temperature sensor I used before, and using Celsius unit. It seems it works quite well. This is not a clear code, but I could check the communication works well between Arduino and RPi with sensor data.





댓글 없음:

댓글 쓰기