jana.manoj99
Posts: 21
Joined: Tue Jun 25, 2019 8:43 am

error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 6:25 am

Hello everyone

I am trying to get save real-time sensor/potentiometer raw data through ADS-1115 with date when push button is HIGH.
I could save the date but not pot values, now it says can not concatenate 'str' and 'int' and error loading program.
Here is my code...

Code: Select all


import RPi.GPIO as GPIO
import datetime
import csv
import time

# Import the ADS1x15 module.
import Adafruit_ADS1x15

#collector=5
adc = Adafruit_ADS1x15.ADS1115()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5,GPIO.IN,pull_up_down=GPIO.PUD_UP)

#2/3=+/-6.144V #1=+/-4.096V #2=+/-2.048V
#4=+/-1.024V #8 = +/-0.512V #16=+/-0.256V

GAIN = 1
#count = 0

# Main loop.
while True:
      
  while GPIO.input(5)==0:
     values = [0]*2
     for i in range(2):
       values[i] = adc.read_adc(i, gain=GAIN)
       now=time.strftime('%d/%m/%Y')
       print(now, '{0:>6}'.format(*values))
       with open('ada.csv', 'a') as file:
            file.write(now,values + "\n")
      # Pause for half a second.
     time.sleep(0.5)

Any help, highly appreciated. Thank you

pcmanbob
Posts: 6910
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 9:40 am

You need to change your values from a number to a string before you can write them to a file, you could try this but I have not tested it.

Code: Select all

file.write(now,str(values) + "\n")

you may need to do some thing like this

Code: Select all

file.write(now,str(values[i]) + "\n")
again this is untested.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jana.manoj99
Posts: 21
Joined: Tue Jun 25, 2019 8:43 am

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 11:05 am

pcmanbob wrote:
Thu Jul 11, 2019 9:40 am
You need to change your values from a number to a string before you can write them to a file, you could try this but I have not tested it.

Code: Select all

file.write(now,str(values) + "\n")

you may need to do some thing like this

Code: Select all

file.write(now,str(values[i]) + "\n")
again this is untested.
Wonderful ! Thanks @pcmanbob
But new problem is that the sensor data is saving in square braces. For example - [2354]
How do I avoid the square braces ?
I tried modifying and then ended up not editing the code due to errors.
Thank you

jana.manoj99
Posts: 21
Joined: Tue Jun 25, 2019 8:43 am

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 11:59 am

Also, using this code, at (count = 3). The data isn't continuous as previous code. I want the data continuous at count=3 and data saving off in all other cases.

Code: Select all


import RPi.GPIO as GPIO
import datetime
import csv
import time
from time import sleep
# Import the ADS1x15 module.
import Adafruit_ADS1x15

#collector=5
adc = Adafruit_ADS1x15.ADS1115()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(17,GPIO.OUT) #blue
GPIO.setup(27,GPIO.OUT) #green
#2/3=+/-6.144V #1=+/-4.096V #2=+/-2.048V
#4=+/-1.024V #8 = +/-0.512V #16=+/-0.256V

GAIN = 1
count = 0

# Main loop.
while True:
       # values = [0]*2
   while GPIO.input(5) == 1:
       sleep(0.2)
   count = count + 1
   if count == 1:
       GPIO.output(17,GPIO.HIGH)
   while GPIO.input(5) == 0:
       sleep(1)
       GPIO.output(17,GPIO.LOW)
   print(count)

   if count == 2:
       GPIO.output(27,GPIO.HIGH)
       now=time.strftime('%d/%m/%Y,%H:%M:%S,')
       print(now)
       with open('ada1.csv', 'a') as file:
                file.write(now + "\n")
   if count == 3:
       GPIO.output(27,GPIO.LOW)
       sleep(1)
       GPIO.output(17,GPIO.HIGH)
       sleep(3)
       GPIO.output(17,GPIO.LOW)
       GPIO.output(27,GPIO.HIGH)
       values = [0]*1
       for i in range(1):
           values[i] = adc.read_adc(i, gain=GAIN)
       #    now=time.strftime('%d/%m/%Y,')
           print('{0:>6}'.format(*values))
           with open('ada1.csv', 'a') as file:
                file.write(str(values) + "\n")
       time.sleep(0.5)  # Pause for half a secon
   if count == 4:
       count = 0
       GPIO.output(27,GPIO.LOW)
       


