Holooly Plus Logo

Question 12.8: You want to connect a triple-axis accelerometer to a Raspber......

You want to connect a triple-axis accelerometer to a Raspberry Pi.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use an analog accelerometer with a MCP3008 ADC chip to measure the X, Y, and Z analog outputs.

To try this recipe, you will need:

• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• MCP3008 eight-channel ADC IC (see “Integrated Circuits” on page 381)
• ADXL335 triple-axis accelerometer (see “Modules” on page 381)

Figure 12-14 shows the arrangement for this, using a breadboard. It uses three channels of the ADC to measure the X, Y, and Z acceleration forces.

You will need to set up SPI on your Raspberry Pi, so if you have not already done so, follow Recipe 8.6.

Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called adc_accelerometer.py.

import spidev, time
spi = spidev.SpiDev()
spi.open(0,0)
def analog_read(channel):
r = spi.xfer2([1, (8 + channel) << 4, 0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
while True:
x = analog_read(0)
y = analog_read(1)
z = analog_read(2)
print(“X=%d\tY=%d\tZ=%d” % (x, y, z))
time.sleep(1)

The program simply reads the three forces and prints them out:

$ sudo python adc_accelerometer.py
X=508 Y=503 Z=626
X=508 Y=504 Z=624
X=506 Y=505 Z=627
X=423 Y=517 Z=579
X=411 Y=513 Z=548
X=532 Y=510 Z=623
X=609 Y=518 Z=495
X=607 Y=521 Z=496
X=610 Y=513 Z=499

The first three readings took place with the accelerometer level. In the next three, the whole breadboard was tipped to one side. You can see that the X reading has decreased. Tipping the breadboard the other way causes the X reading to increase.

Discussion

The most common use for an accelerometer is to detect tilt. This works because the Z axis force is dominated by the pull of gravity (Figure 12-15).

When the accelerometer is tilted in one direction, some of that vertical force of gravity becomes active on another axis of the accelerometer.

We can use this principle to detect when the tilt is past a certain threshold. The following program (tilt.py) illustrates this point:

import spidev, time
spi = spidev.SpiDev()

spi.open(0,0)
def analog_read(channel):
r = spi.xfer2([1, (8 + channel) << 4, 0])
adc_out = ((r[1]&3) << 8) + r[2]
return adc_out
while True:
x = analog_read(0)
y = analog_read(1)
z = analog_read(2)
if x < 450:
print(“Left”)
elif x > 550:
print(“Right”)
elif y < 450:
print(“Back”)
elif y > 550:
print(“Forward”)
time.sleep(0.2)

When you run the program, you will start to see direction messages. You could use this to control a roving robot or a motorized pan-tilt head with a webcam attached:

$ sudo python tilt.py
Left
Left
Right
Forward
Forward
Back
Back

See Also

See the datasheet for the accelerometer chip used in the module.

There are many other analog accelerometer modules available. You may find that they give different readings. Make sure that the analog outputs do not exceed 3.3V.

Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
Table A-6. Integrated circuits
7805 voltage regulator SparkFun: COM-00107
L293D motor driver SparkFun: COM-00315, Adafruit: 807
ULN2803 Darlington driver IC SparkFun: COM-00312, Adafruit: 970
DS18B20 temperature sensor SparkFun: SEN-00245, Adafruit: 374
MCP3008 eight-channel ADC IC Adafruit: 856
TMP36 temperature sensor SparkFun: SEN-10988, Adafruit: 165
Table A-8. Modules
Raspberry Pi camera module Adafruit: 1367, MCM: 28-17733, CPC: SC13023
Arduino Uno SparkFun: DEV-11021, Adafruit: 50, CPC: A000066
Level converter, four-way SparkFun: BOB-11978, Adafruit: 757
Level converter eight-way Adafruit: 395
LiPo boost converter/charger SparkFun: PRT-11231
PowerSwitch tail Adafruit: 268
16-channel servo controller Adafruit: 815
Motor driver 1A dual SparkFun: ROB-09457
RaspiRobot board Sparkfun: KIT-11561, raspirobot.com
PiFace digital interface board MCM: 83-14472, CPC: SC12827
Humble Pi MCM: 83-14637, CPC: SC12871
Pi Plate Adafruit: 801
Gertboard MCM: 83-14460, CPC: SC12828
Breakout board with paddle terminals MCM: 83-14876, CPC: SC12885
PIR motion detector Adafruit: 189
Venus GPS module SparkFun: GPS-11058
Methane sensor SparkFun: SEN-09404
Gas sensor breakout board SparkFun: BOB-08891
ADXL335 triple-axis accelerometer Adafruit: 163
4×7-segment LED with I2C backpack Adafruit: 878
Bicolor LED square-pixel matrix with I2C backpack Adafruit: 902
PiLite interface board Ciseco, CPC: SC13018
aLaMode interface board Makershed: MKWY1, Seeedstudio: ARD10251P
Freetronics Arduino LCD shield www.freetronics.com
RTC module Adafruit: 264
16 x 2 HD44780 compatible LCD module SparkFun: LCD-00255, Adafruit: 181
12.14
12.15
12.16

Related Answered Questions

Question: 12.11

Verified Answer:

Use the Tkinter library to open a window and write...
Question: 12.10

Verified Answer:

Use a low cost SR-04 rangefinder. These devices ne...
Question: 12.12

Verified Answer:

Write a Python program that writes the data to a f...
Question: 12.9

Verified Answer:

Use the DS18B20 digital temperature sensor. This d...
Question: 12.7

Verified Answer:

Use an MCP3008 ADC chip. However, unless you need ...
Question: 12.6

Verified Answer:

Use a potential divider with one fixed resistor an...
Question: 12.2

Verified Answer:

Use the same basic recipe and code as Recipe 12.1,...
Question: 12.3

Verified Answer:

Low-cost resistive gas sensors are available that ...
Question: 12.4

Verified Answer:

The Raspberry Pi GPIO connector has only digital i...
Question: 12.5

Verified Answer:

Use a pair of resistors to act as a voltage divide...