Schematic view of sensor with Pins:
- GND: Ground
- VDD: Voltage for Sensor between 3.6 to 5 Volts DC
- SCK: Serial Clock Input is used to synchronize communication between master(microcontroller) and HTU21DF sensor.
- Data: Serial data pin is used to transfer data in and out of sensor and device.
Communication Sequence with Sensors:
- Start Sequence: A single bit. To initiate transmission start bit has to be issued by microcontroller.
- Stop Sequence: A single bit again to indicate completion of transmission of data (along with CRC) and if required release holding of SCK.
- Sending Command: After start bit to indicate initiation transmission, next is to send command to sensor identified by sensor address (similar to MAC address)
- Device Address of HTU21DF address in hex 0x40 (in Decimal system = 64)
- Device address is 7 bits wide with last bit capturing type of operation (Read / Write)
- Thus controller will send initiate communication to HTU21DF Sensor as below for both read and write operations
As I2C protocol specifies, Start –> Address of Sensor (With Read / Write intent)
HTU21DF sensor responds (as its address is 0x40) with ACK (Acknowledgement). Then controller sends command to HTU21DF sensor. Commands their Hex and Binaries are below.
In comment section above there are “Hold Master” and “No Hold Master”. As detailed in previous post, I2C protocol provides options where sensor may choose to control serial clock (SCK).
Hold Master ensures control of SCK is with Sensor and controller can not perform any other actions with other sensors if available but is kept in waiting state till sensor measurement is complete. On the other hand, No Hold Master implies, control is SCK is with master (controller) and controller is free to communicate with other sensor. Additionally a polling is done with read command to check if measurement is done.
HOLD MASTER is analogous to synchronous programming and NO HOLD MASTER is equal to Asynchronous programming but with polling. May be NO HOLD MASTER is good for situations where there are more sensors on board.
Post measurement, data is sent by Sensor 2 bytes MSB(Most Significant Byte) followed by LSB (Least Significant Byte) and a checksum.
Additionally on LSB, last 2 significant bits are reserved for transmitting status information. In LSB (Bit 1 is used to indicate temperature / Humidity). 0 Temperature and 1 Humidity.
Step 1: Start (Initiate) session to Sensor (Slave in this case HTU21DF)
Step 2: Issue Write intent to sensor indicated by address
Step 3: Sensor sends ACK to master
Step 4: Issue “Measure Humidity with HOLD” command
Step 5: Sensor sends ACK
Step 6: Issue read intent to sensor with address of sensor.
Step 7: Sensor takes control of SCK line and makes master (microcontroller) wait till measurement is complete.
Step 8: Send Data. Post measurement sensor uses 14 bits (of data) + 2 bits (status) to send measured data using SDA line as output from device. Data is divided into 2 sections Most Significant Byte (MSB) and Least Significant Byte (LSB). First sent is MSB followed by LSB.
In above example, relative humidity is measured as 31827 (add numbers on right column (except status bits). Status column 1 indicates measurement is humidity (0 implies temperature).
Step 9: HTU21DF sensor provides CRC8 checksum. is computed by sensor and sent as an additionally redundant byte for data verification.
In this manner communication takes place between master (microcontroller) and I2C compliant sensor like HTU21DF.
Next section we will understand formulae for RH (relative humidity), Temperature and also verify data integrity from device using checksum provided in checksum Byte.
Appreciate if readers review and correct mistakes