Exploring I2C buses on ANAVI Info uHAT

I2C stands for Inter-Integrated Circuit, pronounced eye-squared-C, and alternatively known as IIC. It is a synchronous, multi-controller/multi-target (controller/target), packet switched, single-ended, serial communication bus. This protocol is suitable for devices wired at short distances, no more than 2-3m. We use I2C in pretty much all our open source products: Internet of Things, mechanical keyboards and Raspberry Pi HATs.

Anavi Info uHAT
I2C sensor modules attached to ANAVI Info uHAT

I2C was originally developed in 1982 by Philips. While that makes it 40 years old, it is still a very convenient and widely used bus. There are many I2C sensors and peripherals. It is in pretty much every smartphone, embedded electronics, microcontroller, personal computer and of course Raspberry Pi.

ANAVI Info uHAT with 4 slots for I2C sensors and mini OLED dsiplay

Actually, since the introduction of the famous 40-pin header in 2014, Raspberry Pi single board computers have not one but two I2C buses! We use them both on ANAVI Info uHAT and our other HATs. Th first I2C bus is on pins 3 and 5. On the ANAVI Info uHAT, it is used for the three I2C slots for sensors and the 4th dedicated slot for the mini OLED display.

The I2C bus on ANAVI Info uHAT in KiCad’s Schematic Layout Editor.

The second I2C bus is on pins 27 and 28 of the Raspberry Pi and is reserved exclusively for attaching an ID EEPROM. The ID EEPROM contains a software description of the hardware so the operating system on your Raspberry Pi can automatically identify the add-on board.

The EEPROM on ANAVI Info uHAT attached on the 2nd I2C bus

The I2C bus consists of two signals: SDA (Serial Data) is a data signal, SCL (Serial Clock) is a clock signal. I2C modules also need power, so the dedicated I2C connectors on the ANAVI Info uHAT and our other open source hardware provide two additional pins for VCC and GND. Typically, the VCC for the I2C connectors on our add-on boards for Raspberry Pi are 3.3V.

The I2C bus drivers are “open drain”, which means they can only pull the corresponding signal line at low level. They cannot drive it high. To restore the signal to high when no device is asserting it low, a pull-up resistor has to be added to each signal line. For example, on the ANAVI Info uHAT, we have 4.7K pull-up resistors R4 and R5 connected to SDA and SCL.

Resistor selection varies depending on the devices attached to the bus. In some specific use cases, further adjustment of the resistance value might be required. For systems with lots of devices or longer wires, smaller resistors are better.

How to Enable I2C on Raspberry Pi OS

Raspberry Pi OS, previously known as Raspbian, is the default and recommended Linux distribution for all models and versions of the Raspberry Pi single board computer. By default, I2C is not enabled. There are several ways to enable it, but probably the easiest is using the command-line tool raspi-config to perform few basic commands:

  • Open a terminal or login remotely via SSH to your Raspberry Pi and type in the following command: sudo raspi-config
  • Select Interfacing Options > I2C and enable it
  • Reboot the board

More details are available in the user’s manual for the ANAVI Info uHAT.

I2C Addresses

Each I2C device must have a unique address. The I2C reference design has a 7-bit address space, although rarely it might be used with a 10-bit extension. The 7-bit addresses range from 0 to 127 (0 to 0x7F hexadecimal). This is a limitation because it is not possible to have two I2C devices with the same address on the same I2C bus. For example, the I2C address on the mini OLED display included in all ANAVI Info uHAT kits is 0x3C. From the software side, this address is used in the example Python 3 script to access the display.

Python3 script controlling the mini OLED display over I2C on ANAVI Info uHAT

For Linux distributions, including Raspberry Pi OS, there is a package with a heterogeneous set of I2C tools called i2c-tools. To install it on Raspberry Pi OS, open a terminal and execute: sudo apt install -y i2c-tools. Once you have it installed, you can list attached I2C devices by their addresses with i2cdetect. For example, if the HTU21 temperature and humidity sensor module is attached to the Raspberry Pi, the output will be:

pi@raspberrypi:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

I2C Sensors and Peripherals

I2C Sensors and Peripherals

ANAVI Info uHAT officially supports the following I2C devices:

  • Mini OLED SSD1306 0.96″ display
  • HTU21D sensor for temperature and humidity
  • BH1750 sensor for light
  • BMP180 sensor for barometric pressure

Some of the other popular I2C sensor modules in maker community include:

  • APDS-9960 sensor for RGB color and gesture detection
  • BME680 for temperature, humidity, pressure and gas sensor
  • MCP9808 for temperature
  • MPL115A2 for barometric pressure
  • ADT7410 for temperature
  • MPU-6050 for triple axis acccelerometer and gyroscope

Which is your favorite I2C device? Join the discussion and let us know on Twitter!

You may also like

Building an Air Quality Monitor with ANAVI Infrared pHAT and MH-Z19

Takuya Matsuyama, a developer from Japan making a Markdown note-taking app called Inkdrop, published a wonderful tutorial how to build an air quality monitor using Raspberry Pi Zero W, ANAVI Infrared pHAT and MH-Z19B NDIR infrared gas module.

How to build an air quality monitor using Raspberry Pi Zero W + ANAVI Infrared pHAT

