Supports

I2C Code with Arduino(Sensor ID:0x58)

/**

  ******************************************************************************

  * @file    Sencoch_Sensor_0x58_Driver.ino

  * @brief   Generic I2C driver for Sencoch sensors with device address 0x58

  * @version V2.0

  * @date    2025-07-19

  * @note    Compatible with: Arduino Uno, Nano, Mega, ESP8266, ESP32, 등.

  *          Supports all Sencoch sensors with I2C address 0x58

  ******************************************************************************

  */

 

/*============================================================================*/

/*                              Pin Definitions */

/*============================================================================*/

#define SCL_PIN2

#define SDA_PIN3

 

/*============================================================================*/

/*                       Sencoch Sensor I2C Configuration */

/*============================================================================*/

#define SENCOCH_I2C_ADDR0x58

#define SENCOCH_REG_DATA0x04

#define SENCOCH_REG_EOF0x20

#define SENCOCH_REG_SHIFT0x21

 

/*============================================================================*/

/*                        Sencoch Sensor Parameters */

/*============================================================================*/

/*

 * 노트: Modify PMIN and PMAX according to your specific sensor model

 * 

 * Common Ranges for I2C ADD 0x58 Sensors (unit: Pa):

 *   0 ~ 0.5kPa:     PMIN = 0.0f,       PMAX = 500.0f

 *   0 ~ 5kPa:       PMIN = 0.0f,       PMAX = 5000.0f

 *   0 ~ 1000kPa:    PMIN = 0.0f,       PMAX = 1000000.0f

 *   -1 ~ 1kPa:      PMIN = -1000.0f,   PMAX = 1000.0f

 *   -5 ~ 5kPa:      PMIN = -5000.0f,   PMAX = 5000.0f

 *   -100 ~ 100kPa:  PMIN = -100000.0f, PMAX = 100000.0f

 */

#define PMIN-100000.0f

#define PMAX100000.0f

 

/*============================================================================*/

/*                      Data Buffer Definitions */

/*============================================================================*/

#define SENCOCH_READ_LEN5

 

/*============================================================================*/

/*                      Public Data Structure */

/*============================================================================*/

typedef struct {

    int32_t pressure_AD;

    int16_t temperature_AD;

    uint8_t temp_calib_offset_code;

    uint8_t temp_calib_gain_exp;

    int16_t eof_out;

    뜨다 shift_n;

    뜨다 압력;

    뜨다 온도;

} Sencoch_Sensor_Data_t;

 

/*============================================================================*/

/*                           Delay Function */

/*============================================================================*/

static 무효의 delay_us(uint32_t us)

{

    delayMicroseconds(us);

}

 

/*============================================================================*/

/*                             I2C GPIO Initialization */

/*============================================================================*/

무효의 I2C_Init(무효의)

{

    pinMode(SCL_PIN, OUTPUT);

    pinMode(SDA_PIN, OUTPUT);

    digitalWrite(SCL_PIN, HIGH);

    digitalWrite(SDA_PIN, HIGH);

}

 

/*============================================================================*/

/*                         I2C Timing Generation */

/*============================================================================*/

 

static 무효의 I2C_시작(무효의)

{

    digitalWrite(SDA_PIN, HIGH);

    delay_us(5);

    digitalWrite(SCL_PIN, HIGH);

    delay_us(5);

    digitalWrite(SDA_PIN, LOW);

    delay_us(5);

    digitalWrite(SCL_PIN, LOW);

    delay_us(5);

}

 

static 무효의 I2C_중지(무효의)

{

    digitalWrite(SDA_PIN, LOW);

    delay_us(5);

    digitalWrite(SCL_PIN, HIGH);

    delay_us(5);

    digitalWrite(SDA_PIN, HIGH);

    delay_us(5);

}

 

static uint8_t I2C_WaitAck(무효의)

{

    uint8_t ack= 0;

    pinMode(SDA_PIN, INPUT_PULLUP);

    delay_us(5);

    digitalWrite(SCL_PIN, HIGH);

    delay_us(5);

    만약 (digitalRead(SDA_PIN)) {

        ack= 1;

    }

    digitalWrite(SCL_PIN, LOW);

    delay_us(5);

    pinMode(SDA_PIN, OUTPUT);

    반품 ack;

}

 

static 무효의 I2C_SendByte(uint8_t 바이트)

{

    ~을 위한 (uint8_t 나= 0; 나< 8; 나++) {

        digitalWrite(SCL_PIN, LOW);

        delay_us(5);

        만약 (바이트& 0x80) {

            digitalWrite(SDA_PIN, HIGH);

        } 또 다른 {

            digitalWrite(SDA_PIN, LOW);

        }

        바이트<<= 1;

        delay_us(5);

        digitalWrite(SCL_PIN, HIGH);

        delay_us(5);

    }

    digitalWrite(SCL_PIN, LOW);

    delay_us(5);

}

 

static uint8_t I2C_ReadByte(uint8_t ack)

