Connecting ds18b20 to nrf9160dk

Hi, 

I am looking to connect the ds18b20 sensor to the nrf9160dk. I followed this post, but when I tried running it, I was getting a "create_nordic_project.py failed (1)" saying "error: cmake failed". 

I am a complete beginner when using the development kit so please bear with my questions. 

I have added "CONFIG_GPIO" in the prj.conf file, is there anything else I should be adding. 

Additionally, should I be making changes to the cmake file? The link above only mentioned the .c and .h file so I wasn't sure what else should be changed. 

If anyone could help me with this or could point me to resources that will help me solve this, I would really appreciate it. 

Update : The code is now running without any build errors but not printing any values. I had been using the hello_world sample to run it before which was giving the error, but then I tried switching to the blinky sample and running on that and it's building and running without any errors. (Would anybody know why this is).

My issue now is that it is not printing any values or anything to the Debug terminal on SES or the LTE Link Monitor. Does anyone know why this may be?

  • Hello Nandini,

    My issue now is that it is not printing any values or anything to the Debug terminal on SES or the LTE Link Monitor. Does anyone know why this may be?

    The blinky sample does not print anything to the terminal by default, more than the booting sequence. Have you added any “printk/printf” or “LOG_xxx” line to it that won’t show up in the application log?

    Update : The code is now running without any build errors but not printing any values. I had been using the hello_world sample to run it before which was giving the error, but then I tried switching to the blinky sample and running on that and it's building and running without any errors. (Would anybody know why this is).

    This is hard to say without a build log. Can you build the hello_world sample via the command line and share the application log?

    Thanks & regards,

    Markus

  • Hi Markus, 

    I think you might have misunderstood. Essentially I am trying to read the input from a ds18b20 sensor (uses one-wire interface) using the nrf9160dk. I was using the code referred to in this post to try and read values from the sensor. I used the blinky sample as a base but replaced all the code in the original main file with the code in the post and added the ds18b20.c and ds18b20. files. But I am not seeing any output and I don't understand why.

    Do you happen to know why this might be? Additionally I was wondering if you knew how to print to the debug terminal. 

    I have added my files below (essentially the same as the one in the post)

    This is the main.c file

    //THIS IS THE MAIN.C FILE
    
    #include <drivers/gpio.h>
    #include <stdio.h>
    
    #include "ds18b20.h"
    
    //Datasheet: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
    
    static float temp = 0;
    
    void main()
    {
        ds18b20_setResolution(12);
    
        temp = ds18b20_get_temp_method_2();
        printf("Temperature: %.3f \r\n", temp);
    
    
        while(1)
        {
            k_sleep(K_MSEC(2000));
            temp = ds18b20_get_temp_method_2();
            printf("Temperature: %.3f \r\n", temp);
        }
    }
    
    
    /** @} */

    This is the ds18b20.h file

    #THIS IS ds18b20.h
    
    float ds18b20_get_temp(void);
    void ds18b20_setResolution(uint8_t resolution);
    float ds18b20_get_temp_method_2(void);
    /** @} */

    This is the ds18b20.c file

    //THIS IS ds18b20.c
    
    #include <drivers/gpio.h>
    
    #include "ds18b20.h"
    
    #define DS_PIN 1
    
    // Commands
    #define STARTCONVO      0x44
    #define READSCRATCH     0xBE
    #define WRITESCRATCH    0x4E
    
    // Scratchpad locations
    #define TEMP_LSB        0
    #define TEMP_MSB        1
    
    // Device resolution
    #define TEMP_9_BIT  0x1F //  9 bit
    #define TEMP_10_BIT 0x3F // 10 bit
    #define TEMP_11_BIT 0x5F // 11 bit
    #define TEMP_12_BIT 0x7F // 12 bit
    
    typedef uint8_t ScratchPad[9];
    static const struct device *gpio_dev;
    
    /**@brief Function for sending one bit to bus.
     */
    void ds18b20_send(char bit)
    {
        //nrf_gpio_cfg_output(DS_PIN);
        //nrf_gpio_pin_clear(DS_PIN);
        //nrf_delay_us(5);
    
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
        gpio_pin_set(gpio_dev, DS_PIN, 0);
        k_sleep(K_USEC(5));
    
        if(bit==1)
        {
            //nrf_gpio_pin_set(DS_PIN);
            gpio_pin_set(gpio_dev, DS_PIN, 1);
        }
        //nrf_delay_us(80);
        //nrf_gpio_pin_set(DS_PIN);
    
        k_sleep(K_USEC(80));
        gpio_pin_set(gpio_dev, DS_PIN, 1);
        
    }
    
    
    /**@brief Function for reading one bit from bus.
     */
    unsigned char ds18b20_read(void)
    {
        unsigned char presence=0;
        
        //nrf_gpio_cfg_output(DS_PIN);
        //nrf_gpio_pin_clear(DS_PIN);
        //nrf_delay_us(2);
    
        //nrf_gpio_pin_set(DS_PIN);;
        //nrf_delay_us(15);
    
        //nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL);
    
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
        gpio_pin_set(gpio_dev, DS_PIN, 0);
        k_sleep(K_USEC(2));
    
        gpio_pin_set(gpio_dev, DS_PIN, 1);
        k_sleep(K_USEC(15));
    
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT);
    
        //if(nrf_gpio_pin_read(DS_PIN))
        if (gpio_pin_get_raw(gpio_dev, DS_PIN))
        {
            presence = 1;
        }
        else
        {
            presence = 0;
        }
    
        printk("\npresence = %d", presence);
        
        return presence;
    }
    
    
    /**@brief Function for sending one byte to bus.
     */
    void ds18b20_send_byte(char data)
    {
        unsigned char i;
        unsigned char x;
        for(i=0;i<8;i++)
        {
          x = data>>i;
          x &= 0x01;
          ds18b20_send(x);
        }
        //nrf_delay_us(100);
        k_sleep(K_USEC(100));
    }
    
    
    /**@brief Function for reading one byte from bus.
     */
    unsigned char ds18b20_read_byte(void)
    {
        unsigned char i;
        unsigned char data = 0;
        for (i=0;i<8;i++)
        {
            if(ds18b20_read()) data|=0x01<<i;
            //nrf_delay_us(15);
            k_sleep(K_USEC(15));
        }
        return(data);
    }
    
    
    /**@brief Function for sending reset pulse.
     */
    unsigned char ds18b20_reset(void)
    {
      unsigned char presence;
    
      //nrf_gpio_cfg_output(DS_PIN);
      //nrf_gpio_pin_clear(DS_PIN);
    
      //nrf_delay_us(500);
      //nrf_gpio_pin_set(DS_PIN);
    
      //nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL); // usikkert p? pull her. m? sjekkes
      //nrf_delay_us(30);
    
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
        gpio_pin_set(gpio_dev, DS_PIN, 0);
    
        k_sleep(K_USEC(500));
        gpio_pin_set(gpio_dev, DS_PIN, 1);
    
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT);
        k_sleep(K_USEC(30));
    
        if(gpio_pin_get_raw(gpio_dev, DS_PIN) == 0)
        {
            presence = 1;
        }
        else
        {
            presence = 0;
        }
    
        //nrf_delay_us(470);
        k_sleep(K_USEC(470));
    
        if(gpio_pin_get_raw(gpio_dev, DS_PIN) == 1)
        {
            presence = 1;
        }
        else
        {
            presence = 0;
        }
    
      return presence;
    }
    
    
    /**@brief Function for reading temperature.
     */
    float ds18b20_get_temp(void)
    {
        unsigned char check;
        char temp1=0, temp2=0;
    
        check=ds18b20_reset();
        if(check)
        {
            ds18b20_send_byte(0xCC); //Skip ROM.
            ds18b20_send_byte(0x44); //Start temp conversion.
            //nrf_delay_ms(600UL);
            k_sleep(K_MSEC(600UL));
            check=ds18b20_reset();
            ds18b20_send_byte(0xCC); //Skip ROM.
            ds18b20_send_byte(0xBE); //Read temp from Scratchpad.
            temp1=ds18b20_read_byte();
            temp2=ds18b20_read_byte();
            check=ds18b20_reset();
            float temp=0;
            temp=(float)(temp1+(temp2*256))/16;
            return temp;
        }
          return 0;
    }
    
    
    /**@brief Function for reading bit.
     */
    uint8_t OneWire_read_bit(void)
    {
        uint8_t r;
    
        //nrf_gpio_cfg_output(DS_PIN);
        //nrf_gpio_pin_clear(DS_PIN);
        //nrf_delay_us(3);
        //nrf_gpio_cfg_input(DS_PIN,NRF_GPIO_PIN_NOPULL);
        //nrf_delay_us(10);
        //r =nrf_gpio_pin_read(DS_PIN);
        //nrf_delay_us(53);
    
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_OUTPUT_LOW);
        gpio_pin_set(gpio_dev, DS_PIN, 0);
        k_sleep(K_USEC(3));
        gpio_pin_configure(gpio_dev, DS_PIN, GPIO_INPUT);
        k_sleep(K_USEC(10));
        r = gpio_pin_get_raw(gpio_dev, DS_PIN);
        printk("\nr = %d", r);
        k_sleep(K_USEC(53));
        return r;
    }
    
    
    /**@brief Function for reading.
     */
    uint8_t OneWire_read()
    {
        uint8_t bitMask;
        uint8_t r = 0;
    
        for (bitMask = 0x01; bitMask; bitMask <<= 1) {
    	if ( OneWire_read_bit()) r |= bitMask;
        }
        return r;
    }
    
    
    /**@brief Function for reading scratchpad value
     */
    void ds18b20_readScratchPad(uint8_t *scratchPad, uint8_t fields)
    {
        ds18b20_reset();
        ds18b20_send_byte(0xCC);
        ds18b20_send_byte(READSCRATCH);
    
        for(uint8_t i=0; i < fields; i++)
        {
            scratchPad[i] = OneWire_read();
    
        }
        ds18b20_reset();
    }
    
    
    /**@brief Function for request temperature reading
     */
    void ds18b20_requestTemperatures(void)
    {
        ds18b20_reset();
        ds18b20_send_byte(0xCC);
        ds18b20_send_byte(STARTCONVO);
    }
    
    
    /**@brief Function for reading temperature method 2
     */
    float ds18b20_get_temp_method_2(void)
    {
        ds18b20_requestTemperatures();
        unsigned char check;
    
        ScratchPad scratchPad;
        ds18b20_readScratchPad(scratchPad, 2);
        int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 8) | scratchPad[TEMP_LSB];
        float temp = 0.0625 * rawTemperature;
    
        return temp;
    }
    
    
    /**@brief Function for setting temperature resolution
     */
    void ds18b20_setResolution(uint8_t resolution)
    {
        ds18b20_reset();
        ds18b20_send_byte(0xCC);
        ds18b20_send_byte(WRITESCRATCH);
        // two dummy values for LOW & HIGH ALARM
        ds18b20_send_byte(0);
        ds18b20_send_byte(100);
        switch (resolution)
        {
            case 12:
                ds18b20_send_byte(TEMP_12_BIT);
                break;
    
            case 11:
                ds18b20_send_byte(TEMP_11_BIT);
                break;
    
            case 10:
                ds18b20_send_byte(TEMP_10_BIT);
                break;
    
            case 9:
            default:
                ds18b20_send_byte(TEMP_9_BIT);
                break;
        }
        ds18b20_reset();
    }
    
    /*Funcion para inicializar GPIO*/
    
    void Init_GPIO(){
    
        gpio_dev = device_get_binding("GPIO_0");
        if (!gpio_dev) {
    	    printk("Error getting GPIO_0: failed.\n");
        }
    
    }

    This is the prj.conf file

    CONFIG_GPIO=y

    Ultimately I want to read the sensor values at regular intervals and print the values to the debug terminal/lte link monitor. Is there something wrong with the code. 

    Additionally I noticed that I was not getting 5V output from the 5V pin on the nrf9160dk. Do I need to add a configuration in the prj.conf file to enable this?

  • Hello Nandini,

    nrffan18 said:
    I think you might have misunderstood.

    My apologies for misunderstanding you.

    nrffan18 said:
    Do you happen to know why this might be?

    I assume that the devicetree configuration for the GPIO is not completely correct. In your main.c, I for instance can’t see that the necessary device binding in the Init_GPIO() function is called. This should at least trigger a warning during compilation, when using a not initialised device with the gpio_pin_configure() function.

    nrffan18 said:
    Additionally I was wondering if you knew how to print to the debug terminal. 

    I recommend you to use the printk command, as illustrated in the Hello World sample.

    nrffan18 said:
    Additionally I noticed that I was not getting 5V output from the 5V pin on the nrf9160dk. Do I need to add a configuration in the prj.conf file to enable this?

    What voltage do you measure on the 5V pin?

    Regards,

    Markus

  •  Hi Markus,

    What is the correct format for the GPIO device tree and how do I go about adding it correctly in a way that doesn't mess with anything else?

    I'm assuming I should be adding it with the label "GPIO_0" since that was what was configured in the initial function in ds18b20.c file?

  • Also looks like the voltage on the 5V pin is correct. Apologies for that

Related