MH-Z19B is an intelligent infrared CO2 module which interacts with the Raspberry Pi using UART (universal asynchronous receiver-transmitter). Takuya uses the UART port on ANAVI Infrared pHAT to attach MH-Z19B. The rest of the sensor modules for his setup are included in ANAVI Infrared pHAT Advanced kit: HTU21D for temperature and humidity, BMP180 for barometric pressure and BH1750 for light.

By the way, initially we had published open source examples for using HTU21D, BMP180 and BH1750 in the C programming languages using the library wiringpi. Takuya also based his setup on wiringpi. However, wiringpi is now deprecated therefore we have replaced it with another library called libi2c-dev. Furthermore we added examples written in Python 3.

You may also like

“Spiderman” Raspberry Pi with ANAVI Infrared pHAT

Jesús Figueres, a data scientist interested in artificial intelligence (AI), shared in Twitter his setup of Raspberry Pi and ANAVI Infrared pHAT. It is living upside down on his lab’s ceiling so he calls it “Spiderman”. No doubt this is a suitable name in this case!

Jesús has attached various sensors for collecting data as we as a Raspberry Pi camera to take pictures of the room. He has developed energy efficiency algorithms running in the cloud which make decisions based on the data from the sensor and after that ANAVI Infrared pHAT takes care for transmitting commands as a stream of infrared signals to his air conditioner.

ANAVI Infrared pHAT
ANAVI Infrared pHAT

ANAVI Infrared pHAT is a low-cost open source hardware add-on board for Raspberry Pi with infrared receiver and transmitted. Furthermore it has slots for attaching up to 3 I2C sensor modules as well as convenient UART pins. We launched it in 2017 and it is one of our best-selling products. ANAVI Infrared pHAT is available at our distributors around the world and you can order it to build a similar home automation solution.

You may also like

Remote HVAC Control with ANAVI Infrared pHAT


Morten Mathiasen,  a highly skilled professional with 25 years of experience in software development and teaching, implemented an open source solution for HVAC control with Raspberry Pi, HTU21D temperature and humidity I2C sensor module as well as our open source hardware ANAVI Infrared pHAT. He recently shared details in a Crowd Supply Field Report.

Raspberry Pi 4 with ANAVI Infrared pHAT and HTU21D I2C sensor module

To save energy and to reduce global warming in his vacation house, Morten turns off the Panasonic HVAC system when his family is not there. Unfortunately, as a result next time when he arrives at the vacation house, it is too cold in the winter and too hot in the summer. It then takes up to 24 hours to get a comfortable temperature in the house.

Controlling the Panasonic HVAC through infrared signals and Home Assistant thanks to ANAVI Infrared pHAT and a Raspberry Pi

To solve this problem, Morten wrote in the C programming language a Home Assistant-based system that uses a Raspberry Pi with ANAVI Infrared pHAT to make an internet-connected remote control. Now, he can turn on the system remotely 24 hours before arriving to ensure a comfortable temperature.

Morten shared the source code under MIT License in GitHub. Give a star to the GitHub repo and spread the word about his fascinating open source project!

You may also like

Unifying Remote Controls with Infrared pHAT

Recently Michel submitted a very interesting Crowd Supply Field report about his awesome project with ANAVI Infrared pHAT. He used a Raspberry Pi Zero W and ANAVI Infrared pHAT to consolidate all the remote controls (TV, DVD, cable box, etc.) in his household to a single interface available on a tablet or smartphone.

Web interface for controlling remote controls over the web with Raspberry Pi, ANAVI Infrared pHAT and LIRC

Michel runs a local Apache2 web server on the Raspberry Pi Zero W and the Infrared pHAT can record and play back the infrared signals from any brand of remote control using the popular open source software Linux Infrared Remote Control (LIRC). He shared details in Crowd Supply and GitHub.

Furthermore Michel crafted a fantastic wooden box and shared with us a couple of photos!

Wooden box for Raspberry Pi Zero W and ANAVI Infrared pHAT
Inside the wooden box the two infrared LEDs have been carefully extended with appropriate wires

You may also like

Share Your Experience to Win!

Together with Crowd Supply we successfully completed several crowd funding campaigns over the past two years. Thanks to these efforts people all over the world are using our open source hardware developer boards and kits for their awesome projects.

Do you have a cool project with any of our products? Would you like to share your story?

No matter what you did, Crowd Supply and we would like to learn more. Send a Field Report of 100 words or so along with any supplemental pictures or other resources. If it is suitable, Crowd Supply will publish it as an update and reward you with a $25 Crowd Supply credit! Furthermore, all publishable entries received before Nov. 15 will be entered into a random prize drawing for more cool ANAVI gadgets:

  • 1st prize: ANAVI Miracle Controller and ANAVI Light Controller
  • 2nd prize: ANAVI Infrared pHAT and ANAVI Play pHAT
  • 3rd prize: ANAVI Thermometer

Please submit your entries here. May the open source be with you!

You may also like

ANAVI Infrared pHAT with Raspbian Buster

ANAVI Infrared pHAT is an open source hardware add-on board that converts your Raspberry Pi into a smart remote control that can bring your old consumer electronic devices, like air conditioning, TV, set top boxes and Hi-Fi systems to the Internet of Things (IoT) era.

Continue reading “ANAVI Infrared pHAT with Raspbian Buster”

You may also like