Raspberry Pi I²C

The I²C is a multi-master serial bus invented by Philips that is used to attach low-speed peripherals to a processors like the Raspberry Pi.

Pins 3 and 5 on the GPIO header are often used by digital I/O boards. They cannot be used with I2C peripherals if also connected to an expansion board in this way. To overcome this problem, we have a bespoke header board with on-board jumpers to isolate them.

Configuring the Raspberry Pi

Before you can use I²C devices on the Raspberry Pi, some basic configuration is required. This is well described on the Adafruit website.

It is worth installing the i2c-tools utility:
sudo apt-get install python-smbus
and this installed the i2c-tools package as well for us.

We had a file called /etc/modprobe.d/raspi-blacklist.conf and had to edit it to comment out these two lines with a #:
blacklist spi-bcm2708
blacklist i2c-bcm2708

After a reboot, we ran this command on our 256MB Raspberry Pi Model A: sudo i2cdetect -y 0

On our new 512MB 512MB Raspberry Pi Model B, this command is used: sudo i2cdetect -y 1

To avoid having to run the I2C tools at root add the pi user to the I2C group: sudo adduser pi i2c

i2cdetect command output
This is what is then revealed with the command: i2cdetect -y 0. This shows the analogue channels being addressed at 0x68 and 0x69.

ADC Pi V2

ADC Pi V2 - Raspberry Pi Analogue to Digital converter
As an initial piece of test hardware, we decided to purchase the ADC Pi V2 - Raspberry Pi Analogue to Digital converter from AB Electronics.

The ADC Pi V2 is an 8 channel, 17-bit analogue to digital converter designed to work with the Raspberry Pi. It is based on two Microchip MCP3424 A/D converters, each containing four analogue inputs with up to 18-bit resolution.

Note that there is also a basic method to read analogue values from the GPIO (digital) pins but, it has many limitations.

ADC Pi V2 with 90 degree header
We attached a 90° 10-way header to our board, to make it easier to experiment with various sensors.

Despite changtes to the board layout it is possible to maintain the same connectivity, by bending a couple of pins on our 10-way headers:

board versions

The one on the left is V2.0 and the one on the right is V2.2. We expose all 8 channels, ground and +5V via a standard 10-way in-line header.

Analogue Sensors

We are testing this board out with a number of sensors:

Honeywell HIH-4021 Humidity Sensor

HIH-4021 humidity sensor
The HIH-4021 is an analogue humidity sensor, with an output voltage proportional to the percentage humidity.

The voltage out from this device depends on the supply voltage so an accurate supply is better. When we connected it to our Raspberry Pi, it also caused it to reboot, so we think it takes too much current from the GPIO pins (or is the straw that breaks the camels back). So we are using an external supply that has been accurately calibrated to provide very close to 5.00V.

The relative humidity (%) = (((Vout / Vsupply) + 0.16) / 0.0062)

Light Dependent Resistor

Light Dependent Resistor (LDR)
There are numerous LDR devices with varying specifications. The ones we are using vary in resistance from about 150Ω in direct sunlight, to around 1.2MΩ in complete darkness.

LDR circuit
We connect one side to +5V and the other to the analogue input, which is also connected to GND to via a 10KΩ resistor. In darkness, the voltage at the input is thus ~0.04V, rising to ~4.91V in the brightest sunlight.

LM35DZ

LM35DZ
The LM35DZ is an analogue temperature sensor, with an output voltage proportional to the temperature in °C.

Interfacing is easy as this device has just three pins for power and the analogue output voltage. It is also powered using 5V dc.

With this device connected, we were seeing a voltage of 0.218994 volts. The output is 10mV per degree centigrade, so this equates to 21.9°C.

Ultrasonic Rangefinder

Ultrasonic rangefinder
We are testing this ultrasonic rangefinder. It takes a 5V dc feed and provides a number of outputs, one of which is analogue voltage proportional to distance of detected objects.

Soil Moisture Level Sensor

T.B.C.

Battery Voltage Monitoring

Although the analogue input use a 5V reference voltage and have a maximum of 5V input voltage, it is possible to monitor higher voltage levels such as the state of a 12V battery. This is achieved by using a voltage divider circuit. A 12V lead-acid battery actually has provides more than 12V and when charging, the voltage across it can be as much as 14V. To accommodate these higher voltages, we use a divider circuit that divides the input voltage by 4. This means we see 3.0V on the analogue input, when the battery voltage is 12.0V.

battery level measurement
This is the circuit we use to measure 12V battery levels. The 10K linear potentiometer/trimmer is used to set the voltage division to 4 as accurately as possible.

Note that in an ideal world we would use the whole of the 5V input range but, the presence of the Zener diode results in the measured voltage diverting from its linear relationship with the battery level, as its rated voltage is approached. The graphs below show the effect of the ender diodes as we measured it. The green line is the desired linear relationship. The blue line is the above circuit with a ender diode rated at 4.3V. The red line is with a ender diode rated at 5.1V.

CMPS10 Electronic Compass

CMPS10 electronic compass
We are also testing with this CMPS10 module as it supports both an I²C and a serial interface. It requires a 3.3 to 5V power supply and draws 25mA.

We have ordered one of these and are awaiting delivery.

Programming & Software

Python

To check which version of Python you have installed, run the command: python -V

There are some code samples on the AB Electronics website. These have a number of dependencies:

  • Python 3 seemed to be installed already. To install Python 3 run this command from an administrator account: sudo apt-get install python3
  • The GPIO API depends on the Quick2Wire GPIO Admin.
  • Quick2wire Python API from github.

We downloaded the ZIP files onto a USB pen drive and extracted them, resulting in two folders called quick2wire-gpio-admin-master and quick2wire-python-api-master.

GPIO Admin installation

This is described here.

Quick2Wire Python API installation

This is described here. It doesn't have to be installed as such. You can use the library without installation, by adding the full path of the source tree to the PYTHONPATH environment variable:
export QUICK2WIRE_API_HOME=/home/pi/lib/quick2wire-python-api
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME

This is a useful page on understanding imports and PYTHONPATH.

Pi4J (Java)

T.B.C.

Share ...
We are on ...
Facebook Twitter
YouTube Flickr Follow us on Pinterest