Dtynin
Dtynin
Real-Time Temperature Monitoring with ATmega2560, MCP9808, and Zephyr OS: Device Driver Not Found
hey guys who has implemented real-time temperature monitoring with an ATmega2560 microcontroller? I'm using a MCP9808 sensor, and Zephyr OS, and currently trying to fix the sensor initialization error. Despite configuring the I2C peripheral in Zephyr, integrating the MCP9808 sensor, and writing code to read sensor data, I encounter the error "Sensor: Device driver not found." Here's my code:
#include <zephyr.h>
#include <device.h>
#include <drivers/i2c.h>
#include <sys/printk.h>
#include <drivers/sensor.h>

#define I2C_DEV_NAME DT_LABEL(DT_NODELABEL(i2c1))
#define MCP9808_ADDR 0x18

const struct device *i2c_dev;
const struct device *sensor_dev;

void main(void)
{
struct sensor_value temp;

// Initialize I2C device
i2c_dev = device_get_binding(I2C_DEV_NAME);
if (!i2c_dev) {
printk("I2C: Device driver not found.\n");
return;
}

// Initialize sensor device
sensor_dev = device_get_binding(DT_LABEL(DT_INST(0, microchip_mcp9808)));
if (!sensor_dev) {
printk("Sensor: Device driver not found.\n");
return;
}

if (sensor_sample_fetch(sensor_dev) < 0) {
printk("Sensor: Failed to fetch data.\n");
return;
}

sensor_channel_get(sensor_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
}
#include <zephyr.h>
#include <device.h>
#include <drivers/i2c.h>
#include <sys/printk.h>
#include <drivers/sensor.h>

#define I2C_DEV_NAME DT_LABEL(DT_NODELABEL(i2c1))
#define MCP9808_ADDR 0x18

const struct device *i2c_dev;
const struct device *sensor_dev;

void main(void)
{
struct sensor_value temp;

// Initialize I2C device
i2c_dev = device_get_binding(I2C_DEV_NAME);
if (!i2c_dev) {
printk("I2C: Device driver not found.\n");
return;
}

// Initialize sensor device
sensor_dev = device_get_binding(DT_LABEL(DT_INST(0, microchip_mcp9808)));
if (!sensor_dev) {
printk("Sensor: Device driver not found.\n");
return;
}

if (sensor_sample_fetch(sensor_dev) < 0) {
printk("Sensor: Failed to fetch data.\n");
return;
}

sensor_channel_get(sensor_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
}
If the implementation is successful, the output should display the current temperature readings from the MCP9808 sensor.
9 replies