<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89727/getting-temperature-from-ds18b20-with-nrf5340-dk</link><description>Hello everyone, 
 
 I am experiencing issues to get temperature from DS18B20 sensor with a nRF5340 board. 
 
 The probleme I have is that the only values my code reads is 0xFF. 
 
 I am using nRF Connect SDK v2.0.0. and here is my code : 
 
 Im am using</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 13 Jul 2022 14:27:10 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89727/getting-temperature-from-ds18b20-with-nrf5340-dk" /><item><title>RE: Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/thread/376810?ContentTypeID=1</link><pubDate>Wed, 13 Jul 2022 14:27:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7cb340a0-a1af-4b2a-ab3f-61abf501b425</guid><dc:creator>Elfving</dc:creator><description>&lt;p&gt;Great! I am glad to hear it. And no problem at all.&lt;/p&gt;
&lt;p&gt;And great that you&amp;#39;ve contributed by adding your code and solution here, so that it may help others down the line. It seems that there is a lot of people on DevZone that have been struggling with the same thing as you.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Elfving&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/thread/376715?ContentTypeID=1</link><pubDate>Wed, 13 Jul 2022 07:21:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:78669fc8-1db0-40ea-8938-b0c7d38d0a2e</guid><dc:creator>Sulian</dc:creator><description>&lt;p&gt;Hello Elfving,&lt;/p&gt;
&lt;p&gt;I looked at my signal with an oscilloscope and the issue was indeed the fault of k_sleep(K_USEC(timeout)) which couldn&amp;#39;t apparently work with microseconds (same problem with k_usleep(timeout)).&lt;/p&gt;
&lt;p&gt;I have seen somewhere that upgrading the value of CONFIG_SYS_CLOCK_TICKS_PER_SEC could have helped but the default value seems to already be the highest for nRF5340 (32768).&lt;/p&gt;
&lt;p&gt;So I tried to use timers :&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;K_TIMER_DEFINE(timer, NULL, NULL);

/* Do some work */

/* When a 15 usec wait is needed */
k_timer_start(&amp;amp;timer, K_USEC(15), K_NO_WAIT);

/* Wait the end of the timer */
k_timer_status_sync(&amp;amp;timer);

/* Continue the work */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;But it wasn&amp;#39;t working under a hundred of microseconds nor precise.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;At this moment I didn&amp;#39;t see the k_busy_wait function so I made my own timer function :&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void timer(uint64_t iTimeout)
{
    uint64_t iUptime = k_uptime_ticks();

    while ((k_uptime_ticks() - iUptime) &amp;lt; k_us_to_ticks_near64(iTimeout));
    
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And it works nicely.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I tried replacing my timer function with k_busy_wait and it&amp;#39;s still working perfectly !&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks you very much Eflving !&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If someone is interesting by my code, here it is :&lt;/p&gt;
&lt;p&gt;(I made it board agnostic by replacing the nrf_gpio function, and the k_busy_wait functions can be replace by the timer function).&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;kernel.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;

#include &amp;quot;ds18b20.h&amp;quot;

// #define DS_PIN 26
#define DS_PIN 10

// 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];

const struct device * dev;

/**@brief Custome timer.
 */
void timer(uint64_t iTimeout)
{
    uint64_t iUptime = k_uptime_ticks();

    while ((k_uptime_ticks() - iUptime) &amp;lt; k_us_to_ticks_near64(iTimeout));
    
}


/**@brief Function for sending one bit to bus.
 */
void ds18b20_send(char bit)
{
    dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
    gpio_pin_configure(dev, DS_PIN, GPIO_OUTPUT_INACTIVE);

    
    k_busy_wait(5);

    if(bit==1)
        gpio_pin_set(dev, DS_PIN, 1);
    
    k_busy_wait(80);
    
    gpio_pin_set(dev, DS_PIN, 1);
}


/**@brief Function for reading one bit from bus.
 */
unsigned char ds18b20_read(void)
{
    unsigned char presence=0;

    dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
    gpio_pin_configure(dev, DS_PIN, GPIO_OUTPUT_INACTIVE);

    k_busy_wait(2);

    gpio_pin_set(dev, DS_PIN, 1);
    k_busy_wait(15);

    gpio_pin_configure(dev, DS_PIN, GPIO_INPUT);

    if(gpio_pin_get(dev, DS_PIN))
        presence = 1;
    
    else
        presence = 0;
    
    
    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&amp;lt;8;i++)
    {
      x = data&amp;gt;&amp;gt;i;
      x &amp;amp;= 0x01;
      ds18b20_send(x);
    }

    k_busy_wait(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&amp;lt;8;i++)
    {
        if(ds18b20_read()) data|=0x01&amp;lt;&amp;lt;i;
        k_busy_wait(15);
    }

    return(data);
}


/**@brief Function for sending reset pulse.
 */
