Electric Dollar Store

The RT3000 is a low power and high accuracy 3 axis acceleration sensor. It continuously senses the acceleration and reports it as a 16-bit (x,y,z) triple. In addition, it has orientation and motion alarms, a self-test mode, and low-power and high accuracy modes.

import sys
import struct
import time

from i2cdriver import I2CDriver, EDS

if __name__ == '__main__':
    i2 = I2CDriver(sys.argv[1])
    i2.scan()

    d = EDS.Accel(i2)
    while True:
        print("x=%+.3f  y=%+.3f  z=%+.3f" % d.measurement())
#include <Wire.h>

class accel {
  int a;
public:
  float x, y, z;
  void begin(byte _a = 0x19) {
    a = _a;
    regwr(0x20, 0b01000111); // CTRL_REG1: 50 Hz, enable X,Y,Z
    regwr(0x23, 0b00000000); // CTRL_REG4: High resolution mode
  }
  void regwr(byte addr, byte val) {
    Wire.beginTransmission(a);
    Wire.write(addr);
    Wire.write(val);
    Wire.endTransmission();
  }
  byte regrd(byte addr) {
    Wire.beginTransmission(a);
    Wire.write(addr);
    Wire.endTransmission(false);
    Wire.requestFrom(a, 1);
    while (Wire.available() < 1)
      ;
    return Wire.read();
  }
  int16_t rd16(byte addr) {
    int16_t r = regrd(addr) | (regrd(addr + 1) << 8);
    Serial.println(r);
    return r / 16384.0;
  }
  void read() {
    while ((regrd(0x27) & 8) == 0)
      ;
    x = rd16(0x28);
    y = rd16(0x30);
    z = rd16(0x32);
  }
};

accel Accel;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Accel.begin();
}

void loop() {
  Accel.read();
  Serial.print(Accel.x); Serial.print(' ');
  Serial.print(Accel.y); Serial.print(' ');
  Serial.print(Accel.z); Serial.println();
}
from machine import I2C
import struct
import time

class Accel:
    """ ACCEL is a Richtek RT3000C 3-Axis Digital Accelerometer """

    def __init__(self, i2, a = 0x19):
        self.i2 = i2
        self.a = a
        self.regwr(0x20, 0b01000111) # CTRL_REG1: 50 Hz, enable X,Y,Z
        self.regwr(0x23, 0b00000000) # CTRL_REG4: High resolution mode

    def regwr(self, memaddr, val):
        self.i2.writeto(self.a, bytes((memaddr, val)))

    def regrd(self, memaddr):
        return self.i2.readfrom_mem(self.a, memaddr, 1)[0]

    def measurement(self):
        """ Wait for a new reading, return the (x,y,z) acceleration in g """

        # Note that the RT3000A does not support multibyte
        # reads. So must read the data one byte at a time.

        while True:
            STS_REG = self.regrd(0x27)
            if STS_REG & 8:
                regs = [self.regrd(i) for i in range(0x28, 0x2e)]
                xyz = struct.unpack("<3h", bytes(regs))
                return tuple([c / 16384. for c in xyz])

def main():
    i2 = I2C(1, freq = 100000)

    d = Accel(i2)

    while 0:
        print(d.regrd(0x27))
        time.sleep(1)

    while True:
        print("x=%+.3f  y=%+.3f  z=%+.3f" % d.measurement())

Default I²C address 0x19 (0b0011001)
Current consumption (typ.) 5 mA
Vcc 3.3 V