Display address of a DS18B20 on LCD display

The goal is to display the address of a DS18B20 on LCD display. Parts: Nano, 20 * 4 LCD display, and 1602 LCD Display IIC I2C Adapter IIC Serial Interface Adapter. Some code used from Arduino 1-Wire Address Finder . The problem is 5th, 6th, and 7th set of numbers get shortened to 3 digits, when they should be 4 digits. See picture.
The correct address for this sensor is 0x28, 0xB7, 0x25, 0x2A, 0xOD, 0x00, 0x00, 0xCD . The display works properly with other sketchs.
DS18B20.ino (1.1 KB)


lcd.print("0x");
if( addr[i] < 0x10)
  lcd.print( "0");
lcd.print(addr[i], HEX);
1 Like

sprintf can be used to format complete strings for display on an LCD

byte addr [] = { 0x28, 0xB7, 0x25, 0x2A, 0x0D, 0x00, 0x00, 0xCD };

char s [80];

void
setup ()
{
    Serial.begin (9600);

    sprintf (s, " %02x %02x %02x %02x %02x %02x %02x %02x",
        addr [0], addr [1], addr [2], addr [3],
        addr [4], addr [5], addr [6], addr [7] );
    Serial.println (s);

    sprintf (s, " 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
        addr [0], addr [1], addr [2], addr [3],
        addr [4], addr [5], addr [6], addr [7] );
    Serial.println (s);


    sprintf (s, " 0x%02x 0x%02x 0x%02x 0x%02x",
        addr [0], addr [1], addr [2], addr [3]);
    Serial.println (s);
    sprintf (s, " 0x%02x 0x%02x 0x%02x 0x%02x",
        addr [4], addr [5], addr [6], addr [7]);
    Serial.println (s);
}

void
loop ()
{
}

outputs

 28 b7 25 2a 0d 00 00 cd
 0x28 0xb7 0x25 0x2a 0x0d 0x00 0x00 0xcd
 0x28 0xb7 0x25 0x2a
 0x0d 0x00 0x00 0xcd
1 Like

Thank you very much! Worked

Thank you for explaining sprintf. Good lesson.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.