blob: d635e3ccc1ff60da04db2ff5c04711436a29cedd [file] [log] [blame]
Paul Sokolovskya2e0d922015-10-28 21:04:03 +03001# This is an example on how to access accelerometer on
2# PyBoard directly using I2C bus. As such, it's more
3# intended to be an I2C example, rather than accelerometer
4# example. For the latter, using pyb.Accel class is
5# much easier.
6
Paul Sokolovsky9d0d6d32015-11-29 02:56:02 +02007from machine import Pin
8from machine import I2C
Paul Sokolovskya2e0d922015-10-28 21:04:03 +03009import time
10
11# Accelerometer needs to be powered on first. Even
12# though signal is called "AVDD", and there's separate
13# "DVDD", without AVDD, it won't event talk on I2C bus.
Paul Sokolovsky9d0d6d32015-11-29 02:56:02 +020014accel_pwr = Pin("MMA_AVDD")
Paul Sokolovskya2e0d922015-10-28 21:04:03 +030015accel_pwr.value(1)
16
Paul Sokolovsky9d0d6d32015-11-29 02:56:02 +020017i2c = I2C(1, baudrate=100000)
Paul Sokolovskya2e0d922015-10-28 21:04:03 +030018addrs = i2c.scan()
19print("Scanning devices:", [hex(x) for x in addrs])
20if 0x4c not in addrs:
21 print("Accelerometer is not detected")
22
23ACCEL_ADDR = 0x4c
24ACCEL_AXIS_X_REG = 0
25ACCEL_MODE_REG = 7
26
27# Now activate measurements
28i2c.mem_write(b"\x01", ACCEL_ADDR, ACCEL_MODE_REG)
29
30print("Try to move accelerometer and watch the values")
31while True:
32 val = i2c.mem_read(1, ACCEL_ADDR, ACCEL_AXIS_X_REG)
33 print(val[0])
34 time.sleep(1)