Skip to content

Commit 53146ab

Browse files
authored
Merge pull request #55 from jerryneedell/jerryn_adc
fix read_adc_raw, update lis3dh_adc example
2 parents de75d00 + 91fbe68 commit 53146ab

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

adafruit_lis3dh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def read_adc_raw(self, adc):
203203
if adc < 1 or adc > 3:
204204
raise ValueError('ADC must be a value 1 to 3!')
205205

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

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

286286
if click_cfg is None:
287287
if tap == 1:

examples/lis3dh_adc.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
# Analog to digital converter example.
22
# Will loop forever printing ADC channel 1 raw and mV values every second.
3-
# Open the serial port after running to see the output printed.
4-
# NOTE the ADC can only read voltages in the range of ~900mV to 1200mV!
5-
# Author: Tony DiCola
3+
# NOTE the ADC can only read voltages in the range of ~900mV to 1800mV!
4+
65
import time
76
import board
87
import busio
98
import adafruit_lis3dh
9+
# Uncomment if using SPI
10+
#import digitalio
1011

1112

12-
# Uncomment _one_ of the hardware setups below depending on your wiring:
13-
14-
# Hardware I2C setup:
15-
i2c = busio.I2C(board.SCL, board.SDA)
16-
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
17-
18-
# Software I2C setup:
19-
#import bitbangio
20-
#i2c = bitbangio.I2C(board.SCL, board.SDA)
21-
#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
13+
# Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
14+
# otherwise check I2C pins.
15+
if hasattr(board, 'ACCELEROMETER_SCL'):
16+
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
17+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
18+
else:
19+
i2c = busio.I2C(board.SCL, board.SDA)
20+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
2221

2322
# Hardware SPI setup:
24-
#import busio
25-
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
26-
#cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin!
27-
#lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
23+
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
24+
# cs = digitalio.DigitalInOut(board.D5) # Set to correct CS pin!
25+
# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
26+
27+
#PyGamer I2C Setup:
28+
#i2c = busio.I2C(board.SCL, board.SDA)
29+
#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
2830

2931

3032
# Loop forever printing ADC readings.

0 commit comments

Comments
 (0)