LM75 sensor/Using it/08. The first reading
Using it · 08 of 9

The first reading

Four wires and a sketch that prints the temperature twice a second, with no library to install. It asks for register 0, reads two bytes into a signed 16-bit number and divides by 256. Hold a finger on the chip and the number climbs.

Four wires

Four wires
Your board
VCC to
3V3
SDA, SCL to
GPIO 8, GPIO 9
Address
0x48
GND to GND, VCC to 3V3, SDA to GPIO 8, SCL to GPIO 9: the ESP32-S3's default I2C pins, so Wire.begin() needs nothing in its brackets. Not 5V: the pull-ups would then hold two of the ESP32-S3's pins at 5 V.

GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the others. SDA and SCL to your board's I2C pins: A4 and A5 on an Uno, GPIO 21 and 22 on an ESP32, GPIO 8 and 9 on an ESP32-S3, GP4 and GP5 on a Pico.

Those are each board's default I2C pins, so Wire.begin() needs nothing in its brackets. They are the same pins the SHT31 and EEPROM books use, so the three blocks can share one set of four wires.

The sketch

readCelsius writes one byte, 0x00, to point the chip at its temperature register, then asks for two bytes back. If the chip does not acknowledge, or fewer than two bytes arrive, it returns false instead of a made-up number.

The two bytes go into an int16_t, high byte first, and the result over 256 is degrees. Degrees in two bytes explains why that one line is right above and below zero.

setup() reads once and throws the answer away, because the first reading after power-up is not to be trusted. Then loop() prints twice a second.

What you should see

At 115200 the serial monitor prints a line like this every half second, the number being whatever the chip measures:

23.625 C
23.625 C
23.750 C

The last digits move in eighths, .000, .125, .250 and so on, or in sixteenths if your chip is the 12-bit part UMW's datasheet describes. Hold a finger on the chip and the number climbs within a second or two. If it says No LM75 at 0x48, the chip did not acknowledge: check the four wires, counting from the square pad.

The code

No library to install: Wire comes with every board. readCelsius is the whole conversation with the chip: register 0, two bytes back, one line of arithmetic. Change nothing but the wiring.

lm75_first_reading.ino
/*
  LM75 Temperature Sensor - first reading           TK42 / /p/tk42

  Wiring. Count from the square pad on the TinkerBlock board,
  parts up, header at the bottom:

    GND -> GND
    VCC -> your board's logic supply: 5V on an Uno, 3V3 on an
           ESP32 or ESP32-S3, 3V3(OUT) on a Pico. Never 5V
           beside a 3.3 V board.
    SDA -> A4 on an Uno, GPIO 21 on an ESP32, GPIO 8 on an
           ESP32-S3, GP4 on a Raspberry Pi Pico
    SCL -> A5 on an Uno, GPIO 22 on an ESP32, GPIO 9 on an
           ESP32-S3, GP5 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board            your board, e.g. Arduino Uno
    Tools > Port             the one that appears when you plug in
    Tools > USB CDC On Boot  Enabled   (ESP32-S3 only)
    No library to install: Wire comes with every board.
    Serial Monitor at 115200.
*/

#include <Wire.h>

const int LM75_ADDR = 0x48;  // fixed: A0 to A2 are tied to GND

// True, and the temperature in c, if the chip answered.
bool readCelsius(float &c) {
  Wire.beginTransmission(LM75_ADDR);
  Wire.write(0x00);                    // register 0: temperature
  if (Wire.endTransmission() != 0) return false;
  if (Wire.requestFrom(LM75_ADDR, 2) != 2) return false;
  uint8_t hi = Wire.read();            // whole degrees
  uint8_t lo = Wire.read();            // the fraction
  int16_t raw = (int16_t)((hi << 8) | lo);
  c = raw / 256.0;                     // right below zero too
  return true;
}

void setup() {
  Serial.begin(115200);
  delay(1000);                         // time for the monitor
  Wire.begin();                        // the default I2C pins
  float c;
  readCelsius(c);                      // the first one is thrown away
}

void loop() {
  float c;
  if (readCelsius(c)) {
    Serial.print(c, 3);
    Serial.println(" C");
  } else {
    Serial.println("No LM75 at 0x48: check SDA and SCL.");
  }
  delay(500);
}

The int16_t is what makes it right below zero on every board. Code that puts the bytes in an int and shifts them right by five works on an Uno and prints about 250 on an ESP32 or a Pico once the temperature drops under zero.

View on GitHub · blocks/tk42-lm75/arduino/lm75_first_reading/lm75_first_reading.ino @ v1.0

When it does not work

It prints No LM75 at 0x48.

The chip did not acknowledge. Count from the square pad: GND, VCC, SDA, SCL, and check SDA and SCL are not swapped: SDA is the third pin. Then check VCC is on your board's logic supply and actually connected.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print.

The numbers are garbage, not an error.

Set the serial monitor to 115200 baud, the speed in Serial.begin. At any other speed the characters arrive scrambled. If the baud is right and the numbers still jump about, check nothing else on the bus is at 0x48.

It prints about 25 °C and does not move when I warm the room.

It will, slowly: the board takes something like a minute to follow the air. A finger on the chip is the quick test, and the number should start to climb within a second or two.

Where this goes next

No answer, a zero, 250 degrees in the cold, and a board that runs warm.

When the reading is wrong →

Edit this page — content/books/lm75/the-first-reading.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

LM75 Temperature Sensor

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →