Quickstart¶
Read the temperature from your MCU: wire three signals, write a small port class, call two driver functions.
Datasheet sections this page relies on
From Microchip Technology Inc. DS20001942 L:
- Pins and packages: Package Types; 3.0 Pin Descriptions, Table 3-1, p. 2
All of them, with the driver API for each: Datasheet map.
1. Wire it¶
| MCU | Sensor | Notes |
|---|---|---|
| ADC input | VOUT | The input must reach at least 2 V for an MCP9700 (1.75 V at +125 °C) |
| 3.3 V | VDD | 100 nF to GND close to the pin; an MCP9701 or MCP9701A needs 3.1 V or more |
| GND | GND | Same ground as the ADC reference |
The pin numbers depend on the package; Hardware has all three. Do not put a resistor in series with VOUT. If you want the driver to switch the sensor off, power VDD from an MCU pin through 200 Ω instead (see Hardware).
2. Add the driver¶
3. Write the port¶
class MyPort : public mcp9700::AdcInterface<MyPort> {
public:
// Bring the ADC up once; later calls return true at once.
bool EnsureInitialized() noexcept {
if (!ready_) {
ready_ = platform_adc_init();
}
return ready_;
}
// One conversion of `channel`, calibrated to millivolts.
bool ReadVoltageMv(uint8_t channel, uint32_t& voltage_mv) noexcept {
return platform_adc_read_mv(channel, voltage_mv);
}
// Wait at least `us` microseconds: power-up and sample spacing depend on it.
void DelayUs(uint32_t us) noexcept {
platform_delay_us(us);
}
private:
bool ready_ = false;
};
The port returns millivolts at the ADC pin, calibrated. It does not return raw counts: the driver needs a voltage, and a count scaled by a guess is not one.
4. Read the temperature¶
#include "mcp9700.hpp"
int read_board_temperature(MyPort& port, int32_t& milli_celsius) {
mcp9700::MCP9700<MyPort> sensor(port); // no I/O yet: an MCP9700 on a 12 bit, 3.3 V ADC
if (auto r = sensor.EnsureInitialized(); !r) { // 1. ADC up, settle, one validated reading
return static_cast<int>(r.error); // DeviceIdMismatch: wrong channel or no sensor
}
const auto t = sensor.ReadTemperatureMilliC(); // 2. discard 1, average 8, check, convert
if (!t) {
return static_cast<int>(t.error); // OutputLow, OutputHigh, Saturated, Unstable or Bus
}
milli_celsius = t.value; // 3. 25000 is 25.000 C
return 0;
}
EnsureInitialized()checks the configuration, brings the ADC up, waits for the sensor to turn on and takes one validated reading, a weak analogue of a chip-ID check (it rules out a dead or grounded line, not a floating one).ReadTemperatureMilliC()discards one dummy conversion, averages eight, checks them for a clipped ADC, noise and an output outside the specified range, and applies the transfer function. 25000 is 25.000 °C.
The defaults describe an MCP9700 behind a 12-bit ADC with a 3.3 V full scale. For another part or ADC, set the
variant and the ADC's full scale and resolution in Config (Configuration): a wrong full scale
hides a clipped reading.
Next: API guide.