{

    uint8_t 바이트= 0;

    pinMode(SDA_PIN, INPUT_PULLUP);

    ~을 위한 (uint8_t 나= 0; 나< 8; 나++) {

        바이트<<= 1;

        digitalWrite(SCL_PIN, HIGH);

        delay_us(5);

        만약 (digitalRead(SDA_PIN)) {

            바이트|= 0x01;

        }

        digitalWrite(SCL_PIN, LOW);

        delay_us(5);

    }

    pinMode(SDA_PIN, OUTPUT);

    만약 (ack) {

        digitalWrite(SDA_PIN, LOW);

    } 또 다른 {

        digitalWrite(SDA_PIN, HIGH);

    }

    delay_us(5);

    digitalWrite(SCL_PIN, HIGH);

    delay_us(5);

    digitalWrite(SCL_PIN, LOW);

    delay_us(5);

    반품 바이트;

}

 

/*============================================================================*/

/*                       High-Level I2C Operations */

/*============================================================================*/

 

uint8_t Sencoch_ReadBytes(uint8_t reg_addruint8_t *datauint8_t len)

{

    I2C_시작();

    I2C_SendByte(SENCOCH_I2C_ADDR<< 1);

    만약 (I2C_WaitAck()) {

        I2C_중지();

        반품 1;

    }

    I2C_SendByte(reg_addr);

    만약 (I2C_WaitAck()) {

        I2C_중지();

        반품 1;

    }

    I2C_시작();

    I2C_SendByte((SENCOCH_I2C_ADDR<< 1| 0x01);

    만약 (I2C_WaitAck()) {

        I2C_중지();

        반품 1;

    }

    ~을 위한 (uint8_t 나= 0; 나< len; 나++) {

        data[나] = I2C_ReadByte(나< len- 1);

    }

    I2C_중지();

    반품 0;

}

 

uint8_t Sencoch_ReadByte(uint8_t reg_addr)

{

    uint8_t data;

    Sencoch_ReadBytes(reg_addr&data1);

    반품 data;

}

 

/*============================================================================*/

/*                   Sencoch Sensor-Specific Operations */

/*============================================================================*/

 

static uint8_t Sencoch_ReadCalibration(Sencoch_Sensor_Data_t *data)

{

    data->temp_calib_offset_code= Sencoch_ReadByte(SENCOCH_REG_EOF);

    data->temp_calib_gain_exp= Sencoch_ReadByte(SENCOCH_REG_SHIFT);

 

    switch (data->temp_calib_offset_code) {

        case 0x0C:  data->eof_out= 4096;   break;

        case 0x8C:  data->eof_out= -4096;  break;

        case 0x0D:  data->eof_out= 8192;   break;

        case 0x8D:  data->eof_out= -8192;  break;

        case 0x0E:  data->eof_out= 16384;  break;

        case 0x8E:  data->eof_out= -16384break;

        default:    data->eof_out= 0;       break;

    }

 

    data->shift_n= pow(2, data->temp_calib_gain_exp/ 10.0f);

    반품 0;

}

 

static 뜨다 Sencoch_ConvertPressure(int32_t raw)

{

    const int32_t TWO_POW_21= 2097152;

    반품 ((뜨다)raw/ TWO_POW_21* (PMAX- PMIN);

}

 

static 뜨다 Sencoch_ConvertTemperature(int16_t rawint16_t eof_out뜨다 shift_n)

{

    반품 ((뜨다)(raw- eof_out/ shift_n+ 25.0f;

}

 

uint8_t Sencoch_ReadPressureAndTemp(Sencoch_Sensor_Data_t *data)

{

    uint8_t raw_data[SENCOCH_READ_LEN];

    int32_t pressure_raw;

    int16_t temp_raw;

    uint8_t result;

 

    result= Sencoch_ReadBytes(SENCOCH_REG_DATA, raw_data, SENCOCH_READ_LEN);

    만약 (result!= 0) {

        반품 1;

    }

 

    pressure_raw= ((int32_t)raw_data[0<< 16|

                   ((int32_t)raw_data[1<< 8)  |

                   (int32_t)raw_data[2];

 

    만약 (pressure_raw& 0x800000) {

        pressure_raw|= 0xFF000000;

    }

 

    temp_raw= ((int16_t)raw_data[3<< 8|

               (int16_t)raw_data[4];

 

    data->pressure_AD= pressure_raw;

    data->temperature_AD= temp_raw;

 

    Sencoch_ReadCalibration(data);

 

    data->압력= Sencoch_ConvertPressure(pressure_raw);

    data->온도= Sencoch_ConvertTemperature(temp_raw, data->eof_out, data->shift_n);

 

    반품 0;

}

 

/*============================================================================*/

/*                              Setup Function */

/*============================================================================*/

 

무효의 setup()

{

    Serial.begin(115200);

    Serial.println("I2C ADD 0x58 Sensor Driver Started");

    Serial.println("==========================================");

    Serial.print("Pressure Range: ");

    Serial.print(PMIN);

    Serial.print(" ~ ");

    Serial.print(PMAX);

    Serial.println(" Pa");

    

    I2C_Init();

    지연(100);

}

 

/*============================================================================*/

/*                              Loop Function */

/*============================================================================*/

 

무효의 loop()

{

    Sencoch_Sensor_Data_t sensor_data;

 

    만약 (Sencoch_ReadPressureAndTemp(&sensor_data== 0) {

        Serial.print("Pressure: ");

        Serial.print(sensor_data.pressure/ 1000.0f2);

        Serial.print(" kPa|  온도: ");

        Serial.print(sensor_data.temperature2);

        Serial.println(" 씨");

    } 또 다른 {

        Serial.println("Error: Sensor communication failed!");

    }

 

    지연(100);

}