Skip to content

fix read_adc_raw, update lis3dh_adc example #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions adafruit_lis3dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def read_adc_raw(self, adc):
if adc < 1 or adc > 3:
raise ValueError('ADC must be a value 1 to 3!')

return struct.unpack('<h', self._read_register((_REG_OUTADC1_L+((adc-1)*2)) | 0x80, 2))[0]
return struct.unpack('<h',
self._read_register((_REG_OUTADC1_L+((adc-1)*2)) | 0x80, 2)[0:2])[0]

def read_adc_mV(self, adc): # pylint: disable=invalid-name
"""Read the specified analog to digital converter value in millivolts.
Expand Down Expand Up @@ -280,8 +281,7 @@ def set_tap(self, tap, threshold, *,
self._write_register_byte(_REG_CTRL3, ctrl3 & ~(0x80)) # Turn off I1_CLICK.
self._write_register_byte(_REG_CLICKCFG, 0)
return
else:
self._write_register_byte(_REG_CTRL3, ctrl3 | 0x80) # Turn on int1 click output
self._write_register_byte(_REG_CTRL3, ctrl3 | 0x80) # Turn on int1 click output

if click_cfg is None:
if tap == 1:
Expand Down
36 changes: 19 additions & 17 deletions examples/lis3dh_adc.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
# Analog to digital converter example.
# Will loop forever printing ADC channel 1 raw and mV values every second.
# Open the serial port after running to see the output printed.
# NOTE the ADC can only read voltages in the range of ~900mV to 1200mV!
# Author: Tony DiCola
# NOTE the ADC can only read voltages in the range of ~900mV to 1800mV!

import time
import board
import busio
import adafruit_lis3dh
# Uncomment if using SPI
#import digitalio


# Uncomment _one_ of the hardware setups below depending on your wiring:

# Hardware I2C setup:
i2c = busio.I2C(board.SCL, board.SDA)
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)

# Software I2C setup:
#import bitbangio
#i2c = bitbangio.I2C(board.SCL, board.SDA)
#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
# Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
# otherwise check I2C pins.
if hasattr(board, 'ACCELEROMETER_SCL'):
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
else:
i2c = busio.I2C(board.SCL, board.SDA)
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)

# Hardware SPI setup:
#import busio
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
#cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin!
#lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# cs = digitalio.DigitalInOut(board.D5) # Set to correct CS pin!
# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)

#PyGamer I2C Setup:
#i2c = busio.I2C(board.SCL, board.SDA)
#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)


# Loop forever printing ADC readings.
Expand Down