Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
KhoaNguyen
Level 2
Level 2
50 sign-ins 10 likes given 5 replies posted

Hello community
I'm a beginner
Currently I have a project to read BME280 sensor on PSoC 6 using PSoC Creator 4.4 (BLE_Sensor_Notify.cydsn.zip)
I'm trying to edit from another project written on PSoC 5. (BME280.cydsn.zip)
But I'm having some problems and I can't read the BME280 sensor as expected!
Can you help me find the error and fix it for me?
Thank you everybody!

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I took a look at the BLE_Sensor_Notify.cydsn.

First what I found was the following values were not defined, so the project could not be built,
So I defined them as

#define I2C_Master_MSTR_NO_ERROR CY_SCB_I2C_SUCCESS
#define I2C_Master_WRITE_XFER_MODE CY_SCB_I2C_WRITE_XFER
#define I2C_Master_READ_XFER_MODE CY_SCB_I2C_READ_XFER
#define I2C_Master_ACK_DATA CY_SCB_I2C_ACK
#define I2C_Master_NAK_DATA CY_SCB_I2C_NAK

The second thing I noticed was I2C_MasterReadByte() function was used incorrectly

// original, wrong
*data = I2C_MasterReadByte(I2C_Master_NAK_DATA,0,100);   
// modified
error = I2C_MasterReadByte(I2C_Master_NAK_DATA,data,100);  

The third thing I noticed was/were there are points where sensor needs some delay.
So I added CyDelay() to

