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.
Solution:
make sure you've enabled the MCP9808 driver in your Zephyr configuration. Add these lines to your prj.conf file: CopyCONFIG_I2C=y CONFIG_SENSOR=y CONFIG_MCP9808=y...
Jump to solution
7 Replies
ZacckOsiemo
ZacckOsiemo3mo ago
Is that sensor defined in your board device tree?
ZacckOsiemo
ZacckOsiemo3mo ago
Also I see you are passing in i2c1 into the node identifier, if I recall correctly you should be passing in your sensor definition.
ZacckOsiemo
ZacckOsiemo3mo ago
Also device_get_binding fails if a device is not initialised successfully, you may want to check that.
Dtynin
Dtynin3mo ago
Oh alright I see 😊 @ZacckOsiemo I had worked it out earlier and I tried just what you recommended 😁
ZacckOsiemo
ZacckOsiemo3mo ago
Hope it worked!
Solution
wafa_ath
wafa_ath3mo ago
make sure you've enabled the MCP9808 driver in your Zephyr configuration. Add these lines to your prj.conf file: CopyCONFIG_I2C=y CONFIG_SENSOR=y CONFIG_MCP9808=y
Dtynin
Dtynin3mo ago
Thanks @wafa_ath I checked the driver again and was able to get a lead on it 😁
Want results from more Discord servers?
Add your server