I need date and time in column A,B and data on Column C. Is it possible to program just with (comma (,) ?)
Thannks pcmanbob :)

pcmanbob
Posts: 6910
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 1:11 pm

jana.manoj99 wrote:
Thu Jul 11, 2019 11:05 am
But new problem is that the sensor data is saving in square braces. For example - [2354]
How do I avoid the square braces ?
So have modified your code to only write to the file at the end of each loop reading the ADC.

Code: Select all


import RPi.GPIO as GPIO
import datetime
import csv
import time

# Import the ADS1x15 module.
import Adafruit_ADS1x15

#collector=5
adc = Adafruit_ADS1x15.ADS1115()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5,GPIO.IN,pull_up_down=GPIO.PUD_UP)

#2/3=+/-6.144V #1=+/-4.096V #2=+/-2.048V
#4=+/-1.024V #8 = +/-0.512V #16=+/-0.256V

GAIN = 1
#count = 0
results = ""
# Main loop.
while True:
      
  while GPIO.input(5)==0:
    values = [0]*2
        for i in range(2):
        values[i] = adc.read_adc(i, gain=GAIN)
        now=time.strftime('%d/%m/%Y')
        print(now, '{0:>6}'.format(*values))
        results = results + "," + str(values[i])


    with open('ada.csv', 'a') as file:
        file.write(now + results + "\n")
    results = ""
    # Pause for half a second.
    time.sleep(0.5)
I have not tested this actual version of the code as I don't have an adc setup at the moment.

but simulating the code with random numbers produced this out put in the csv file

Code: Select all

11/07/2019,891,727
11/07/2019,143,555
11/07/2019,615,535
11/07/2019,220,184
11/07/2019,534,878

We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 6910
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 1:29 pm

jana.manoj99 wrote:
Thu Jul 11, 2019 11:59 am
Also, using this code, at (count = 3). The data isn't continuous as previous code. I want the data continuous at count=3 and data saving off in all other cases.

I need date and time in column A,B and data on Column C. Is it possible to program just with (comma (,) ?)
So if you don't want to start a new line after you write the date to the file then don't include the new line command. ( \n)

I have adjusted your code so it should do what you want but it is untested, and please can you stick to one standard for indentation having a mixture of 3 and 4 spaces make following your code more difficult.
having indentation in 4's makes things easier to read

Code: Select all

zero indentation
    1st level 4 speces
        2nd level 8 speces
            3rd level 12 spaces
            

Code: Select all


import RPi.GPIO as GPIO
import datetime
import csv
import time
from time import sleep
# Import the ADS1x15 module.
import Adafruit_ADS1x15

#collector=5
adc = Adafruit_ADS1x15.ADS1115()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(17,GPIO.OUT) #blue
GPIO.setup(27,GPIO.OUT) #green
#2/3=+/-6.144V #1=+/-4.096V #2=+/-2.048V
#4=+/-1.024V #8 = +/-0.512V #16=+/-0.256V

