Kevin
Kevin

Reputation: 391

Problem reading from ADS1115 from Raspberry Pi using i2c

I am trying to use an adc converter (ads1115 ).

But when I try to run the example script; simpletest.py

I get:

Reading ADS1x15 values, press Ctrl-C to quit...

| 0 | 1 | 2 | 3 |

Traceback (most recent call last): File "simpletest.py", line 43, in values[i] = adc.read_adc(i, gain=GAIN) File "build/bdist.linux-armv7l/egg/Adafruit_ADS1x15/ADS1x15.py", line 192, in read_adc File "build/bdist.linux-armv7l/egg/Adafruit_ADS1x15/ADS1x15.py", line 133, in _read File "build/bdist.linux-armv7l/egg/Adafruit_GPIO/I2C.py", line 136, in readList File "build/bdist.linux-armv7l/egg/Adafruit_PureIO/smbus.py", line 216, in read_i2c_block_data TypeError: one character string expected

When i run the diagnostic command: i2cdetect I get this

i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --        

Full code of python script (simpletest.py) showing the error message:

# Simple demo of reading each analog input from the ADS1x15 and printing it to
# the screen.
# Author: Tony DiCola
# License: Public Domain
import time

# Import the ADS1x15 module.
import Adafruit_ADS1x15


# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

# Or create an ADS1015 ADC (12-bit) instance.
#adc = Adafruit_ADS1x15.ADS1015()

# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
#adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)

# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
#  - 2/3 = +/-6.144V
#  -   1 = +/-4.096V
#  -   2 = +/-2.048V
#  -   4 = +/-1.024V
#  -   8 = +/-0.512V
#  -  16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1

print('Reading ADS1x15 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
print('-' * 37)
# Main loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*4
    for i in range(4):
        # Read the specified ADC channel using the previously set gain value.
        values[i] = adc.read_adc(i, gain=GAIN)
        # Note you can also pass in an optional data_rate parameter that controls
        # the ADC conversion time (in samples/second). Each chip has a different
        # set of allowed data rate values, see datasheet Table 9 config register
        # DR bit values.
        #values[i] = adc.read_adc(i, gain=GAIN, data_rate=128)
        # Each value will be a 12 or 16 bit signed integer value depending on the
        # ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
    # Print the ADC values.
    print('| {0:>6`enter code here`} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.5) 

please advise.

Kevin

Upvotes: 5

Views: 7125

Answers (4)

ginne yu
ginne yu

Reputation: 21

I met the same problem as you.

It seems that it happened because your raspberry pi uses the default python2.

Try to install python3 on your raspberry pi.

pip install python3

then change the python3 as the default one. What i did was deleting the link of python2, and setting up a new link of python3.

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.7 /usr/bin/python

please code pythonto find out which one is used in your raspberry. If python3 is shown, then you will successfully read the ADS1115.

That is what I ddi to solve this problem. I wish it may help you.

Upvotes: 1

Juan Ramirez Jardua
Juan Ramirez Jardua

Reputation: 11

uffff long search. No issues using python 3, but my ROS is installed with Python 2.7, so I need to resolve the issue. This is how I did following a Chinese forum:

Raspberry pi:

  1. mkdir ~/temp

  2. cd /usr/local/lib/python2.7/dist-packages

  3. cp -p Adafruit_PureIO-1.1.5-py2.7.egg ~/temp

  4. cd ~/temp

  5. unzip *.egg

  6. cd Adafruit_PureIO

  7. edit the file smbus.py line 255 change: cmdstring[i] = val by cmdstring[i] = str(val) save the file

  8. cd ..

  9. zip -r Adafruit_PureIO-1.1.5-py2.7.egg Adafruit_PureIO-1.1.5-py2.7.egg /usr/local/lib/python2.7/dist-packages

I wrote this steps after I find the solution.

Now you can run the example and your own code

pi@raspberrypi:~/Adafruit_Python_ADS1x15/examples $ python simpletest.py Reading ADS1x15 values, press Ctrl-C to quit... | 0 | 1 | 2 | 3 |

| 1157 | 845 | 292 | 292 | | 1157 | 845 | 296 | 294 | | 1157 | 845 | 293 | 296 |

Juan.

Upvotes: 1

charan ghumman
charan ghumman

Reputation: 489

I got the same issue with python2.7 then I install Adafruit ADS1115 library using pip3 install Adafruit-ADS1x15 and after that I run my script using python3 and it works.

Upvotes: 1

gulux
gulux

Reputation: 71

I would like to add that inapt wiring can cause the I2C address to change during operation, so that the device is no more accessible at the original address.

I wrote a sample program to try several configurations for ADS1115 without using AdaFruit library, which seems to have helped some people: http://smartypies.com/projects/ads1115-with-raspberrypi-and-python/ads1115runner

Upvotes: 0

Related Questions