I2C Bus Analysis
Students will measure temperature and humidity from a fully integrated sensor over an I2C bus. This will require using the I2C bus and programming the communication sequence. A successful communication sequence with the sensor will require a predefined exchange of data, including reading calibration data. A framework of the data exchange sequence will be provided. The final program will state humidity and temperature every second. Students will attach the digital logic analyzer the i2c bus to observe the exchanged data.
Learning outcome: Understanding serial communication with external sensors.
Left: Humidity Sensor, Right Pressure Sensor
Driver Software
You will need to install i2c software and drivers as listed in the Air Quality Sensing laboratory. Above are pictures for the HTU21D humidity sensor and the BMP280 pressure and temperature sensor.
BMP180
No modifications to code should be necessary.
BMP280
in your program will need to be based on the bmp280_normal_mode.py example. In particular it will need to have the following line (otherwise it will become more difficult to understand the data transmitted on the SDA line:
bmp280.mode = adafruit_bmp280.MODE_NORMAL
HTU21D
You will need to modify the driver for HTU21D sensor. The current implementation is not giving the sensor enough time to convert the data and constantly asks for data.
Open the file /home/pi/.local/lib/python3.9/site-packages/adafruit_htu21d.py
If you don't have that file in that folder you can also try to find it in /lib/python3.9/site-packages
If you have an newer or older verion of python the folder python3.x will need to be adapted to the version you are having. You can simply list the folder with ls /home/pi/.local/lib/py*
add
def relative_humidity(self):
"""The measured relative humidity in percent."""
self.measurement(HUMIDITY)
self._measurement = 0
time.sleep(0.017) # added 16ms delay
return self._data() * 125.0 / 65536.0 - 6.0
def temperature(self):
"""The measured temperature in degrees Celcius."""
self.measurement(TEMPERATURE)
self._measurement = 0
time.sleep(0.051) # added 50ms delay
return self._data() * 175.72 / 65536.0 - 46.85
Connecting the Breakout Board
You will be using the airquality sensor hat. Attach your BMP280 sensor and an HTU21D.
Debugging I2C with the Logic Analyzer of the Oscilloscope
Watch youtube video below at position 26min and later.
Attach two probes of the oscilloscope to the I2C databus;
One probe to I2C clock. This is pin 3 on the raspberry.
One probe to I2C data. This is pin 5 on the raspberry.
The Probe ground to ground
Use jumper wires to make the connections:
Analysis of I2C Bus HTU21D
You will see that the i2c bus is repeating two patterns continuously when the test program runs. One is related to temperature measurement and the other to humidity measurement.
The Humidity sensor is located at hex 40. That is 0100 0000 in binary. When you write to the sensor the last bit in the address is 0 and when you read its a 1 e.g. 1000 0000 for write to hex address 40. After the raspi sent the sensor address it will transmit the command. The humidity sensor command is either "give me temperature" or "give me humidity" . In our case those commands are either hex F3 or F5. Then the raspi will need to wait until the humidity or temperature is determined by the sensor. The data sheet lists how long that will take. For this the raspi is sending a read command to hex 40 (bin 1000 0001). As response it will receive 3 bytes of data. The first 2 bytes is the measured data, but the last two bits of the second byte need to be replaced with 00 as those indicate the status of the sensor. The last byte received is the checksum. The driver code is comparing the received data with the checksum to determine if its a valid data transmission.
Take a screen shot of an example when a command is sent to the sensor. (2 bytes long) Oscilloscope has save to USB function.
Take a screen shot of an example when data is received. (4 bytes long)
You can save a screen shot onto a USB flash drive on the Siglent oscilloscope. Alternatively take an image with your cell phone.
Turn In & Grading
We will want you to take screen shots of a read request as well as the reading of the data. This can either be a temperature, humidity or pressure read and usually you will capture both. You have example below for BMP180 sensor.
We will want you to identify the device address and the conversion instruction. We will want to illustrate where the acknowledgement occurred.
Based on the sensor specifications we will want you to identify what instruction was sent.
On the long example we will want to identify the returned data, the status bits and the checksum (HTU21D only).
Include your analysis and screen shots in your write up. Submit your write up below.