BME280_ErrorCode BME280_I2C_Interface_ReadRegisterMulti(uint8_t device_address,
                                            uint8_t register_address,
                                            uint8_t register_count,
                                            uint8_t* data)
{
    // Send start condition
    uint8_t error = I2C_MasterSendStart(device_address,I2C_Master_WRITE_XFER_MODE,100);
    if (error == I2C_Master_MSTR_NO_ERROR)
    {
        // Write address of register to be read
        error = I2C_MasterWriteByte(register_address,100);
        if (error == I2C_Master_MSTR_NO_ERROR)
        {
CyDelay(10) ;
            // Send restart condition
            error = I2C_MasterSendReStart(device_address, I2C_Master_READ_XFER_MODE,100);
            if (error == I2C_Master_MSTR_NO_ERROR)
            {
                // Continue reading until we have register to read
                uint8_t counter = register_count;
                while(counter>1)
                {
CyDelay(10) ;
//                    data[register_count-counter] =
//                        I2C_MasterReadByte(I2C_Master_ACK_DATA,0,100);
                    I2C_MasterReadByte(I2C_Master_ACK_DATA,&data[register_count-counter],100) ;
                    counter-- ;
                }
CyDelay(10) ;
                // Read last data without acknowledgement
//                data[register_count-1]
//                    = I2C_MasterReadByte(I2C_Master_NAK_DATA,0,100);
                I2C_MasterReadByte(I2C_Master_NAK_DATA,&data[register_count-1],100) ;
            }
        }
    }

and

    // Check null pointer
    error = BME280_NullPtrCheck(bme280);
    if ( error == BME280_OK)
    {
        while(try_counts)
        {
            // Check device presence on I2C bus
            error = BME280_ReadWhoAmI(&who_am_i_value);
            if (error == BME280_OK && who_am_i_value == BME280_WHO_AM_I)
            {
                bme280->chip_id = BME280_WHO_AM_I;
                error = BME280_Reset(bme280);
CyDelay(100) ;
                if ( error == BME280_OK)
                {
                    // Read calibration data
                    error = BME280_ReadCalibrationData(bme280);
                }
                break;
            }
            
            CyDelay(1);
            --try_counts;
        }

And BME280 seems to be working OK

001-TeraTerm-log.JPG

moto

View solution in original post

3 Replies
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I took a look at the BLE_Sensor_Notify.cydsn.

First what I found was the following values were not defined, so the project could not be built,
So I defined them as

#define I2C_Master_MSTR_NO_ERROR CY_SCB_I2C_SUCCESS
#define I2C_Master_WRITE_XFER_MODE CY_SCB_I2C_WRITE_XFER
#define I2C_Master_READ_XFER_MODE CY_SCB_I2C_READ_XFER
#define I2C_Master_ACK_DATA CY_SCB_I2C_ACK
#define I2C_Master_NAK_DATA CY_SCB_I2C_NAK

The second thing I noticed was I2C_MasterReadByte() function was used incorrectly

// original, wrong
*data = I2C_MasterReadByte(I2C_Master_NAK_DATA,0,100);   
// modified
error = I2C_MasterReadByte(I2C_Master_NAK_DATA,data,100);  

The third thing I noticed was/were there are points where sensor needs some delay.
So I added CyDelay() to

BME280_ErrorCode BME280_I2C_Interface_ReadRegisterMulti(uint8_t device_address,
                                            uint8_t register_address,
                                            uint8_t register_count,
                                            uint8_t* data)
{
    // Send start condition
    uint8_t error = I2C_MasterSendStart(device_address,I2C_Master_WRITE_XFER_MODE,100);
    if (error == I2C_Master_MSTR_NO_ERROR)
    {
        // Write address of register to be read
        error = I2C_MasterWriteByte(register_address,100);
        if (error == I2C_Master_MSTR_NO_ERROR)
        {
CyDelay(10) ;
            // Send restart condition
            error = I2C_MasterSendReStart(device_address, I2C_Master_READ_XFER_MODE,100);
            if (error == I2C_Master_MSTR_NO_ERROR)
            {
                // Continue reading until we have register to read
                uint8_t counter = register_count;
                while(counter>1)
                {
CyDelay(10) ;
//                    data[register_count-counter] =
//                        I2C_MasterReadByte(I2C_Master_ACK_DATA,0,100);
                    I2C_MasterReadByte(I2C_Master_ACK_DATA,&data[register_count-counter],100) ;
                    counter-- ;
                }
CyDelay(10) ;
                // Read last data without acknowledgement
//                data[register_count-1]
//                    = I2C_MasterReadByte(I2C_Master_NAK_DATA,0,100);
                I2C_MasterReadByte(I2C_Master_NAK_DATA,&data[register_count-1],100) ;
            }
        }
    }

and

    // Check null pointer
    error = BME280_NullPtrCheck(bme280);
    if ( error == BME280_OK)
    {
        while(try_counts)
        {
            // Check device presence on I2C bus
            error = BME280_ReadWhoAmI(&who_am_i_value);
            if (error == BME280_OK && who_am_i_value == BME280_WHO_AM_I)
            {
                bme280->chip_id = BME280_WHO_AM_I;
                error = BME280_Reset(bme280);
CyDelay(100) ;
                if ( error == BME280_OK)
                {
                    // Read calibration data
                    error = BME280_ReadCalibrationData(bme280);
                }
                break;
            }
            
            CyDelay(1);
            --try_counts;
        }

And BME280 seems to be working OK

001-TeraTerm-log.JPG

moto

Thank you very much
I was able to read the sensor
But I still do not understand that the temperature, humidity, pressure values are saved in and how to call and print those values on the Tera Term screen.
We hope you explain more
Thanks!

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

But I still do not understand that the temperature, humidity, pressure values are saved in

> and how to call and print those values on the Tera Term screen.

For this matter, you are supposed to read and study BME280.h in which each functions are explained.

Having written above, I just hacked and replaced

            // Pressure
            data_array[2] = ((uint8_t) (bme280.data.pressure >> 24) & 0xFF);
            data_array[3] = ((uint8_t) (bme280.data.pressure >> 16) & 0xFF);
            data_array[4] = ((uint8_t) (bme280.data.pressure >> 😎 & 0xFF);
            data_array[5] = ((uint8_t) (bme280.data.pressure) & 0xFF);
            // Temperature
            data_array[6] = ((uint8_t) (bme280.data.temperature >> 24) & 0xFF);
            data_array[7] = ((uint8_t) (bme280.data.temperature >> 16) & 0xFF);
            data_array[8] = ((uint8_t) (bme280.data.temperature >> 😎 & 0xFF);
            data_array[9] = ((uint8_t) (bme280.data.temperature) & 0xFF);
            // Humidity
            data_array[10] = ((uint8_t) (bme280.data.humidity >> 24) & 0xFF);
            data_array[11] = ((uint8_t) (bme280.data.humidity >> 16) & 0xFF);
            data_array[12] = ((uint8_t) (bme280.data.humidity >> 😎 & 0xFF);
            data_array[13] = ((uint8_t) (bme280.data.humidity) & 0xFF);
            UART_PutArray(data_array, PACKET_SIZE);

with

            snprintf(buf_txt, 24, "Pressure: %d ", bme280.data.pressure) ;
            UART_PutString(buf_txt) ;
            snprintf(buf_txt, 24, "Temperature: %d ", bme280.data.temperature) ;
            UART_PutString(buf_txt) ;
            snprintf(buf_txt, 24, "Humidity: %d\n\r", bme280.data.humidity) ;
            UART_PutString(buf_txt) ;  

Then the TeraTerm output was something like,

002-TeraTerm-log2.JPG

moto