Jun 16, 2013

Make TMP36 work on Beaglebone Black

Here is how to use the Adadfruit temperature sensor TMP36 (https://www.sparkfun.com/products/10988) on Beaglebone black for reading temperature
1) Wiring the TMP36 to BBB
Refer the following picture for TMP36 pin output

Then we need to
- Connect 1st pin to port 32 of P9 expansion slot (3.3V output)
- Connect 3rd pin to port 1 of P9 expansion slot
- Connect 2nd pin to port 39 (AIN0) of the P9 expansion slot
You can connect the 2nd pin to AIN another port, refer following image for how it is designed



2) Enable AIN on the BBB
We need to enable the AIN driver, by doing following command on the BBB console
# echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots
The above might need a kernel module BONE_CAPE or bone_iio_helper

3) Read the input
Now we can read the AIN input by cat it, for example
root@arm:~# cat /sys/devices/ocp.2/helper.14/AIN0
287
But the AIN0 give me some weird value, I need to use the voltage0_raw under "/sys/bus/iio/devices/iio\:device0/in_voltage0_raw";
root@arm:~# cat /sys/bus/iio/devices/iio\:device0/in_voltage0_raw
1767
with a C application

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

//const char AIN_DEV[] = "/sys/devices/ocp.2/helper.14/AIN0";
const char AIN_DEV[] = "/sys/bus/iio/devices/iio\:device0/in_voltage0_raw";

double CtoF(double c)
{
  return (c * 9.0 / 5.0) + 32.0;
}

double temperature(char *string)
{
  int value = atoi(string);
  double millivolts = (value / 4096.0) * 1800;
  double temperature = (millivolts - 500.0) / 10.0;
  return temperature;
}

int main()
{
  int fd = open(AIN_DEV, O_RDONLY);

  while (1)
  {
    char buffer[1024];
    int ret = read(fd, buffer, sizeof(buffer));
    if (ret != -1)
    {
      buffer[ret] = '\0';
      double celsius = temperature(buffer);
      double fahrenheit = CtoF(celsius);
      printf("digital value: %s  celsius: %f  fahrenheit: %f\n", buffer, celsius, fahrenheit);
      lseek(fd, 0, 0);
    }
    sleep(1);
  }

  close(fd);
  return 0;
}

The convert formula is as following
Step Description Example
1 Read the digital value from the ADC interface. #cat
/sys/devices/ocp.2/helper.14/AIN0
1670
2 Convert the digital value to millivolts.
(value / 4096) * 1800mV
(1670 / 4096) * 1800mV = 733.8867mV
3 Convert the millivolts to Celsius temperature.
(millivolts - 500mV) / 10
(733.8867mV – 500mv) / 10 = 23.38867°C
4 Convert Celsius to Fahrenheit.
(Celsius * 9.0 / 5.0) + 32.0
(23.38867°C * 9 / 5) + 32 = 74.09961°F


And here is my wiring


Ref:

Post a Comment