Getting ready

The I2C bus is not enabled in all Raspberry Pi images; therefore, we need to enable the module and install some supporting tools. Newer versions of Raspbian use device trees to handle hardware peripherals and drivers.

In order to make use of the I2C bus, we need to enable the ARM I2C in the bootconfig.txt file.

You can do this automatically using the following command:

sudo raspi-config

Select Advanced Options from the menu and then select I2C, as shown in the following screenshot. When asked, select Yes to enable the interface and then click Yes to load the module by default:

The raspi-config menu

From the menu, select I2C

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.