Search code examples
pythonraspberry-piadc

Sum the total of the values this python produces from an analog to digital converter connected to a Raspberry Pi?


I'm working on a beginner project in school that uses and Raspberry Pi 3, 10-bit MCP3008 Analog to Digital Converter (ADC), and 5v Solar Cell to read the amount of energy the solar cell is taking from light. I've taken the code that came with the analog to digital converter and modified it some, but I want to sum the numbers that display while the program runs is finished running and output to excel. It is set to show a value each second so I want to sum all the values at the end. How would I do that? The portion of the code that generates the ADC value was given to me by Adafruit website with the ADC so I'm still struggling to understand it. Running Python 2.7. Here is my code:

# Simple example of reading the MCP3008 analog input channels and printing
# them all out for solar test

import time
import datetime
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008


# Software SPI configuration:
CLK  = 18
MISO = 23
#MISO = Master Input Slave Output
MOSI = 24
#MOSI = Master Output Slave Input
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Hardware SPI configuration:
# SPI_PORT   = 0
# SPI_DEVICE = 0
# mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
runTime = input("How many minutes should the program run?: ")

t_end = time.time() + runTime * 60

print('Reading MCP3008 values, press Ctrl+C to quit...')
# Print nice channel column headers.
file = open('solartest.xls','a')
st = time.time()
print (datetime.datetime.fromtimestamp(st).strftime('%Y-%m-%d %H:%M:%S'))
file.write(datetime.datetime.fromtimestamp(st).strftime('%Y-%m-%d %H:%M:%S') + "\n")
print('| {0:>4} |'.format(*range(8)))
#file.write('{0:>4}'.format(*range(8)) + "\t")
print('-' * 8)
# Main program loop.
#while True:
while time.time() < t_end:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('| {0:>4} |'.format(*values))
    file.write('{0:>4}'.format(*values) + '\n')
    # Pause for a second.
    time.sleep(1)
#end = datetime.datetime.fromtimestamp().strftime('%Y-%m-%d %H:%M:%S')
ed = time.time()
print (datetime.datetime.fromtimestamp(ed).strftime('%Y-%m-%d %H:%M:%S'))
file.write(datetime.datetime.fromtimestamp(ed).strftime('%Y-%m-%d %H:%M:%S'))
file.close()

If possible I'd like to calculate the voltage for each ADC Value and write to excel in another column as well. The formula to calculate the voltage is V = (5/1024) * ADC Value. The ADC Value are the numbers that print each second when the program is ran. I've tried installing openpyxl but it won't work since my version of python is not 3.6 or higher and I can't update it as it is school equipment.


Solution

  • #while True:
    allValues = []
    while time.time() < t_end:
        # Read all the ADC channel values in a list.
        values = [0]*8
        for i in range(8):
            # The read_adc function will get the value of the specified channel (0-7).
            values[i] = mcp.read_adc(i)
        allValues.append(values)
        # Print the ADC values.
        print('| {0:>4} |'.format(*values))
        file.write('{0:>4}'.format(*values) + '\n')
        # Pause for a second.
        time.sleep(1)
    
    print(sum(sum(v) for v in allValues))