In this chapter, we will learn how to use the I2C bus to configure and read the live acceleration data from an accelerometer. We will connect the circuit to your Starter Kit, create interface code, and then output the measurement data over a serial port to your computer in real time. We will then create an interrupt routine to detect a “freefall” event to trigger an LED on the Starter Kit.
This chapter aims to first teach how to connect to an I2C device with the help of the EFM32 I2C library. I2C is a commonly utilized communication interface for embedded designs. Thanks to only requiring two pins on the MCU, plus the fact that many devices can connect over those two pins makes it very versatile. In order to give us an interesting I2C device to talk to on the other end of that bus, I chose an accelerometer.
An accelerometer is a device that can measure acceleration on one or more axes. It can be used to detect the orientation of your device relative to the earth’s gravity. This can be useful for tilt sensors, to tell your object if it is right-side up, or upside-down. But they can also measure acceleration due to movement, which would be good know if your device needs to know how it is moving through space. And it doesn’t just tell you that it is moving, but how much it is accelerating, and in which direction even if that means that there is no acceleration on your device at all, as in a free fall situation. That information could be helpful if your device is delicate needs to prepare itself for a hard landing!
Accelerometers are a relatively new invention available to electronics developers. Long ago, an accelerometer was a large mechanical device that contained springs and plates. They were heavy, large, and expensive. That has changed for two reasons 1) Miniaturization of the mechanical parts of an accelerometer has shrunk it down to fit on a single surface-mounted chip and 2) they are employed in smart phones the world over, which drives down the cost for the rest of us.
Materials Needed for This Lesson
Modern accelerometers are Micro Electro-Mechanical Systems (MEMS) devices, which means that they are able to fit on a small chip inside the smallest of gadgets. One method to measure acceleration employed by MEMS accelerometers is to utilize a tiny conductive mass suspended on springs. The acceleration of the device causes the springs to stretch or contract, and the deflection of the conductive mass can be measured through a change in capacitance to nearby, fixed plates.
Accelerometers are specified by the following features:
Axes of the ADXL345 Accelerometer
The ADXL345 accelerometer used in this lesson is a digital output accelerometer that has three axes and a selectable g-range of 2/4/8/16 g’s. It connects to the MCU using an I2C or SPI interface. Note that we are going to demonstrate the I2C bus for this lesson, but I2C limits the measurement frequency to 800Hz whereas SPI allows a higher frequency of 1600Hz. Keep that in mind if your application requires the higher frequency.
Connect the Accelerometer to the Starter Kit via I2C
The I2C bus is a shared bus that consists of only two signals, a clock line (SCL) and a data line (SDA). A single I2C bus can connect two or more devices and any device can be a master or a slave (the same device can even assume both roles at different times). Only masters can initiate communication. Slave devices are assigned unique 7-bit or 10-bit device addresses through hardware configuration and monitor the bus for their address to be called, responding to any master device that begins a transaction. That means that you have to ensure that each slave device on your I2C bus has a unique address.
An I2C bus requires pull-ups on the SDA and SCL lines to keep the bus at a high voltage level when neither the master nor slave device is driving the bus. The pull-ups are created by attaching each of the interface wires to the supply voltage through an appropriately-sized resistor. The size of the resistor is chosen by the length of trace that will be used, which impacts the capacitance of the trace, and the speed of the I2C bus. Long traces create higher capacitance and require a lower value resistor to return the wire to a high voltage level after an I2C device has stopped driving the wire to a low voltage level.
Your ADXL345 breakout board is shipped without header pins attached. You have to solder them to the board as was demonstrated in Lesson 4, where we did the same thing to our Starter Kit.
In order to connect the accelerometer to the Starter Kit via the I2C bus, I had to figure out which pins were available on the Starter Kit that were connected to an I2C peripheral inside the Wonder Gecko MCU. I started with the Datasheet for the Wonder Gecko, which I found in the tile view in the Simplicity Studio home page. I then looked to section 4.2 Alternate Functionality Pinout and found the available I2C interfaces and locations where those interfaces routed to GPIO pins.
I found that the location 0 on I2C0 was not available on the Starter Kit, but the second one was available. I connected PD7 to the SCL pin on the ADXL345 breakout board and connected PD6 to the SDA pin on the breakout board. I then connected 3V3 on the Starter Kit to the VIN pin on the ADXL345 breakout board. I then placed a wire between GND pins on each board.
NOTE: The 3V3 pin on the ADXL345 breakout board is an output pin to power other devices, not an input pin for the board. The input pin is labeled as VIN.
Note that you could have built your own breakout board if you wanted to use an I2C device that had no breakout board or evaluation board available. Follow the instructions in Lesson 9, where a flash chip was given a breakout board with the help of a blank Schmartboard breakout board.
In the next section, we will cover software configuration of the I2C interface on the MCU for the ADXL345 accelerometer.