GAIN = 1
count = 0
results = ""
# Main loop.
while True:
       # values = [0]*2
   while GPIO.input(5) == 1:
       sleep(0.2)
   count = count + 1
   if count == 1:
       GPIO.output(17,GPIO.HIGH)
   while GPIO.input(5) == 0:
       sleep(1)
       GPIO.output(17,GPIO.LOW)
   print(count)

   if count == 2:
       GPIO.output(27,GPIO.HIGH)
       now=time.strftime('%d/%m/%Y,%H:%M:%S,')
       print(now)
       with open('ada1.csv', 'a') as file:
                file.write(now + ",")
   if count == 3:
       GPIO.output(27,GPIO.LOW)
       sleep(1)
       GPIO.output(17,GPIO.HIGH)
       sleep(3)
       GPIO.output(17,GPIO.LOW)
       GPIO.output(27,GPIO.HIGH)
       values = [0]*1
       for i in range(1):
           values[i] = adc.read_adc(i, gain=GAIN)
       #    now=time.strftime('%d/%m/%Y,')
           print('{0:>6}'.format(*values))
           results = results + "," + str(values[i])

       with open('ada1.csv', 'a') as file:
            file.write(results + "\n")
       results = ""
       time.sleep(0.5)  # Pause for half a secon
   if count == 4:
       count = 0
       GPIO.output(27,GPIO.LOW)
       
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jana.manoj99
Posts: 21
Joined: Tue Jun 25, 2019 8:43 am

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 8:03 pm

pcmanbob wrote:
Thu Jul 11, 2019 1:29 pm
jana.manoj99 wrote:
Thu Jul 11, 2019 11:59 am
Also, using this code, at (count = 3). The data isn't continuous as previous code. I want the data continuous at count=3 and data saving off in all other cases.

I need date and time in column A,B and data on Column C. Is it possible to program just with (comma (,) ?)
So if you don't want to start a new line after you write the date to the file then don't include the new line command. ( \n)

I have adjusted your code so it should do what you want but it is untested, and please can you stick to one standard for indentation having a mixture of 3 and 4 spaces make following your code more difficult.
having indentation in 4's makes things easier to read

Code: Select all

zero indentation
    1st level 4 speces
        2nd level 8 speces
            3rd level 12 spaces
            

Code: Select all


import RPi.GPIO as GPIO
import datetime
import csv
import time
from time import sleep
# Import the ADS1x15 module.
import Adafruit_ADS1x15

#collector=5
adc = Adafruit_ADS1x15.ADS1115()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(17,GPIO.OUT) #blue
GPIO.setup(27,GPIO.OUT) #green
#2/3=+/-6.144V #1=+/-4.096V #2=+/-2.048V
#4=+/-1.024V #8 = +/-0.512V #16=+/-0.256V

GAIN = 1
count = 0
results = ""
# Main loop.
while True:
       # values = [0]*2
   while GPIO.input(5) == 1:
       sleep(0.2)
   count = count + 1
   if count == 1:
       GPIO.output(17,GPIO.HIGH)
   while GPIO.input(5) == 0:
       sleep(1)
       GPIO.output(17,GPIO.LOW)
   print(count)

   if count == 2:
       GPIO.output(27,GPIO.HIGH)
       now=time.strftime('%d/%m/%Y,%H:%M:%S,')
       print(now)
       with open('ada1.csv', 'a') as file:
                file.write(now + ",")
   if count == 3:
       GPIO.output(27,GPIO.LOW)
       sleep(1)
       GPIO.output(17,GPIO.HIGH)
       sleep(3)
       GPIO.output(17,GPIO.LOW)
       GPIO.output(27,GPIO.HIGH)
       values = [0]*1
       for i in range(1):
           values[i] = adc.read_adc(i, gain=GAIN)
       #    now=time.strftime('%d/%m/%Y,')
           print('{0:>6}'.format(*values))
           results = results + "," + str(values[i])

       with open('ada1.csv', 'a') as file:
            file.write(results + "\n")
       results = ""
       time.sleep(0.5)  # Pause for half a secon
   if count == 4:
       count = 0
       GPIO.output(27,GPIO.LOW)
       
Dear pcmanbob, this is perfect sir. Thank you... but the analog data is not continuous, I want the analog value to keep loading continuously at only count=3. How can I change that ?
Thank you so much once again... between congratulations on your win #cwc19 #ENGvsAUS

pcmanbob
Posts: 6910
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Jul 11, 2019 10:59 pm

That's because your program program stops waiting for a button press, you would need to rewrite you program to remove this block , but then you would be writing the time and date on every loop as well.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jana.manoj99
Posts: 21
Joined: Tue Jun 25, 2019 8:43 am

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Aug 01, 2019 12:10 pm

