I'm using a digispark as slave device that reads data from an analog sensor this is the code
#include "TinyWireS.h" //include the I2C library
#define I2C_SLAVE_ADDR 0x26 // define the I2C slave address of the device
int val = 0;
void requestEvent()
{
val = analogRead(0); // read the analog value from the sensor
TinyWireS.send((val >> 8) & 0xFF); // Send the high byte
TinyWireS.send(val & 0xFF); // Send the low byte // send the value over the I2C bus
}
void setup() {
TinyWireS.begin(I2C_SLAVE_ADDR); //Initializes the I2C communication
TinyWireS.onRequest(requestEvent); // this function will respond to master requests
}
void loop() {
TinyWireS_stop_check(); //checks for an I2C stop condition
}
and I'm trying to read the data via i2c bus on esp32(master device) using esp-idf framework so I created the first task for i2c initialization and the second task for data reading
but I keep getting the same wrong value on the monitor
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "driver/i2c.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_FREQ_HZ 100000 // I2C master clock frequency
#define I2C_MUX_ADDR 0x70 // I2C address of the multiplexer
#define ATTINY85_ADDR 0x26 // ATtiny85 I2C address
#define SENSOR_DATA_LENGTH 2
//task1
void initTask(void *voidparam)
{
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = I2C_MASTER_SDA_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = I2C_MASTER_SCL_IO;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(I2C_MASTER_NUM, &conf);
i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
printf("I2C initialized\n");
vTaskDelete(NULL);
}
//task2
void i2cTask(void *voidparam)
{
while (1) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (ATTINY85_ADDR << 1) | I2C_MASTER_READ, true);
uint8_t data[SENSOR_DATA_LENGTH];
i2c_master_read(cmd, data, SENSOR_DATA_LENGTH, I2C_MASTER_LAST_NACK); // Read all data bytes
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdMS_TO_TICKS(1000));
i2c_cmd_link_delete(cmd);
int16_t receivedValue = (data[0] << 8) | data[1]; // Combine two bytes
printf("Received value: %d\n", receivedValue);
vTaskDelay(1000 / pdMS_TO_TICKS(1000));
}
}
void app_main(void)
{
xTaskCreate(initTask,"initTask",10242,NULL,2,NULL);
xTaskCreate(i2cTask,"i2cTask",10242,NULL,2,NULL);
}
I tried to change esp32 code