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
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 CThe 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 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.0The same reading in MicroPython, for an ESP32, an ESP32-S3 or a Pico. readfrom_mem asks for register 0 and two bytes; struct turns them into a signed 16-bit number.
"""
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 -> 3V3 on an ESP32 or ESP32-S3, 3V3(OUT) on a Pico.
Never 5V: the pull-ups would put 5 V on your pins.
SDA -> GPIO 21 on an ESP32, GPIO 8 on an ESP32-S3,
GP4 on a Raspberry Pi Pico
SCL -> GPIO 22 on an ESP32, GPIO 9 on an ESP32-S3,
GP5 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Nothing to install: machine, struct and time are built in.
"""
import struct
import time
from machine import I2C, Pin
# SDA, SCL. ESP32: 21, 22. ESP32-S3: 8, 9. Pico: 4, 5.
i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=100_000)
LM75 = 0x48 # fixed: A0 to A2 are tied to GND
def celsius():
two = i2c.readfrom_mem(LM75, 0x00, 2) # register 0
return struct.unpack(">h", two)[0] / 256
time.sleep_ms(250)
celsius() # the first one is thrown away
while True:
print("{:.3f} C".format(celsius()))
time.sleep_ms(500)There is no Uno here: an Uno cannot run MicroPython. The '>h' in struct.unpack means big-endian and signed, which is the order the chip sends and the sign it needs below zero.
View on GitHub · blocks/tk42-lm75/micropython/lm75_first_reading.py @ v1.0When it does not work
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.
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.
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 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.
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
Questions about this product
See what other owners have asked, and read their solutions.
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.