Share us



stats

RSS feeds

STM32duino IV. - Read temperature and humidity from Si7021 (I2C bus)Print

The fourth part of STM32duino tutorial we will check the I2C bus.
We connect the temperature and humidity sensor Si7021 to default I2C and also to alternative connection of I2C bus.

Note: I2C on STM32(G071) was tested on 12.6.2023 with other sensors like SHT40 (Adafruit library), BMP180 (Adafruit library), SCD41 (Sensirion library) and OLED with SSD1306 (Adafruit librar

All articles about STM32duino:
STM32duino I. - How to start with STM32 like Arduino
STM32duino II. - control GPIO

STM32duino III. - How to use Serial (USART)
STM32duino IV. - Read temperature and humidity from Si7021 (I2C bus)


The STM32 microcontrollers can include more I2C buses.
This document is important for this tutorial about STM32duino. There is decribed the connection of Nucleo development boards.
http://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf

I used this Arduino library for Si7021 temperature/humidity sensor.
https://github.com/LowPowerLab/SI7021

Where are the I2C buses connected, you see below.
The screenshot is from PeripheralPins.cpp file which is included in STM32 Arduino package.



The default connection is following: SDA is connected to PB9 (D14), SCL to PB8 (D15). The alternative connection is SCL-PB6 (D10) and SDA-PB7.



Default I2C1 bus:
Whole example code you can find here.
#include "Wire.h"
#include "SI7021.h"

SI7021 sensor;

// the setup function runs once when you press reset or power the board
void setup()
{
sensor.begin(); // initialization of Si7021 sensor
Serial.begin(9600); // initialization of Serial (STLink), 9600 Bd

// wait for serial
while (!Serial)
{
delay(100);
}

Serial.println("Test of Si7021");

if (!sensor.begin())
{
Serial.println("Si7021 sensor wasn't found");
while (true);
}
}


// the loop function runs over and over again forever
void loop()
{
// send data through onboard USB-UART converter (STLink)
Serial.print("Temperature: ");
Serial.print(String(sensor.getCelsiusHundredths()/100.0, 2*/)); // 2450 = 24.50 ?C
Serial.println(" ?C");

Serial.print("Humidity: ");
Serial.print(sensor.getHumidityPercent());
Serial.println(" %");
delay(1000);
}


I2C1 bus wit alternative pinout:
Whole example code you find here.
The important change is using of these fucntions. Wire.setSCL(PB6) and Wire.setSDA(PB7). These functions define where will be connected SDA and SCL of I2C. The connection has to corespond with document which is mentioned above (Pinout of Nucleo develepment boards).

#include "Wire.h"
#include "SI7021.h"

SI7021 sensor;

// the setup function runs once when you press reset or power the board
void setup()
{
Wire.setSCL(PB6); // alternative pinout of I2C1_SCL - PB6 (Morpho con) instead of PB8 (D15)
Wire.setSDA(PB7); // alternative pinout of I2C1_SDA - PB7 (D10) instead of PB9 (D14)

sensor.begin(); // initialization of Si7021 sensor
Serial.begin(9600); // initialization of Serial (STLink), 9600 Bd

// wait for serial
while (!Serial)
{
delay(100);
}

Serial.println("Test of Si7021 and STM32duino");

if (!sensor.begin())
{
Serial.println("Si7021 sensor wasn't found");
while (true);
}
}

// the loop function runs over and over again forever
void loop()
{
// send data through onboard USB-UART converter (STLink)
Serial.print("Temperature: ");
Serial.print(String(sensor.getCelsiusHundredths()/100.0, 2)); // 2450 = 24.50 ?C
Serial.println(" ?C");

Serial.print("Humidity: ");
Serial.print(sensor.getHumidityPercent());
Serial.println(" %");
delay(1000);
}




I2C2 bus instead of I2C1 - default pinout
Whole example code you finde here.

It is the same case like using of alternative pinout of I2C1.
You set the SDA (PB7) and SCL (PB6) through funtions and the STM32duino arduino package enables I2C2 bus instead of I2C1.
setSDA and setSCL.
The I2C2 is now default I2C bus.

#include "Wire.h"
#include "SI7021.h"

SI7021 sensor;

// the setup function runs once when you press reset or power the board
void setup()
{
Wire.setSCL(PB_6); // alternative pinout of I2C1_SCL - PB6 (Morpho con) instead of PB8 (D15)
Wire.setSDA(PB_7); // alternative pinout of I2C1_SDA - PB7 (D10) instead of PB9 (D14)

sensor.begin(); // initialization of Si7021 sensor
Serial.begin(9600); // initialization of Serial (STLink), 9600 Bd

// wait for serial
while (!Serial)
{
delay(100);
}

Serial.println("Test of Si7021 and STM32duino");

if (!sensor.begin())
{
Serial.println("Si7021 sensor wasn't found");
while (true);
}
}

// the loop function runs over and over again forever
void loop()
{
// send data through onboard USB-UART converter (STLink)
Serial.print("Temperature: ");
Serial.print(String(sensor.getCelsiusHundredths()/100.0, 2)); // 2450 = 24.50 ?C
Serial.println(" ?C");

Serial.print("Humidity: ");
Serial.print(sensor.getHumidityPercent());
Serial.println(" %");
delay(1000);
}


If you want to use I2C3 bus, the steps are the same like previous cases.
Don't forget to check PeripheralPins.cpp where are described pinouts of I2C buses.



And that's all, if you like the STM32duino tutorial, please, share is with #STM32duino hashtag.
No Comments have been Posted.

Post Comment

Please Login to Post a Comment.