pcmanbob wrote:
Thu Jul 11, 2019 10:59 pm
That's because your program program stops waiting for a button press, you would need to rewrite you program to remove this block , but then you would be writing the time and date on every loop as well.
Everything going fine, how do I increase my sampling rate ? The maximum sampling rate is 860/sec. How can I increase as my sampling rate is just 100/sec. Though I am changing the time to 0.0001, I get just 100/sec.
Please help me

Thank you

ghp
Posts: 1408
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Fri Aug 02, 2019 4:10 pm

Hello, python - what a surprise - is no ultrafast application. But with a certain idea where performance bottlenecks are there can be major improvements.

File open-close is slow. Opening a file, writing few data and close is slow. Better to open file once, write all data and close when program terminates. This needs restructuring of code, of course.
Here some sample code.

Code: Select all

import time
import os

N = 100000

print("open,write-close file each iteration")
try:
    os.remove('ada.csv')
except:
    pass

t0 = time.time()

for _ in range(N):    
    now="2019-08-02 17:18:42"
    results = "22.3"
    
    with open('ada.csv', 'a+') as file:
        file.write(now + results + "\n")
        
t1 = time.time()

print("time open-close", N, (t1-t0) )


print("open close for all iterations only once")
os.remove('ada.csv')
t0 = time.time()

with open('ada.csv', 'a+') as file:
    for _ in range(N):    
        now="2019-08-02 17:18:42"
        results = "22.3"
    
        file.write(now + results + "\n")
        
t1 = time.time()

print("time open-close", 1, (t1-t0) )

The results on my laptop are
open,write-close file each iteration
time open-close 100000 53.49705982208252
open close for all iterations only once
time open-close 1 0.3240184783935547

On a Pi3 with a moderate speed 8GB card

open,write-close file each iteration
time open-close 100000 15.036004066467285
open close for all iterations only once
time open-close 1 0.8090319633483887

Speed improvement on laptop: 170 times faster
Speed improvement on pi: 20 times faster

sleep takes more time than expected
time,.sleep() for small times is slow. The following code measures this:

Code: Select all

import time

N = 1000
for t in [ 0.1, 0.01, 0.001, 0.0001, 0.00001, ]:
    t0 = time.time()
    for _ in range(N):
        time.sleep(t)
    t1 = time.time()
    
    print("measured:", t, (t1-t0))
delays down to 0.001 sec are pretty fine. Smaller times do not sleep shorter. So when your delays need to be accurate and very short, look for another programming language or remove the time command.

Result on pi3:
measured: 0.1 100.17456126213074 expected 100.0
measured: 0.01 10.137107133865356 expected 10.0
measured: 0.001 1.0904154777526855 expected 1.0
measured: 0.0001 0.16770648956298828 expected 0.1 // 1,7 slower
measured: 1e-05 0.07406210899353027 expected 0.01 // 7,4 times slower

Result on laptop, windows, python3
measured: 0.1 100.09972524642944 expected 100.0
measured: 0.01 10.030573606491089 expected 10.0
measured: 0.001 1.0060577392578125 expected 1.0
measured: 0.0001 1.0200583934783936 expected 0.1 // strange result
measured: 1e-05 1.0310587882995605 expected 0.01 // strange result

jana.manoj99
Posts: 21
Joined: Tue Jun 25, 2019 8:43 am

Re: error saving adafruit ADS 1115 input to .csv through RPi 3B+

Thu Sep 05, 2019 1:01 pm

pcmanbob wrote:
Thu Jul 11, 2019 10:59 pm
That's because your program program stops waiting for a button press, you would need to rewrite you program to remove this block , but then you would be writing the time and date on every loop as well.
Can you kindly help me with this ? "https://www.raspberrypi.org/forums/view ... 2&t=250649"
I have been trying for more than 6 weeks so far, to get 860sps.
Addition of line of code ? I'm beginner to 'python', hence requesting.

Thanks in advance

Return to “Python”