Zebra (VL-EPC-2701) Yocto Linux User Guide
35
Advanced Features and Commands
Sensors
The Zebra board can be built with optional accelerometer and magnetometer. If present, their
values can be read with the
i2cget
command. Below is a sample script that reads and displays
these values in real time.
#!/bin/bash
# Accelerometer & magnetometer sensor reading script
# set active mode
i2cset -f -y 2 0x1C 0x02A 0x01
# enable both sensors
i2cset -f -y 2 0x1C 0x05B 0x03
acc_sens=0.244 # nominal sensitivity for 2g accelerometer mode
mag_sens=0.1 # nominal sensitivity for magnetometer
echo "Accelerometer and magnetometer real-time measurement"
echo " X Y Z"
while true
do
# read 14-bit acceleration measurements
acc_x=$(( $( i2cget -f -y 2 0x1C 0x01 ) << 6 | $( i2cget -f -y
2 0x1C 0x02 ) >> 2))
acc_y=$(( $( i2cget -f -y 2 0x1C 0x03 ) << 6 | $( i2cget -f -y
2 0x1C 0x04 ) >> 2))
acc_z=$(( $( i2cget -f -y 2 0x1C 0x05 ) << 6 | $( i2cget -f -y
2 0x1C 0x06 ) >> 2))
# read 16-bit magnetic measurements
mag_x=$(( $( i2cget -f -y 2 0x1C 0x01 ) << 8 | $( i2cget -f -y
2 0x1C 0x02 ) ))
mag_y=$(( $( i2cget -f -y 2 0x1C 0x03 ) << 8 | $( i2cget -f -y
2 0x1C 0x04 ) ))
mag_z=$(( $( i2cget -f -y 2 0x1C 0x05 ) << 8 | $( i2cget -f -y
2 0x1C 0x06 ) ))
10