Temperature Sensor Board with NuttX

Temperature sensors are used in various embedded applications for programming the system to give the desired temperature based response. The following example shows how Zilogic’s Temperature Sensor Board can be programmed using NuttX API. This board uses NXP’s SA56004 temperature sensor and has capabilities to drive external devices based on sensed temperature. To demonstrate this board with NuttX, we would be interfacing it with the ZKit-ARM-1769 board.

Block Diagram

The Temperature Sensor Board is interfaced to ZKit-ARM-1769 using a 10-pin FRC connector.

Interfacing Temperature sensor with ZNet
Figure 1. Block diagram

Program

The following program reads the room temperature stored in a register of the Temperature Sensor Board and displays it in the console.

#include <nuttx/config.h>
#include <nuttx/i2c.h>
#include <stdio.h>

/* I2C Device Address */
#define TEMP_SENSOR 0x48

/* Register Addresses */
#define LTHB    0x00

#define I2C_PORT 0
#define FREQUENCY 100000
#define ADDRESS_BITS 7

struct i2c_dev_s *i2c_init(int port, uint32_t frequency, int addr, int nbits)
{
        uint32_t result;
        struct i2c_dev_s *i2c;

        i2c = up_i2cinitialize(I2C_PORT);
        if (i2c == NULL) {
                printf("nuttx-i2c: Error initializing I2C: %d\n", i2c);
                exit(0);
        }
        printf("nuttx-i2c: I2C Successfully initialized\n");

        I2C_SETFREQUENCY(i2c, frequency);
        result = I2C_SETADDRESS(i2c, addr, nbits);
        if (result != OK) {
                printf("nuttx-i2c: Wrong I2C Address.\n");
                exit(0);
        }
        return i2c;
}

int app_main(void)
{
        int result;
        uint8_t lthb_reg = LTHB;
        uint8_t temp;
        struct i2c_dev_s *i2c;

        i2c = i2c_init(I2C_PORT, FREQUENCY, TEMP_SENSOR, ADDRESS_BITS);

        result = I2C_WRITE(i2c, &lthb_reg, 1);
        if (result < 0) {
                printf("nuttx-i2c: Error Writing. Terminating\n");
                return 1;
        }

        result = I2C_READ(i2c, &temp, 1);
        if (result < 0) {
                printf("nuttx-i2c: Error Reading. Terminating\n");
                return 1;
        }

        printf("Temperature = %d\n", temp);
}

The initialization of the I2C device involves the following operations:

  • up_i2cinitialize() function initializes the I2C port and returns a structure struct i2c_dev_s. This structure is then used by other higher level APIs.

  • I2C_SETFREQUENCY() is used to set the frequency of operation to 100 KHz.

  • I2C_SETADDRESS() sets the slave address of the I2C device to 0x48.

The sensed room temperature is stored in a register called Local Temperature High Byte Register(LTHB).

The steps to read the local temperature are as follows:

  • Write the address of the register to be read i.e LTHB register address 0x00 using the I2C_WRITE() function.

  • A subsequent read operation using I2C_READ() function returns the local temperature stored in the LTHB register.

Build and Flash

The following commands are executed to compile and flash the program into ZKit-ARM-1769.

$ flags=$(nuttx-config --cflags --libs zkit-arm-1769)
$ arm-none-eabi-gcc tmpsen.c $flags -o tmpsen.elf
$ arm-none-eabi-objcopy tmpsen.elf --target=ihex tmpsen.hex
$ lpc21isp -control tmpsen.hex /dev/ttyUSB0 115200 12000

Credits

Thanks to Visual Pharm for the Thermometer Icon.