unsigned char ds18b20_reset(void)
{
    unsigned char presence;

    dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
    gpio_pin_configure(dev, DS_PIN, GPIO_OUTPUT_INACTIVE);

    k_busy_wait(500);
    
    gpio_pin_set(dev, DS_PIN, 1);

    gpio_pin_configure(dev, DS_PIN, GPIO_INPUT);
    k_busy_wait(30);


    if(gpio_pin_get(dev, DS_PIN) == 0)    
        presence = 1;   
    else    
        presence = 0;
    
    k_busy_wait(470);

    if(gpio_pin_get(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);
        ds18b20_send_byte(0x44);

        k_busy_wait(600);

        check=ds18b20_reset();
        ds18b20_send_byte(0xCC);
        ds18b20_send_byte(0xBE);

        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;

    dev = device_get_binding(&amp;quot;GPIO_0&amp;quot;);
    gpio_pin_configure(dev, DS_PIN, GPIO_OUTPUT_INACTIVE);

    k_busy_wait(3);

    gpio_pin_configure(dev, DS_PIN, GPIO_INPUT);

    k_busy_wait(10);

    r = gpio_pin_get(dev, DS_PIN);

    k_busy_wait(53);

    return r;
}


/**@brief Function for reading.
 */
uint8_t OneWire_read()
{
    uint8_t bitMask;
    uint8_t r = 0;

    for (bitMask = 0x01; bitMask; bitMask &amp;lt;&amp;lt;= 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 &amp;lt; 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();

    ScratchPad scratchPad;
    ds18b20_readScratchPad(scratchPad, 2);

    int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) &amp;lt;&amp;lt; 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 &amp;amp; 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();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks you again Elfving !&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Sulian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/thread/376651?ContentTypeID=1</link><pubDate>Tue, 12 Jul 2022 14:20:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1502bae-faeb-40ca-8598-8360293c6f92</guid><dc:creator>Elfving</dc:creator><description>&lt;p&gt;Hello again Sulian, thank you for your patience&lt;/p&gt;
[quote user=""]Im am using two different sources I found on internet [/quote]
&lt;p&gt;Just in case you didn&amp;#39;t notice: the first code snippet that you&amp;#39;ve found here is based on the nRF5 SDK, it might not work on NCS straight out of the box. &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/85189/connecting-ds18b20-to-nrf9160dk"&gt;But it seems that there are other people in devzone who has been in your shoes before and tried to migrate it to NCS.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user=""]&lt;p&gt;What I observe is that, during the reading part, it only reads high state values (=0xFF).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;[/quote]
&lt;p&gt;Yeah, it might be that it isn&amp;#39;t connected correctly though it is hard to say where the issue is exactly. Do you have a logic analyzer?&lt;/p&gt;
&lt;p&gt;Interestingly enough, the case I linked you to ran into a similar issue. Replacing his&amp;nbsp;&lt;span&gt;k_usleep with&amp;nbsp;k_busy_wait() ended up fixing it for him. Does that help in your case?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Elfving&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/thread/376115?ContentTypeID=1</link><pubDate>Fri, 08 Jul 2022 07:20:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ca86c46d-44d9-4c3b-b48e-be21b384dfa4</guid><dc:creator>Sulian</dc:creator><description>&lt;p&gt;Hello everyone,&lt;/p&gt;
&lt;p&gt;I come here with some news.&lt;/p&gt;
&lt;p&gt;I have tested the sensor with an Arduino Uno, by following this tuto : &lt;a id="" href="https://create.arduino.cc/projecthub/TheGadgetBoy/ds18b20-digital-temperature-sensor-and-arduino-9cc806"&gt;https://create.arduino.cc/projecthub/TheGadgetBoy/ds18b20-digital-temperature-sensor-and-arduino-9cc806&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here a pic of the hard : &lt;img style="max-height:145px;max-width:322px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/644x290/__key/communityserver-discussions-components-files/4/IMG_5F00_20220708_5F00_090858.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;And it works perfectly. So I am pretty sure the issue with the nRF5340 comes from the soft.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Sulian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/thread/376042?ContentTypeID=1</link><pubDate>Thu, 07 Jul 2022 14:01:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ead30c8b-2dbc-4e2a-bc42-034f01ab98d5</guid><dc:creator>Sulian</dc:creator><description>&lt;p&gt;Hello Elfving,&lt;/p&gt;
&lt;p&gt;Thanks you very much. It&amp;#39;s okay don&amp;#39;t worry.&lt;/p&gt;
&lt;p&gt;I also keep trying on my side and come here if I have any updates.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Sulian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Getting temperature from DS18B20 with nRF5340-DK</title><link>https://devzone.nordicsemi.com/thread/376038?ContentTypeID=1</link><pubDate>Thu, 07 Jul 2022 13:50:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:832600bf-cc3e-4b47-bbfc-78d4f61f3051</guid><dc:creator>Elfving</dc:creator><description>&lt;p&gt;Hello Sulian,&lt;/p&gt;
&lt;p&gt;I am looking into it. Note that w&lt;span&gt;e have entered the summer holiday period in Norway so staffing is low, some delayed response time should be expected.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Elfving&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>