def accelerometer(i2c): bma = bma42x.BMA42X(i2c) accel_conf = {} bma.init() # There is no hardware reset capability so issue a software reset # instead. bma.set_command_register(0xb6) time.sleep(0.20) # Upload the configuration file to enable the features of the sensor. bma.write_config_file() # Enable the accelerometer bma.set_accel_enable(True) # Accelerometer Configuration Setting # Output data Rate accel_conf['odr'] = bma42x.OUTPUT_DATA_RATE_100HZ # Gravity range of the sensor (+/- 2G, 4G, 8G, 16G) accel_conf['range'] = bma42x.ACCEL_RANGE_2G # Bandwidth configure number of sensor samples required to average # if value = 2, then 4 samples are averaged # averaged samples = 2^(val(accel bandwidth)) # Note1 : More info refer datasheets # Note2 : A higher number of averaged samples will result in a lower noise # level of the signal, but since the performance power mode phase is # increased, the power consumption will also rise. accel_conf['bandwidth'] = bma42x.ACCEL_NORMAL_AVG4 # Enable the filter performance mode where averaging of samples # will be done based on above set bandwidth and ODR. # There are two modes # 0 -> Averaging samples (Default) # 1 -> No averaging # For more info on No Averaging mode refer datasheets. accel_conf['perf_mode'] = bma42x.CIC_AVG_MODE # Set the accel configurations bma.set_accel_config(**accel_conf) print("Ax[m/s2], Ay[m/s2], Az[m/s2]") for i in range(ACCEL_SAMPLE_COUNT): (x, y, z) = bma.read_accel_xyz() # Converting lsb to meters per seconds square for 12 bit accelerometer # at 2G range x = lsb_to_ms2(x, 2, 12) y = lsb_to_ms2(y, 2, 12) z = lsb_to_ms2(z, 2, 12) # Print the data in m/s2/ print("{:.2f}, {:.2f}, {:.2f}".format(x, y, z)) # Pause for 10ms, 100Hz output data rate time.sleep(0.01)
def temperature(i2c): bma = bma42x.BMA42X(i2c) accel_conf = {} # Sensor initialization bma.init() # There is no hardware reset capability so issue a software reset # instead. bma.set_command_register(0xb6) time.sleep(0.20) # Upload the configuration file to enable the features of the sensor. bma.write_config_file() # Get temperature in degree C get_temp_C = bma.get_temperature(bma42x.DEG) # Get temperature in degree F get_temp_F = bma.get_temperature(bma42x.FAHREN) # Get temperature in degree K get_temp_K = bma.get_temperature(bma42x.KELVIN) # Scale the output to get the actual temperature actual_temp = get_temp_C / bma42x.SCALE_TEMP print("Actual temperature in degree celsius is {:.2f} degrees C".format(actual_temp)) actual_temp = get_temp_F / bma42x.SCALE_TEMP print("Actual temperature in degree fahranheit is {:.2f} degrees F".format(actual_temp)) actual_temp = get_temp_K / bma42x.SCALE_TEMP print("Actual temperature in degree kelvin is {:.2f} degrees K".format(actual_temp))
def __init__(self, i2c): """Configure the driver. :param machine.I2C i2c: I2C bus used to access the sensor. """ self._dev = bma42x.BMA42X(i2c)
def step_counter(i2c): bma = bma42x.BMA42X(i2c) accel_conf = {} bma.init() # There is no hardware reset capability so issue a software reset # instead. bma.set_command_register(0xb6) time.sleep(0.20) # Upload the configuration file to enable the features of the sensor. bma.write_config_file() # Enable the accelerometer bma.set_accel_enable(True) # Accelerometer Configuration Setting # Output data Rate accel_conf['odr'] = bma42x.OUTPUT_DATA_RATE_100HZ # Gravity range of the sensor (+/- 2G, 4G, 8G, 16G) accel_conf['range'] = bma42x.ACCEL_RANGE_2G # Bandwidth configure number of sensor samples required to average # if value = 2, then 4 samples are averaged # averaged samples = 2^(val(accel bandwidth)) # Note1 : More info refer datasheets # Note2 : A higher number of averaged samples will result in a lower noise # level of the signal, but since the performance power mode phase is # increased, the power consumption will also rise. accel_conf['bandwidth'] = bma42x.ACCEL_NORMAL_AVG4 # Enable the filter performance mode where averaging of samples # will be done based on above set bandwidth and ODR. # There are two modes # 0 -> Averaging samples (Default) # 1 -> No averaging # For more info on No Averaging mode refer datasheets. accel_conf['perf_mode'] = bma42x.CIC_AVG_MODE # Set the accel configurations bma.set_accel_config(**accel_conf) # Enable step counter bma.feature_enable(bma42x.STEP_CNTR, True) # Map the interrupt pin with that of step counter interrupts # Interrupt will be generated when step activity is generated. bma.map_interrupt(bma42x.INTR1_MAP, bma42x.STEP_CNTR_INT, True) # Set water-mark level 1 to get interrupt after 20 steps. # Range of step counter interrupt is 0 to 20460(resolution of 20 steps). bma.step_counter_set_watermark(1) print("Move/perform the walk/step action with the sensor") while True: # Read the interrupt register to check whether step counter interrupt is # received int_status = bma.read_int_status() # Check if step counter interrupt is triggered if int_status & bma42x.STEP_CNTR_INT: print("Step counter interrupt received") # On interrupt, Get step counter output step_out = bma.step_counter_output() break print("The step counter output is {}".format(step_out))